webgl_loader_gltf.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - glTF</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> loader
  60. <br>
  61. monster by <a href="http://www.3drt.com/downloads.htm" target="_blank">3drt</a> - COLLADA duck by Sony -
  62. Cesium models by <a href="http://cesiumjs.org/" target="_blank">Cesium</a> -
  63. BoomBox by <a href="https://www.microsoft.com/" target="_blank">Microsoft</a>
  64. </div>
  65. <div id="container"></div>
  66. <div id="controls">
  67. <div id="status">Loading...</div>
  68. <hr />
  69. <div>
  70. Model
  71. <select id="scenes_list" size="1" onchange="selectScene();" ondblclick="selectScene();"></select>
  72. </div>
  73. <div>
  74. Camera
  75. <select id="cameras_list" size="1" onchange="selectCamera();" ondblclick="selectCamera();"></select>
  76. </div>
  77. <div>
  78. Animations
  79. <input type="checkbox" checked onclick="toggleAnimations();">Play</input>
  80. </div>
  81. <div>
  82. Extension
  83. <select id="extensions_list" onchange="selectExtension();">
  84. <option value="glTF">None</option>
  85. <option value="glTF-MaterialsCommon">Built-in shaders</option>
  86. <option value="glTF-Binary">Binary</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/GLTF2Loader.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. defaultCamera = new THREE.PerspectiveCamera( 45, container.offsetWidth / container.offsetHeight, 1, 20000 );
  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.setClearColor( 0x222222 );
  141. renderer.setPixelRatio( window.devicePixelRatio );
  142. renderer.setSize( window.innerWidth, window.innerHeight );
  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. shading: THREE.SmoothShading
  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.GLTF2Loader();
  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. extensionSelect.value = extension = 'glTF';
  172. }
  173. }
  174. var url = sceneInfo.url;
  175. var r = eval("/" + '\%s' + "/g");
  176. url = url.replace(r, extension);
  177. if (extension === 'glTF-Binary') {
  178. url = url.replace('.gltf', '.glb');
  179. }
  180. var loadStartTime = performance.now();
  181. var status = document.getElementById("status");
  182. status.innerHTML = "Loading...";
  183. loader.load( url, function(data) {
  184. gltf = data;
  185. var object = gltf.scene;
  186. status.innerHTML = "Load time: " + ( performance.now() - loadStartTime ).toFixed( 2 ) + " ms.";
  187. if (sceneInfo.cameraPos)
  188. defaultCamera.position.copy(sceneInfo.cameraPos);
  189. if (sceneInfo.center) {
  190. orbitControls.target.copy(sceneInfo.center);
  191. }
  192. if (sceneInfo.objectPosition) {
  193. object.position.copy(sceneInfo.objectPosition);
  194. if (spot1) {
  195. spot1.position.set(sceneInfo.objectPosition.x - 100, sceneInfo.objectPosition.y + 200, sceneInfo.objectPosition.z - 100 );
  196. spot1.target.position.copy(sceneInfo.objectPosition);
  197. }
  198. }
  199. if (sceneInfo.objectRotation)
  200. object.rotation.copy(sceneInfo.objectRotation);
  201. if (sceneInfo.objectScale)
  202. object.scale.copy(sceneInfo.objectScale);
  203. cameraIndex = 0;
  204. cameras = [];
  205. cameraNames = [];
  206. if (gltf.cameras && gltf.cameras.length) {
  207. var i, len = gltf.cameras.length;
  208. for (i = 0; i < len; i++) {
  209. var addCamera = true;
  210. var cameraName = gltf.cameras[i].parent.name;
  211. if (sceneInfo.cameras && !(cameraName in sceneInfo.cameras)) {
  212. addCamera = false;
  213. }
  214. if (addCamera) {
  215. cameraNames.push(cameraName);
  216. cameras.push(gltf.cameras[i]);
  217. }
  218. }
  219. updateCamerasList();
  220. switchCamera(1);
  221. } else {
  222. updateCamerasList();
  223. switchCamera(0);
  224. }
  225. var animations = gltf.animations;
  226. if ( animations && animations.length ) {
  227. mixer = new THREE.AnimationMixer( object );
  228. for ( var i = 0; i < animations.length; i ++ ) {
  229. var animation = animations[ i ];
  230. // There's .3333 seconds junk at the tail of the Monster animation that
  231. // keeps it from looping cleanly. Clip it at 3 seconds
  232. if ( sceneInfo.animationTime )
  233. animation.duration = sceneInfo.animationTime;
  234. mixer.clipAction( animation ).play();
  235. }
  236. }
  237. scene.add( object );
  238. onWindowResize();
  239. });
  240. orbitControls = new THREE.OrbitControls(defaultCamera, renderer.domElement);
  241. }
  242. function onWindowResize() {
  243. defaultCamera.aspect = container.offsetWidth / container.offsetHeight;
  244. defaultCamera.updateProjectionMatrix();
  245. var i, len = cameras.length;
  246. for (i = 0; i < len; i++) { // just do it for default
  247. cameras[i].aspect = container.offsetWidth / container.offsetHeight;
  248. cameras[i].updateProjectionMatrix();
  249. }
  250. renderer.setSize( window.innerWidth, window.innerHeight );
  251. }
  252. function animate() {
  253. requestAnimationFrame( animate );
  254. if (mixer) mixer.update(clock.getDelta());
  255. if (cameraIndex == 0)
  256. orbitControls.update();
  257. render();
  258. }
  259. function render() {
  260. renderer.render( scene, camera );
  261. }
  262. function onKeyDown(event) {
  263. var chr = String.fromCharCode(event.keyCode);
  264. if (chr == ' ') {
  265. index = cameraIndex + 1;
  266. if (index > cameras.length)
  267. index = 0;
  268. switchCamera(index);
  269. } else {
  270. var index = parseInt(chr);
  271. if (!isNaN(index) && (index <= cameras.length)) {
  272. switchCamera(index);
  273. }
  274. }
  275. }
  276. var sceneList = [
  277. {
  278. name : "Monster", url : "./models/gltf/monster/%s/monster.gltf",
  279. cameraPos: new THREE.Vector3(30, 10, 70),
  280. objectScale: new THREE.Vector3(0.4, 0.4, 0.4),
  281. objectPosition: new THREE.Vector3(2, 1, 0),
  282. objectRotation: new THREE.Euler(0, - 3 * Math.PI / 4, 0),
  283. animationTime: 3,
  284. addLights:true,
  285. shadows:true,
  286. addGround:true,
  287. extensions: ["glTF", "glTF-MaterialsCommon", "glTF-Binary"]
  288. },
  289. {
  290. name : "BoomBox (PBR)", url : "./models/gltf/BoomBox/%s/BoomBox.gltf",
  291. cameraPos: new THREE.Vector3(2, 1, 3),
  292. objectRotation: new THREE.Euler(0, Math.PI, 0),
  293. addLights:true,
  294. extensions: ["glTF"]
  295. },
  296. {
  297. name : "Duck", url : "./models/gltf/duck/%s/duck.gltf",
  298. cameraPos: new THREE.Vector3(0, 3, 5),
  299. addLights:true,
  300. addGround:true,
  301. shadows:true,
  302. extensions: ["glTF", "glTF-MaterialsCommon", "glTF-Binary"]
  303. },
  304. {
  305. name : "Cesium Man", url : "./models/gltf/CesiumMan/%s/Cesium_Man.gltf",
  306. cameraPos: new THREE.Vector3(0, 3, 10),
  307. objectRotation: new THREE.Euler(0, 0, 0),
  308. addLights:true,
  309. addGround:true,
  310. shadows:true,
  311. extensions: ["glTF", "glTF-MaterialsCommon", "glTF-Binary"]
  312. },
  313. {
  314. name : "Cesium Milk Truck",
  315. url : "./models/gltf/CesiumMilkTruck/%s/CesiumMilkTruck.gltf",
  316. cameraPos: new THREE.Vector3(0, 3, 10),
  317. addLights:true,
  318. addGround:true,
  319. shadows:true,
  320. extensions: ["glTF", "glTF-MaterialsCommon", "glTF-Binary"]
  321. },
  322. {
  323. name : "Rigged Simple",
  324. url : "./models/gltf/RiggedSimple/%s/RiggedSimple.gltf",
  325. cameraPos: new THREE.Vector3(0, 5, 15),
  326. objectRotation: new THREE.Euler(0, 90, 0),
  327. addLights:true,
  328. shadows:true,
  329. extensions: ["glTF"]
  330. },
  331. {
  332. name : "Snowflake",
  333. url : "./models/gltf/snowflake/snowFlake.gltf",
  334. objectScale: new THREE.Vector3(4, 4, 1),
  335. cameraPos: new THREE.Vector3(0, 100, 5000),
  336. objectPosition: new THREE.Vector3(0, 0, 0),
  337. addLights:false,
  338. addGround:false,
  339. shadows:false,
  340. extensions: ["glTF"]
  341. },
  342. {
  343. name : "Snowflakes",
  344. url : "./models/gltf/snowflakes/snowFlakes.gltf",
  345. cameraPos: new THREE.Vector3(0, 100, 5000),
  346. objectPosition: new THREE.Vector3(-1200, -1200, 0),
  347. addLights:false,
  348. addGround:false,
  349. shadows:false,
  350. extensions: ["glTF"]
  351. }
  352. ];
  353. function buildSceneList() {
  354. var elt = document.getElementById('scenes_list');
  355. while( elt.hasChildNodes() ){
  356. elt.removeChild(elt.lastChild);
  357. }
  358. var i, len = sceneList.length;
  359. for (i = 0; i < len; i++) {
  360. option = document.createElement("option");
  361. option.text=sceneList[i].name;
  362. elt.add(option);
  363. }
  364. }
  365. function switchScene(index) {
  366. cleanup();
  367. initScene(index);
  368. var elt = document.getElementById('scenes_list');
  369. elt.selectedIndex = index;
  370. }
  371. function selectScene() {
  372. var select = document.getElementById("scenes_list");
  373. var index = select.selectedIndex;
  374. if (index >= 0) {
  375. switchScene(index);
  376. }
  377. }
  378. function switchCamera(index) {
  379. cameraIndex = index;
  380. if (cameraIndex == 0) {
  381. camera = defaultCamera;
  382. }
  383. if (cameraIndex >= 1 && cameraIndex <= cameras.length) {
  384. camera = cameras[cameraIndex - 1];
  385. }
  386. var elt = document.getElementById('cameras_list');
  387. elt.selectedIndex = cameraIndex;
  388. }
  389. function updateCamerasList() {
  390. var elt = document.getElementById('cameras_list');
  391. while( elt.hasChildNodes() ){
  392. elt.removeChild(elt.lastChild);
  393. }
  394. option = document.createElement("option");
  395. option.text="[default]";
  396. elt.add(option);
  397. var i, len = cameraNames.length;
  398. for (i = 0; i < len; i++) {
  399. option = document.createElement("option");
  400. option.text=cameraNames[i];
  401. elt.add(option);
  402. }
  403. }
  404. function selectCamera() {
  405. var select = document.getElementById("cameras_list");
  406. var index = select.selectedIndex;
  407. if (index >= 0) {
  408. switchCamera(index);
  409. }
  410. }
  411. function toggleAnimations() {
  412. var i, len = gltf.animations.length;
  413. for (i = 0; i < len; i++) {
  414. var clip = gltf.animations[i];
  415. var action = mixer.existingAction( clip );
  416. if (action.isRunning()) {
  417. action.stop();
  418. } else {
  419. action.play();
  420. }
  421. }
  422. }
  423. var extensionSelect = document.getElementById("extensions_list");
  424. var extension = extensionSelect.value;
  425. function selectExtension()
  426. {
  427. extension = extensionSelect.value;
  428. selectScene();
  429. }
  430. function cleanup() {
  431. if (container && renderer) {
  432. container.removeChild(renderer.domElement);
  433. }
  434. cameraIndex = 0;
  435. cameras = [];
  436. cameraNames = [];
  437. defaultCamera = null;
  438. if (!loader || !mixer)
  439. return;
  440. mixer.stopAllAction();
  441. }
  442. onload();
  443. </script>
  444. </body>
  445. </html>