ReflectNode.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import Node from '../core/Node.js';
  2. import {
  3. nodeObject, transformedNormalView, positionViewDirection,
  4. transformDirection, negate, reflect, vec3, cameraViewMatrix
  5. } from '../shadernode/ShaderNodeBaseElements.js';
  6. class ReflectNode extends Node {
  7. static VECTOR = 'vector';
  8. static CUBE = 'cube';
  9. constructor( scope = ReflectNode.CUBE ) {
  10. super( 'vec3' );
  11. this.scope = scope;
  12. }
  13. getHash( /*builder*/ ) {
  14. return `reflect-${this.scope}`;
  15. }
  16. construct() {
  17. const scope = this.scope;
  18. let outputNode = null;
  19. if ( scope === ReflectNode.VECTOR ) {
  20. const reflectView = reflect( negate( positionViewDirection ), transformedNormalView );
  21. const reflectVec = transformDirection( reflectView, cameraViewMatrix );
  22. outputNode = reflectVec;
  23. } else if ( scope === ReflectNode.CUBE ) {
  24. const reflectVec = nodeObject( new ReflectNode( ReflectNode.VECTOR ) );
  25. const cubeUV = vec3( negate( reflectVec.x ), reflectVec.yz );
  26. outputNode = cubeUV;
  27. }
  28. return outputNode;
  29. }
  30. serialize( data ) {
  31. super.serialize( data );
  32. data.scope = this.scope;
  33. }
  34. deserialize( data ) {
  35. super.deserialize( data );
  36. this.scope = data.scope;
  37. }
  38. }
  39. export default ReflectNode;