SubsurfaceScatteringShader.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. console.warn( "THREE.SubsurfaceScatteringShader: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation." );
  2. /**
  3. * ------------------------------------------------------------------------------------------
  4. * Subsurface Scattering shader
  5. * Based on GDC 2011 – Approximating Translucency for a Fast, Cheap and Convincing Subsurface Scattering Look
  6. * https://colinbarrebrisebois.com/2011/03/07/gdc-2011-approximating-translucency-for-a-fast-cheap-and-convincing-subsurface-scattering-look/
  7. *------------------------------------------------------------------------------------------
  8. */
  9. function replaceAll( string, find, replace ) {
  10. return string.split( find ).join( replace );
  11. }
  12. var meshphong_frag_head = THREE.ShaderChunk[ "meshphong_frag" ].slice( 0, THREE.ShaderChunk[ "meshphong_frag" ].indexOf( 'void main() {' ) );
  13. var meshphong_frag_body = THREE.ShaderChunk[ "meshphong_frag" ].slice( THREE.ShaderChunk[ "meshphong_frag" ].indexOf( 'void main() {' ) );
  14. THREE.SubsurfaceScatteringShader = {
  15. uniforms: THREE.UniformsUtils.merge( [
  16. THREE.ShaderLib[ "phong" ].uniforms,
  17. {
  18. "thicknessMap": { value: null },
  19. "thicknessColor": { value: new THREE.Color( 0xffffff ) },
  20. "thicknessDistortion": { value: 0.1 },
  21. "thicknessAmbient": { value: 0.0 },
  22. "thicknessAttenuation": { value: 0.1 },
  23. "thicknessPower": { value: 2.0 },
  24. "thicknessScale": { value: 10.0 }
  25. }
  26. ] ),
  27. vertexShader: [
  28. "#define USE_UV",
  29. THREE.ShaderChunk[ "meshphong_vert" ],
  30. ].join( "\n" ),
  31. fragmentShader: [
  32. "#define USE_UV",
  33. "#define SUBSURFACE",
  34. meshphong_frag_head,
  35. "uniform sampler2D thicknessMap;",
  36. "uniform float thicknessPower;",
  37. "uniform float thicknessScale;",
  38. "uniform float thicknessDistortion;",
  39. "uniform float thicknessAmbient;",
  40. "uniform float thicknessAttenuation;",
  41. "uniform vec3 thicknessColor;",
  42. "void RE_Direct_Scattering(const in IncidentLight directLight, const in vec2 uv, const in GeometricContext geometry, inout ReflectedLight reflectedLight) {",
  43. " vec3 thickness = thicknessColor * texture2D(thicknessMap, uv).r;",
  44. " vec3 scatteringHalf = normalize(directLight.direction + (geometry.normal * thicknessDistortion));",
  45. " float scatteringDot = pow(saturate(dot(geometry.viewDir, -scatteringHalf)), thicknessPower) * thicknessScale;",
  46. " vec3 scatteringIllu = (scatteringDot + thicknessAmbient) * thickness;",
  47. " reflectedLight.directDiffuse += scatteringIllu * thicknessAttenuation * directLight.color;",
  48. "}",
  49. meshphong_frag_body.replace( "#include <lights_fragment_begin>",
  50. replaceAll(
  51. THREE.ShaderChunk[ 'lights_fragment_begin' ],
  52. 'RE_Direct( directLight, geometry, material, reflectedLight );',
  53. [
  54. "RE_Direct( directLight, geometry, material, reflectedLight );",
  55. "#if defined( SUBSURFACE ) && defined( USE_UV )",
  56. " RE_Direct_Scattering(directLight, vUv, geometry, reflectedLight);",
  57. "#endif",
  58. ].join( "\n" )
  59. ),
  60. ),
  61. ].join( "\n" ),
  62. };