Skip to content

Commit

Permalink
Merge pull request #15519 from donmccurdy/feat-gltfexporter-khr_light…
Browse files Browse the repository at this point in the history
…s_punctual

GLTFExporter: Implement KHR_lights_punctual.
  • Loading branch information
mrdoob authored Jan 23, 2019
2 parents 61bbcb5 + e9bb5b3 commit d2ea94f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/examples/exporters/GLTFExporter.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ <h2>Extensions</h2>
</p>

<ul>
<li>KHR_lights_punctual</li>
<li>KHR_materials_unlit</li>
<li>KHR_texture_transform</li>
</ul>
Expand Down
78 changes: 71 additions & 7 deletions examples/js/exporters/GLTFExporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -1591,20 +1591,66 @@ 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
* @return {Integer} Index of the node in the nodes list
*/
function processNode( object ) {

if ( object.isLight ) {

console.warn( 'GLTFExporter: Unsupported node type:', object.constructor.name );
return null;

}

if ( ! outputJSON.nodes ) {

outputJSON.nodes = [];
Expand Down Expand Up @@ -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 ) {
Expand Down
4 changes: 3 additions & 1 deletion examples/misc_exporter_gltf.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down

0 comments on commit d2ea94f

Please sign in to comment.