ReflectorNode.js 2.1 KB

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