webgl_loader_gltf.html 11 KB

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