BumpMapNode.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { TempNode } from '../core/TempNode.js';
  2. import { FloatNode } from '../inputs/FloatNode.js';
  3. import { FunctionNode } from '../core/FunctionNode.js';
  4. import { NormalNode } from '../accessors/NormalNode.js';
  5. import { PositionNode } from '../accessors/PositionNode.js';
  6. class BumpMapNode extends TempNode {
  7. constructor( value, scale ) {
  8. super( 'v3' );
  9. this.value = value;
  10. this.scale = scale || new FloatNode( 1 );
  11. this.toNormalMap = false;
  12. }
  13. generate( builder, output ) {
  14. if ( builder.isShader( 'fragment' ) ) {
  15. if ( this.toNormalMap ) {
  16. const bumpToNormal = builder.include( BumpMapNode.Nodes.bumpToNormal );
  17. return builder.format( bumpToNormal + '( ' + this.value.build( builder, 'sampler2D' ) + ', ' +
  18. this.value.uv.build( builder, 'v2' ) + ', ' +
  19. this.scale.build( builder, 'f' ) + ' )', this.getType( builder ), output );
  20. } else {
  21. const derivativeHeight = builder.include( BumpMapNode.Nodes.dHdxy_fwd ),
  22. perturbNormalArb = builder.include( BumpMapNode.Nodes.perturbNormalArb );
  23. this.normal = this.normal || new NormalNode();
  24. this.position = this.position || new PositionNode( PositionNode.VIEW );
  25. const derivativeHeightCode = derivativeHeight + '( ' + this.value.build( builder, 'sampler2D' ) + ', ' +
  26. this.value.uv.build( builder, 'v2' ) + ', ' +
  27. this.scale.build( builder, 'f' ) + ' )';
  28. return builder.format( perturbNormalArb + '( -' + this.position.build( builder, 'v3' ) + ', ' +
  29. this.normal.build( builder, 'v3' ) + ', ' +
  30. derivativeHeightCode + ' )', this.getType( builder ), output );
  31. }
  32. } else {
  33. console.warn( 'THREE.BumpMapNode is not compatible with ' + builder.shader + ' shader.' );
  34. return builder.format( 'vec3( 0.0 )', this.getType( builder ), output );
  35. }
  36. }
  37. copy( source ) {
  38. super.copy( source );
  39. this.value = source.value;
  40. this.scale = source.scale;
  41. return this;
  42. }
  43. toJSON( meta ) {
  44. let data = this.getJSONNode( meta );
  45. if ( ! data ) {
  46. data = this.createJSONNode( meta );
  47. data.value = this.value.toJSON( meta ).uuid;
  48. data.scale = this.scale.toJSON( meta ).uuid;
  49. }
  50. return data;
  51. }
  52. }
  53. BumpMapNode.Nodes = ( function () {
  54. // Bump Mapping Unparametrized Surfaces on the GPU by Morten S. Mikkelsen
  55. // http://api.unrealengine.com/attachments/Engine/Rendering/LightingAndShadows/BumpMappingWithoutTangentSpace/mm_sfgrad_bump.pdf
  56. // Evaluate the derivative of the height w.r.t. screen-space using forward differencing (listing 2)
  57. const dHdxy_fwd = new FunctionNode( /* glsl */`
  58. vec2 dHdxy_fwd( sampler2D bumpMap, vec2 vUv, float bumpScale ) {
  59. vec2 dSTdx = dFdx( vUv );
  60. vec2 dSTdy = dFdy( vUv );
  61. float Hll = bumpScale * texture2D( bumpMap, vUv ).x;
  62. float dBx = bumpScale * texture2D( bumpMap, vUv + dSTdx ).x - Hll;
  63. float dBy = bumpScale * texture2D( bumpMap, vUv + dSTdy ).x - Hll;
  64. return vec2( dBx, dBy );
  65. }`
  66. , null, { derivatives: true } );
  67. const perturbNormalArb = new FunctionNode( /* glsl */`
  68. vec3 perturbNormalArb( vec3 surf_pos, vec3 surf_norm, vec2 dHdxy ) {
  69. vec3 vSigmaX = vec3( dFdx( surf_pos.x ), dFdx( surf_pos.y ), dFdx( surf_pos.z ) );
  70. vec3 vSigmaY = vec3( dFdy( surf_pos.x ), dFdy( surf_pos.y ), dFdy( surf_pos.z ) );
  71. vec3 vN = surf_norm; // normalized
  72. vec3 R1 = cross( vSigmaY, vN );
  73. vec3 R2 = cross( vN, vSigmaX );
  74. float fDet = dot( vSigmaX, R1 );
  75. fDet *= ( float( gl_FrontFacing ) * 2.0 - 1.0 );
  76. vec3 vGrad = sign( fDet ) * ( dHdxy.x * R1 + dHdxy.y * R2 );
  77. return normalize( abs( fDet ) * surf_norm - vGrad );
  78. }`
  79. , [ dHdxy_fwd ], { derivatives: true } );
  80. const bumpToNormal = new FunctionNode( /* glsl */`
  81. vec3 bumpToNormal( sampler2D bumpMap, vec2 uv, float scale ) {
  82. vec2 dSTdx = dFdx( uv );
  83. vec2 dSTdy = dFdy( uv );
  84. float Hll = texture2D( bumpMap, uv ).x;
  85. float dBx = texture2D( bumpMap, uv + dSTdx ).x - Hll;
  86. float dBy = texture2D( bumpMap, uv + dSTdy ).x - Hll;
  87. return vec3( .5 - ( dBx * scale ), .5 - ( dBy * scale ), 1.0 );
  88. }`
  89. , null, { derivatives: true } );
  90. return {
  91. dHdxy_fwd: dHdxy_fwd,
  92. perturbNormalArb: perturbNormalArb,
  93. bumpToNormal: bumpToNormal
  94. };
  95. } )();
  96. BumpMapNode.prototype.nodeType = 'BumpMap';
  97. BumpMapNode.prototype.hashProperties = [ 'toNormalMap' ];
  98. export { BumpMapNode };