Forked from momo-the-monster/threejsatlasloader.js
Last active
September 13, 2021 13:11
-
-
Save endel/9feddcf676d55304faff to your computer and use it in GitHub Desktop.
three.js - JSON Atlas Loader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// three.js JSON Atlas Loader | |
// Requires: whatwg-fetch (https://github.com/github/fetch) | |
var materials = {} | |
fetch('images/spritesheet.json').then(function(response) { | |
return response.json() | |
}).then(function(json) { | |
var atlasTexture = THREE.ImageUtils.loadTexture('images/' + json.meta.image, undefined, function() { | |
for (var key in json.frames) { | |
var texture = atlasTexture.clone(); | |
var frame = json.frames[key].frame; | |
texture.repeat.x = ( frame.w / atlasTexture.image.width ); | |
texture.repeat.y = ( frame.h / atlasTexture.image.height ); | |
texture.offset.x = ( Math.abs( frame.x ) / atlasTexture.image.width ); | |
texture.offset.y = ( Math.abs( frame.y )/ atlasTexture.image.height ); | |
texture.needsUpdate = true; | |
var material = new THREE.MeshPhongMaterial({ | |
transparent:true, | |
map: texture, | |
side: THREE.DoubleSide | |
}); | |
materials[ key.replace(".png", "") ] = material | |
} | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment