ReflectNode.js 3.1 KB

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