ReflectorNode.js 2.1 KB

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