Skip to content

Instantly share code, notes, and snippets.

@endel
Forked from momo-the-monster/threejsatlasloader.js
Last active September 13, 2021 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save endel/9feddcf676d55304faff to your computer and use it in GitHub Desktop.
Save endel/9feddcf676d55304faff to your computer and use it in GitHub Desktop.
three.js - JSON Atlas Loader
// 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