webgl_loader_gltf2.html 15 KB

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