@@ -27,7 +27,9 @@ import { VertexBuffer } from '../../graphics/vertex-buffer.js';
27
27
import { VertexFormat } from '../../graphics/vertex-format.js' ;
28
28
29
29
import {
30
- BLEND_NONE , BLEND_NORMAL , LIGHTFALLOFF_INVERSESQUARED
30
+ BLEND_NONE , BLEND_NORMAL , LIGHTFALLOFF_INVERSESQUARED ,
31
+ PROJECTION_ORTHOGRAPHIC , PROJECTION_PERSPECTIVE ,
32
+ ASPECT_MANUAL , ASPECT_AUTO
31
33
} from '../../scene/constants.js' ;
32
34
import { calculateNormals } from '../../scene/procedural.js' ;
33
35
import { GraphNode } from '../../scene/graph-node.js' ;
@@ -61,6 +63,7 @@ class GlbResources {
61
63
this . renders = null ;
62
64
this . skins = null ;
63
65
this . lights = null ;
66
+ this . cameras = null ;
64
67
}
65
68
66
69
destroy ( ) {
@@ -1398,6 +1401,42 @@ const createNode = function (gltfNode, nodeIndex) {
1398
1401
return entity ;
1399
1402
} ;
1400
1403
1404
+ // creates a camera component on the supplied node, and returns it
1405
+ const createCamera = function ( gltfCamera , node ) {
1406
+
1407
+ const projection = gltfCamera . type === "orthographic" ? PROJECTION_ORTHOGRAPHIC : PROJECTION_PERSPECTIVE ;
1408
+ const gltfProperties = projection === PROJECTION_ORTHOGRAPHIC ? gltfCamera . orthographic : gltfCamera . perspective ;
1409
+
1410
+ const componentData = {
1411
+ enabled : false ,
1412
+ projection : projection ,
1413
+ nearClip : gltfProperties . znear ,
1414
+ aspectRatioMode : ASPECT_AUTO
1415
+ } ;
1416
+
1417
+ if ( gltfProperties . zfar ) {
1418
+ componentData . farClip = gltfProperties . zfar ;
1419
+ }
1420
+
1421
+ if ( projection === PROJECTION_ORTHOGRAPHIC ) {
1422
+ componentData . orthoHeight = 0.5 * gltfProperties . ymag ;
1423
+ if ( gltfProperties . ymag ) {
1424
+ componentData . aspectRatioMode = ASPECT_MANUAL ;
1425
+ componentData . aspectRatio = gltfProperties . xmag / gltfProperties . ymag ;
1426
+ }
1427
+ } else {
1428
+ componentData . fov = gltfProperties . yfov * math . RAD_TO_DEG ;
1429
+ if ( gltfProperties . aspectRatio ) {
1430
+ componentData . aspectRatioMode = ASPECT_MANUAL ;
1431
+ componentData . aspectRatio = gltfProperties . aspectRatio ;
1432
+ }
1433
+ }
1434
+
1435
+ const cameraEntity = new Entity ( gltfCamera . name ) ;
1436
+ cameraEntity . addComponent ( "camera" , componentData ) ;
1437
+ return cameraEntity ;
1438
+ } ;
1439
+
1401
1440
// creates light component, adds it to the node and returns the created light component
1402
1441
const createLight = function ( gltfLight , node ) {
1403
1442
@@ -1421,13 +1460,12 @@ const createLight = function (gltfLight, node) {
1421
1460
1422
1461
// Rotate to match light orientation in glTF specification
1423
1462
// Note that this adds a new entity node into the hierarchy that does not exist in the gltf hierarchy
1424
- var lightNode = new Entity ( node . name ) ;
1425
- lightNode . rotateLocal ( 90 , 0 , 0 ) ;
1463
+ const lightEntity = new Entity ( node . name ) ;
1464
+ lightEntity . rotateLocal ( 90 , 0 , 0 ) ;
1426
1465
1427
1466
// add component
1428
- lightNode . addComponent ( "light" , lightProps ) ;
1429
-
1430
- return lightNode ;
1467
+ lightEntity . addComponent ( "light" , lightProps ) ;
1468
+ return lightEntity ;
1431
1469
} ;
1432
1470
1433
1471
const createSkins = function ( device , gltf , nodes , bufferViews ) {
@@ -1563,6 +1601,41 @@ const createScenes = function (gltf, nodes) {
1563
1601
return scenes ;
1564
1602
} ;
1565
1603
1604
+ const createCameras = function ( gltf , nodes , options ) {
1605
+
1606
+ let cameras = null ;
1607
+
1608
+ if ( gltf . hasOwnProperty ( 'nodes' ) && gltf . hasOwnProperty ( 'cameras' ) && gltf . cameras . length > 0 ) {
1609
+
1610
+ const preprocess = options && options . camera && options . camera . preprocess ;
1611
+ const process = options && options . camera && options . camera . process || createCamera ;
1612
+ const postprocess = options && options . camera && options . camera . postprocess ;
1613
+
1614
+ gltf . nodes . forEach ( function ( gltfNode , nodeIndex ) {
1615
+ if ( gltfNode . hasOwnProperty ( 'camera' ) ) {
1616
+ const gltfCamera = gltf . cameras [ gltfNode . camera ] ;
1617
+ if ( gltfCamera ) {
1618
+ if ( preprocess ) {
1619
+ preprocess ( gltfCamera ) ;
1620
+ }
1621
+ const camera = process ( gltfCamera , nodes [ nodeIndex ] ) ;
1622
+ if ( postprocess ) {
1623
+ postprocess ( gltfCamera , camera ) ;
1624
+ }
1625
+
1626
+ // add the camera to node->camera map
1627
+ if ( camera ) {
1628
+ if ( ! cameras ) cameras = new Map ( ) ;
1629
+ cameras . set ( gltfNode , camera ) ;
1630
+ }
1631
+ }
1632
+ }
1633
+ } ) ;
1634
+ }
1635
+
1636
+ return cameras ;
1637
+ } ;
1638
+
1566
1639
const createLights = function ( gltf , nodes , options ) {
1567
1640
1568
1641
let lights = null ;
@@ -1637,6 +1710,7 @@ const createResources = function (device, gltf, bufferViews, textureAssets, opti
1637
1710
const nodes = createNodes ( gltf , options ) ;
1638
1711
const scenes = createScenes ( gltf , nodes ) ;
1639
1712
const lights = createLights ( gltf , nodes , options ) ;
1713
+ const cameras = createCameras ( gltf , nodes , options ) ;
1640
1714
const animations = createAnimations ( gltf , nodes , bufferViews , options ) ;
1641
1715
const materials = createMaterials ( gltf , textureAssets . map ( function ( textureAsset ) {
1642
1716
return textureAsset . resource ;
@@ -1663,6 +1737,7 @@ const createResources = function (device, gltf, bufferViews, textureAssets, opti
1663
1737
result . renders = renders ;
1664
1738
result . skins = skins ;
1665
1739
result . lights = lights ;
1740
+ result . cameras = cameras ;
1666
1741
1667
1742
if ( postprocess ) {
1668
1743
postprocess ( gltf , result ) ;
0 commit comments