webgl_loader_gltf_extensions.html 15 KB

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