SpriteNodeMaterial.js 2.4 KB

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