NormalMapNode.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import {
  2. BackSide
  3. } from '../../../../build/three.module.js';
  4. import { TempNode } from '../core/TempNode.js';
  5. import { Vector2Node } from '../inputs/Vector2Node.js';
  6. import { FunctionNode } from '../core/FunctionNode.js';
  7. import { UVNode } from '../accessors/UVNode.js';
  8. import { NormalNode } from '../accessors/NormalNode.js';
  9. import { PositionNode } from '../accessors/PositionNode.js';
  10. function NormalMapNode( value, scale ) {
  11. TempNode.call( this, 'v3' );
  12. this.value = value;
  13. this.scale = scale || new Vector2Node( 1, 1 );
  14. }
  15. NormalMapNode.Nodes = ( function () {
  16. var perturbNormal2Arb = new FunctionNode(
  17. // Per-Pixel Tangent Space Normal Mapping
  18. // http://hacksoflife.blogspot.ch/2009/11/per-pixel-tangent-space-normal-mapping.html
  19. `vec3 perturbNormal2Arb( vec3 eye_pos, vec3 surf_norm, vec3 map, vec2 vUv, vec2 normalScale ) {
  20. // Workaround for Adreno 3XX dFd*( vec3 ) bug. See #9988
  21. vec3 q0 = vec3( dFdx( eye_pos.x ), dFdx( eye_pos.y ), dFdx( eye_pos.z ) );
  22. vec3 q1 = vec3( dFdy( eye_pos.x ), dFdy( eye_pos.y ), dFdy( eye_pos.z ) );
  23. vec2 st0 = dFdx( vUv.st );
  24. vec2 st1 = dFdy( vUv.st );
  25. float scale = sign( st1.t * st0.s - st0.t * st1.s ); // we do not care about the magnitude
  26. vec3 S = normalize( ( q0 * st1.t - q1 * st0.t ) * scale );
  27. vec3 T = normalize( ( - q0 * st1.s + q1 * st0.s ) * scale );
  28. vec3 N = normalize( surf_norm );
  29. vec3 mapN = map * 2.0 - 1.0;
  30. mapN.xy *= normalScale;
  31. #ifdef DOUBLE_SIDED
  32. // Workaround for Adreno GPUs gl_FrontFacing bug. See #15850 and #10331
  33. if ( dot( cross( S, T ), N ) < 0.0 ) mapN.xy *= - 1.0;
  34. #else
  35. mapN.xy *= ( float( gl_FrontFacing ) * 2.0 - 1.0 );
  36. #endif
  37. mat3 tsn = mat3( S, T, N );
  38. return normalize( tsn * mapN );
  39. }`, null, { derivatives: true } );
  40. return {
  41. perturbNormal2Arb: perturbNormal2Arb
  42. };
  43. } )();
  44. NormalMapNode.prototype = Object.create( TempNode.prototype );
  45. NormalMapNode.prototype.constructor = NormalMapNode;
  46. NormalMapNode.prototype.nodeType = 'NormalMap';
  47. NormalMapNode.prototype.generate = function ( builder, output ) {
  48. if ( builder.isShader( 'fragment' ) ) {
  49. var perturbNormal2Arb = builder.include( NormalMapNode.Nodes.perturbNormal2Arb );
  50. this.normal = this.normal || new NormalNode();
  51. this.position = this.position || new PositionNode( PositionNode.VIEW );
  52. this.uv = this.uv || new UVNode();
  53. var scale = this.scale.build( builder, 'v2' );
  54. if ( builder.material.side === BackSide ) {
  55. scale = '-' + scale;
  56. }
  57. return builder.format( perturbNormal2Arb + '( -' + this.position.build( builder, 'v3' ) + ', ' +
  58. this.normal.build( builder, 'v3' ) + ', ' +
  59. this.value.build( builder, 'v3' ) + ', ' +
  60. this.uv.build( builder, 'v2' ) + ', ' +
  61. scale + ' )', this.getType( builder ), output );
  62. } else {
  63. console.warn( 'THREE.NormalMapNode is not compatible with ' + builder.shader + ' shader.' );
  64. return builder.format( 'vec3( 0.0 )', this.getType( builder ), output );
  65. }
  66. };
  67. NormalMapNode.prototype.copy = function ( source ) {
  68. TempNode.prototype.copy.call( this, source );
  69. this.value = source.value;
  70. this.scale = source.scale;
  71. return this;
  72. };
  73. NormalMapNode.prototype.toJSON = function ( meta ) {
  74. var data = this.getJSONNode( meta );
  75. if ( ! data ) {
  76. data = this.createJSONNode( meta );
  77. data.value = this.value.toJSON( meta ).uuid;
  78. data.scale = this.scale.toJSON( meta ).uuid;
  79. }
  80. return data;
  81. };
  82. export { NormalMapNode };