TranslucentShader.js 5.9 KB

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