webgl_loader_gltf.html 11 KB

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