SpriteMaterial.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. *
  4. * parameters = {
  5. * color: <hex>,
  6. * opacity: <float>,
  7. * map: new THREE.Texture( <Image> ),
  8. *
  9. * blending: THREE.NormalBlending,
  10. * depthTest: <bool>,
  11. * depthWrite: <bool>,
  12. *
  13. * useScreenCoordinates: <bool>,
  14. * sizeAttenuation: <bool>,
  15. * scaleByViewport: <bool>,
  16. * alignment: THREE.SpriteAlignment.center,
  17. *
  18. * uvOffset: new THREE.Vector2(),
  19. * uvScale: new THREE.Vector2(),
  20. *
  21. * fog: <bool>
  22. * }
  23. */
  24. THREE.SpriteMaterial = function ( parameters ) {
  25. THREE.Material.call( this );
  26. // defaults
  27. this.color = new THREE.Color( 0xffffff );
  28. this.map = new THREE.Texture();
  29. this.useScreenCoordinates = true;
  30. this.depthTest = !this.useScreenCoordinates;
  31. this.sizeAttenuation = !this.useScreenCoordinates;
  32. this.scaleByViewport = !this.sizeAttenuation;
  33. this.alignment = THREE.SpriteAlignment.center.clone();
  34. this.fog = false;
  35. this.uvOffset = new THREE.Vector2( 0, 0 );
  36. this.uvScale = new THREE.Vector2( 1, 1 );
  37. // set parameters
  38. this.setValues( parameters );
  39. // override coupled defaults if not specified explicitly by parameters
  40. parameters = parameters || {};
  41. if ( parameters.depthTest === undefined ) this.depthTest = !this.useScreenCoordinates;
  42. if ( parameters.sizeAttenuation === undefined ) this.sizeAttenuation = !this.useScreenCoordinates;
  43. if ( parameters.scaleByViewport === undefined ) this.scaleByViewport = !this.sizeAttenuation;
  44. };
  45. THREE.SpriteMaterial.prototype = Object.create( THREE.Material.prototype );
  46. THREE.SpriteMaterial.prototype.clone = function () {
  47. var material = new THREE.SpriteMaterial();
  48. THREE.Material.prototype.clone.call( this, material );
  49. material.color.copy( this.color );
  50. material.map = this.map;
  51. material.useScreenCoordinates = this.useScreenCoordinates;
  52. material.sizeAttenuation = this.sizeAttenuation;
  53. material.scaleByViewport = this.scaleByViewport;
  54. material.alignment.copy( this.alignment );
  55. material.uvOffset.copy( this.uvOffset );
  56. material.uvScale.copy( this.uvScale );
  57. material.fog = this.fog;
  58. return material;
  59. };
  60. // Alignment enums
  61. THREE.SpriteAlignment = {};
  62. THREE.SpriteAlignment.topLeft = new THREE.Vector2( 1, -1 );
  63. THREE.SpriteAlignment.topCenter = new THREE.Vector2( 0, -1 );
  64. THREE.SpriteAlignment.topRight = new THREE.Vector2( -1, -1 );
  65. THREE.SpriteAlignment.centerLeft = new THREE.Vector2( 1, 0 );
  66. THREE.SpriteAlignment.center = new THREE.Vector2( 0, 0 );
  67. THREE.SpriteAlignment.centerRight = new THREE.Vector2( -1, 0 );
  68. THREE.SpriteAlignment.bottomLeft = new THREE.Vector2( 1, 1 );
  69. THREE.SpriteAlignment.bottomCenter = new THREE.Vector2( 0, 1 );
  70. THREE.SpriteAlignment.bottomRight = new THREE.Vector2( -1, 1 );