ReflectorNode.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. THREE.ReflectorNode = function ( mirror ) {
  2. THREE.TempNode.call( this, 'v4' );
  3. if ( mirror ) this.setMirror( mirror );
  4. };
  5. THREE.ReflectorNode.prototype = Object.create( THREE.TempNode.prototype );
  6. THREE.ReflectorNode.prototype.constructor = THREE.ReflectorNode;
  7. THREE.ReflectorNode.prototype.nodeType = "Reflector";
  8. THREE.ReflectorNode.prototype.setMirror = function ( mirror ) {
  9. this.mirror = mirror;
  10. this.textureMatrix = new THREE.Matrix4Node( this.mirror.material.uniforms.textureMatrix.value );
  11. this.localPosition = new THREE.PositionNode( THREE.PositionNode.LOCAL );
  12. this.coord = new THREE.OperatorNode( this.textureMatrix, this.localPosition, THREE.OperatorNode.MUL );
  13. this.coordResult = new THREE.OperatorNode( null, this.coord, THREE.OperatorNode.ADD );
  14. this.texture = new THREE.TextureNode( this.mirror.material.uniforms.tDiffuse.value, this.coord, null, true );
  15. };
  16. THREE.ReflectorNode.prototype.generate = function ( builder, output ) {
  17. var material = builder.material;
  18. if ( builder.isShader( 'fragment' ) ) {
  19. this.coordResult.a = this.offset;
  20. this.texture.coord = this.offset ? this.coordResult : this.coord;
  21. if ( output === 'sampler2D' ) {
  22. return this.texture.build( builder, output );
  23. }
  24. return builder.format( this.texture.build( builder, this.type ), this.type, output );
  25. } else {
  26. console.warn( "THREE.ReflectorNode is not compatible with " + builder.shader + " shader." );
  27. return builder.format( 'vec4(0.0)', this.type, output );
  28. }
  29. };
  30. THREE.ReflectorNode.prototype.toJSON = function ( meta ) {
  31. var data = this.getJSONNode( meta );
  32. if ( ! data ) {
  33. data = this.createJSONNode( meta );
  34. data.mirror = this.mirror.uuid;
  35. if ( this.offset ) data.offset = this.offset.toJSON( meta ).uuid;
  36. }
  37. return data;
  38. };