NormalMapNode.js 3.4 KB

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