SpriteNodeMaterial.js 2.5 KB

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