webgl_loader_gltf.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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: #fff;
  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:100px;
  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. #info a, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
  73. </style>
  74. </head>
  75. <body>
  76. <div id="info">
  77. <a href="http://threejs.org" target="_blank">three.js</a> -
  78. <a href="http://gltf.gl/" target="_blank">glTF</a> loader -
  79. <br></br>
  80. monster by <a href="http://www.3drt.com/downloads.htm" target="_blank">3drt</a> -
  81. COLLADA duck by Sony
  82. </div>
  83. <div id="container">
  84. </div>
  85. <div id="status">
  86. </div>
  87. <div id="controls">
  88. <div class="control" id="scenes_control">
  89. Model
  90. <select class="controlValue" id="scenes_list" size="1" onchange="selectScene();" ondblclick="selectScene();">
  91. </select>
  92. </div>
  93. <div class="control" id="cameras_control">
  94. Camera
  95. <select class="controlValue" id="cameras_list" size="1" onchange="selectCamera();" ondblclick="selectCamera();">
  96. </select>
  97. </div>
  98. <div class="control" id="animations_control">
  99. Animations
  100. <div class="controlValue"><input type="checkbox" checked onclick="toggleAnimations();">Play</input></div>
  101. </div>
  102. </div>
  103. <script src="../build/three.min.js"></script>
  104. <script src="js/controls/OrbitControls.js"></script>
  105. <script src="js/loaders/gltf/glTF-parser.js"></script>
  106. <script src="js/loaders/gltf/glTFLoader.js"></script>
  107. <script src="js/loaders/gltf/glTFLoaderUtils.js"></script>
  108. <script src="js/loaders/gltf/glTFAnimation.js"></script>
  109. <script>
  110. var orbitControls = null;
  111. var container, camera, scene, renderer, loader;
  112. var cameraIndex = 0;
  113. var cameras = [];
  114. var cameraNames = [];
  115. var defaultCamera = null;
  116. var gltf = null;
  117. function onload() {
  118. window.addEventListener( 'resize', onWindowResize, false );
  119. document.addEventListener( 'keypress',
  120. function(e) { onKeyPress(e); }, false );
  121. buildSceneList();
  122. switchScene(0);
  123. animate();
  124. }
  125. function initScene(index) {
  126. container = document.getElementById( 'container' );
  127. scene = new THREE.Scene();
  128. defaultCamera = new THREE.PerspectiveCamera( 45, container.offsetWidth / container.offsetHeight, 1, 20000 );
  129. //defaultCamera.up = new THREE.Vector3( 0, 1, 0 );
  130. scene.add( defaultCamera );
  131. camera = defaultCamera;
  132. var sceneInfo = sceneList[index];
  133. var spot1 = null;
  134. if (sceneInfo.addLights) {
  135. var ambient = new THREE.AmbientLight( 0x888888 );
  136. scene.add( ambient );
  137. var directionalLight = new THREE.DirectionalLight( 0xdddddd );
  138. directionalLight.position.set( 0, -1, 1 ).normalize();
  139. scene.add( directionalLight );
  140. spot1 = new THREE.SpotLight( 0xffffff, 1 );
  141. spot1.position.set( -100, 200, 100 );
  142. spot1.target.position.set( 0, 0, 0 );
  143. if (sceneInfo.shadows) {
  144. spot1.shadowCameraNear = 1;
  145. spot1.shadowCameraFar = 1024;
  146. spot1.castShadow = true;
  147. spot1.shadowDarkness = 0.3;
  148. spot1.shadowBias = 0.0001;
  149. spot1.shadowMapWidth = 2048;
  150. spot1.shadowMapHeight = 2048;
  151. }
  152. scene.add( spot1 );
  153. }
  154. // RENDERER
  155. renderer = new THREE.WebGLRenderer({antialias:true});
  156. renderer.setPixelRatio( window.devicePixelRatio );
  157. renderer.setSize( window.innerWidth, window.innerHeight );
  158. if (sceneInfo.shadows) {
  159. renderer.shadowMapEnabled = true;
  160. renderer.shadowMapSoft = true;
  161. renderer.shadowMapType = THREE.PCFSoftShadowMap;
  162. }
  163. container.appendChild( renderer.domElement );
  164. var ground = null;
  165. if (sceneInfo.addGround) {
  166. var groundMaterial = new THREE.MeshPhongMaterial({
  167. color: 0xFFFFFF,
  168. shading: THREE.SmoothShading,
  169. });
  170. ground = new THREE.Mesh( new THREE.PlaneBufferGeometry(1024, 1024), groundMaterial);
  171. if (sceneInfo.shadows) {
  172. ground.receiveShadow = true;
  173. }
  174. if (sceneInfo.groundPos) {
  175. ground.position.copy(sceneInfo.groundPos);
  176. }
  177. else {
  178. ground.position.z = -70;
  179. }
  180. ground.rotation.x = -Math.PI / 2;
  181. scene.add(ground);
  182. }
  183. loader = new THREE.glTFLoader;
  184. var loadStartTime = Date.now();
  185. var status = document.getElementById("status");
  186. status.innerHTML = "Loading...";
  187. loader.load( sceneInfo.url, function(data) {
  188. gltf = data;
  189. var object = gltf.scene;
  190. var loadEndTime = Date.now();
  191. var loadTime = (loadEndTime - loadStartTime) / 1000;
  192. status.innerHTML = "Load time: " + loadTime.toFixed(2) + " seconds.";
  193. if (sceneInfo.cameraPos)
  194. defaultCamera.position.copy(sceneInfo.cameraPos);
  195. if (sceneInfo.center) {
  196. orbitControls.center.copy(sceneInfo.center);
  197. }
  198. if (sceneInfo.objectPosition) {
  199. object.position.copy(sceneInfo.objectPosition);
  200. if (spot1) {
  201. spot1.position.set(sceneInfo.objectPosition.x - 100, sceneInfo.objectPosition.y + 200, sceneInfo.objectPosition.z - 100 );
  202. spot1.target.position.copy(sceneInfo.objectPosition);
  203. }
  204. }
  205. if (sceneInfo.objectRotation)
  206. object.rotation.copy(sceneInfo.objectRotation);
  207. if (sceneInfo.objectScale)
  208. object.scale.copy(sceneInfo.objectScale);
  209. cameraIndex = 0;
  210. cameras = [];
  211. cameraNames = [];
  212. if (gltf.cameras && gltf.cameras.length)
  213. {
  214. var i, len = gltf.cameras.length;
  215. for (i = 0; i < len; i++)
  216. {
  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. }
  230. else
  231. {
  232. updateCamerasList();
  233. switchCamera(0);
  234. }
  235. if (gltf.animations && gltf.animations.length) {
  236. var i, len = gltf.animations.length;
  237. for (i = 0; i < len; i++) {
  238. var animation = gltf.animations[i];
  239. animation.loop = true;
  240. // There's .3333 seconds junk at the tail of the Monster animation that
  241. // keeps it from looping cleanly. Clip it at 3 seconds
  242. if (sceneInfo.animationTime)
  243. animation.duration = sceneInfo.animationTime;
  244. animation.play();
  245. }
  246. }
  247. scene.add( object );
  248. onWindowResize();
  249. });
  250. orbitControls = new THREE.OrbitControls(defaultCamera, renderer.domElement);
  251. }
  252. function onWindowResize() {
  253. defaultCamera.aspect = container.offsetWidth / container.offsetHeight;
  254. defaultCamera.updateProjectionMatrix();
  255. var i, len = cameras.length;
  256. for (i = 0; i < len; i++) // just do it for default
  257. {
  258. cameras[i].aspect = container.offsetWidth / container.offsetHeight;
  259. cameras[i].updateProjectionMatrix();
  260. }
  261. renderer.setSize( window.innerWidth, window.innerHeight );
  262. }
  263. function animate() {
  264. requestAnimationFrame( animate );
  265. THREE.glTFAnimator.update();
  266. if (cameraIndex == 0)
  267. orbitControls.update();
  268. render();
  269. }
  270. function render() {
  271. renderer.render( scene, camera );
  272. }
  273. function onKeyPress(event) {
  274. var chr = String.fromCharCode(event.keyCode);
  275. if (chr == ' ')
  276. {
  277. index = cameraIndex + 1;
  278. if (index > cameras.length)
  279. index = 0;
  280. switchCamera(index);
  281. }
  282. else {
  283. var index = parseInt(chr);
  284. if (!isNaN(index) && (index <= cameras.length)) {
  285. switchCamera(index);
  286. }
  287. }
  288. }
  289. var sceneList =
  290. [
  291. { name : "Monster", url : "./models/gltf/monster/monster.json",
  292. cameraPos: new THREE.Vector3(30, 10, 70),
  293. objectScale: new THREE.Vector3(0.01, 0.01, 0.01),
  294. objectPosition: new THREE.Vector3(0, 1, 0),
  295. objectRotation: new THREE.Euler(-Math.PI / 2, 0, -Math.PI / 2),
  296. animationTime: 3,
  297. addLights:true,
  298. shadows:true,
  299. addGround:true },
  300. { name : "Duck", url : "./models/gltf/duck/duck.json",
  301. cameraPos: new THREE.Vector3(0, 30, -50),
  302. objectScale: new THREE.Vector3(0.1, 0.1, 0.1),
  303. addLights:true,
  304. addGround:true,
  305. shadows:true },
  306. ];
  307. function buildSceneList()
  308. {
  309. var elt = document.getElementById('scenes_list');
  310. while( elt.hasChildNodes() ){
  311. elt.removeChild(elt.lastChild);
  312. }
  313. var i, len = sceneList.length;
  314. for (i = 0; i < len; i++)
  315. {
  316. option = document.createElement("option");
  317. option.text=sceneList[i].name;
  318. elt.add(option);
  319. }
  320. }
  321. function switchScene(index)
  322. {
  323. cleanup();
  324. initScene(index);
  325. var elt = document.getElementById('scenes_list');
  326. elt.selectedIndex = index;
  327. }
  328. function selectScene()
  329. {
  330. var select = document.getElementById("scenes_list");
  331. var index = select.selectedIndex;
  332. if (index >= 0)
  333. {
  334. switchScene(index);
  335. }
  336. }
  337. function switchCamera(index)
  338. {
  339. cameraIndex = index;
  340. if (cameraIndex == 0) {
  341. camera = defaultCamera;
  342. }
  343. if (cameraIndex >= 1 && cameraIndex <= cameras.length) {
  344. camera = cameras[cameraIndex - 1];
  345. }
  346. var elt = document.getElementById('cameras_list');
  347. elt.selectedIndex = cameraIndex;
  348. }
  349. function updateCamerasList() {
  350. var elt = document.getElementById('cameras_list');
  351. while( elt.hasChildNodes() ){
  352. elt.removeChild(elt.lastChild);
  353. }
  354. option = document.createElement("option");
  355. option.text="[default]";
  356. elt.add(option);
  357. var i, len = cameraNames.length;
  358. for (i = 0; i < len; i++)
  359. {
  360. option = document.createElement("option");
  361. option.text=cameraNames[i];
  362. elt.add(option);
  363. }
  364. }
  365. function selectCamera() {
  366. var select = document.getElementById("cameras_list");
  367. var index = select.selectedIndex;
  368. if (index >= 0)
  369. {
  370. switchCamera(index);
  371. }
  372. }
  373. function toggleAnimations() {
  374. var i, len = gltf.animations.length;
  375. for (i = 0; i < len; i++) {
  376. var animation = gltf.animations[i];
  377. if (animation.running) {
  378. animation.stop();
  379. }
  380. else {
  381. animation.play();
  382. }
  383. }
  384. }
  385. function cleanup() {
  386. if (container && renderer)
  387. {
  388. container.removeChild(renderer.domElement);
  389. }
  390. cameraIndex = 0;
  391. cameras = [];
  392. cameraNames = [];
  393. defaultCamera = null;
  394. if (!loader || !gltf.animations)
  395. return;
  396. var i, len = gltf.animations.length;
  397. for (i = 0; i < len; i++) {
  398. var animation = gltf.animations[i];
  399. if (animation.running) {
  400. animation.stop();
  401. }
  402. }
  403. }
  404. onload();
  405. </script>
  406. </body>
  407. </html>