SubsurfaceScatteringShader.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. ( function () {
  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. const meshphong_frag_head = THREE.ShaderChunk[ 'meshphong_frag' ].slice( 0, THREE.ShaderChunk[ 'meshphong_frag' ].indexOf( 'void main() {' ) );
  13. const meshphong_frag_body = THREE.ShaderChunk[ 'meshphong_frag' ].slice( THREE.ShaderChunk[ 'meshphong_frag' ].indexOf( 'void main() {' ) );
  14. const SubsurfaceScatteringShader = {
  15. uniforms: THREE.UniformsUtils.merge( [ THREE.ShaderLib[ 'phong' ].uniforms, {
  16. 'thicknessMap': {
  17. value: null
  18. },
  19. 'thicknessColor': {
  20. value: new THREE.Color( 0xffffff )
  21. },
  22. 'thicknessDistortion': {
  23. value: 0.1
  24. },
  25. 'thicknessAmbient': {
  26. value: 0.0
  27. },
  28. 'thicknessAttenuation': {
  29. value: 0.1
  30. },
  31. 'thicknessPower': {
  32. value: 2.0
  33. },
  34. 'thicknessScale': {
  35. value: 10.0
  36. }
  37. } ] ),
  38. vertexShader: [ '#define USE_UV', THREE.ShaderChunk[ 'meshphong_vert' ] ].join( '\n' ),
  39. fragmentShader: [ '#define USE_UV', '#define SUBSURFACE', meshphong_frag_head, 'uniform sampler2D thicknessMap;', 'uniform float thicknessPower;', 'uniform float thicknessScale;', 'uniform float thicknessDistortion;', 'uniform float thicknessAmbient;', 'uniform float thicknessAttenuation;', 'uniform vec3 thicknessColor;', 'void RE_Direct_Scattering(const in IncidentLight directLight, const in vec2 uv, const in GeometricContext geometry, inout ReflectedLight reflectedLight) {', ' vec3 thickness = thicknessColor * texture2D(thicknessMap, uv).r;', ' vec3 scatteringHalf = normalize(directLight.direction + (geometry.normal * thicknessDistortion));', ' float scatteringDot = pow(saturate(dot(geometry.viewDir, -scatteringHalf)), thicknessPower) * thicknessScale;', ' vec3 scatteringIllu = (scatteringDot + thicknessAmbient) * thickness;', ' reflectedLight.directDiffuse += scatteringIllu * thicknessAttenuation * directLight.color;', '}', meshphong_frag_body.replace( '#include <lights_fragment_begin>', replaceAll( THREE.ShaderChunk[ 'lights_fragment_begin' ], 'RE_Direct( directLight, geometry, material, reflectedLight );', [ 'RE_Direct( directLight, geometry, material, reflectedLight );', '#if defined( SUBSURFACE ) && defined( USE_UV )', ' RE_Direct_Scattering(directLight, vUv, geometry, reflectedLight);', '#endif' ].join( '\n' ) ) ) ].join( '\n' )
  40. };
  41. THREE.SubsurfaceScatteringShader = SubsurfaceScatteringShader;
  42. } )();