SubsurfaceScatteringShader.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import {
  2. Color,
  3. ShaderChunk,
  4. ShaderLib,
  5. UniformsUtils
  6. } from 'three';
  7. function replaceAll( string, find, replace ) {
  8. return string.split( find ).join( replace );
  9. }
  10. const meshphong_frag_head = ShaderChunk[ 'meshphong_frag' ].slice( 0, ShaderChunk[ 'meshphong_frag' ].indexOf( 'void main() {' ) );
  11. const meshphong_frag_body = ShaderChunk[ 'meshphong_frag' ].slice( ShaderChunk[ 'meshphong_frag' ].indexOf( 'void main() {' ) );
  12. /** @module SubsurfaceScatteringShader */
  13. /**
  14. * Subsurface Scattering shader.
  15. *
  16. * Based on GDC 2011 – [Approximating Translucency for a Fast, Cheap and Convincing Subsurface Scattering Look]{@link https://colinbarrebrisebois.com/2011/03/07/gdc-2011-approximating-translucency-for-a-fast-cheap-and-convincing-subsurface-scattering-look/}
  17. *
  18. * @constant
  19. * @type {Object}
  20. */
  21. const SubsurfaceScatteringShader = {
  22. name: 'SubsurfaceScatteringShader',
  23. uniforms: UniformsUtils.merge( [
  24. ShaderLib[ 'phong' ].uniforms,
  25. {
  26. 'thicknessMap': { value: null },
  27. 'thicknessColor': { value: new Color( 0xffffff ) },
  28. 'thicknessDistortion': { value: 0.1 },
  29. 'thicknessAmbient': { value: 0.0 },
  30. 'thicknessAttenuation': { value: 0.1 },
  31. 'thicknessPower': { value: 2.0 },
  32. 'thicknessScale': { value: 10.0 }
  33. }
  34. ] ),
  35. vertexShader: [
  36. '#define USE_UV',
  37. ShaderChunk[ 'meshphong_vert' ],
  38. ].join( '\n' ),
  39. fragmentShader: [
  40. '#define USE_UV',
  41. '#define SUBSURFACE',
  42. meshphong_frag_head,
  43. 'uniform sampler2D thicknessMap;',
  44. 'uniform float thicknessPower;',
  45. 'uniform float thicknessScale;',
  46. 'uniform float thicknessDistortion;',
  47. 'uniform float thicknessAmbient;',
  48. 'uniform float thicknessAttenuation;',
  49. 'uniform vec3 thicknessColor;',
  50. 'void RE_Direct_Scattering(const in IncidentLight directLight, const in vec2 uv, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, inout ReflectedLight reflectedLight) {',
  51. ' vec3 thickness = thicknessColor * texture2D(thicknessMap, uv).r;',
  52. ' vec3 scatteringHalf = normalize(directLight.direction + (geometryNormal * thicknessDistortion));',
  53. ' float scatteringDot = pow(saturate(dot(geometryViewDir, -scatteringHalf)), thicknessPower) * thicknessScale;',
  54. ' vec3 scatteringIllu = (scatteringDot + thicknessAmbient) * thickness;',
  55. ' reflectedLight.directDiffuse += scatteringIllu * thicknessAttenuation * directLight.color;',
  56. '}',
  57. meshphong_frag_body.replace( '#include <lights_fragment_begin>',
  58. replaceAll(
  59. ShaderChunk[ 'lights_fragment_begin' ],
  60. 'RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );',
  61. [
  62. 'RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );',
  63. '#if defined( SUBSURFACE ) && defined( USE_UV )',
  64. ' RE_Direct_Scattering(directLight, vUv, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, reflectedLight);',
  65. '#endif',
  66. ].join( '\n' )
  67. ),
  68. ),
  69. ].join( '\n' ),
  70. };
  71. export { SubsurfaceScatteringShader };
粤ICP备19079148号