Created
March 9, 2012 21:13
-
-
Save calvinte/2008730 to your computer and use it in GitHub Desktop.
Simple function that abstracts sprite mapping in Three.js
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
/** | |
* @param paramaters as object | |
* | |
* Prepare the object in | |
* the following format: | |
* | |
* material = spriteMapper({ | |
* image: 'sprite.png', | |
* imageSize: 256, // size of image in px (must be square) | |
* spriteSize: 16, // size of sprite in px (must be square) | |
* sides:{ | |
* left:{ | |
* spriteX: 8, // number of sprites from the left | |
* spriteY: 0 // number of sprites from the top | |
* }, | |
* right:{ | |
* spriteX: 8, | |
* spriteY: 0 | |
* }, | |
* top:{ | |
* spriteX: 9, | |
* spriteY: 0 | |
* }, | |
* bottom:{ | |
* spriteX: 10, | |
* spriteY: 0 | |
* }, | |
* front:{ | |
* spriteX: 8, | |
* spriteY: 0 | |
* }, | |
* back:{ | |
* spriteX: 8, | |
* spriteY: 0 | |
* } | |
* } | |
* }); | |
* | |
*/ | |
function spriteMapper(paramaters) { | |
materials = new Array(); | |
for (side in paramaters.sides) { | |
texture = THREE.ImageUtils.loadTexture(paramaters.image); | |
// change these as required | |
texture.wrapS = THREE.ClampToEdgeWrapping; | |
texture.wrapT = THREE.ClampToEdgeWrapping; | |
texture.magFilter = THREE.NearestFilter; | |
texture.minFilter = THREE.LinearMipMapLinearFilter; | |
texture.repeat.x = paramaters.spriteSize / paramaters.imageSize; | |
texture.repeat.y = paramaters.spriteSize / paramaters.imageSize; | |
texture.offset.x = paramaters.sides[side].spriteX / ( paramaters.imageSize / paramaters.spriteSize ); | |
texture.offset.y = paramaters.sides[side].spriteY / ( paramaters.imageSize / paramaters.spriteSize ); | |
// change material type as required | |
materials.push(new THREE.MeshLambertMaterial( {map:texture, transparent: true} )); | |
} | |
return materials; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment