BumpMapNode.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import TempNode from '../core/TempNode.js';
  2. import { addNodeClass } from '../core/Node.js';
  3. import { uv } from '../accessors/UVNode.js';
  4. import { normalView } from '../accessors/NormalNode.js';
  5. import { positionView } from '../accessors/PositionNode.js';
  6. import { faceDirection } from './FrontFacingNode.js';
  7. import { addNodeElement, tslFn, nodeProxy, float, vec2 } from '../shadernode/ShaderNode.js';
  8. // Bump Mapping Unparametrized Surfaces on the GPU by Morten S. Mikkelsen
  9. // https://mmikk.github.io/papers3d/mm_sfgrad_bump.pdf
  10. // Evaluate the derivative of the height w.r.t. screen-space using forward differencing (listing 2)
  11. const dHdxy_fwd = tslFn( ( { textureNode, bumpScale } ) => {
  12. let texNode = textureNode;
  13. if ( texNode.isTextureNode !== true ) {
  14. texNode.traverse( ( node ) => {
  15. if ( node.isTextureNode === true ) texNode = node;
  16. } );
  17. }
  18. if ( texNode.isTextureNode !== true ) {
  19. throw new Error( 'THREE.TSL: dHdxy_fwd() requires a TextureNode.' );
  20. }
  21. const Hll = float( textureNode );
  22. const uvNode = texNode.uvNode || uv();
  23. // It's used to preserve the same TextureNode instance
  24. const sampleTexture = ( uv ) => textureNode.cache().context( { getUV: () => uv, forceUVContext: true } );
  25. return vec2(
  26. float( sampleTexture( uvNode.add( uvNode.dFdx() ) ) ).sub( Hll ),
  27. float( sampleTexture( uvNode.add( uvNode.dFdy() ) ) ).sub( Hll )
  28. ).mul( bumpScale );
  29. } );
  30. const perturbNormalArb = tslFn( ( inputs ) => {
  31. const { surf_pos, surf_norm, dHdxy } = inputs;
  32. // normalize is done to ensure that the bump map looks the same regardless of the texture's scale
  33. const vSigmaX = surf_pos.dFdx().normalize();
  34. const vSigmaY = surf_pos.dFdy().normalize();
  35. const vN = surf_norm; // normalized
  36. const R1 = vSigmaY.cross( vN );
  37. const R2 = vN.cross( vSigmaX );
  38. const fDet = vSigmaX.dot( R1 ).mul( faceDirection );
  39. const vGrad = fDet.sign().mul( dHdxy.x.mul( R1 ).add( dHdxy.y.mul( R2 ) ) );
  40. return fDet.abs().mul( surf_norm ).sub( vGrad ).normalize();
  41. } );
  42. class BumpMapNode extends TempNode {
  43. constructor( textureNode, scaleNode = null ) {
  44. super( 'vec3' );
  45. this.textureNode = textureNode;
  46. this.scaleNode = scaleNode;
  47. }
  48. setup() {
  49. const bumpScale = this.scaleNode !== null ? this.scaleNode : 1;
  50. const dHdxy = dHdxy_fwd( { textureNode: this.textureNode, bumpScale } );
  51. return perturbNormalArb( {
  52. surf_pos: positionView,
  53. surf_norm: normalView,
  54. dHdxy
  55. } );
  56. }
  57. }
  58. export default BumpMapNode;
  59. export const bumpMap = nodeProxy( BumpMapNode );
  60. addNodeElement( 'bumpMap', bumpMap );
  61. addNodeClass( 'BumpMapNode', BumpMapNode );