webgl_loader_gltf.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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: #EEF;
  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:250px;
  33. bottom:0%;
  34. right:0%;
  35. height:132px;
  36. background-color:White;
  37. opacity:.9;
  38. font: 13px/1.231 "Lucida Grande", Lucida, Verdana, sans-serif;
  39. }
  40. #status {
  41. position:absolute;
  42. width:25%;
  43. left:2%;
  44. top:95%;
  45. height:5%;
  46. opacity:.9;
  47. font: 13px/1.231 "Lucida Grande", Lucida, Verdana, sans-serif;
  48. }
  49. .control {
  50. position:absolute;
  51. margin-left:12px;
  52. width:100%;
  53. font-weight:bold;
  54. }
  55. .controlValue {
  56. position:absolute;
  57. left:36%;
  58. top:0%;
  59. }
  60. #scenes_control {
  61. position:absolute;
  62. top:8px;
  63. }
  64. #cameras_control {
  65. position:absolute;
  66. top:40px;
  67. }
  68. #animations_control {
  69. position:absolute;
  70. top:72px;
  71. }
  72. #materials_extension_control {
  73. position:absolute;
  74. top:104px;
  75. }
  76. #info a, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
  77. </style>
  78. </head>
  79. <body>
  80. <div id="info">
  81. <a href="http://threejs.org" target="_blank">three.js</a> -
  82. <a href="https://github.com/KhronosGroup/glTF" target="_blank">glTF</a> loader -
  83. <br></br>
  84. monster by <a href="http://www.3drt.com/downloads.htm" target="_blank">3drt</a> -
  85. COLLADA duck by Sony - other models courtesy <a href="http://cesiumjs.org/" target="_blank">Cesium</a>
  86. </div>
  87. <div id="container">
  88. </div>
  89. <div id="status">
  90. </div>
  91. <div id="controls">
  92. <div class="control" id="scenes_control">
  93. Model
  94. <select class="controlValue" id="scenes_list" size="1" onchange="selectScene();" ondblclick="selectScene();">
  95. </select>
  96. </div>
  97. <div class="control" id="cameras_control">
  98. Camera
  99. <select class="controlValue" id="cameras_list" size="1" onchange="selectCamera();" ondblclick="selectCamera();">
  100. </select>
  101. </div>
  102. <div class="control" id="animations_control">
  103. Animations
  104. <div class="controlValue"><input type="checkbox" checked onclick="toggleAnimations();">Play</input></div>
  105. </div>
  106. <div class="control" id="materials_extension_control">
  107. Shaders
  108. <div class="controlValue">
  109. <input type="checkbox" id="materials_extension_checkbox" checked onclick="toggleMaterialsExtension();">Use built-in</input>
  110. </div>
  111. </div>
  112. </div>
  113. <script src="../build/three.min.js"></script>
  114. <script src="js/controls/OrbitControls.js"></script>
  115. <script src="js/loaders/gltf/glTF-parser.js"></script>
  116. <script src="js/loaders/gltf/glTFLoader.js"></script>
  117. <script src="js/loaders/gltf/glTFLoaderUtils.js"></script>
  118. <script src="js/loaders/gltf/glTFAnimation.js"></script>
  119. <script src="js/loaders/gltf/glTFShaders.js"></script>
  120. <script>
  121. var orbitControls = null;
  122. var container, camera, scene, renderer, loader;
  123. var cameraIndex = 0;
  124. var cameras = [];
  125. var cameraNames = [];
  126. var defaultCamera = null;
  127. var gltf = null;
  128. function onload() {
  129. window.addEventListener( 'resize', onWindowResize, false );
  130. document.addEventListener( 'keydown', function(e) { onKeyDown(e); }, false );
  131. buildSceneList();
  132. switchScene(0);
  133. animate();
  134. }
  135. function initScene(index) {
  136. container = document.getElementById( 'container' );
  137. scene = new THREE.Scene();
  138. defaultCamera = new THREE.PerspectiveCamera( 45, container.offsetWidth / container.offsetHeight, 1, 20000 );
  139. //defaultCamera.up = new THREE.Vector3( 0, 1, 0 );
  140. scene.add( defaultCamera );
  141. camera = defaultCamera;
  142. var sceneInfo = sceneList[index];
  143. var spot1 = null;
  144. if (sceneInfo.addLights) {
  145. var ambient = new THREE.AmbientLight( 0x222222 );
  146. scene.add( ambient );
  147. var directionalLight = new THREE.DirectionalLight( 0xdddddd );
  148. directionalLight.position.set( 0, 0, 1 ).normalize();
  149. scene.add( directionalLight );
  150. spot1 = new THREE.SpotLight( 0xffffff, 1 );
  151. spot1.position.set( 100, 200, 100 );
  152. spot1.target.position.set( 0, 0, 0 );
  153. if (sceneInfo.shadows) {
  154. spot1.shadow.cameraNear = 1;
  155. spot1.shadow.cameraFar = 1024;
  156. spot1.castShadow = true;
  157. spot1.shadow.bias = 0.0001;
  158. spot1.shadow.mapSize.width = 2048;
  159. spot1.shadow.mapSize.height = 2048;
  160. }
  161. scene.add( spot1 );
  162. }
  163. // RENDERER
  164. renderer = new THREE.WebGLRenderer({antialias:true});
  165. renderer.setPixelRatio( window.devicePixelRatio );
  166. renderer.setSize( window.innerWidth, window.innerHeight );
  167. if (sceneInfo.shadows) {
  168. renderer.shadowMap.enabled = true;
  169. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  170. }
  171. container.appendChild( renderer.domElement );
  172. var ground = null;
  173. if (sceneInfo.addGround) {
  174. var groundMaterial = new THREE.MeshPhongMaterial({
  175. color: 0xFFFFFF,
  176. shading: THREE.SmoothShading,
  177. });
  178. ground = new THREE.Mesh( new THREE.PlaneBufferGeometry(1024, 1024), groundMaterial);
  179. if (sceneInfo.shadows) {
  180. ground.receiveShadow = true;
  181. }
  182. if (sceneInfo.groundPos) {
  183. ground.position.copy(sceneInfo.groundPos);
  184. } else {
  185. ground.position.z = -70;
  186. }
  187. ground.rotation.x = -Math.PI / 2;
  188. scene.add(ground);
  189. }
  190. loader = new THREE.glTFLoader;
  191. var loadStartTime = Date.now();
  192. var status = document.getElementById("status");
  193. status.innerHTML = "Loading...";
  194. var url = sceneInfo.url;
  195. var r = eval("/" + '\%s' + "/g");
  196. var dir = useMaterialsExtension ? 'glTF-MaterialsCommon' : 'glTF';
  197. url = url.replace(r, dir);
  198. var loadStartTime = Date.now();
  199. var status = document.getElementById("status");
  200. status.innerHTML = "Loading...";
  201. loader.load( url, function(data) {
  202. gltf = data;
  203. var object = gltf.scene;
  204. var loadEndTime = Date.now();
  205. var loadTime = (loadEndTime - loadStartTime) / 1000;
  206. status.innerHTML = "Load time: " + loadTime.toFixed(2) + " seconds.";
  207. if (sceneInfo.cameraPos)
  208. defaultCamera.position.copy(sceneInfo.cameraPos);
  209. if (sceneInfo.center) {
  210. orbitControls.center.copy(sceneInfo.center);
  211. }
  212. if (sceneInfo.objectPosition) {
  213. object.position.copy(sceneInfo.objectPosition);
  214. if (spot1) {
  215. spot1.position.set(sceneInfo.objectPosition.x - 100, sceneInfo.objectPosition.y + 200, sceneInfo.objectPosition.z - 100 );
  216. spot1.target.position.copy(sceneInfo.objectPosition);
  217. }
  218. }
  219. if (sceneInfo.objectRotation)
  220. object.rotation.copy(sceneInfo.objectRotation);
  221. if (sceneInfo.objectScale)
  222. object.scale.copy(sceneInfo.objectScale);
  223. cameraIndex = 0;
  224. cameras = [];
  225. cameraNames = [];
  226. if (gltf.cameras && gltf.cameras.length) {
  227. var i, len = gltf.cameras.length;
  228. for (i = 0; i < len; i++) {
  229. var addCamera = true;
  230. var cameraName = gltf.cameras[i].parent.name;
  231. if (sceneInfo.cameras && !(cameraName in sceneInfo.cameras)) {
  232. addCamera = false;
  233. }
  234. if (addCamera) {
  235. cameraNames.push(cameraName);
  236. cameras.push(gltf.cameras[i]);
  237. }
  238. }
  239. updateCamerasList();
  240. switchCamera(1);
  241. } else {
  242. updateCamerasList();
  243. switchCamera(0);
  244. }
  245. if (gltf.animations && gltf.animations.length) {
  246. var i, len = gltf.animations.length;
  247. for (i = 0; i < len; i++) {
  248. var animation = gltf.animations[i];
  249. animation.loop = true;
  250. // There's .3333 seconds junk at the tail of the Monster animation that
  251. // keeps it from looping cleanly. Clip it at 3 seconds
  252. if (sceneInfo.animationTime)
  253. animation.duration = sceneInfo.animationTime;
  254. animation.play();
  255. }
  256. }
  257. scene.add( object );
  258. onWindowResize();
  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. THREE.glTFAnimator.update();
  275. THREE.glTFShaders.update(scene, camera);
  276. if (cameraIndex == 0)
  277. orbitControls.update();
  278. render();
  279. }
  280. function render() {
  281. renderer.render( scene, camera );
  282. }
  283. function onKeyDown(event) {
  284. var chr = String.fromCharCode(event.keyCode);
  285. if (chr == ' ') {
  286. index = cameraIndex + 1;
  287. if (index > cameras.length)
  288. index = 0;
  289. switchCamera(index);
  290. } else {
  291. var index = parseInt(chr);
  292. if (!isNaN(index) && (index <= cameras.length)) {
  293. switchCamera(index);
  294. }
  295. }
  296. }
  297. var sceneList = [
  298. {
  299. name : "Monster", url : "./models/gltf/monster/%s/monster.gltf",
  300. cameraPos: new THREE.Vector3(30, 10, 70),
  301. objectScale: new THREE.Vector3(0.01, 0.01, 0.01),
  302. objectPosition: new THREE.Vector3(2, 6, 0),
  303. objectRotation: new THREE.Euler(0, - 3 * Math.PI / 4, 0),
  304. animationTime: 3,
  305. addLights:true,
  306. shadows:true,
  307. addGround:true
  308. },
  309. {
  310. name : "Duck", url : "./models/gltf/duck/%s/duck.gltf",
  311. cameraPos: new THREE.Vector3(0, 3, 5),
  312. addLights:true,
  313. addGround:true,
  314. shadows:true
  315. },
  316. {
  317. name : "Cesium Man", url : "./models/gltf/CesiumMan/%s/Cesium_Man.gltf",
  318. cameraPos: new THREE.Vector3(0, 3, 10),
  319. objectRotation: new THREE.Euler(0, 0, 0),
  320. addLights:true,
  321. addGround:true,
  322. shadows:true
  323. },
  324. {
  325. name : "Cesium Milk Truck",
  326. url : "./models/gltf/CesiumMilkTruck/%s/CesiumMilkTruck.gltf",
  327. cameraPos: new THREE.Vector3(0, 3, 10),
  328. addLights:true,
  329. addGround:true,
  330. shadows:true
  331. },
  332. ];
  333. function buildSceneList() {
  334. var elt = document.getElementById('scenes_list');
  335. while( elt.hasChildNodes() ){
  336. elt.removeChild(elt.lastChild);
  337. }
  338. var i, len = sceneList.length;
  339. for (i = 0; i < len; i++) {
  340. option = document.createElement("option");
  341. option.text=sceneList[i].name;
  342. elt.add(option);
  343. }
  344. }
  345. function switchScene(index) {
  346. cleanup();
  347. initScene(index);
  348. var elt = document.getElementById('scenes_list');
  349. elt.selectedIndex = index;
  350. }
  351. function selectScene() {
  352. var select = document.getElementById("scenes_list");
  353. var index = select.selectedIndex;
  354. if (index >= 0) {
  355. switchScene(index);
  356. }
  357. }
  358. function switchCamera(index) {
  359. cameraIndex = index;
  360. if (cameraIndex == 0) {
  361. camera = defaultCamera;
  362. }
  363. if (cameraIndex >= 1 && cameraIndex <= cameras.length) {
  364. camera = cameras[cameraIndex - 1];
  365. }
  366. var elt = document.getElementById('cameras_list');
  367. elt.selectedIndex = cameraIndex;
  368. }
  369. function updateCamerasList() {
  370. var elt = document.getElementById('cameras_list');
  371. while( elt.hasChildNodes() ){
  372. elt.removeChild(elt.lastChild);
  373. }
  374. option = document.createElement("option");
  375. option.text="[default]";
  376. elt.add(option);
  377. var i, len = cameraNames.length;
  378. for (i = 0; i < len; i++) {
  379. option = document.createElement("option");
  380. option.text=cameraNames[i];
  381. elt.add(option);
  382. }
  383. }
  384. function selectCamera() {
  385. var select = document.getElementById("cameras_list");
  386. var index = select.selectedIndex;
  387. if (index >= 0) {
  388. switchCamera(index);
  389. }
  390. }
  391. function toggleAnimations() {
  392. var i, len = gltf.animations.length;
  393. for (i = 0; i < len; i++) {
  394. var animation = gltf.animations[i];
  395. if (animation.running) {
  396. animation.stop();
  397. } else {
  398. animation.play();
  399. }
  400. }
  401. }
  402. var useextmaterials = document.getElementById("materials_extension_checkbox");
  403. var useMaterialsExtension = useextmaterials.hasAttribute("checked");
  404. function toggleMaterialsExtension()
  405. {
  406. useMaterialsExtension = !useMaterialsExtension;
  407. selectScene();
  408. }
  409. function cleanup() {
  410. if (container && renderer) {
  411. container.removeChild(renderer.domElement);
  412. }
  413. cameraIndex = 0;
  414. cameras = [];
  415. cameraNames = [];
  416. defaultCamera = null;
  417. if (!loader || !gltf.animations)
  418. return;
  419. var i, len = gltf.animations.length;
  420. for (i = 0; i < len; i++) {
  421. var animation = gltf.animations[i];
  422. if (animation.running) {
  423. animation.stop();
  424. }
  425. }
  426. }
  427. onload();
  428. </script>
  429. </body>
  430. </html>