SubsurfaceScatteringShader.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * @author daoshengmu / http://dsmu.me/
  3. *
  4. * ------------------------------------------------------------------------------------------
  5. * Subsurface Scattering shader
  6. * Based 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. function replaceAll( string, find, replace ) {
  11. return string.split( find ).join( replace );
  12. }
  13. var meshphong_frag_head = THREE.ShaderChunk[ "meshphong_frag" ].slice( 0, THREE.ShaderChunk[ "meshphong_frag" ].indexOf( 'void main() {' ) );
  14. var meshphong_frag_body = THREE.ShaderChunk[ "meshphong_frag" ].slice( THREE.ShaderChunk[ "meshphong_frag" ].indexOf( 'void main() {' ) );
  15. THREE.SubsurfaceScatteringShader = {
  16. uniforms: THREE.UniformsUtils.merge( [
  17. THREE.ShaderLib[ "phong" ].uniforms,
  18. {
  19. "thicknessMap": { value: null },
  20. "thicknessColor": { value: new THREE.Color( 0xffffff ) },
  21. "thicknessDistortion": { value: 0.1 },
  22. "thicknessAmbient": { value: 0.0 },
  23. "thicknessAttenuation": { value: 0.1 },
  24. "thicknessPower": { value: 2.0 },
  25. "thicknessScale": { value: 10.0 }
  26. }
  27. ] ),
  28. vertexShader: [
  29. "#define USE_UV",
  30. THREE.ShaderChunk[ "meshphong_vert" ],
  31. ].join( "\n" ),
  32. fragmentShader: [
  33. "#define USE_UV",
  34. "#define SUBSURFACE",
  35. meshphong_frag_head,
  36. "uniform sampler2D thicknessMap;",
  37. "uniform float thicknessPower;",
  38. "uniform float thicknessScale;",
  39. "uniform float thicknessDistortion;",
  40. "uniform float thicknessAmbient;",
  41. "uniform float thicknessAttenuation;",
  42. "uniform vec3 thicknessColor;",
  43. "void RE_Direct_Scattering(const in IncidentLight directLight, const in vec2 uv, const in GeometricContext geometry, inout ReflectedLight reflectedLight) {",
  44. " vec3 thickness = thicknessColor * texture2D(thicknessMap, uv).r;",
  45. " vec3 scatteringHalf = normalize(directLight.direction + (geometry.normal * thicknessDistortion));",
  46. " float scatteringDot = pow(saturate(dot(geometry.viewDir, -scatteringHalf)), thicknessPower) * thicknessScale;",
  47. " vec3 scatteringIllu = (scatteringDot + thicknessAmbient) * thickness;",
  48. " reflectedLight.directDiffuse += scatteringIllu * thicknessAttenuation * directLight.color;",
  49. "}",
  50. meshphong_frag_body.replace( "#include <lights_fragment_begin>",
  51. replaceAll(
  52. THREE.ShaderChunk[ 'lights_fragment_begin' ],
  53. 'RE_Direct( directLight, geometry, material, reflectedLight );',
  54. [
  55. "RE_Direct( directLight, geometry, material, reflectedLight );",
  56. "#if defined( SUBSURFACE ) && defined( USE_UV )",
  57. " RE_Direct_Scattering(directLight, vUv, geometry, reflectedLight);",
  58. "#endif",
  59. ].join( "\n" )
  60. ),
  61. ),
  62. ].join( "\n" ),
  63. };