SpriteNodeMaterial.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import NodeMaterial from './NodeMaterial.js';
  2. import { SpriteMaterial } from 'three';
  3. import {
  4. vec2, vec3, vec4,
  5. uniform, mul,
  6. positionLocal, cos, sin,
  7. modelViewMatrix, cameraProjectionMatrix, modelWorldMatrix, materialRotation
  8. } from '../shadernode/ShaderNodeElements.js';
  9. const defaultValues = new SpriteMaterial();
  10. class SpriteNodeMaterial extends NodeMaterial {
  11. constructor( parameters ) {
  12. super();
  13. this.isSpriteNodeMaterial = true;
  14. this.lights = false;
  15. this.normals = false;
  16. this.colorNode = null;
  17. this.opacityNode = null;
  18. this.alphaTestNode = null;
  19. this.lightNode = null;
  20. this.positionNode = null;
  21. this.rotationNode = null;
  22. this.scaleNode = null;
  23. this.setDefaultValues( defaultValues );
  24. this.setValues( parameters );
  25. }
  26. constructPosition( { object, context } ) {
  27. // < VERTEX STAGE >
  28. const { positionNode, rotationNode, scaleNode } = this;
  29. const vertex = positionLocal;
  30. let mvPosition = mul( modelViewMatrix, positionNode ? vec4( positionNode.xyz, 1 ) : vec4( 0, 0, 0, 1 ) );
  31. let scale = vec2(
  32. vec3( modelWorldMatrix[ 0 ].x, modelWorldMatrix[ 0 ].y, modelWorldMatrix[ 0 ].z ).length(),
  33. vec3( modelWorldMatrix[ 1 ].x, modelWorldMatrix[ 1 ].y, modelWorldMatrix[ 1 ].z ).length()
  34. );
  35. if ( scaleNode !== null ) {
  36. scale = scale.mul( scaleNode );
  37. }
  38. let alignedPosition = vertex.xy;
  39. if ( object.center && object.center.isVector2 === true ) {
  40. alignedPosition = alignedPosition.sub( uniform( object.center ).sub( vec2( 0.5 ) ) );
  41. }
  42. alignedPosition = mul( alignedPosition, scale );
  43. const rotation = rotationNode || materialRotation;
  44. const rotatedPosition = vec2(
  45. cos( rotation ).mul( alignedPosition.x ).sub( sin( rotation ).mul( alignedPosition.y ) ),
  46. sin( rotation ).mul( alignedPosition.x ).add( cos( rotation ).mul( alignedPosition.y ) )
  47. );
  48. mvPosition = vec4( mvPosition.xy.add( rotatedPosition.xy ), mvPosition.z, mvPosition.w );
  49. const modelViewProjection = cameraProjectionMatrix.mul( mvPosition );
  50. context.vertex = vertex;
  51. return modelViewProjection;
  52. }
  53. copy( source ) {
  54. this.colorNode = source.colorNode;
  55. this.opacityNode = source.opacityNode;
  56. this.alphaTestNode = source.alphaTestNode;
  57. this.lightNode = source.lightNode;
  58. this.positionNode = source.positionNode;
  59. this.rotationNode = source.rotationNode;
  60. this.scaleNode = source.scaleNode;
  61. return super.copy( source );
  62. }
  63. }
  64. export default SpriteNodeMaterial;