瀏覽代碼

Merge pull request #13511 from daoshengmu/subsurfaceScattering

Subsurface scattering support in Blinn-Phong material
Mr.doob 7 年之前
父節點
當前提交
afcad785ed

+ 206 - 0
examples/js/ShaderTranslucent.js

@@ -0,0 +1,206 @@
+/**
+ * @author daoshengmu / http://dsmu.me/
+ *
+ */
+
+
+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( [
+
+		THREE.UniformsLib[ "common" ],
+		THREE.UniformsLib[ "lights" ],
+
+		{
+			"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 },
+			"shininess": { value: 1 },
+
+			"thicknessMap": { value: null },
+			"thicknessColor": { value: new THREE.Color( 0xffffff ) },
+			"thicknessDistortion": { value: 0.1 },
+			"thicknessAmbient": { value: 0.0 },
+			"thicknessAttenuation": { value: 0.1 },
+			"thicknessPower": { value: 2.0 },
+			"thicknessScale": { value: 10.0 }
+		}
+
+	] );
+
+	this.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;",
+
+		THREE.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 ) );",
+
+			THREE.ShaderChunk[ "map_fragment" ],
+			THREE.ShaderChunk[ "color_fragment" ],
+			THREE.ShaderChunk[ "specularmap_fragment" ],
+
+		"	vec3 totalEmissiveRadiance = emissive;",
+
+			THREE.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",
+			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"],
+
+		"}"
+
+	].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" )
+
+};

二進制
examples/models/fbx/bunny_thickness.png


文件差異過大導致無法顯示
+ 306 - 0
examples/models/fbx/stanford-bunny.fbx


二進制
examples/models/fbx/white.jpg


+ 176 - 0
examples/webgl_materials_translucency.html

@@ -0,0 +1,176 @@
+<!DOCTYPE html>
+<html lang="en">
+  <head>
+	<title>three.js webgl - Fast subsurface scattering in Blinn-Phong shading demo</title>
+	<meta charset="utf-8">
+	<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
+	<style>
+	  body {
+			color: #fff;
+			font-family: Monospace;
+			font-size: 13px;
+			text-align: center;
+
+			background-color: #000;
+			margin: 0px;
+			overflow: hidden;
+	  }
+
+	  #info {
+			position: absolute;
+			top: 0px; width: 100%;
+			padding: 5px;
+	  }
+	</style>
+  </head>
+  <body>
+
+	<div id="container"></div>
+	<div id="info"><a href="http://threejs.org" target="_blank" rel="noopener">three.js</a>
+		<br/>Fast subsurface scattering in Blinn-Phong shading demo<br/>
+		[Thanks for the art support from <a href="https://github.com/shaochun">Shaochun Lin</a>]
+	</div>
+	<script src="../build/three.js"></script>
+	<script src="js/controls/OrbitControls.js"></script>
+
+	<script src="js/Detector.js"></script>
+	<script src="js/libs/stats.min.js"></script>
+	<script src="js/loaders/FBXLoader.js"></script>
+	<script src="js/ShaderTranslucent.js"></script>
+
+	<script>
+
+		if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
+
+		var container, stats, font;
+
+		var camera, scene, renderer, controls;
+
+		init();
+		animate();
+
+		function init() {
+
+			container = document.createElement( 'div' );
+			document.body.appendChild( container );
+			font = font;
+
+			camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 5000 );
+			camera.position.set( 0.0, 300, 400 * 4 );
+
+			scene = new THREE.Scene();
+
+			// Lights
+
+			scene.add( new THREE.AmbientLight( 0x888888 ) );
+
+			var directionalLight = new THREE.DirectionalLight( 0xffffff, 0.03 );
+			directionalLight.position.set( 0.0, 0.5, 0.5 ).normalize();
+			scene.add( directionalLight );
+
+			var pointLight1 = new THREE.Mesh( new THREE.SphereBufferGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0x888888 } ) );
+			pointLight1.add( new THREE.PointLight( 0x888888, 7.0, 300 ) );
+			scene.add( pointLight1 );
+			pointLight1.position.x = 0;
+			pointLight1.position.y = 0;
+			pointLight1.position.z = 350;
+
+			var pointLight2 = new THREE.Mesh( new THREE.SphereBufferGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0x888800 } ) );
+			pointLight2.add( new THREE.PointLight( 0x888800, 1.0, 500 ) );
+			scene.add( pointLight2 );
+			pointLight2.position.x = 150;
+			pointLight2.position.y = -100;
+			pointLight2.position.z = -250;
+
+			renderer = new THREE.WebGLRenderer( { antialias: true } );
+			renderer.setPixelRatio( window.devicePixelRatio );
+			renderer.setSize( window.innerWidth, window.innerHeight );
+			container.appendChild( renderer.domElement );
+
+			renderer.gammaInput = true;
+			renderer.gammaOutput = true;
+
+			//
+
+			stats = new Stats();
+			container.appendChild( stats.dom );
+
+			controls = new THREE.OrbitControls( camera );
+
+			window.addEventListener( 'resize', onWindowResize, false );
+
+			initMaterial();
+		}
+
+		function initMaterial() {
+			var shader = new THREE.TranslucentShader();
+			var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
+
+			// Materials
+
+			var loader = new THREE.TextureLoader();
+			var imgTexture = loader.load("models/fbx/white.jpg");
+			var thicknessTexture = loader.load("models/fbx/bunny_thickness.png");
+			imgTexture.wrapS = imgTexture.wrapT = THREE.RepeatWrapping;
+
+			uniforms[ "map" ].value = imgTexture;
+
+			uniforms[ "diffuse" ].value = new THREE.Vector3(1.0, 0.2, 0.2);
+			uniforms[ "shininess" ].value = 500;
+
+			uniforms[ "thicknessMap" ].value = thicknessTexture;
+			uniforms[ "thicknessColor" ].value = new THREE.Vector3(0.5, 0.3, 0.3);
+			uniforms[ "thicknessDistortion" ].value = 0.1;
+			uniforms[ "thicknessAmbient" ].value = 0.1;
+			uniforms[ "thicknessAttenuation" ].value = 0.8;
+			uniforms[ "thicknessPower" ].value = 2;
+			uniforms[ "thicknessScale" ].value = 10.0;
+
+			var parameters = { fragmentShader: shader.fragmentShader, vertexShader: shader.vertexShader, uniforms: uniforms, lights: true };
+			var material = new THREE.ShaderMaterial( parameters );
+			material.extensions.derivatives = true;
+
+			// LOADER
+
+			var loader = new THREE.FBXLoader();
+			loader.load("models/fbx/stanford-bunny.fbx", function(object) {
+				loadedModel(object, 1, material);
+			});
+		}
+
+		function loadedModel(object, scale, material) {
+			object.children[0].position.x = 0;
+			object.children[0].position.y = 0;
+			object.children[0].position.z = 10;
+			object.children[0].scale.set(scale, scale, scale);
+			object.children[0].material = material;
+			scene.add(object);
+		}
+
+		function onWindowResize() {
+			camera.aspect = window.innerWidth / window.innerHeight;
+			camera.updateProjectionMatrix();
+
+			renderer.setSize( window.innerWidth, window.innerHeight );
+		}
+
+		//
+
+		function animate() {
+			requestAnimationFrame( animate );
+
+			render();
+			stats.update();
+		}
+
+		function render() {
+			var timer = Date.now() * 0.00025;
+
+			camera.lookAt( scene.position );
+			renderer.render( scene, camera );
+		}
+
+	</script>
+
+  </body>
+</html>

部分文件因文件數量過多而無法顯示