ShaderTranslucent.js 6.4 KB

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