webgl_loader_gltf.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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:134px;
  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. #buffer_geometry_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="http://gltf.gl/" 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
  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="buffer_geometry_control">
  107. Geometry
  108. <div class="controlValue">
  109. <input type="checkbox" id="buffer_geometry_checkbox" checked onclick="toggleBufferGeometry();">BufferGeometry</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>
  120. var orbitControls = null;
  121. var container, camera, scene, renderer, loader;
  122. var cameraIndex = 0;
  123. var cameras = [];
  124. var cameraNames = [];
  125. var defaultCamera = null;
  126. var gltf = null;
  127. function onload() {
  128. window.addEventListener( 'resize', onWindowResize, false );
  129. document.addEventListener( 'keypress',
  130. function(e) { onKeyPress(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( 0x888888 );
  146. scene.add( ambient );
  147. var directionalLight = new THREE.DirectionalLight( 0xdddddd );
  148. directionalLight.position.set( 0, -1, 1 ).normalize();
  149. scene.add( directionalLight );
  150. spot1 = new THREE.SpotLight( 0xffffff, 1 );
  151. spot1.position.set( -100, 200, 100 );
  152. spot1.target.position.set( 0, 0, 0 );
  153. if (sceneInfo.shadows) {
  154. spot1.shadowCameraNear = 1;
  155. spot1.shadowCameraFar = 1024;
  156. spot1.castShadow = true;
  157. spot1.shadowDarkness = 0.3;
  158. spot1.shadowBias = 0.0001;
  159. spot1.shadowMapWidth = 2048;
  160. spot1.shadowMapHeight = 2048;
  161. }
  162. scene.add( spot1 );
  163. }
  164. // RENDERER
  165. renderer = new THREE.WebGLRenderer({antialias:true});
  166. renderer.setSize( container.offsetWidth, container.offsetHeight );
  167. if (sceneInfo.shadows) {
  168. renderer.shadowMapEnabled = true;
  169. renderer.shadowMapSoft = true;
  170. renderer.shadowMapType = 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. ambient: 0x888888,
  178. shading: THREE.SmoothShading,
  179. });
  180. ground = new THREE.Mesh( new THREE.PlaneGeometry(1024, 1024), groundMaterial);
  181. if (sceneInfo.shadows) {
  182. ground.receiveShadow = true;
  183. }
  184. if (sceneInfo.groundPos) {
  185. ground.position.copy(sceneInfo.groundPos);
  186. }
  187. else {
  188. ground.position.z = -70;
  189. }
  190. ground.rotation.x = -Math.PI / 2;
  191. scene.add(ground);
  192. }
  193. loader = new THREE.glTFLoader;
  194. loader.useBufferGeometry = useBufferGeometry;
  195. var loadStartTime = Date.now();
  196. var status = document.getElementById("status");
  197. status.innerHTML = "Loading...";
  198. loader.load( sceneInfo.url, function(data) {
  199. gltf = data;
  200. var object = gltf.scene;
  201. var loadEndTime = Date.now();
  202. var loadTime = (loadEndTime - loadStartTime) / 1000;
  203. status.innerHTML = "Load time: " + loadTime.toFixed(2) + " seconds.";
  204. if (sceneInfo.cameraPos)
  205. defaultCamera.position.copy(sceneInfo.cameraPos);
  206. if (sceneInfo.center) {
  207. orbitControls.center.copy(sceneInfo.center);
  208. }
  209. if (sceneInfo.objectPosition) {
  210. object.position.copy(sceneInfo.objectPosition);
  211. if (spot1) {
  212. spot1.position.set(sceneInfo.objectPosition.x - 100, sceneInfo.objectPosition.y + 200, sceneInfo.objectPosition.z - 100 );
  213. spot1.target.position.copy(sceneInfo.objectPosition);
  214. }
  215. }
  216. if (sceneInfo.objectRotation)
  217. object.rotation.copy(sceneInfo.objectRotation);
  218. if (sceneInfo.objectScale)
  219. object.scale.copy(sceneInfo.objectScale);
  220. cameraIndex = 0;
  221. cameras = [];
  222. cameraNames = [];
  223. if (gltf.cameras && gltf.cameras.length)
  224. {
  225. var i, len = gltf.cameras.length;
  226. for (i = 0; i < len; i++)
  227. {
  228. var addCamera = true;
  229. var cameraName = gltf.cameras[i].parent.name;
  230. if (sceneInfo.cameras && !(cameraName in sceneInfo.cameras)) {
  231. addCamera = false;
  232. }
  233. if (addCamera) {
  234. cameraNames.push(cameraName);
  235. cameras.push(gltf.cameras[i]);
  236. }
  237. }
  238. updateCamerasList();
  239. switchCamera(1);
  240. }
  241. else
  242. {
  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. {
  269. cameras[i].aspect = container.offsetWidth / container.offsetHeight;
  270. cameras[i].updateProjectionMatrix();
  271. }
  272. renderer.setSize( container.offsetWidth, container.offsetHeight );
  273. }
  274. function animate() {
  275. requestAnimationFrame( animate );
  276. THREE.glTFAnimator.update();
  277. if (cameraIndex == 0)
  278. orbitControls.update();
  279. render();
  280. }
  281. function render() {
  282. renderer.render( scene, camera );
  283. }
  284. function onKeyPress(event) {
  285. var chr = String.fromCharCode(event.keyCode);
  286. if (chr == ' ')
  287. {
  288. index = cameraIndex + 1;
  289. if (index > cameras.length)
  290. index = 0;
  291. switchCamera(index);
  292. }
  293. else {
  294. var index = parseInt(chr);
  295. if (!isNaN(index) && (index <= cameras.length)) {
  296. switchCamera(index);
  297. }
  298. }
  299. }
  300. var sceneList =
  301. [
  302. { name : "Monster", url : "./models/gltf/monster/monster.json",
  303. cameraPos: new THREE.Vector3(30, 10, 70),
  304. objectScale: new THREE.Vector3(0.01, 0.01, 0.01),
  305. objectPosition: new THREE.Vector3(0, 1, 0),
  306. objectRotation: new THREE.Euler(-Math.PI / 2, 0, -Math.PI / 2),
  307. animationTime: 3,
  308. addLights:true,
  309. shadows:true,
  310. addGround:true },
  311. { name : "Duck", url : "./models/gltf/duck/duck.json",
  312. cameraPos: new THREE.Vector3(0, 30, -50),
  313. objectScale: new THREE.Vector3(0.1, 0.1, 0.1),
  314. addLights:true,
  315. addGround:true,
  316. shadows:true },
  317. ];
  318. function buildSceneList()
  319. {
  320. var elt = document.getElementById('scenes_list');
  321. while( elt.hasChildNodes() ){
  322. elt.removeChild(elt.lastChild);
  323. }
  324. var i, len = sceneList.length;
  325. for (i = 0; i < len; i++)
  326. {
  327. option = document.createElement("option");
  328. option.text=sceneList[i].name;
  329. elt.add(option);
  330. }
  331. }
  332. function switchScene(index)
  333. {
  334. cleanup();
  335. initScene(index);
  336. var elt = document.getElementById('scenes_list');
  337. elt.selectedIndex = index;
  338. }
  339. function selectScene()
  340. {
  341. var select = document.getElementById("scenes_list");
  342. var index = select.selectedIndex;
  343. if (index >= 0)
  344. {
  345. switchScene(index);
  346. }
  347. }
  348. function switchCamera(index)
  349. {
  350. cameraIndex = index;
  351. if (cameraIndex == 0) {
  352. camera = defaultCamera;
  353. }
  354. if (cameraIndex >= 1 && cameraIndex <= cameras.length) {
  355. camera = cameras[cameraIndex - 1];
  356. }
  357. var elt = document.getElementById('cameras_list');
  358. elt.selectedIndex = cameraIndex;
  359. }
  360. function updateCamerasList() {
  361. var elt = document.getElementById('cameras_list');
  362. while( elt.hasChildNodes() ){
  363. elt.removeChild(elt.lastChild);
  364. }
  365. option = document.createElement("option");
  366. option.text="[default]";
  367. elt.add(option);
  368. var i, len = cameraNames.length;
  369. for (i = 0; i < len; i++)
  370. {
  371. option = document.createElement("option");
  372. option.text=cameraNames[i];
  373. elt.add(option);
  374. }
  375. }
  376. function selectCamera() {
  377. var select = document.getElementById("cameras_list");
  378. var index = select.selectedIndex;
  379. if (index >= 0)
  380. {
  381. switchCamera(index);
  382. }
  383. }
  384. function toggleAnimations() {
  385. var i, len = gltf.animations.length;
  386. for (i = 0; i < len; i++) {
  387. var animation = gltf.animations[i];
  388. if (animation.running) {
  389. animation.stop();
  390. }
  391. else {
  392. animation.play();
  393. }
  394. }
  395. }
  396. var usebuf = document.getElementById("buffer_geometry_checkbox");
  397. var useBufferGeometry = usebuf.hasAttribute("checked");
  398. function toggleBufferGeometry()
  399. {
  400. useBufferGeometry = !useBufferGeometry;
  401. selectScene();
  402. }
  403. function cleanup() {
  404. if (container && renderer)
  405. {
  406. container.removeChild(renderer.domElement);
  407. }
  408. cameraIndex = 0;
  409. cameras = [];
  410. cameraNames = [];
  411. defaultCamera = null;
  412. if (!loader || !gltf.animations)
  413. return;
  414. var i, len = gltf.animations.length;
  415. for (i = 0; i < len; i++) {
  416. var animation = gltf.animations[i];
  417. if (animation.running) {
  418. animation.stop();
  419. }
  420. }
  421. }
  422. onload();
  423. </script>
  424. </body>
  425. </html>