ReflectNode.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.ReflectNode = function( scope ) {
  5. THREE.TempNode.call( this, 'v3', { unique: true } );
  6. this.scope = scope || THREE.ReflectNode.CUBE;
  7. };
  8. THREE.ReflectNode.CUBE = 'cube';
  9. THREE.ReflectNode.SPHERE = 'sphere';
  10. THREE.ReflectNode.VECTOR = 'vector';
  11. THREE.ReflectNode.prototype = Object.create( THREE.TempNode.prototype );
  12. THREE.ReflectNode.prototype.constructor = THREE.ReflectNode;
  13. THREE.ReflectNode.prototype.getType = function( builder ) {
  14. switch ( this.scope ) {
  15. case THREE.ReflectNode.SPHERE:
  16. return 'v2';
  17. }
  18. return this.type;
  19. };
  20. THREE.ReflectNode.prototype.generate = function( builder, output ) {
  21. var result;
  22. switch ( this.scope ) {
  23. case THREE.ReflectNode.VECTOR:
  24. builder.material.addFragmentNode( 'vec3 reflectVec = inverseTransformDirection( reflect( -normalize( vViewPosition ), normal ), viewMatrix );' );
  25. result = 'reflectVec';
  26. break;
  27. case THREE.ReflectNode.CUBE:
  28. var reflectVec = new THREE.ReflectNode( THREE.ReflectNode.VECTOR ).build( builder, 'v3' );
  29. builder.material.addFragmentNode( 'vec3 reflectCubeVec = vec3( -1.0 * ' + reflectVec + '.x, ' + reflectVec + '.yz );' );
  30. result = 'reflectCubeVec';
  31. break;
  32. case THREE.ReflectNode.SPHERE:
  33. var reflectVec = new THREE.ReflectNode( THREE.ReflectNode.VECTOR ).build( builder, 'v3' );
  34. builder.material.addFragmentNode( 'vec2 reflectSphereVec = normalize((viewMatrix * vec4(' + reflectVec + ', 0.0 )).xyz + vec3(0.0,0.0,1.0)).xy * 0.5 + 0.5;' );
  35. result = 'reflectSphereVec';
  36. break;
  37. }
  38. return builder.format( result, this.getType( this.type ), output );
  39. };