ReflectorNode.js 2.1 KB

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