Sfoglia il codice sorgente

Merge pull request #16628 from Mugen87/dev33

JSM: Added module and TS file for TranslucentShader.
Mr.doob 6 anni fa
parent
commit
38a49a5c3c

+ 1 - 0
docs/manual/en/introduction/Import-via-modules.html

@@ -290,6 +290,7 @@
 						<li>TechnicolorShader</li>
 						<li>TerrainShader</li>
 						<li>ToneMapShader</li>
+						<li>TranslucentShader</li>
 						<li>TriangleBlurShader</li>
 						<li>UnpackDepthRGBAShader</li>
 						<li>VerticalBlurShader</li>

+ 44 - 47
examples/js/ShaderTranslucent.js → examples/js/shaders/TranslucentShader.js

@@ -1,25 +1,22 @@
 /**
  * @author daoshengmu / http://dsmu.me/
  *
+ * ------------------------------------------------------------------------------------------
+ * Subsurface Scattering shader
+ * Base on GDC 2011 – Approximating Translucency for a Fast, Cheap and Convincing Subsurface Scattering Look
+ * https://colinbarrebrisebois.com/2011/03/07/gdc-2011-approximating-translucency-for-a-fast-cheap-and-convincing-subsurface-scattering-look/
+ *------------------------------------------------------------------------------------------
  */
 
