webgl_loader_gltf2.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - glTF 2.0</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #222222;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. #info {
  15. color: #808080;
  16. position: absolute;
  17. top: 10px;
  18. width: 100%;
  19. text-align: center;
  20. z-index: 100;
  21. display:block;
  22. }
  23. #container {
  24. position: absolute;
  25. top: 0px;
  26. width:100%;
  27. height:100%;
  28. z-index: -1;
  29. }
  30. #controls {
  31. position: absolute;
  32. width: 200px;
  33. bottom: 0px;
  34. left: 0px;
  35. padding: 10px;
  36. background-color: White;
  37. font: 13px "Lucida Grande", Lucida, Verdana, sans-serif;
  38. }
  39. #controls > div {
  40. margin-bottom: 8px;
  41. }
  42. #controls hr {
  43. border: 0px;
  44. height: 1px;
  45. margin-bottom: 10px;
  46. background-color: #bbb;
  47. }
  48. #info a, .button {
  49. color: #f00;
  50. font-weight: bold;
  51. text-decoration: underline;
  52. cursor: pointer
  53. }
  54. </style>
  55. </head>
  56. <body>
  57. <div id="info">
  58. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> -
  59. <a href="https://github.com/KhronosGroup/glTF" target="_blank" rel="noopener">glTF</a> 2.0 loader
  60. <br>
  61. BoomBox by <a href="https://www.microsoft.com/" target="_blank" rel="noopener">Microsoft</a>
  62. </div>
  63. <div id="container"></div>
  64. <div id="controls">
  65. <div id="status">Loading...</div>
  66. <hr />
  67. <div>
  68. Model
  69. <select id="scenes_list" size="1" onchange="selectScene();" ondblclick="selectScene();"></select>
  70. </div>
  71. <div>
  72. Camera
  73. <select id="cameras_list" size="1" onchange="selectCamera();" ondblclick="selectCamera();"></select>
  74. </div>
  75. <div>
  76. Animations
  77. <input type="checkbox" checked onclick="toggleAnimations();">Play</input>
  78. </div>
  79. <div>
  80. Extension
  81. <select id="extensions_list" onchange="selectExtension();">
  82. <option value="glTF">None (Default)</option>
  83. <option value="glTF-Embedded">None (Embedded)</option>
  84. <option value="glTF-Binary">None (Binary)</option>
  85. <option value="glTF-MaterialsCommon">Common Materials</option>
  86. <option value="glTF-pbrSpecularGlossiness">Specular-Glossiness (PBR)</option>
  87. <option value="glTF-techniqueWebGL">GLSL</option>
  88. </select>
  89. </div>
  90. </div>
  91. <script src="../build/three.js"></script>
  92. <script src="js/controls/OrbitControls.js"></script>
  93. <script src="js/loaders/GLTF2Loader.js"></script>
  94. <script>
  95. var orbitControls = null;
  96. var container, camera, scene, renderer, loader;
  97. var cameraIndex = 0;
  98. var cameras = [];
  99. var cameraNames = [];
  100. var defaultCamera = null;
  101. var gltf = null;
  102. var mixer = null;
  103. var clock = new THREE.Clock();
  104. function onload() {
  105. window.addEventListener( 'resize', onWindowResize, false );
  106. document.addEventListener( 'keydown', function(e) { onKeyDown(e); }, false );
  107. buildSceneList();
  108. switchScene(0);
  109. animate();
  110. }
  111. function initScene(index) {
  112. container = document.getElementById( 'container' );
  113. scene = new THREE.Scene();
  114. scene.background = new THREE.Color( 0x222222 );
  115. defaultCamera = new THREE.PerspectiveCamera( 45, container.offsetWidth / container.offsetHeight, 0.001, 1000 );
  116. //defaultCamera.up = new THREE.Vector3( 0, 1, 0 );
  117. scene.add( defaultCamera );
  118. camera = defaultCamera;
  119. var sceneInfo = sceneList[index];
  120. var spot1 = null;
  121. if (sceneInfo.addLights) {
  122. var ambient = new THREE.AmbientLight( 0x222222 );
  123. scene.add( ambient );
  124. var directionalLight = new THREE.DirectionalLight( 0xdddddd );
  125. directionalLight.position.set( 0, 0, 1 ).normalize();
  126. scene.add( directionalLight );
  127. spot1 = new THREE.SpotLight( 0xffffff, 1 );
  128. spot1.position.set( 10, 20, 10 );
  129. spot1.angle = 0.25;
  130. spot1.distance = 1024;
  131. spot1.penumbra = 0.75;
  132. if ( sceneInfo.shadows ) {
  133. spot1.castShadow = true;
  134. spot1.shadow.bias = 0.0001;
  135. spot1.shadow.mapSize.width = 2048;
  136. spot1.shadow.mapSize.height = 2048;
  137. }
  138. scene.add( spot1 );
  139. }
  140. // RENDERER
  141. renderer = new THREE.WebGLRenderer( { antialias: true } );
  142. renderer.setPixelRatio( window.devicePixelRatio );
  143. renderer.setSize( window.innerWidth, window.innerHeight );
  144. if (sceneInfo.shadows) {
  145. renderer.shadowMap.enabled = true;
  146. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  147. }
  148. container.appendChild( renderer.domElement );
  149. var ground = null;
  150. if (sceneInfo.addGround) {
  151. var groundMaterial = new THREE.MeshPhongMaterial({
  152. color: 0xFFFFFF
  153. });
  154. ground = new THREE.Mesh( new THREE.PlaneBufferGeometry(512, 512), groundMaterial);
  155. if (sceneInfo.shadows) {
  156. ground.receiveShadow = true;
  157. }
  158. if (sceneInfo.groundPos) {
  159. ground.position.copy(sceneInfo.groundPos);
  160. } else {
  161. ground.position.z = -70;
  162. }
  163. ground.rotation.x = -Math.PI / 2;
  164. scene.add(ground);
  165. }
  166. loader = new THREE.GLTF2Loader();
  167. for (var i = 0; i < extensionSelect.children.length; i++) {
  168. var child = extensionSelect.children[i];
  169. child.disabled = sceneInfo.extensions.indexOf(child.value) === -1;
  170. if (child.disabled && child.selected) {
  171. extensionSelect.value = extension = 'glTF';
  172. }
  173. }
  174. var url = sceneInfo.url;
  175. var r = eval("/" + '\%s' + "/g");
  176. url = url.replace(r, extension);
  177. if (extension === 'glTF-Binary') {
  178. url = url.replace('.gltf', '.glb');
  179. }
  180. var loadStartTime = performance.now();
  181. var status = document.getElementById("status");
  182. status.innerHTML = "Loading...";
  183. loader.load( url, function(data) {
  184. gltf = data;
  185. var object = gltf.scene;
  186. status.innerHTML = "Load time: " + ( performance.now() - loadStartTime ).toFixed( 2 ) + " ms.";
  187. if (sceneInfo.cameraPos)
  188. defaultCamera.position.copy(sceneInfo.cameraPos);
  189. if (sceneInfo.center) {
  190. orbitControls.target.copy(sceneInfo.center);
  191. }
  192. if (sceneInfo.objectPosition) {
  193. object.position.copy(sceneInfo.objectPosition);
  194. if (spot1) {
  195. spot1.position.set(sceneInfo.objectPosition.x - 100, sceneInfo.objectPosition.y + 200, sceneInfo.objectPosition.z - 100 );
  196. spot1.target.position.copy(sceneInfo.objectPosition);
  197. }
  198. }
  199. if (sceneInfo.objectRotation)
  200. object.rotation.copy(sceneInfo.objectRotation);
  201. if (sceneInfo.objectScale)
  202. object.scale.copy(sceneInfo.objectScale);
  203. if ( sceneInfo.addEnvMap ) {
  204. var envMap = getEnvMap();
  205. object.traverse( function( node ) {
  206. if ( node.material && ( node.material.isMeshStandardMaterial ||
  207. ( node.material.isShaderMaterial && node.material.envMap !== undefined ) ) ) {
  208. node.material.envMap = envMap;
  209. node.material.needsUpdate = true;
  210. }
  211. } );
  212. scene.background = envMap;
  213. }
  214. cameraIndex = 0;
  215. cameras = [];
  216. cameraNames = [];
  217. if (gltf.cameras && gltf.cameras.length) {
  218. var i, len = gltf.cameras.length;
  219. for (i = 0; i < len; i++) {
  220. var addCamera = true;
  221. var cameraName = gltf.cameras[i].parent.name || ('camera_' + i);
  222. if (sceneInfo.cameras && !(cameraName in sceneInfo.cameras)) {
  223. addCamera = false;
  224. }
  225. if (addCamera) {
  226. cameraNames.push(cameraName);
  227. cameras.push(gltf.cameras[i]);
  228. }
  229. }
  230. updateCamerasList();
  231. switchCamera(1);
  232. } else {
  233. updateCamerasList();
  234. switchCamera(0);
  235. }
  236. var animations = gltf.animations;
  237. if ( animations && animations.length ) {
  238. mixer = new THREE.AnimationMixer( object );
  239. for ( var i = 0; i < animations.length; i ++ ) {
  240. var animation = animations[ i ];
  241. // There's .3333 seconds junk at the tail of the Monster animation that
  242. // keeps it from looping cleanly. Clip it at 3 seconds
  243. if ( sceneInfo.animationTime )
  244. animation.duration = sceneInfo.animationTime;
  245. mixer.clipAction( animation ).play();
  246. }
  247. }
  248. scene.add( object );
  249. onWindowResize();
  250. });
  251. orbitControls = new THREE.OrbitControls(defaultCamera, renderer.domElement);
  252. }
  253. function onWindowResize() {
  254. defaultCamera.aspect = container.offsetWidth / container.offsetHeight;
  255. defaultCamera.updateProjectionMatrix();
  256. var i, len = cameras.length;
  257. for (i = 0; i < len; i++) { // just do it for default
  258. cameras[i].aspect = container.offsetWidth / container.offsetHeight;
  259. cameras[i].updateProjectionMatrix();
  260. }
  261. renderer.setSize( window.innerWidth, window.innerHeight );
  262. }
  263. function animate() {
  264. requestAnimationFrame( animate );
  265. if (mixer) mixer.update(clock.getDelta());
  266. if (cameraIndex == 0)
  267. orbitControls.update();
  268. render();
  269. }
  270. function render() {
  271. renderer.render( scene, camera );
  272. }
  273. function onKeyDown(event) {
  274. var chr = String.fromCharCode(event.keyCode);
  275. if (chr == ' ') {
  276. index = cameraIndex + 1;
  277. if (index > cameras.length)
  278. index = 0;
  279. switchCamera(index);
  280. } else {
  281. var index = parseInt(chr);
  282. if (!isNaN(index) && (index <= cameras.length)) {
  283. switchCamera(index);
  284. }
  285. }
  286. }
  287. var envMap;
  288. function getEnvMap() {
  289. if ( envMap ) {
  290. return envMap;
  291. }
  292. var path = 'textures/cube/Park2/';
  293. var format = '.jpg';
  294. var urls = [
  295. path + 'posx' + format, path + 'negx' + format,
  296. path + 'posy' + format, path + 'negy' + format,
  297. path + 'posz' + format, path + 'negz' + format
  298. ];
  299. envMap = new THREE.CubeTextureLoader().load( urls );
  300. envMap.format = THREE.RGBFormat;
  301. return envMap;
  302. }
  303. var sceneList = [
  304. {
  305. name : 'BoomBox (PBR)', url : './models/gltf/BoomBox/%s/BoomBox.gltf',
  306. cameraPos: new THREE.Vector3(0.02, 0.01, 0.03),
  307. objectRotation: new THREE.Euler(0, Math.PI, 0),
  308. addLights:true,
  309. extensions: ['glTF', 'glTF-pbrSpecularGlossiness', 'glTF-Binary'],
  310. addEnvMap: true
  311. },
  312. {
  313. name : 'MetalRoughSpheres (PBR)', url : './models/gltf/MetalRoughSpheres/%s/MetalRoughSpheres.gltf',
  314. cameraPos: new THREE.Vector3(2, 1, 15),
  315. objectRotation: new THREE.Euler(0, 0, 0),
  316. addLights:true,
  317. extensions: ['glTF', 'glTF-Embedded'],
  318. addEnvMap: true
  319. },
  320. {
  321. name : 'Duck', url : './models/gltf/Duck/%s/Duck.gltf',
  322. cameraPos: new THREE.Vector3(0, 3, 5),
  323. addLights:true,
  324. addGround:true,
  325. shadows:true,
  326. // TODO: 'glTF-techniqueWebGL'
  327. extensions: ['glTF', 'glTF-Embedded', 'glTF-MaterialsCommon', 'glTF-pbrSpecularGlossiness', 'glTF-Binary']
  328. },
  329. {
  330. name : "Monster", url : "./models/gltf/Monster/%s/Monster.gltf",
  331. cameraPos: new THREE.Vector3(30, 10, 70),
  332. objectScale: new THREE.Vector3(0.4, 0.4, 0.4),
  333. objectPosition: new THREE.Vector3(2, 1, 0),
  334. objectRotation: new THREE.Euler(0, - 3 * Math.PI / 4, 0),
  335. animationTime: 3,
  336. addLights:true,
  337. shadows:true,
  338. addGround:true,
  339. // TODO: 'glTF-MaterialsCommon', 'glTF-techniqueWebGL'
  340. extensions: ['glTF', 'glTF-Embedded', 'glTF-pbrSpecularGlossiness', 'glTF-Binary']
  341. },
  342. {
  343. name : "Cesium Man", url : "./models/gltf/CesiumMan/%s/CesiumMan.gltf",
  344. cameraPos: new THREE.Vector3(0, 3, 10),
  345. objectRotation: new THREE.Euler(0, 0, 0),
  346. addLights:true,
  347. addGround:true,
  348. shadows:true,
  349. // TODO: 'glTF-MaterialsCommon', 'glTF-techniqueWebGL'
  350. extensions: ['glTF', 'glTF-Embedded', 'glTF-pbrSpecularGlossiness', 'glTF-Binary']
  351. },
  352. {
  353. name : "Cesium Milk Truck",
  354. url : "./models/gltf/CesiumMilkTruck/%s/CesiumMilkTruck.gltf",
  355. cameraPos: new THREE.Vector3(0, 3, 10),
  356. addLights:true,
  357. addGround:true,
  358. shadows:true,
  359. // TODO: 'glTF-MaterialsCommon', 'glTF-techniqueWebGL'
  360. extensions: ['glTF', 'glTF-Embedded', 'glTF-pbrSpecularGlossiness', 'glTF-Binary']
  361. },
  362. {
  363. name : "Rigged Simple",
  364. url : "./models/gltf/RiggedSimple/%s/RiggedSimple.gltf",
  365. cameraPos: new THREE.Vector3(0, 5, 15),
  366. objectRotation: new THREE.Euler(0, 90, 0),
  367. addLights:true,
  368. shadows:true,
  369. // TODO: 'glTF-MaterialsCommon', 'glTF-techniqueWebGL'
  370. extensions: ['glTF', 'glTF-Embedded', 'glTF-pbrSpecularGlossiness', 'glTF-Binary']
  371. },
  372. {
  373. name : 'Outlined Box',
  374. url : './models/gltf/OutlinedBox/OutlinedBox.gltf',
  375. cameraPos: new THREE.Vector3(0, 5, 15),
  376. objectScale: new THREE.Vector3(0.01, 0.01, 0.01),
  377. objectRotation: new THREE.Euler(0, 90, 0),
  378. addLights:true,
  379. shadows:true,
  380. extensions: ['glTF']
  381. },
  382. ];
  383. function buildSceneList() {
  384. var elt = document.getElementById('scenes_list');
  385. while( elt.hasChildNodes() ){
  386. elt.removeChild(elt.lastChild);
  387. }
  388. var i, len = sceneList.length;
  389. for (i = 0; i < len; i++) {
  390. var option = document.createElement("option");
  391. option.text=sceneList[i].name;
  392. elt.add(option);
  393. }
  394. }
  395. function switchScene(index) {
  396. cleanup();
  397. initScene(index);
  398. var elt = document.getElementById('scenes_list');
  399. elt.selectedIndex = index;
  400. }
  401. function selectScene() {
  402. var select = document.getElementById("scenes_list");
  403. var index = select.selectedIndex;
  404. if (index >= 0) {
  405. switchScene(index);
  406. }
  407. }
  408. function switchCamera(index) {
  409. cameraIndex = index;
  410. if (cameraIndex == 0) {
  411. camera = defaultCamera;
  412. }
  413. if (cameraIndex >= 1 && cameraIndex <= cameras.length) {
  414. camera = cameras[cameraIndex - 1];
  415. }
  416. var elt = document.getElementById('cameras_list');
  417. elt.selectedIndex = cameraIndex;
  418. }
  419. function updateCamerasList() {
  420. var elt = document.getElementById('cameras_list');
  421. while( elt.hasChildNodes() ){
  422. elt.removeChild(elt.lastChild);
  423. }
  424. var option = document.createElement("option");
  425. option.text="[default]";
  426. elt.add(option);
  427. var i, len = cameraNames.length;
  428. for (i = 0; i < len; i++) {
  429. var option = document.createElement("option");
  430. option.text=cameraNames[i];
  431. elt.add(option);
  432. }
  433. }
  434. function selectCamera() {
  435. var select = document.getElementById("cameras_list");
  436. var index = select.selectedIndex;
  437. if (index >= 0) {
  438. switchCamera(index);
  439. }
  440. }
  441. function toggleAnimations() {
  442. var i, len = gltf.animations.length;
  443. for (i = 0; i < len; i++) {
  444. var clip = gltf.animations[i];
  445. var action = mixer.existingAction( clip );
  446. if (action.isRunning()) {
  447. action.stop();
  448. } else {
  449. action.play();
  450. }
  451. }
  452. }
  453. var extensionSelect = document.getElementById("extensions_list");
  454. var extension = extensionSelect.value;
  455. function selectExtension()
  456. {
  457. extension = extensionSelect.value;
  458. selectScene();
  459. }
  460. function cleanup() {
  461. if (container && renderer) {
  462. container.removeChild(renderer.domElement);
  463. }
  464. cameraIndex = 0;
  465. cameras = [];
  466. cameraNames = [];
  467. defaultCamera = null;
  468. if (!loader || !mixer)
  469. return;
  470. mixer.stopAllAction();
  471. }
  472. onload();
  473. </script>
  474. </body>
  475. </html>