TranslucentShader.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**
  2. * @author daoshengmu / http://dsmu.me/
  3. *
  4. * ------------------------------------------------------------------------------------------
  5. * Subsurface Scattering shader
  6. * Base on GDC 2011 – Approximating Translucency for a Fast, Cheap and Convincing Subsurface Scattering Look
  7. * https://colinbarrebrisebois.com/2011/03/07/gdc-2011-approximating-translucency-for-a-fast-cheap-and-convincing-subsurface-scattering-look/
  8. *------------------------------------------------------------------------------------------
  9. */
  10. import {
  11. Color,
  12. ShaderChunk,
  13. UniformsLib,
  14. UniformsUtils
  15. } from "../../../build/three.module.js";
  16. var TranslucentShader = {
  17. uniforms: UniformsUtils.merge( [
  18. UniformsLib[ "common" ],
  19. UniformsLib[ "lights" ],
  20. {
  21. "color": { value: new Color( 0xffffff ) },
  22. "diffuse": { value: new Color( 0xffffff ) },
  23. "specular": { value: new Color( 0xffffff ) },
  24. "emissive": { value: new Color( 0x000000 ) },
  25. "opacity": { value: 1 },
  26. "shininess": { value: 1 },
  27. "thicknessMap": { value: null },
  28. "thicknessColor": { value: new Color( 0xffffff ) },
  29. "thicknessDistortion": { value: 0.1 },
  30. "thicknessAmbient": { value: 0.0 },
  31. "thicknessAttenuation": { value: 0.1 },
  32. "thicknessPower": { value: 2.0 },
  33. "thicknessScale": { value: 10.0 }
  34. }
  35. ] ),
  36. vertexShader: [
  37. "varying vec3 vNormal;",
  38. "varying vec2 vUv;",
  39. "varying vec3 vViewPosition;",
  40. ShaderChunk[ "common" ],
  41. "void main() {",
  42. " vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
  43. " vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  44. " vViewPosition = -mvPosition.xyz;",
  45. " vNormal = normalize( normalMatrix * normal );",
  46. " vUv = uv;",
  47. " gl_Position = projectionMatrix * mvPosition;",
  48. "}",
  49. ].join( "\n" ),
  50. fragmentShader: [
  51. "#define USE_UV",
  52. "#define USE_MAP",
  53. "#define PHONG",
  54. "#define TRANSLUCENT",
  55. "#include <common>",
  56. "#include <bsdfs>",
  57. "#include <uv_pars_fragment>",
  58. "#include <map_pars_fragment>",
  59. "#include <lights_phong_pars_fragment>",
  60. "varying vec3 vColor;",
  61. "uniform vec3 diffuse;",
  62. "uniform vec3 specular;",
  63. "uniform vec3 emissive;",
  64. "uniform float opacity;",
  65. "uniform float shininess;",
  66. // Translucency
  67. "uniform sampler2D thicknessMap;",
  68. "uniform float thicknessPower;",
  69. "uniform float thicknessScale;",
  70. "uniform float thicknessDistortion;",
  71. "uniform float thicknessAmbient;",
  72. "uniform float thicknessAttenuation;",
  73. "uniform vec3 thicknessColor;",
  74. ShaderChunk[ "lights_pars_begin" ],
  75. "void RE_Direct_Scattering(const in IncidentLight directLight, const in vec2 uv, const in GeometricContext geometry, inout ReflectedLight reflectedLight) {",
  76. " vec3 thickness = thicknessColor * texture2D(thicknessMap, uv).r;",
  77. " vec3 scatteringHalf = normalize(directLight.direction + (geometry.normal * thicknessDistortion));",
  78. " float scatteringDot = pow(saturate(dot(geometry.viewDir, -scatteringHalf)), thicknessPower) * thicknessScale;",
  79. " vec3 scatteringIllu = (scatteringDot + thicknessAmbient) * thickness;",
  80. " reflectedLight.directDiffuse += scatteringIllu * thicknessAttenuation * directLight.color;",
  81. "}",
  82. "void main() {",
  83. " vec3 normal = normalize( vNormal );",
  84. " vec3 viewerDirection = normalize( vViewPosition );",
  85. " vec4 diffuseColor = vec4( diffuse, opacity );",
  86. " ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );",
  87. ShaderChunk[ "map_fragment" ],
  88. ShaderChunk[ "color_fragment" ],
  89. ShaderChunk[ "specularmap_fragment" ],
  90. " vec3 totalEmissiveRadiance = emissive;",
  91. ShaderChunk[ "lights_phong_fragment" ],
  92. // Doing lights fragment begin.
  93. " GeometricContext geometry;",
  94. " geometry.position = - vViewPosition;",
  95. " geometry.normal = normal;",
  96. " geometry.viewDir = normalize( vViewPosition );",
  97. " IncidentLight directLight;",
  98. " #if ( NUM_POINT_LIGHTS > 0 ) && defined( RE_Direct )",
  99. " PointLight pointLight;",
  100. " #pragma unroll_loop",
  101. " for ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {",
  102. " pointLight = pointLights[ i ];",
  103. " getPointDirectLightIrradiance( pointLight, geometry, directLight );",
  104. " #ifdef USE_SHADOWMAP",
  105. " directLight.color *= all( bvec2( pointLight.shadow, directLight.visible ) ) ? getPointShadow( pointShadowMap[ i ], pointLight.shadowMapSize, pointLight.shadowBias, pointLight.shadowRadius, vPointShadowCoord[ i ], pointLight.shadowCameraNear, pointLight.shadowCameraFar ) : 1.0;",
  106. " #endif",
  107. " RE_Direct( directLight, geometry, material, reflectedLight );",
  108. " #if defined( TRANSLUCENT ) && defined( USE_UV )",
  109. " RE_Direct_Scattering(directLight, vUv, geometry, reflectedLight);",
  110. " #endif",
  111. " }",
  112. " #endif",
  113. " #if ( NUM_DIR_LIGHTS > 0 ) && defined( RE_Direct )",
  114. " DirectionalLight directionalLight;",
  115. " #pragma unroll_loop",
  116. " for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {",
  117. " directionalLight = directionalLights[ i ];",
  118. " getDirectionalDirectLightIrradiance( directionalLight, geometry, directLight );",
  119. " #ifdef USE_SHADOWMAP",
  120. " directLight.color *= all( bvec2( directionalLight.shadow, directLight.visible ) ) ? getShadow( directionalShadowMap[ i ], directionalLight.shadowMapSize, directionalLight.shadowBias, directionalLight.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;",
  121. " #endif",
  122. " RE_Direct( directLight, geometry, material, reflectedLight );",
  123. " #if defined( TRANSLUCENT ) && defined( USE_UV )",
  124. " RE_Direct_Scattering(directLight, vUv, geometry, reflectedLight);",
  125. " #endif",
  126. " }",
  127. " #endif",
  128. " #if defined( RE_IndirectDiffuse )",
  129. " vec3 irradiance = getAmbientLightIrradiance( ambientLightColor );",
  130. " #if ( NUM_HEMI_LIGHTS > 0 )",
  131. " #pragma unroll_loop",
  132. " for ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {",
  133. " irradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], geometry );",
  134. " }",
  135. " #endif",
  136. " #endif",
  137. " #if defined( RE_IndirectSpecular )",
  138. " vec3 radiance = vec3( 0.0 );",
  139. " vec3 clearcoatRadiance = vec3( 0.0 );",
  140. " #endif",
  141. ShaderChunk[ "lights_fragment_end" ],
  142. " vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance;",
  143. " gl_FragColor = vec4( outgoingLight, diffuseColor.a );", // TODO, this should be pre-multiplied to allow for bright highlights on very transparent objects
  144. ShaderChunk[ "encodings_fragment" ],
  145. "}"
  146. ].join( "\n" ),
  147. };
  148. export { TranslucentShader };