ReflectNode.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import Node from '../core/Node.js';
  2. import { nodeObject, normalWorld, positionWorld, cameraPosition, sub, normalize, vec3, negate, reflect } from '../shadernode/ShaderNodeBaseElements.js';
  3. class ReflectNode extends Node {
  4. static VECTOR = 'vector';
  5. static CUBE = 'cube';
  6. constructor( scope = ReflectNode.CUBE ) {
  7. super( 'vec3' );
  8. this.scope = scope;
  9. }
  10. getHash( /*builder*/ ) {
  11. return `reflect-${this.scope}`;
  12. }
  13. generate( builder ) {
  14. const scope = this.scope;
  15. if ( scope === ReflectNode.VECTOR ) {
  16. const cameraToFrag = normalize( sub( positionWorld, cameraPosition ) );
  17. const reflectVec = reflect( cameraToFrag, normalWorld );
  18. return reflectVec.build( builder );
  19. } else if ( scope === ReflectNode.CUBE ) {
  20. const reflectVec = nodeObject( new ReflectNode( ReflectNode.VECTOR ) );
  21. const cubeUV = vec3( negate( reflectVec.x ), reflectVec.yz );
  22. return cubeUV.build( builder );
  23. }
  24. }
  25. serialize( data ) {
  26. super.serialize( data );
  27. data.scope = this.scope;
  28. }
  29. deserialize( data ) {
  30. super.deserialize( data );
  31. this.scope = data.scope;
  32. }
  33. }
  34. export default ReflectNode;