webgl_loader_gltf2.html 12 KB

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