webgl_loader_gltf.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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: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.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( 10, 20, 10 );
  152. spot1.angle = 0.25;
  153. spot1.distance = 1024;
  154. spot1.penumbra = 0.75;
  155. if ( sceneInfo.shadows ) {
  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.setClearColor( 0x222222 );
  166. renderer.setPixelRatio( window.devicePixelRatio );
  167. renderer.setSize( window.innerWidth, window.innerHeight );
  168. if (sceneInfo.shadows) {
  169. renderer.shadowMap.enabled = true;
  170. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  171. }
  172. container.appendChild( renderer.domElement );
  173. var ground = null;
  174. if (sceneInfo.addGround) {
  175. var groundMaterial = new THREE.MeshPhongMaterial({
  176. color: 0xFFFFFF,
  177. shading: THREE.SmoothShading,
  178. });
  179. ground = new THREE.Mesh( new THREE.PlaneBufferGeometry(512, 512), groundMaterial);
  180. if (sceneInfo.shadows) {
  181. ground.receiveShadow = true;
  182. }
  183. if (sceneInfo.groundPos) {
  184. ground.position.copy(sceneInfo.groundPos);
  185. } else {
  186. ground.position.z = -70;
  187. }
  188. ground.rotation.x = -Math.PI / 2;
  189. scene.add(ground);
  190. }
  191. loader = new THREE.glTFLoader;
  192. var loadStartTime = Date.now();
  193. var status = document.getElementById("status");
  194. status.innerHTML = "Loading...";
  195. var url = sceneInfo.url;
  196. var r = eval("/" + '\%s' + "/g");
  197. var dir = useMaterialsExtension ? 'glTF-MaterialsCommon' : 'glTF';
  198. url = url.replace(r, dir);
  199. var loadStartTime = Date.now();
  200. var status = document.getElementById("status");
  201. status.innerHTML = "Loading...";
  202. loader.load( url, function(data) {
  203. gltf = data;
  204. var object = gltf.scene;
  205. var loadEndTime = Date.now();
  206. var loadTime = (loadEndTime - loadStartTime) / 1000;
  207. status.innerHTML = "Load time: " + loadTime.toFixed(2) + " seconds.";
  208. if (sceneInfo.cameraPos)
  209. defaultCamera.position.copy(sceneInfo.cameraPos);
  210. if (sceneInfo.center) {
  211. orbitControls.target.copy(sceneInfo.center);
  212. }
  213. if (sceneInfo.objectPosition) {
  214. object.position.copy(sceneInfo.objectPosition);
  215. if (spot1) {
  216. spot1.position.set(sceneInfo.objectPosition.x - 100, sceneInfo.objectPosition.y + 200, sceneInfo.objectPosition.z - 100 );
  217. spot1.target.position.copy(sceneInfo.objectPosition);
  218. }
  219. }
  220. if (sceneInfo.objectRotation)
  221. object.rotation.copy(sceneInfo.objectRotation);
  222. if (sceneInfo.objectScale)
  223. object.scale.copy(sceneInfo.objectScale);
  224. cameraIndex = 0;
  225. cameras = [];
  226. cameraNames = [];
  227. if (gltf.cameras && gltf.cameras.length) {
  228. var i, len = gltf.cameras.length;
  229. for (i = 0; i < len; i++) {
  230. var addCamera = true;
  231. var cameraName = gltf.cameras[i].parent.name;
  232. if (sceneInfo.cameras && !(cameraName in sceneInfo.cameras)) {
  233. addCamera = false;
  234. }
  235. if (addCamera) {
  236. cameraNames.push(cameraName);
  237. cameras.push(gltf.cameras[i]);
  238. }
  239. }
  240. updateCamerasList();
  241. switchCamera(1);
  242. } else {
  243. updateCamerasList();
  244. switchCamera(0);
  245. }
  246. if (gltf.animations && gltf.animations.length) {
  247. var i, len = gltf.animations.length;
  248. for (i = 0; i < len; i++) {
  249. var animation = gltf.animations[i];
  250. animation.loop = true;
  251. // There's .3333 seconds junk at the tail of the Monster animation that
  252. // keeps it from looping cleanly. Clip it at 3 seconds
  253. if (sceneInfo.animationTime)
  254. animation.duration = sceneInfo.animationTime;
  255. animation.play();
  256. }
  257. }
  258. scene.add( object );
  259. onWindowResize();
  260. });
  261. orbitControls = new THREE.OrbitControls(defaultCamera, renderer.domElement);
  262. }
  263. function onWindowResize() {
  264. defaultCamera.aspect = container.offsetWidth / container.offsetHeight;
  265. defaultCamera.updateProjectionMatrix();
  266. var i, len = cameras.length;
  267. for (i = 0; i < len; i++) { // just do it for default
  268. cameras[i].aspect = container.offsetWidth / container.offsetHeight;
  269. cameras[i].updateProjectionMatrix();
  270. }
  271. renderer.setSize( window.innerWidth, window.innerHeight );
  272. }
  273. function animate() {
  274. requestAnimationFrame( animate );
  275. THREE.glTFAnimator.update();
  276. THREE.glTFShaders.update(scene, camera);
  277. if (cameraIndex == 0)
  278. orbitControls.update();
  279. render();
  280. }
  281. function render() {
  282. renderer.render( scene, camera );
  283. }
  284. function onKeyDown(event) {
  285. var chr = String.fromCharCode(event.keyCode);
  286. if (chr == ' ') {
  287. index = cameraIndex + 1;
  288. if (index > cameras.length)
  289. index = 0;
  290. switchCamera(index);
  291. } else {
  292. var index = parseInt(chr);
  293. if (!isNaN(index) && (index <= cameras.length)) {
  294. switchCamera(index);
  295. }
  296. }
  297. }
  298. var sceneList = [
  299. {
  300. name : "Monster", url : "./models/gltf/monster/%s/monster.gltf",
  301. cameraPos: new THREE.Vector3(30, 10, 70),
  302. objectScale: new THREE.Vector3(0.01, 0.01, 0.01),
  303. objectPosition: new THREE.Vector3(2, 6, 0),
  304. objectRotation: new THREE.Euler(0, - 3 * Math.PI / 4, 0),
  305. animationTime: 3,
  306. addLights:true,
  307. shadows:true,
  308. addGround:true
  309. },
  310. {
  311. name : "Duck", url : "./models/gltf/duck/%s/duck.gltf",
  312. cameraPos: new THREE.Vector3(0, 3, 5),
  313. addLights:true,
  314. addGround:true,
  315. shadows:true
  316. },
  317. {
  318. name : "Cesium Man", url : "./models/gltf/CesiumMan/%s/Cesium_Man.gltf",
  319. cameraPos: new THREE.Vector3(0, 3, 10),
  320. objectRotation: new THREE.Euler(0, 0, 0),
  321. addLights:true,
  322. addGround:true,
  323. shadows:true
  324. },
  325. {
  326. name : "Cesium Milk Truck",
  327. url : "./models/gltf/CesiumMilkTruck/%s/CesiumMilkTruck.gltf",
  328. cameraPos: new THREE.Vector3(0, 3, 10),
  329. addLights:true,
  330. addGround:true,
  331. shadows:true
  332. },
  333. ];
  334. function buildSceneList() {
  335. var elt = document.getElementById('scenes_list');
  336. while( elt.hasChildNodes() ){
  337. elt.removeChild(elt.lastChild);
  338. }
  339. var i, len = sceneList.length;
  340. for (i = 0; i < len; i++) {
  341. option = document.createElement("option");
  342. option.text=sceneList[i].name;
  343. elt.add(option);
  344. }
  345. }
  346. function switchScene(index) {
  347. cleanup();
  348. initScene(index);
  349. var elt = document.getElementById('scenes_list');
  350. elt.selectedIndex = index;
  351. }
  352. function selectScene() {
  353. var select = document.getElementById("scenes_list");
  354. var index = select.selectedIndex;
  355. if (index >= 0) {
  356. switchScene(index);
  357. }
  358. }
  359. function switchCamera(index) {
  360. cameraIndex = index;
  361. if (cameraIndex == 0) {
  362. camera = defaultCamera;
  363. }
  364. if (cameraIndex >= 1 && cameraIndex <= cameras.length) {
  365. camera = cameras[cameraIndex - 1];
  366. }
  367. var elt = document.getElementById('cameras_list');
  368. elt.selectedIndex = cameraIndex;
  369. }
  370. function updateCamerasList() {
  371. var elt = document.getElementById('cameras_list');
  372. while( elt.hasChildNodes() ){
  373. elt.removeChild(elt.lastChild);
  374. }
  375. option = document.createElement("option");
  376. option.text="[default]";
  377. elt.add(option);
  378. var i, len = cameraNames.length;
  379. for (i = 0; i < len; i++) {
  380. option = document.createElement("option");
  381. option.text=cameraNames[i];
  382. elt.add(option);
  383. }
  384. }
  385. function selectCamera() {
  386. var select = document.getElementById("cameras_list");
  387. var index = select.selectedIndex;
  388. if (index >= 0) {
  389. switchCamera(index);
  390. }
  391. }
  392. function toggleAnimations() {
  393. var i, len = gltf.animations.length;
  394. for (i = 0; i < len; i++) {
  395. var animation = gltf.animations[i];
  396. if (animation.running) {
  397. animation.stop();
  398. } else {
  399. animation.play();
  400. }
  401. }
  402. }
  403. var useextmaterials = document.getElementById("materials_extension_checkbox");
  404. var useMaterialsExtension = useextmaterials.hasAttribute("checked");
  405. function toggleMaterialsExtension()
  406. {
  407. useMaterialsExtension = !useMaterialsExtension;
  408. selectScene();
  409. }
  410. function cleanup() {
  411. if (container && renderer) {
  412. container.removeChild(renderer.domElement);
  413. }
  414. cameraIndex = 0;
  415. cameras = [];
  416. cameraNames = [];
  417. defaultCamera = null;
  418. if (!loader || !gltf.animations)
  419. return;
  420. var i, len = gltf.animations.length;
  421. for (i = 0; i < len; i++) {
  422. var animation = gltf.animations[i];
  423. if (animation.running) {
  424. animation.stop();
  425. }
  426. }
  427. }
  428. onload();
  429. </script>
  430. </body>
  431. </html>