11/**
22 * @author daoshengmu / http://dsmu.me/
33 *
4+ * ------------------------------------------------------------------------------------------
5+ * Subsurface Scattering shader
6+ * Base 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+ *------------------------------------------------------------------------------------------
49 */
510
11+ THREE . TranslucentShader = {
612
7- THREE . TranslucentShader = function TranslucentShader ( ) {
8-
9- /* ------------------------------------------------------------------------------------------
10- // Subsurface Scattering shader
11- // - Base on GDC 2011 – Approximating Translucency for a Fast, Cheap and Convincing Subsurface Scattering Look
12- // https://colinbarrebrisebois.com/2011/03/07/gdc-2011-approximating-translucency-for-a-fast-cheap-and-convincing-subsurface-scattering-look/
13- // ------------------------------------------------------------------------------------------ */
14-
15- this . uniforms = THREE . UniformsUtils . merge ( [
13+ uniforms : THREE . UniformsUtils . merge ( [
1614
1715 THREE . UniformsLib [ "common" ] ,
1816 THREE . UniformsLib [ "lights" ] ,
19-
2017 {
21- "color" : { value : new THREE . Color ( 0xffffff ) } ,
22- "diffuse" : { value : new THREE . Color ( 0xffffff ) } ,
18+ "color" : { value : new THREE . Color ( 0xffffff ) } ,
19+ "diffuse" : { value : new THREE . Color ( 0xffffff ) } ,
2320 "specular" : { value : new THREE . Color ( 0xffffff ) } ,
2421 "emissive" : { value : new THREE . Color ( 0x000000 ) } ,
2522 "opacity" : { value : 1 } ,
@@ -34,9 +31,36 @@ THREE.TranslucentShader = function TranslucentShader() {
3431 "thicknessScale" : { value : 10.0 }
3532 }
3633
37- ] ) ;
34+ ] ) ,
35+
36+ vertexShader : [
37+
38+ "varying vec3 vNormal;" ,
39+ "varying vec2 vUv;" ,
40+
41+ "varying vec3 vViewPosition;" ,
42+
43+ THREE . ShaderChunk [ "common" ] ,
44+
45+ "void main() {" ,
46+
47+ " vec4 worldPosition = modelMatrix * vec4( position, 1.0 );" ,
48+
49+ " vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );" ,
50+
51+ " vViewPosition = -mvPosition.xyz;" ,
52+
53+ " vNormal = normalize( normalMatrix * normal );" ,
54+
55+ " vUv = uv;" ,
56+
57+ " gl_Position = projectionMatrix * mvPosition;" ,
58+
59+ "}" ,
60+
61+ ] . join ( "\n" ) ,
3862
39- this . fragmentShader = [
63+ fragmentShader : [
4064 "#define USE_MAP" ,
4165 "#define PHONG" ,
4266 "#define TRANSLUCENT" ,
@@ -82,13 +106,13 @@ THREE.TranslucentShader = function TranslucentShader() {
82106 " vec4 diffuseColor = vec4( diffuse, opacity );" ,
83107 " ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );" ,
84108
85- THREE . ShaderChunk [ "map_fragment" ] ,
86- THREE . ShaderChunk [ "color_fragment" ] ,
87- THREE . ShaderChunk [ "specularmap_fragment" ] ,
109+ THREE . ShaderChunk [ "map_fragment" ] ,
110+ THREE . ShaderChunk [ "color_fragment" ] ,
111+ THREE . ShaderChunk [ "specularmap_fragment" ] ,
88112
89113 " vec3 totalEmissiveRadiance = emissive;" ,
90114
91- THREE . ShaderChunk [ "lights_phong_fragment" ] ,
115+ THREE . ShaderChunk [ "lights_phong_fragment" ] ,
92116
93117 // Doing lights fragment begin.
94118 " GeometricContext geometry;" ,
@@ -165,42 +189,15 @@ THREE.TranslucentShader = function TranslucentShader() {
165189 " vec3 clearCoatRadiance = vec3( 0.0 );" ,
166190
167191 " #endif" ,
168- THREE . ShaderChunk [ "lights_fragment_end" ] ,
192+ THREE . ShaderChunk [ "lights_fragment_end" ] ,
169193
170194 " vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance;" ,
171195 " gl_FragColor = vec4( outgoingLight, diffuseColor.a );" , // TODO, this should be pre-multiplied to allow for bright highlights on very transparent objects
172196
173- THREE . ShaderChunk [ "encodings_fragment" ] ,
197+ THREE . ShaderChunk [ "encodings_fragment" ] ,
174198
175199 "}"
176200
177201 ] . join ( "\n" ) ,
178202
179- this . vertexShader = [
180-
181- "varying vec3 vNormal;" ,
182- "varying vec2 vUv;" ,
183-
184- "varying vec3 vViewPosition;" ,
185-
186- THREE . ShaderChunk [ "common" ] ,
187-
188- "void main() {" ,
189-
190- " vec4 worldPosition = modelMatrix * vec4( position, 1.0 );" ,
191-
192- " vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );" ,
193-
194- " vViewPosition = -mvPosition.xyz;" ,
195-
196- " vNormal = normalize( normalMatrix * normal );" ,
197-
198- " vUv = uv;" ,
199-
200- " gl_Position = projectionMatrix * mvPosition;" ,
201-
202- "}" ,
203-
204- ] . join ( "\n" )
205-
206203} ;
0 commit comments