webgl_loader_gltf.html 13 KB

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