Forráskód Böngészése

Merge pull request #15519 from donmccurdy/feat-gltfexporter-khr_lights_punctual

GLTFExporter: Implement KHR_lights_punctual.
Mr.doob 6 éve
szülő
commit
d2ea94fb8b

+ 1 - 0
docs/examples/exporters/GLTFExporter.html

@@ -29,6 +29,7 @@
 		</p>
 
 		<ul>
+			<li>KHR_lights_punctual</li>
 			<li>KHR_materials_unlit</li>
 			<li>KHR_texture_transform</li>
 		</ul>

+ 71 - 7
examples/js/exporters/GLTFExporter.js

@@ -1591,6 +1591,59 @@ THREE.GLTFExporter.prototype = {
 
 		}
 
+		function processLight( light ) {
+
+			var lightDef = {};
+
+			if ( light.name ) lightDef.name = light.name;
+
+			lightDef.color = light.color.toArray();
+
+			lightDef.intensity = light.intensity;
+
+			if ( light.isDirectionalLight ) {
+
+				lightDef.type = 'directional';
+
+			} else if ( light.isPointLight ) {
+
+				lightDef.type = 'point';
+				lightDef.range = light.distance;
+
+			} else if ( light.isSpotLight ) {
+
+				lightDef.type = 'spot';
+				lightDef.range = light.distance;
+				lightDef.spot = {};
+				lightDef.spot.innerConeAngle = ( light.penumbra - 1.0 ) * light.angle * -1.0;
+				lightDef.spot.outerConeAngle = light.angle;
+
+			}
+
+			if ( light.decay !== undefined && light.decay !== 2 ) {
+
+				console.warn( 'THREE.GLTFExporter: Light decay may be lost. glTF is physically-based, '
+					+ 'and expects light.decay=2.' );
+
+			}
+
+			if ( light.target
+					&& ( light.target.parent !== light
+					 || light.target.position.x !== 0
+					 || light.target.position.y !== 0
+					 || light.target.position.z !== -1 ) ) {
+
+				console.warn( 'THREE.GLTFExporter: Light direction may be lost. For best results, '
+					+ 'make light.target a child of the light with position 0,0,-1.' );
+
+			}
+
+			var lights = outputJSON.extensions[ 'KHR_lights_punctual' ].lights;
+			lights.push( lightDef );
+			return lights.length - 1;
+
+		}
+
 		/**
 		 * Process Object3D node
 		 * @param  {THREE.Object3D} node Object3D to processNode
@@ -1598,13 +1651,6 @@ THREE.GLTFExporter.prototype = {
 		 */
 		function processNode( object ) {
 
-			if ( object.isLight ) {
-
-				console.warn( 'GLTFExporter: Unsupported node type:', object.constructor.name );
-				return null;
-
-			}
-
 			if ( ! outputJSON.nodes ) {
 
 				outputJSON.nodes = [];
@@ -1675,6 +1721,24 @@ THREE.GLTFExporter.prototype = {
 
 				gltfNode.camera = processCamera( object );
 
+			} else if ( object.isDirectionalLight || object.isPointLight || object.isSpotLight ) {
+
+				if ( ! extensionsUsed[ 'KHR_lights_punctual' ] ) {
+
+					outputJSON.extensions = outputJSON.extensions || {};
+					outputJSON.extensions[ 'KHR_lights_punctual' ] = { lights: [] };
+					extensionsUsed[ 'KHR_lights_punctual' ] = true;
+
+				}
+
+				gltfNode.extensions = gltfNode.extensions || {};
+				gltfNode.extensions[ 'KHR_lights_punctual' ] = { light: processLight( object ) };
+
+			} else if ( object.isLight ) {
+
+				console.warn( 'THREE.GLTFExporter: Only directional, point, and spot lights are supported.' );
+				return null;
+
 			}
 
 			if ( object.isSkinnedMesh ) {

+ 3 - 1
examples/misc_exporter_gltf.html

@@ -182,7 +182,9 @@
 				// DirectLight
 				// ---------------------------------------------------------------------
 				light = new THREE.DirectionalLight( 0xffffff, 1 );
-				light.position.set( 1, 1, 0 );
+				light.target.position.set( 0, 0, -1 );
+				light.add( light.target );
+				light.lookAt( -1, -1, 0 );
 				light.name = 'DirectionalLight';
 				scene1.add( light );