webgl_loader_gltf.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. #rendercontainer {
  31. position: absolute;
  32. top: 0px;
  33. width:100%;
  34. height:100%;
  35. z-index: -1;
  36. background-color:Red;
  37. }
  38. #controls {
  39. position:absolute;
  40. width:25%;
  41. left:75%;
  42. bottom:0%;
  43. height:134px;
  44. background-color:White;
  45. opacity:.9;
  46. font: 13px/1.231 "Lucida Grande", Lucida, Verdana, sans-serif;
  47. }
  48. #status {
  49. position:absolute;
  50. width:25%;
  51. left:2%;
  52. top:95%;
  53. height:5%;
  54. opacity:.9;
  55. font: 13px/1.231 "Lucida Grande", Lucida, Verdana, sans-serif;
  56. }
  57. .control {
  58. position:absolute;
  59. margin-left:12px;
  60. width:100%;
  61. font-weight:bold;
  62. }
  63. .controlValue {
  64. position:absolute;
  65. left:50%;
  66. top:0%;
  67. }
  68. #scenes_control {
  69. position:absolute;
  70. top:8px;
  71. }
  72. #cameras_control {
  73. position:absolute;
  74. top:40px;
  75. }
  76. #animations_control {
  77. position:absolute;
  78. top:72px;
  79. }
  80. #buffer_geometry_control {
  81. position:absolute;
  82. top:104px;
  83. }
  84. #info a, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
  85. </style>
  86. </head>
  87. <body>
  88. <div id="info">
  89. <a href="http://threejs.org" target="_blank">three.js</a> -
  90. monster by <a href="http://www.3drt.com/downloads.htm" target="_blank">3drt</a>
  91. </div>
  92. <div id="container">
  93. </div>
  94. <div id="status">
  95. </div>
  96. <div id="controls">
  97. <div class="control" id="scenes_control">
  98. Model
  99. <select class="controlValue" id="scenes_list" size="1" onchange="selectScene();" ondblclick="selectScene();">
  100. </select>
  101. </div>
  102. <div class="control" id="cameras_control">
  103. Camera
  104. <select class="controlValue" id="cameras_list" size="1" onchange="selectCamera();" ondblclick="selectCamera();">
  105. </select>
  106. </div>
  107. <div class="control" id="animations_control">
  108. Animations
  109. <div class="controlValue"><input type="checkbox" checked onclick="toggleAnimations();">Play</input></div>
  110. </div>
  111. <div class="control" id="buffer_geometry_control">
  112. Geometry
  113. <div class="controlValue">
  114. <input type="checkbox" id="buffer_geometry_checkbox" checked onclick="toggleBufferGeometry();">BufferGeometry</input>
  115. </div>
  116. </div>
  117. </div>
  118. <script src="../build/three.js"></script>
  119. <script src="./js/controls/OrbitControls.js"></script>
  120. <script src="./js/loaders/gltf/glTF-parser.js"></script>
  121. <script src="./js/loaders/gltf/glTFLoader.js"></script>
  122. <script src="./js/loaders/gltf/glTFLoaderUtils.js"></script>
  123. <script src="./js/loaders/gltf/glTFAnimation.js"></script>
  124. <script>
  125. var orbitControls = null;
  126. var container, camera, scene, renderer, loader;
  127. var cameraIndex = 0;
  128. var cameras = [];
  129. var cameraNames = [];
  130. var defaultCamera = null;
  131. var gltf = null;
  132. function onload() {
  133. window.addEventListener( 'resize', onWindowResize, false );
  134. document.addEventListener( 'keypress',
  135. function(e) { onKeyPress(e); }, false );
  136. buildSceneList();
  137. switchScene(0);
  138. animate();
  139. }
  140. function initScene(index) {
  141. container = document.getElementById( 'container' );
  142. scene = new THREE.Scene();
  143. defaultCamera = new THREE.PerspectiveCamera( 45, container.offsetWidth / container.offsetHeight, 1, 20000 );
  144. //defaultCamera.up = new THREE.Vector3( 0, 1, 0 );
  145. scene.add( defaultCamera );
  146. camera = defaultCamera;
  147. var sceneInfo = sceneList[index];
  148. var spot1 = null;
  149. if (sceneInfo.addLights) {
  150. var ambient = new THREE.AmbientLight( 0x222222 );
  151. scene.add( ambient );
  152. var directionalLight = new THREE.DirectionalLight( 0xdddddd );
  153. directionalLight.position.set( 0, -1, 1 ).normalize();
  154. scene.add( directionalLight );
  155. spot1 = new THREE.SpotLight( 0x888888, 2 );
  156. spot1.position.set( -100, 200, 100 );
  157. spot1.target.position.set( 0, 0, 0 );
  158. if (sceneInfo.shadows) {
  159. spot1.shadowCameraNear = 1;
  160. spot1.shadowCameraFar = 1024;
  161. spot1.castShadow = true;
  162. spot1.shadowDarkness = 0.3;
  163. spot1.shadowBias = 0.0001;
  164. spot1.shadowMapWidth = 2048;
  165. spot1.shadowMapHeight = 2048;
  166. }
  167. scene.add( spot1 );
  168. }
  169. // RENDERER
  170. renderer = new THREE.WebGLRenderer({antialias:true});
  171. renderer.setSize( container.offsetWidth, container.offsetHeight );
  172. if (sceneInfo.shadows) {
  173. renderer.shadowMapEnabled = true;
  174. renderer.shadowMapSoft = true;
  175. renderer.shadowMapType = THREE.PCFSoftShadowMap;
  176. }
  177. container.appendChild( renderer.domElement );
  178. var ground = null;
  179. if (sceneInfo.addGround) {
  180. var groundMaterial = new THREE.MeshPhongMaterial({
  181. color: 0xFFFFFF,
  182. ambient: 0x888888,
  183. shading: THREE.SmoothShading,
  184. });
  185. ground = new THREE.Mesh( new THREE.PlaneGeometry(1024, 1024), groundMaterial);
  186. if (sceneInfo.shadows) {
  187. ground.receiveShadow = true;
  188. }
  189. if (sceneInfo.groundPos) {
  190. ground.position.copy(sceneInfo.groundPos);
  191. }
  192. else {
  193. ground.position.z = -70;
  194. }
  195. ground.rotation.x = -Math.PI / 2;
  196. scene.add(ground);
  197. }
  198. loader = new THREE.glTFLoader;
  199. loader.useBufferGeometry = useBufferGeometry;
  200. var loadStartTime = Date.now();
  201. var status = document.getElementById("status");
  202. status.innerHTML = "Loading...";
  203. loader.load( sceneInfo.url, function(data) {
  204. gltf = data;
  205. var object = gltf.scene;
  206. var loadEndTime = Date.now();
  207. var loadTime = (loadEndTime - loadStartTime) / 1000;
  208. status.innerHTML = "Load time: " + loadTime.toFixed(2) + " seconds.";
  209. if (sceneInfo.cameraPos)
  210. defaultCamera.position.copy(sceneInfo.cameraPos);
  211. if (sceneInfo.center) {
  212. orbitControls.center.copy(sceneInfo.center);
  213. }
  214. if (sceneInfo.objectPosition) {
  215. object.position.copy(sceneInfo.objectPosition);
  216. if (spot1) {
  217. spot1.position.set(sceneInfo.objectPosition.x - 100, sceneInfo.objectPosition.y + 200, sceneInfo.objectPosition.z - 100 );
  218. spot1.target.position.copy(sceneInfo.objectPosition);
  219. }
  220. }
  221. if (sceneInfo.objectRotation)
  222. object.rotation.copy(sceneInfo.objectRotation);
  223. if (sceneInfo.objectScale)
  224. object.scale.copy(sceneInfo.objectScale);
  225. cameraIndex = 0;
  226. cameras = [];
  227. cameraNames = [];
  228. if (gltf.cameras && gltf.cameras.length)
  229. {
  230. var i, len = gltf.cameras.length;
  231. for (i = 0; i < len; i++)
  232. {
  233. var addCamera = true;
  234. var cameraName = gltf.cameras[i].parent.name;
  235. if (sceneInfo.cameras && !(cameraName in sceneInfo.cameras)) {
  236. addCamera = false;
  237. }
  238. if (addCamera) {
  239. cameraNames.push(cameraName);
  240. cameras.push(gltf.cameras[i]);
  241. }
  242. }
  243. updateCamerasList();
  244. switchCamera(1);
  245. }
  246. else
  247. {
  248. updateCamerasList();
  249. switchCamera(0);
  250. }
  251. if (gltf.animations && gltf.animations.length) {
  252. var i, len = gltf.animations.length;
  253. for (i = 0; i < len; i++) {
  254. var animation = gltf.animations[i];
  255. animation.loop = true;
  256. animation.play();
  257. }
  258. }
  259. scene.add( object );
  260. onWindowResize();
  261. });
  262. orbitControls = new THREE.OrbitControls(defaultCamera, renderer.domElement);
  263. }
  264. function onWindowResize() {
  265. defaultCamera.aspect = container.offsetWidth / container.offsetHeight;
  266. defaultCamera.updateProjectionMatrix();
  267. var i, len = cameras.length;
  268. for (i = 0; i < len; i++) // just do it for default
  269. {
  270. cameras[i].aspect = container.offsetWidth / container.offsetHeight;
  271. cameras[i].updateProjectionMatrix();
  272. }
  273. renderer.setSize( container.offsetWidth, container.offsetHeight );
  274. }
  275. function animate() {
  276. requestAnimationFrame( animate );
  277. THREE.glTFAnimator.update();
  278. if (cameraIndex == 0)
  279. orbitControls.update();
  280. render();
  281. }
  282. function render() {
  283. renderer.render( scene, camera );
  284. }
  285. function onKeyPress(event) {
  286. var chr = String.fromCharCode(event.keyCode);
  287. if (chr == ' ')
  288. {
  289. index = cameraIndex + 1;
  290. if (index > cameras.length)
  291. index = 0;
  292. switchCamera(index);
  293. }
  294. else {
  295. var index = parseInt(chr);
  296. if (!isNaN(index) && (index <= cameras.length)) {
  297. switchCamera(index);
  298. }
  299. }
  300. }
  301. var sceneList =
  302. [
  303. { name : "Monster", url : "./models/gltf/monster/monster.json",
  304. cameraPos: new THREE.Vector3(0, 1, 100),
  305. objectScale: new THREE.Vector3(0.01, 0.01, 0.01),
  306. objectPosition: new THREE.Vector3(0, 0, 0),
  307. objectRotation: new THREE.Euler(-Math.PI / 2, 0, -Math.PI / 2),
  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:false,
  315. addGround:false,
  316. shadows:false },
  317. { name : "Wine Rack", url : "./models/gltf/wine/wine.json",
  318. cameraPos: new THREE.Vector3(60, 30, 100),
  319. objectPosition: new THREE.Vector3(-60, 0, -20),
  320. addLights:true },
  321. { name : "Rambler", url : "./models/gltf/rambler/Rambler.json",
  322. cameraPos: new THREE.Vector3(0, 10, 50),
  323. objectRotation: new THREE.Euler(-Math.PI / 2, 0, 0),
  324. objectPosition: new THREE.Vector3(0, 1, 0),
  325. objectScale: new THREE.Vector3(0.1, 0.1, 0.1),
  326. addLights:true,
  327. addGround:true,
  328. shadows:true },
  329. { name : "Super Murdoch", url : "./models/gltf/SuperMurdoch/SuperMurdoch.json",
  330. cameraPos: new THREE.Vector3(0, 10, 50),
  331. objectScale: new THREE.Vector3(0.01, 0.01, 0.01),
  332. addLights:true,
  333. addGround:true,
  334. shadows:true },
  335. ];
  336. function buildSceneList()
  337. {
  338. var elt = document.getElementById('scenes_list');
  339. while( elt.hasChildNodes() ){
  340. elt.removeChild(elt.lastChild);
  341. }
  342. var i, len = sceneList.length;
  343. for (i = 0; i < len; i++)
  344. {
  345. option = document.createElement("option");
  346. option.text=sceneList[i].name;
  347. elt.add(option);
  348. }
  349. }
  350. function switchScene(index)
  351. {
  352. cleanup();
  353. initScene(index);
  354. var elt = document.getElementById('scenes_list');
  355. elt.selectedIndex = index;
  356. }
  357. function selectScene()
  358. {
  359. var select = document.getElementById("scenes_list");
  360. var index = select.selectedIndex;
  361. if (index >= 0)
  362. {
  363. switchScene(index);
  364. }
  365. }
  366. function switchCamera(index)
  367. {
  368. cameraIndex = index;
  369. if (cameraIndex == 0) {
  370. camera = defaultCamera;
  371. }
  372. if (cameraIndex >= 1 && cameraIndex <= cameras.length) {
  373. camera = cameras[cameraIndex - 1];
  374. }
  375. var elt = document.getElementById('cameras_list');
  376. elt.selectedIndex = cameraIndex;
  377. }
  378. function updateCamerasList() {
  379. var elt = document.getElementById('cameras_list');
  380. while( elt.hasChildNodes() ){
  381. elt.removeChild(elt.lastChild);
  382. }
  383. option = document.createElement("option");
  384. option.text="[default]";
  385. elt.add(option);
  386. var i, len = cameraNames.length;
  387. for (i = 0; i < len; i++)
  388. {
  389. option = document.createElement("option");
  390. option.text=cameraNames[i];
  391. elt.add(option);
  392. }
  393. }
  394. function selectCamera() {
  395. var select = document.getElementById("cameras_list");
  396. var index = select.selectedIndex;
  397. if (index >= 0)
  398. {
  399. switchCamera(index);
  400. }
  401. }
  402. function toggleAnimations() {
  403. var i, len = gltf.animations.length;
  404. for (i = 0; i < len; i++) {
  405. var animation = gltf.animations[i];
  406. if (animation.running) {
  407. animation.stop();
  408. }
  409. else {
  410. animation.play();
  411. }
  412. }
  413. }
  414. var usebuf = document.getElementById("buffer_geometry_checkbox");
  415. var useBufferGeometry = usebuf.hasAttribute("checked");
  416. function toggleBufferGeometry()
  417. {
  418. useBufferGeometry = !useBufferGeometry;
  419. selectScene();
  420. }
  421. function cleanup() {
  422. if (container && renderer)
  423. {
  424. container.removeChild(renderer.domElement);
  425. }
  426. cameraIndex = 0;
  427. cameras = [];
  428. cameraNames = [];
  429. defaultCamera = null;
  430. if (!loader || !gltf.animations)
  431. return;
  432. var i, len = gltf.animations.length;
  433. for (i = 0; i < len; i++) {
  434. var animation = gltf.animations[i];
  435. if (animation.running) {
  436. animation.stop();
  437. }
  438. }
  439. }
  440. onload();
  441. </script>
  442. </body>
  443. </html>
粤ICP备19079148号