webgl_loader_gltf_extensions.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - glTF 2.0 - extensions</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-pbrSpecularGlossiness">Specular-Glossiness (PBR)</option>
  86. <option value="glTF-Draco">Draco (Compressed)</option>
  87. </select>
  88. </div>
  89. </div>
  90. <script src="../build/three.js"></script>
  91. <script src="js/controls/OrbitControls.js"></script>
  92. <script src="js/loaders/DRACOLoader.js"></script>
  93. <script src="js/loaders/GLTFLoader.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. renderer.gammaOutput = true;
  145. if (sceneInfo.shadows) {
  146. renderer.shadowMap.enabled = true;
  147. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  148. }
  149. container.appendChild( renderer.domElement );
  150. var ground = null;
  151. if (sceneInfo.addGround) {
  152. var groundMaterial = new THREE.MeshPhongMaterial({
  153. color: 0xFFFFFF
  154. });
  155. ground = new THREE.Mesh( new THREE.PlaneBufferGeometry(512, 512), groundMaterial);
  156. if (sceneInfo.shadows) {
  157. ground.receiveShadow = true;
  158. }
  159. if (sceneInfo.groundPos) {
  160. ground.position.copy(sceneInfo.groundPos);
  161. } else {
  162. ground.position.z = -70;
  163. }
  164. ground.rotation.x = -Math.PI / 2;
  165. scene.add(ground);
  166. }
  167. loader = new THREE.GLTFLoader();
  168. THREE.DRACOLoader.setDecoderPath( 'js/libs/draco/gltf/' );
  169. loader.setDRACOLoader( new THREE.DRACOLoader() );
  170. for (var i = 0; i < extensionSelect.children.length; i++) {
  171. var child = extensionSelect.children[i];
  172. child.disabled = sceneInfo.extensions.indexOf(child.value) === -1;
  173. if (child.disabled && child.selected) {
  174. extensionSelect.value = extension = 'glTF';
  175. }
  176. }
  177. var url = sceneInfo.url;
  178. var r = eval("/" + '\%s' + "/g");
  179. url = url.replace(r, extension);
  180. if (extension === 'glTF-Binary') {
  181. url = url.replace('.gltf', '.glb');
  182. }
  183. var loadStartTime = performance.now();
  184. var status = document.getElementById("status");
  185. status.innerHTML = "Loading...";
  186. loader.load( url, function(data) {
  187. gltf = data;
  188. var object = gltf.scene;
  189. status.innerHTML = "Load time: " + ( performance.now() - loadStartTime ).toFixed( 2 ) + " ms.";
  190. if (sceneInfo.cameraPos)
  191. defaultCamera.position.copy(sceneInfo.cameraPos);
  192. if (sceneInfo.center) {
  193. orbitControls.target.copy(sceneInfo.center);
  194. }
  195. if (sceneInfo.objectPosition) {
  196. object.position.copy(sceneInfo.objectPosition);
  197. if (spot1) {
  198. spot1.position.set(sceneInfo.objectPosition.x - 100, sceneInfo.objectPosition.y + 200, sceneInfo.objectPosition.z - 100 );
  199. spot1.target.position.copy(sceneInfo.objectPosition);
  200. }
  201. }
  202. if (sceneInfo.objectRotation)
  203. object.rotation.copy(sceneInfo.objectRotation);
  204. if (sceneInfo.objectScale)
  205. object.scale.copy(sceneInfo.objectScale);
  206. if ( sceneInfo.addEnvMap ) {
  207. var envMap = getEnvMap();
  208. object.traverse( function( node ) {
  209. if ( node.material && ( node.material.isMeshStandardMaterial ||
  210. ( node.material.isShaderMaterial && node.material.envMap !== undefined ) ) ) {
  211. node.material.envMap = envMap;
  212. node.material.needsUpdate = true;
  213. }
  214. } );
  215. scene.background = envMap;
  216. }
  217. object.traverse( function ( node ) {
  218. if ( node.isMesh ) node.castShadow = true;
  219. } );
  220. cameraIndex = 0;
  221. cameras = [];
  222. cameraNames = [];
  223. if (gltf.cameras && gltf.cameras.length) {
  224. var i, len = gltf.cameras.length;
  225. for (i = 0; i < len; i++) {
  226. var addCamera = true;
  227. var cameraName = gltf.cameras[i].parent.name || ('camera_' + i);
  228. if (sceneInfo.cameras && !(cameraName in sceneInfo.cameras)) {
  229. addCamera = false;
  230. }
  231. if (addCamera) {
  232. cameraNames.push(cameraName);
  233. cameras.push(gltf.cameras[i]);
  234. }
  235. }
  236. updateCamerasList();
  237. switchCamera(1);
  238. } else {
  239. updateCamerasList();
  240. switchCamera(0);
  241. }
  242. var animations = gltf.animations;
  243. if ( animations && animations.length ) {
  244. mixer = new THREE.AnimationMixer( object );
  245. for ( var i = 0; i < animations.length; i ++ ) {
  246. var animation = animations[ i ];
  247. // There's .3333 seconds junk at the tail of the Monster animation that
  248. // keeps it from looping cleanly. Clip it at 3 seconds
  249. if ( sceneInfo.animationTime )
  250. animation.duration = sceneInfo.animationTime;
  251. mixer.clipAction( animation ).play();
  252. }
  253. }
  254. scene.add( object );
  255. onWindowResize();
  256. }, undefined, function ( error ) {
  257. console.error( error );
  258. } );
  259. orbitControls = new THREE.OrbitControls(defaultCamera, renderer.domElement);
  260. }
  261. function onWindowResize() {
  262. defaultCamera.aspect = container.offsetWidth / container.offsetHeight;
  263. defaultCamera.updateProjectionMatrix();
  264. var i, len = cameras.length;
  265. for (i = 0; i < len; i++) { // just do it for default
  266. cameras[i].aspect = container.offsetWidth / container.offsetHeight;
  267. cameras[i].updateProjectionMatrix();
  268. }
  269. renderer.setSize( window.innerWidth, window.innerHeight );
  270. }
  271. function animate() {
  272. requestAnimationFrame( animate );
  273. if (mixer) mixer.update(clock.getDelta());
  274. if (cameraIndex == 0)
  275. orbitControls.update();
  276. render();
  277. }
  278. function render() {
  279. renderer.render( scene, camera );
  280. }
  281. function onKeyDown(event) {
  282. var chr = String.fromCharCode(event.keyCode);
  283. if (chr == ' ') {
  284. index = cameraIndex + 1;
  285. if (index > cameras.length)
  286. index = 0;
  287. switchCamera(index);
  288. } else {
  289. var index = parseInt(chr);
  290. if (!isNaN(index) && (index <= cameras.length)) {
  291. switchCamera(index);
  292. }
  293. }
  294. }
  295. var envMap;
  296. function getEnvMap() {
  297. if ( envMap ) {
  298. return envMap;
  299. }
  300. var path = 'textures/cube/Park2/';
  301. var format = '.jpg';
  302. var urls = [
  303. path + 'posx' + format, path + 'negx' + format,
  304. path + 'posy' + format, path + 'negy' + format,
  305. path + 'posz' + format, path + 'negz' + format
  306. ];
  307. envMap = new THREE.CubeTextureLoader().load( urls );
  308. envMap.format = THREE.RGBFormat;
  309. return envMap;
  310. }
  311. var sceneList = [
  312. {
  313. name : 'BoomBox (PBR)', url : './models/gltf/BoomBox/%s/BoomBox.gltf',
  314. cameraPos: new THREE.Vector3(0.02, 0.01, 0.03),
  315. objectRotation: new THREE.Euler(0, Math.PI, 0),
  316. addLights:true,
  317. extensions: ['glTF', 'glTF-pbrSpecularGlossiness', 'glTF-Binary'],
  318. addEnvMap: true
  319. },
  320. {
  321. name : 'MetalRoughSpheres (PBR)', url : './models/gltf/MetalRoughSpheres/%s/MetalRoughSpheres.gltf',
  322. cameraPos: new THREE.Vector3(2, 1, 15),
  323. objectRotation: new THREE.Euler(0, 0, 0),
  324. addLights:true,
  325. extensions: ['glTF', 'glTF-Embedded'],
  326. addEnvMap: true
  327. },
  328. {
  329. name : 'Duck', url : './models/gltf/Duck/%s/Duck.gltf',
  330. cameraPos: new THREE.Vector3(0, 3, 5),
  331. addLights:true,
  332. addGround:true,
  333. shadows:true,
  334. extensions: ['glTF', 'glTF-Embedded', 'glTF-pbrSpecularGlossiness', 'glTF-Binary', 'glTF-Draco']
  335. },
  336. {
  337. name : "Monster", url : "./models/gltf/Monster/%s/Monster.gltf",
  338. cameraPos: new THREE.Vector3(30, 10, 70),
  339. objectScale: new THREE.Vector3(0.4, 0.4, 0.4),
  340. objectPosition: new THREE.Vector3(2, 1, 0),
  341. objectRotation: new THREE.Euler(0, - 3 * Math.PI / 4, 0),
  342. animationTime: 3,
  343. addLights:true,
  344. shadows:true,
  345. addGround:true,
  346. extensions: ['glTF', 'glTF-Embedded', 'glTF-pbrSpecularGlossiness', 'glTF-Binary', 'glTF-Draco']
  347. },
  348. {
  349. name : "Cesium Man", url : "./models/gltf/CesiumMan/%s/CesiumMan.gltf",
  350. cameraPos: new THREE.Vector3(0, 3, 10),
  351. objectRotation: new THREE.Euler(0, 0, 0),
  352. addLights:true,
  353. addGround:true,
  354. shadows:true,
  355. extensions: ['glTF', 'glTF-Embedded', 'glTF-pbrSpecularGlossiness', 'glTF-Binary', 'glTF-Draco']
  356. },
  357. {
  358. name : "Cesium Milk Truck",
  359. url : "./models/gltf/CesiumMilkTruck/%s/CesiumMilkTruck.gltf",
  360. cameraPos: new THREE.Vector3(0, 3, 10),
  361. addLights:true,
  362. addGround:true,
  363. shadows:true,
  364. extensions: ['glTF', 'glTF-Embedded', 'glTF-pbrSpecularGlossiness', 'glTF-Binary', 'glTF-Draco']
  365. },
  366. {
  367. name : "Rigged Simple",
  368. url : "./models/gltf/RiggedSimple/%s/RiggedSimple.gltf",
  369. cameraPos: new THREE.Vector3(0, 5, 15),
  370. objectRotation: new THREE.Euler(0, 90, 0),
  371. addLights:true,
  372. shadows:true,
  373. extensions: ['glTF', 'glTF-Embedded', 'glTF-pbrSpecularGlossiness', 'glTF-Binary', 'glTF-Draco']
  374. },
  375. {
  376. name : 'Outlined Box',
  377. url : './models/gltf/OutlinedBox/OutlinedBox.gltf',
  378. cameraPos: new THREE.Vector3(0, 5, 15),
  379. objectScale: new THREE.Vector3(0.01, 0.01, 0.01),
  380. objectRotation: new THREE.Euler(0, 90, 0),
  381. addLights:true,
  382. shadows:true,
  383. extensions: ['glTF']
  384. },
  385. ];
  386. function buildSceneList() {
  387. var elt = document.getElementById('scenes_list');
  388. while( elt.hasChildNodes() ){
  389. elt.removeChild(elt.lastChild);
  390. }
  391. var i, len = sceneList.length;
  392. for (i = 0; i < len; i++) {
  393. var option = document.createElement("option");
  394. option.text=sceneList[i].name;
  395. elt.add(option);
  396. }
  397. }
  398. function switchScene(index) {
  399. cleanup();
  400. initScene(index);
  401. var elt = document.getElementById('scenes_list');
  402. elt.selectedIndex = index;
  403. }
  404. function selectScene() {
  405. var select = document.getElementById("scenes_list");
  406. var index = select.selectedIndex;
  407. if (index >= 0) {
  408. switchScene(index);
  409. }
  410. }
  411. function switchCamera(index) {
  412. cameraIndex = index;
  413. if (cameraIndex == 0) {
  414. camera = defaultCamera;
  415. }
  416. if (cameraIndex >= 1 && cameraIndex <= cameras.length) {
  417. camera = cameras[cameraIndex - 1];
  418. }
  419. var elt = document.getElementById('cameras_list');
  420. elt.selectedIndex = cameraIndex;
  421. }
  422. function updateCamerasList() {
  423. var elt = document.getElementById('cameras_list');
  424. while( elt.hasChildNodes() ){
  425. elt.removeChild(elt.lastChild);
  426. }
  427. var option = document.createElement("option");
  428. option.text="[default]";
  429. elt.add(option);
  430. var i, len = cameraNames.length;
  431. for (i = 0; i < len; i++) {
  432. var option = document.createElement("option");
  433. option.text=cameraNames[i];
  434. elt.add(option);
  435. }
  436. }
  437. function selectCamera() {
  438. var select = document.getElementById("cameras_list");
  439. var index = select.selectedIndex;
  440. if (index >= 0) {
  441. switchCamera(index);
  442. }
  443. }
  444. function toggleAnimations() {
  445. var i, len = gltf.animations.length;
  446. for (i = 0; i < len; i++) {
  447. var clip = gltf.animations[i];
  448. var action = mixer.existingAction( clip );
  449. if (action.isRunning()) {
  450. action.stop();
  451. } else {
  452. action.play();
  453. }
  454. }
  455. }
  456. var extensionSelect = document.getElementById("extensions_list");
  457. var extension = extensionSelect.value;
  458. function selectExtension()
  459. {
  460. extension = extensionSelect.value;
  461. selectScene();
  462. }
  463. function cleanup() {
  464. if (container && renderer) {
  465. container.removeChild(renderer.domElement);
  466. }
  467. cameraIndex = 0;
  468. cameras = [];
  469. cameraNames = [];
  470. defaultCamera = null;
  471. if (!loader || !mixer)
  472. return;
  473. mixer.stopAllAction();
  474. }
  475. onload();
  476. </script>
  477. </body>
  478. </html>