Sprite.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 () {
  40. var sprite = new THREE.Sprite( this.material );
  41. return sprite.copy( this );
  42. };
  43. THREE.Sprite.prototype.copy = function ( source ) {
  44. THREE.Object3D.prototype.copy.call( this, source );
  45. return this;
  46. };
  47. THREE.Sprite.prototype.toJSON = function ( meta ) {
  48. var data = THREE.Object3D.prototype.toJSON.call( this, meta );
  49. // only serialize if not in meta materials cache
  50. if ( meta.materials[ this.material.uuid ] === undefined ) {
  51. meta.materials[ this.material.uuid ] = this.material.toJSON();
  52. }
  53. data.object.material = this.material.uuid;
  54. return data;
  55. };
  56. // Backwards compatibility
  57. THREE.Particle = THREE.Sprite;
粤ICP备19079148号