webgl_loader_gltf_extensions.html 15 KB

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