ReflectorNode.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { TempNode } from '../core/TempNode.js';
  2. import { InputNode } from '../core/InputNode.js';
  3. import { PositionNode } from '../accessors/PositionNode.js';
  4. import { OperatorNode } from '../math/OperatorNode.js';
  5. import { TextureNode } from './TextureNode.js';
  6. import { Matrix4Node } from './Matrix4Node.js';
  7. class ReflectorNode extends TempNode {
  8. constructor( mirror ) {
  9. super( 'v4' );
  10. if ( mirror ) this.setMirror( mirror );
  11. }
  12. setMirror( mirror ) {
  13. this.mirror = mirror;
  14. this.textureMatrix = new Matrix4Node( this.mirror.material.uniforms.textureMatrix.value );
  15. this.localPosition = new PositionNode( PositionNode.LOCAL );
  16. this.uv = new OperatorNode( this.textureMatrix, this.localPosition, OperatorNode.MUL );
  17. this.uvResult = new OperatorNode( null, this.uv, OperatorNode.ADD );
  18. this.texture = new TextureNode( this.mirror.material.uniforms.tDiffuse.value, this.uv, null, true );
  19. }
  20. generate( builder, output ) {
  21. if ( builder.isShader( 'fragment' ) ) {
  22. this.uvResult.a = this.offset;
  23. this.texture.uv = this.offset ? this.uvResult : this.uv;
  24. if ( output === 'sampler2D' ) {
  25. return this.texture.build( builder, output );
  26. }
  27. return builder.format( this.texture.build( builder, this.type ), this.type, output );
  28. } else {
  29. console.warn( 'THREE.ReflectorNode is not compatible with ' + builder.shader + ' shader.' );
  30. return builder.format( 'vec4( 0.0 )', this.type, output );
  31. }
  32. }
  33. copy( source ) {
  34. InputNode.prototype.copy.call( this, source );
  35. this.scope.mirror = source.mirror;
  36. return this;
  37. }
  38. toJSON( meta ) {
  39. let data = this.getJSONNode( meta );
  40. if ( ! data ) {
  41. data = this.createJSONNode( meta );
  42. data.mirror = this.mirror.uuid;
  43. if ( this.offset ) data.offset = this.offset.toJSON( meta ).uuid;
  44. }
  45. return data;
  46. }
  47. }
  48. ReflectorNode.prototype.nodeType = 'Reflector';
  49. export { ReflectorNode };