Sprite.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * @author mikael emtinger / http://gomo.se/
  3. * @author alteredq / http://alteredqualia.com/
  4. */
  5. THREE.Sprite = ( function () {
  6. var indices = new Uint16Array( [ 0, 1, 2, 0, 2, 3 ] );
  7. var vertices = new Float32Array( [ - 0.5, - 0.5, 0, 0.5, - 0.5, 0, 0.5, 0.5, 0, - 0.5, 0.5, 0 ] );
  8. var uvs = new Float32Array( [ 0, 0, 1, 0, 1, 1, 0, 1 ] );
  9. var geometry = new THREE.BufferGeometry();
  10. geometry.addAttribute( 'index', new THREE.BufferAttribute( indices, 1 ) );
  11. geometry.addAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) );
  12. geometry.addAttribute( 'uv', new THREE.BufferAttribute( uvs, 2 ) );
  13. return function ( material ) {
  14. THREE.Object3D.call( this );
  15. this.type = 'Sprite';
  16. this.geometry = geometry;
  17. this.material = ( material !== undefined ) ? material : new THREE.SpriteMaterial();
  18. };
  19. } )();
  20. THREE.Sprite.prototype = Object.create( THREE.Object3D.prototype );
  21. THREE.Sprite.prototype.constructor = THREE.Sprite;
  22. THREE.Sprite.prototype.raycast = ( function () {
  23. var matrixPosition = new THREE.Vector3();
  24. return function ( raycaster, intersects ) {
  25. matrixPosition.setFromMatrixPosition( this.matrixWorld );
  26. var distanceSq = raycaster.ray.distanceSqToPoint( matrixPosition );
  27. var guessSizeSq = this.scale.x * this.scale.y;
  28. if ( distanceSq > guessSizeSq ) {
  29. return;
  30. }
  31. intersects.push( {
  32. distance: Math.sqrt( distanceSq ),
  33. point: this.position,
  34. face: null,
  35. object: this
  36. } );
  37. };
  38. }() );
  39. THREE.Sprite.prototype.clone = function ( object ) {
  40. if ( object === undefined ) object = new THREE.Sprite( this.material );
  41. THREE.Object3D.prototype.clone.call( this, object );
  42. return object;
  43. };
  44. THREE.Sprite.prototype.toJSON = function ( meta ) {
  45. var data = THREE.Object3D.prototype.toJSON.call( this, meta );
  46. // only serialize if not in meta materials cache
  47. if ( meta.materials[ this.material.uuid ] === undefined ) {
  48. meta.materials[ this.material.uuid ] = this.material.toJSON();
  49. }
  50. data.object.material = this.material.uuid;
  51. return data;
  52. };
  53. // Backwards compatibility
  54. THREE.Particle = THREE.Sprite;