ReflectNode.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.ReflectNode = function() {
  5. THREE.TempNode.call( this, 'v3', { unique: true } );
  6. this.worldPosition = new THREE.PositionNode( THREE.PositionNode.WORLD );
  7. };
  8. THREE.ReflectNode.prototype = Object.create( THREE.TempNode.prototype );
  9. THREE.ReflectNode.prototype.constructor = THREE.ReflectNode;
  10. THREE.ReflectNode.prototype.generate = function( builder, output ) {
  11. var material = builder.material;
  12. if ( builder.isShader( 'fragment' ) ) {
  13. material.addFragmentNode( [
  14. 'vec3 cameraToVertex = normalize( ' + this.worldPosition.build( builder, 'v3' ) + ' - cameraPosition );',
  15. 'vec3 worldNormal = inverseTransformDirection( normal, viewMatrix );',
  16. 'vec3 vReflect = reflect( cameraToVertex, worldNormal );'
  17. ].join( "\n" ) );
  18. return builder.format( 'vReflect', this.type, output );
  19. }
  20. else {
  21. console.warn( "THREE.ReflectNode is not compatible with " + builder.shader + " shader" );
  22. return builder.format( 'vec3( 0.0 )', this.type, output );
  23. }
  24. };