ReflectNode.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. import { TempNode } from '../core/TempNode.js';
  5. import { PositionNode } from './PositionNode.js';
  6. import { NormalNode } from './NormalNode.js';
  7. function ReflectNode( scope ) {
  8. TempNode.call( this, 'v3' );
  9. this.scope = scope || ReflectNode.CUBE;
  10. }
  11. ReflectNode.CUBE = 'cube';
  12. ReflectNode.SPHERE = 'sphere';
  13. ReflectNode.VECTOR = 'vector';
  14. ReflectNode.prototype = Object.create( TempNode.prototype );
  15. ReflectNode.prototype.constructor = ReflectNode;
  16. ReflectNode.prototype.nodeType = "Reflect";
  17. ReflectNode.prototype.getUnique = function ( builder ) {
  18. return !builder.context.viewNormal;
  19. };
  20. ReflectNode.prototype.getType = function ( /* builder */ ) {
  21. switch ( this.scope ) {
  22. case ReflectNode.SPHERE:
  23. return 'v2';
  24. }
  25. return this.type;
  26. };
  27. ReflectNode.prototype.generate = function ( builder, output ) {
  28. var isUnique = this.getUnique( builder );
  29. if ( builder.isShader( 'fragment' ) ) {
  30. var result;
  31. switch ( this.scope ) {
  32. case ReflectNode.VECTOR:
  33. var viewNormalNode = builder.context.viewNormal || new NormalNode();
  34. var roughnessNode = builder.context.roughness;
  35. var viewNormal = viewNormalNode.build( builder, 'v3' );
  36. var viewPosition = new PositionNode( PositionNode.VIEW ).build( builder, 'v3' );
  37. var roughness = roughnessNode ? roughnessNode.build( builder, 'f' ) : undefined;
  38. var method = `reflect( -normalize( ${viewPosition} ), ${viewNormal} )`;
  39. if ( viewNormalNode && roughness ) {
  40. // Mixing the reflection with the normal is more accurate and keeps rough objects from gathering light from behind their tangent plane.
  41. method = `normalize( mix( ${method}, ${viewNormal}, ${roughness} * ${roughness} ) )`;
  42. }
  43. var code = `inverseTransformDirection( ${method}, viewMatrix )`;
  44. if ( isUnique ) {
  45. builder.addNodeCode( `vec3 reflectVec = ${code};` );
  46. result = 'reflectVec';
  47. } else {
  48. result = code;
  49. }
  50. break;
  51. case ReflectNode.CUBE:
  52. var reflectVec = new ReflectNode( ReflectNode.VECTOR ).build( builder, 'v3' );
  53. var code = 'vec3( -' + reflectVec + '.x, ' + reflectVec + '.yz )';
  54. if ( isUnique ) {
  55. builder.addNodeCode( `vec3 reflectCubeVec = ${code};` );
  56. result = 'reflectCubeVec';
  57. } else {
  58. result = code;
  59. }
  60. break;
  61. case ReflectNode.SPHERE:
  62. var reflectVec = new ReflectNode( ReflectNode.VECTOR ).build( builder, 'v3' );
  63. var code = 'normalize( ( viewMatrix * vec4( ' + reflectVec + ', 0.0 ) ).xyz + vec3( 0.0, 0.0, 1.0 ) ).xy * 0.5 + 0.5';
  64. if ( isUnique ) {
  65. builder.addNodeCode( `vec2 reflectSphereVec = ${code};` );
  66. result = 'reflectSphereVec';
  67. } else {
  68. result = code;
  69. }
  70. break;
  71. }
  72. return builder.format( result, this.getType( builder ), output );
  73. } else {
  74. console.warn( "THREE.ReflectNode is not compatible with " + builder.shader + " shader." );
  75. return builder.format( 'vec3( 0.0 )', this.type, output );
  76. }
  77. };
  78. ReflectNode.prototype.toJSON = function ( meta ) {
  79. var data = this.getJSONNode( meta );
  80. if ( ! data ) {
  81. data = this.createJSONNode( meta );
  82. data.scope = this.scope;
  83. }
  84. return data;
  85. };
  86. export { ReflectNode };