webgl_loader_gltf_extensions.html 15 KB

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