+THREE.TranslucentShader = {
 
-THREE.TranslucentShader = function TranslucentShader() {
-
-	/* ------------------------------------------------------------------------------------------
-	//	Subsurface Scattering shader
-	// 		- Base on GDC 2011 – Approximating Translucency for a Fast, Cheap and Convincing Subsurface Scattering Look
-	// 			https://colinbarrebrisebois.com/2011/03/07/gdc-2011-approximating-translucency-for-a-fast-cheap-and-convincing-subsurface-scattering-look/
-	// ------------------------------------------------------------------------------------------ */
-
-	this.uniforms = THREE.UniformsUtils.merge( [
+	uniforms: THREE.UniformsUtils.merge( [
 
 		THREE.UniformsLib[ "common" ],
 		THREE.UniformsLib[ "lights" ],
-
 		{
-			"color":  { value: new THREE.Color( 0xffffff ) },
-			"diffuse":  { value: new THREE.Color( 0xffffff ) },
+			"color": { value: new THREE.Color( 0xffffff ) },
+			"diffuse": { value: new THREE.Color( 0xffffff ) },
 			"specular": { value: new THREE.Color( 0xffffff ) },
 			"emissive": { value: new THREE.Color( 0x000000 ) },
 			"opacity": { value: 1 },
@@ -34,9 +31,36 @@ THREE.TranslucentShader = function TranslucentShader() {
 			"thicknessScale": { value: 10.0 }
 		}
 
-	] );
+	] ),
+
+	vertexShader: [
+
+		"varying vec3 vNormal;",
+		"varying vec2 vUv;",
+
+		"varying vec3 vViewPosition;",
+
+		THREE.ShaderChunk[ "common" ],
+
+		"void main() {",
+
+		"	vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
+
+		"	vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
+
+		"	vViewPosition = -mvPosition.xyz;",
+
+		"	vNormal = normalize( normalMatrix * normal );",
+
+		"	vUv = uv;",
+
+		"	gl_Position = projectionMatrix * mvPosition;",
+
+		"}",
+
+	].join( "\n" ),
 
-	this.fragmentShader = [
+	fragmentShader: [
 		"#define USE_MAP",
 		"#define PHONG",
 		"#define TRANSLUCENT",
@@ -82,13 +106,13 @@ THREE.TranslucentShader = function TranslucentShader() {
 		"	vec4 diffuseColor = vec4( diffuse, opacity );",
 		"	ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );",
 
-			THREE.ShaderChunk[ "map_fragment" ],
-			THREE.ShaderChunk[ "color_fragment" ],
-			THREE.ShaderChunk[ "specularmap_fragment" ],
+		THREE.ShaderChunk[ "map_fragment" ],
+		THREE.ShaderChunk[ "color_fragment" ],
+		THREE.ShaderChunk[ "specularmap_fragment" ],
 
 		"	vec3 totalEmissiveRadiance = emissive;",
 
-			THREE.ShaderChunk["lights_phong_fragment"],
+		THREE.ShaderChunk[ "lights_phong_fragment" ],
 
 		// Doing lights fragment begin.
 		"	GeometricContext geometry;",
@@ -165,42 +189,15 @@ THREE.TranslucentShader = function TranslucentShader() {
 		"		vec3 clearCoatRadiance = vec3( 0.0 );",
 
 		"	#endif",
-			THREE.ShaderChunk["lights_fragment_end"],
+		THREE.ShaderChunk[ "lights_fragment_end" ],
 
 		"	vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance;",
 		"	gl_FragColor = vec4( outgoingLight, diffuseColor.a );",	// TODO, this should be pre-multiplied to allow for bright highlights on very transparent objects
 
-			THREE.ShaderChunk["encodings_fragment"],
+		THREE.ShaderChunk[ "encodings_fragment" ],
 
 		"}"
 
 	].join( "\n" ),
 
-	this.vertexShader = [
-
-		"varying vec3 vNormal;",
-		"varying vec2 vUv;",
-
-		"varying vec3 vViewPosition;",
-
-		THREE.ShaderChunk[ "common" ],
-
-		"void main() {",
-
-		"	vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
-
-		"	vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
-
-		"	vViewPosition = -mvPosition.xyz;",
-
-		"	vNormal = normalize( normalMatrix * normal );",
-
-		"	vUv = uv;",
-
-		"	gl_Position = projectionMatrix * mvPosition;",
-
-		"}",
-
-	].join( "\n" )
-
 };

+ 7 - 1
examples/jsm/loaders/GLTFLoader.js

@@ -2705,7 +2705,13 @@ var GLTFLoader = ( function () {
 							? new SkinnedMesh( geometry, material )
 							: new Mesh( geometry, material );
 
-						if ( mesh.isSkinnedMesh === true ) mesh.normalizeSkinWeights(); // #15319
+						if ( mesh.isSkinnedMesh === true && !mesh.geometry.attributes.skinWeight.normalized ) {
+
+							// we normalize floating point skin weight array to fix malformed assets (see #15319)
+							// it's important to skip this for non-float32 data since normalizeSkinWeights assumes non-normalized inputs
+							mesh.normalizeSkinWeights();
+
+						}
 
 						if ( primitive.mode === WEBGL_CONSTANTS.TRIANGLE_STRIP ) {
 

+ 3 - 4
examples/jsm/renderers/RaytracingRenderer.js

@@ -130,8 +130,6 @@ var RaytracingRenderer = function ( parameters ) {
 		canvasWidth = canvas.width;
 		canvasHeight = canvas.height;
 
-		context.fillStyle = 'white';
-
 		pool.forEach( updateSettings );
 
 	};
@@ -181,7 +179,6 @@ var RaytracingRenderer = function ( parameters ) {
 		} );
 
 		context.fillStyle = '#' + worker.color;
-
 		context.fillRect( blockX, blockY, blockSize, blockSize );
 
 	}
@@ -252,7 +249,9 @@ var RaytracingRenderer = function ( parameters ) {
 
 		} );
 
-		context.clearRect( 0, 0, canvasWidth, canvasHeight );
+		context.fillStyle = clearColor.getStyle();
+		context.fillRect( 0, 0, canvasWidth, canvasHeight );
+
 		reallyThen = Date.now();
 
 		xblocks = Math.ceil( canvasWidth / blockSize );

+ 39 - 0
examples/jsm/shaders/TranslucentShader.d.ts

@@ -0,0 +1,39 @@
+import {
+  Uniform
+} from '../../../src/Three';
+
+export interface TranslucentShader {
+  uniforms: {
+    alphaMap: Uniform;
+    ambientLightColor: Uniform;
+    color: Uniform;
+    diffuse: Uniform;
+    directionalLights: Uniform;
+    directionalShadowMap: Uniform;
+    directionalShadowMatrix: Uniform;
+    emissive: Uniform;
+    hemisphereLights: Uniform;
+    lightProbe: Uniform;
+    map: Uniform;
+    opacity: Uniform;
+    pointLights: Uniform;
+    pointShadowMap: Uniform;
+    pointShadowMatrix: Uniform;
+    rectAreaLights: Uniform;
+    shininess: Uniform;
+    specular: Uniform;
+    spotLights: Uniform;
+    spotShadowMap: Uniform;
+    spotShadowMatrix: Uniform;
+    thicknessAmbient: Uniform;
+    thicknessAttenuation: Uniform;
+    thicknessColor: Uniform;
+    thicknessDistortion: Uniform;
+    thicknessMap: Uniform;
+    thicknessPower: Uniform;
+    thicknessScale: Uniform;
+    uvTransform: Uniform;
+  };
+  vertexShader: string;
+  fragmentShader: string;
+}

+ 212 - 0
examples/jsm/shaders/TranslucentShader.js

@@ -0,0 +1,212 @@
+/**
+ * @author daoshengmu / http://dsmu.me/
+ *
+ * ------------------------------------------------------------------------------------------
+ * Subsurface Scattering shader
+ * Base on GDC 2011 – Approximating Translucency for a Fast, Cheap and Convincing Subsurface Scattering Look
+ * https://colinbarrebrisebois.com/2011/03/07/gdc-2011-approximating-translucency-for-a-fast-cheap-and-convincing-subsurface-scattering-look/
+ *------------------------------------------------------------------------------------------
+ */
+
+import {
+	Color,
+	ShaderChunk,
+	UniformsLib,
+	UniformsUtils
+} from "../../../build/three.module.js";
+
+var TranslucentShader = {
+
+	uniforms: UniformsUtils.merge( [
+
+		UniformsLib[ "common" ],
+		UniformsLib[ "lights" ],
+		{
+			"color": { value: new Color( 0xffffff ) },
+			"diffuse": { value: new Color( 0xffffff ) },
+			"specular": { value: new Color( 0xffffff ) },
+			"emissive": { value: new Color( 0x000000 ) },
+			"opacity": { value: 1 },
+			"shininess": { value: 1 },
+
+			"thicknessMap": { value: null },
+			"thicknessColor": { value: new Color( 0xffffff ) },
+			"thicknessDistortion": { value: 0.1 },
+			"thicknessAmbient": { value: 0.0 },
+			"thicknessAttenuation": { value: 0.1 },
+			"thicknessPower": { value: 2.0 },
+			"thicknessScale": { value: 10.0 }
+		}
+
+	] ),
+
+	vertexShader: [
+
+		"varying vec3 vNormal;",
+		"varying vec2 vUv;",
+
+		"varying vec3 vViewPosition;",
+
+		ShaderChunk[ "common" ],
+
+		"void main() {",
+
+		"	vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
+
+		"	vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
+
+		"	vViewPosition = -mvPosition.xyz;",
+
+		"	vNormal = normalize( normalMatrix * normal );",
+
+		"	vUv = uv;",
+
+		"	gl_Position = projectionMatrix * mvPosition;",
+
+		"}",
+
+	].join( "\n" ),
+
+	fragmentShader: [
+		"#define USE_MAP",
+		"#define PHONG",
+		"#define TRANSLUCENT",
+		"#include <common>",
+		"#include <bsdfs>",
+		"#include <uv_pars_fragment>",
+		"#include <map_pars_fragment>",
+		"#include <lights_phong_pars_fragment>",
+
+		"varying vec3 vColor;",
+
+		"uniform vec3 diffuse;",
+		"uniform vec3 specular;",
+		"uniform vec3 emissive;",
+		"uniform float opacity;",
+		"uniform float shininess;",
+
+		// Translucency
+		"uniform sampler2D thicknessMap;",
+		"uniform float thicknessPower;",
+		"uniform float thicknessScale;",
+		"uniform float thicknessDistortion;",
+		"uniform float thicknessAmbient;",
+		"uniform float thicknessAttenuation;",
+		"uniform vec3 thicknessColor;",
+
+		ShaderChunk[ "lights_pars_begin" ],
+
+		"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;",
+		"}",
+
+		"void main() {",
+
+		"	vec3 normal = normalize( vNormal );",
+
+		"	vec3 viewerDirection = normalize( vViewPosition );",
+
+		"	vec4 diffuseColor = vec4( diffuse, opacity );",
+		"	ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );",
+
+		ShaderChunk[ "map_fragment" ],
+		ShaderChunk[ "color_fragment" ],
+		ShaderChunk[ "specularmap_fragment" ],
+
+		"	vec3 totalEmissiveRadiance = emissive;",
+
+		ShaderChunk[ "lights_phong_fragment" ],
+
+		// Doing lights fragment begin.
+		"	GeometricContext geometry;",
+		"	geometry.position = - vViewPosition;",
+		"	geometry.normal = normal;",
+		"	geometry.viewDir = normalize( vViewPosition );",
+
+		"	IncidentLight directLight;",
+
+		"	#if ( NUM_POINT_LIGHTS > 0 ) && defined( RE_Direct )",
+
+		"		PointLight pointLight;",
+
+		"		#pragma unroll_loop",
+		"		for ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {",
+		"		 	pointLight = pointLights[ i ];",
+		"		 	getPointDirectLightIrradiance( pointLight, geometry, directLight );",
+
+		"			#ifdef USE_SHADOWMAP",
+		"			directLight.color *= all( bvec2( pointLight.shadow, directLight.visible ) ) ? getPointShadow( pointShadowMap[ i ], pointLight.shadowMapSize, pointLight.shadowBias, pointLight.shadowRadius, vPointShadowCoord[ i ], pointLight.shadowCameraNear, pointLight.shadowCameraFar ) : 1.0;",
+		"			#endif",
+
+		"			RE_Direct( directLight, geometry, material, reflectedLight );",
+
+		"			#if defined( TRANSLUCENT ) && defined( USE_MAP )",
+		"			RE_Direct_Scattering(directLight, vUv, geometry, reflectedLight);",
+		"			#endif",
+		"		}",
+
+		"		#endif",
+
+		"	#if ( NUM_DIR_LIGHTS > 0 ) && defined( RE_Direct )",
+
+		"		DirectionalLight directionalLight;",
+
+		"		#pragma unroll_loop",
+		"		for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {",
+		"			directionalLight = directionalLights[ i ];",
+		"			getDirectionalDirectLightIrradiance( directionalLight, geometry, directLight );",
+
+		"			#ifdef USE_SHADOWMAP",
+		"			directLight.color *= all( bvec2( directionalLight.shadow, directLight.visible ) ) ? getShadow( directionalShadowMap[ i ], directionalLight.shadowMapSize, directionalLight.shadowBias, directionalLight.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;",
+		"			#endif",
+
+		"			RE_Direct( directLight, geometry, material, reflectedLight );",
+
+		"			#if defined( TRANSLUCENT ) && defined( USE_MAP )",
+		"			RE_Direct_Scattering(directLight, vUv, geometry, reflectedLight);",
+		"			#endif",
+		"		}",
+
+		"	#endif",
+
+		"	#if defined( RE_IndirectDiffuse )",
+
+		"		vec3 irradiance = getAmbientLightIrradiance( ambientLightColor );",
+
+		"		#if ( NUM_HEMI_LIGHTS > 0 )",
+
+		"			#pragma unroll_loop",
+		"			for ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {",
+
+		"				irradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], geometry );",
+
+		"			}",
+
+		"		#endif",
+
+		"	#endif",
+
+		"	#if defined( RE_IndirectSpecular )",
+
+		"		vec3 radiance = vec3( 0.0 );",
+		"		vec3 clearCoatRadiance = vec3( 0.0 );",
+
+		"	#endif",
+		ShaderChunk[ "lights_fragment_end" ],
+
+		"	vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance;",
+		"	gl_FragColor = vec4( outgoingLight, diffuseColor.a );",	// TODO, this should be pre-multiplied to allow for bright highlights on very transparent objects
+
+		ShaderChunk[ "encodings_fragment" ],
+
+		"}"
+
+	].join( "\n" ),
+
+};
+
+export { TranslucentShader };

+ 2 - 2
examples/webgl_materials_translucency.html

@@ -20,7 +20,7 @@
 	<script src="js/libs/stats.min.js"></script>
 	<script src="js/libs/dat.gui.min.js"></script>
 	<script src="js/loaders/FBXLoader.js"></script>
-	<script src="js/ShaderTranslucent.js"></script>
+	<script src="js/shaders/TranslucentShader.js"></script>
 
 	<script>
 
@@ -97,7 +97,7 @@
 			var thicknessTexture = loader.load( 'models/fbx/bunny_thickness.jpg' );
 			imgTexture.wrapS = imgTexture.wrapT = THREE.RepeatWrapping;
 
-			var shader = new THREE.TranslucentShader();
+			var shader = THREE.TranslucentShader;
 			var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
 
 			uniforms[ 'map' ].value = imgTexture;

+ 1 - 0
utils/modularize.js

@@ -179,6 +179,7 @@ var files = [
 	{ path: 'shaders/TechnicolorShader.js', dependencies: [], ignoreList: [] },
 	{ path: 'shaders/TerrainShader.js', dependencies: [], ignoreList: [] },
 	{ path: 'shaders/ToneMapShader.js', dependencies: [], ignoreList: [] },
+	{ path: 'shaders/TranslucentShader.js', dependencies: [], ignoreList: [] },
 	{ path: 'shaders/TriangleBlurShader.js', dependencies: [], ignoreList: [] },
 	{ path: 'shaders/UnpackDepthRGBAShader.js', dependencies: [], ignoreList: [] },
 	{ path: 'shaders/VerticalBlurShader.js', dependencies: [], ignoreList: [] },