Skip to content

Commit d2ea94f

Browse files
authored
Merge pull request #15519 from donmccurdy/feat-gltfexporter-khr_lights_punctual
GLTFExporter: Implement KHR_lights_punctual.
2 parents 61bbcb5 + e9bb5b3 commit d2ea94f

File tree

3 files changed

+75
-8
lines changed

3 files changed

+75
-8
lines changed

docs/examples/exporters/GLTFExporter.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ <h2>Extensions</h2>
2929
</p>
3030

3131
<ul>
32+
<li>KHR_lights_punctual</li>
3233
<li>KHR_materials_unlit</li>
3334
<li>KHR_texture_transform</li>
3435
</ul>

examples/js/exporters/GLTFExporter.js

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,20 +1591,66 @@ THREE.GLTFExporter.prototype = {
15911591

15921592
}
15931593

1594+
function processLight( light ) {
1595+
1596+
var lightDef = {};
1597+
1598+
if ( light.name ) lightDef.name = light.name;
1599+
1600+
lightDef.color = light.color.toArray();
1601+
1602+
lightDef.intensity = light.intensity;
1603+
1604+
if ( light.isDirectionalLight ) {
1605+
1606+
lightDef.type = 'directional';
1607+
1608+
} else if ( light.isPointLight ) {
1609+
1610+
lightDef.type = 'point';
1611+
lightDef.range = light.distance;
1612+
1613+
} else if ( light.isSpotLight ) {
1614+
1615+
lightDef.type = 'spot';
1616+
lightDef.range = light.distance;
1617+
lightDef.spot = {};
1618+
lightDef.spot.innerConeAngle = ( light.penumbra - 1.0 ) * light.angle * -1.0;
1619+
lightDef.spot.outerConeAngle = light.angle;
1620+
1621+
}
1622+
1623+
if ( light.decay !== undefined && light.decay !== 2 ) {
1624+
1625+
console.warn( 'THREE.GLTFExporter: Light decay may be lost. glTF is physically-based, '
1626+
+ 'and expects light.decay=2.' );
1627+
1628+
}
1629+
1630+
if ( light.target
1631+
&& ( light.target.parent !== light
1632+
|| light.target.position.x !== 0
1633+
|| light.target.position.y !== 0
1634+
|| light.target.position.z !== -1 ) ) {
1635+
1636+
console.warn( 'THREE.GLTFExporter: Light direction may be lost. For best results, '
1637+
+ 'make light.target a child of the light with position 0,0,-1.' );
1638+
1639+
}
1640+
1641+
var lights = outputJSON.extensions[ 'KHR_lights_punctual' ].lights;
1642+
lights.push( lightDef );
1643+
return lights.length - 1;
1644+
1645+
}
1646+
15941647
/**
15951648
* Process Object3D node
15961649
* @param {THREE.Object3D} node Object3D to processNode
15971650
* @return {Integer} Index of the node in the nodes list
15981651
*/
15991652
function processNode( object ) {
16001653

1601-
if ( object.isLight ) {
1602-
1603-
console.warn( 'GLTFExporter: Unsupported node type:', object.constructor.name );
1604-
return null;
1605-
1606-
}
1607-
16081654
if ( ! outputJSON.nodes ) {
16091655

16101656
outputJSON.nodes = [];
@@ -1675,6 +1721,24 @@ THREE.GLTFExporter.prototype = {
16751721

16761722
gltfNode.camera = processCamera( object );
16771723

1724+
} else if ( object.isDirectionalLight || object.isPointLight || object.isSpotLight ) {
1725+
1726+
if ( ! extensionsUsed[ 'KHR_lights_punctual' ] ) {
1727+
1728+
outputJSON.extensions = outputJSON.extensions || {};
1729+
outputJSON.extensions[ 'KHR_lights_punctual' ] = { lights: [] };
1730+
extensionsUsed[ 'KHR_lights_punctual' ] = true;
1731+
1732+
}
1733+
1734+
gltfNode.extensions = gltfNode.extensions || {};
1735+
gltfNode.extensions[ 'KHR_lights_punctual' ] = { light: processLight( object ) };
1736+
1737+
} else if ( object.isLight ) {
1738+
1739+
console.warn( 'THREE.GLTFExporter: Only directional, point, and spot lights are supported.' );
1740+
return null;
1741+
16781742
}
16791743

16801744
if ( object.isSkinnedMesh ) {

examples/misc_exporter_gltf.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,9 @@
182182
// DirectLight
183183
// ---------------------------------------------------------------------
184184
light = new THREE.DirectionalLight( 0xffffff, 1 );
185-
light.position.set( 1, 1, 0 );
185+
light.target.position.set( 0, 0, -1 );
186+
light.add( light.target );
187+
light.lookAt( -1, -1, 0 );
186188
light.name = 'DirectionalLight';
187189
scene1.add( light );
188190

0 commit comments

Comments
 (0)