webgl_loader_gltf_extensions.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - glTF 2.0 - extensions</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: #222222;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. #info {
  15. color: #808080;
  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: 200px;
  33. bottom: 0px;
  34. left: 0px;
  35. padding: 10px;
  36. background-color: White;
  37. font: 13px "Lucida Grande", Lucida, Verdana, sans-serif;
  38. }
  39. #controls > div {
  40. margin-bottom: 8px;
  41. }
  42. #controls hr {
  43. border: 0px;
  44. height: 1px;
  45. margin-bottom: 10px;
  46. background-color: #bbb;
  47. }
  48. #info a, .button {
  49. color: #f00;
  50. font-weight: bold;
  51. text-decoration: underline;
  52. cursor: pointer
  53. }
  54. </style>
  55. </head>
  56. <body>
  57. <div id="info">
  58. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> -
  59. <a href="https://github.com/KhronosGroup/glTF" target="_blank" rel="noopener">glTF</a> 2.0 loader
  60. <br>
  61. BoomBox by <a href="https://www.microsoft.com/" target="_blank" rel="noopener">Microsoft</a>
  62. </div>
  63. <div id="container"></div>
  64. <div id="controls">
  65. <div id="status">Loading...</div>
  66. <hr />
  67. <div>
  68. Model
  69. <select id="scenes_list" size="1" onchange="selectScene();" ondblclick="selectScene();"></select>
  70. </div>
  71. <div>
  72. Camera
  73. <select id="cameras_list" size="1" onchange="selectCamera();" ondblclick="selectCamera();"></select>
  74. </div>
  75. <div>
  76. Animations
  77. <input type="checkbox" checked onclick="toggleAnimations();">Play</input>
  78. </div>
  79. <div>
  80. Extension
  81. <select id="extensions_list" onchange="selectExtension();">
  82. <option value="glTF">None (Default)</option>
  83. <option value="glTF-Embedded">None (Embedded)</option>
  84. <option value="glTF-Binary">None (Binary)</option>
  85. <option value="glTF-pbrSpecularGlossiness">Specular-Glossiness (PBR)</option>
  86. <option value="glTF-lights">Lights</option>
  87. <option value="glTF-Draco">Draco (Compressed)</option>
  88. </select>
  89. </div>
  90. </div>
  91. <script src="../build/three.js"></script>
  92. <script src="js/controls/OrbitControls.js"></script>
  93. <script src="js/loaders/draco/DRACOLoader.js"></script>
  94. <script src="js/loaders/GLTFLoader.js"></script>
  95. <script>
  96. var orbitControls = null;
  97. var container, camera, scene, renderer, loader;
  98. var cameraIndex = 0;
  99. var cameras = [];
  100. var cameraNames = [];
  101. var defaultCamera = null;
  102. var gltf = null;
  103. var mixer = null;
  104. var clock = new THREE.Clock();
  105. function onload() {
  106. window.addEventListener( 'resize', onWindowResize, false );
  107. document.addEventListener( 'keydown', function(e) { onKeyDown(e); }, false );
  108. buildSceneList();
  109. switchScene(0);
  110. animate();
  111. }
  112. function initScene(index) {
  113. container = document.getElementById( 'container' );
  114. scene = new THREE.Scene();
  115. scene.background = new THREE.Color( 0x222222 );
  116. defaultCamera = new THREE.PerspectiveCamera( 45, container.offsetWidth / container.offsetHeight, 0.001, 1000 );
  117. //defaultCamera.up = new THREE.Vector3( 0, 1, 0 );
  118. scene.add( defaultCamera );
  119. camera = defaultCamera;
  120. var sceneInfo = sceneList[index];
  121. var spot1 = null;
  122. if (sceneInfo.addLights) {
  123. var ambient = new THREE.AmbientLight( 0x222222 );
  124. scene.add( ambient );
  125. var directionalLight = new THREE.DirectionalLight( 0xdddddd );
  126. directionalLight.position.set( 0, 0, 1 ).normalize();
  127. scene.add( directionalLight );
  128. spot1 = new THREE.SpotLight( 0xffffff, 1 );
  129. spot1.position.set( 10, 20, 10 );
  130. spot1.angle = 0.25;
  131. spot1.distance = 1024;
  132. spot1.penumbra = 0.75;
  133. if ( sceneInfo.shadows ) {
  134. spot1.castShadow = true;
  135. spot1.shadow.bias = 0.0001;
  136. spot1.shadow.mapSize.width = 2048;
  137. spot1.shadow.mapSize.height = 2048;
  138. }
  139. scene.add( spot1 );
  140. }
  141. // RENDERER
  142. renderer = new THREE.WebGLRenderer( { antialias: true } );
  143. renderer.setPixelRatio( window.devicePixelRatio );
  144. renderer.setSize( window.innerWidth, window.innerHeight );
  145. renderer.gammaOutput = true;
  146. renderer.physicallyCorrectLights = true;
  147. if (sceneInfo.shadows) {
  148. renderer.shadowMap.enabled = true;
  149. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  150. }
  151. container.appendChild( renderer.domElement );
  152. var ground = null;
  153. if (sceneInfo.addGround) {
  154. var groundMaterial = new THREE.MeshPhongMaterial({
  155. color: 0xFFFFFF
  156. });
  157. ground = new THREE.Mesh( new THREE.PlaneBufferGeometry(512, 512), groundMaterial);
  158. if (sceneInfo.shadows) {
  159. ground.receiveShadow = true;
  160. }
  161. if (sceneInfo.groundPos) {
  162. ground.position.copy(sceneInfo.groundPos);
  163. } else {
  164. ground.position.z = -70;
  165. }
  166. ground.rotation.x = -Math.PI / 2;
  167. scene.add(ground);
  168. }
  169. loader = new THREE.GLTFLoader();
  170. THREE.DRACOLoader.setDecoderPath( 'js/loaders/draco/gltf/' );
  171. loader.setDRACOLoader( new THREE.DRACOLoader() );
  172. for (var i = 0; i < extensionSelect.children.length; i++) {
  173. var child = extensionSelect.children[i];
  174. child.disabled = sceneInfo.extensions.indexOf(child.value) === -1;
  175. if (child.disabled && child.selected) {
  176. extensionSelect.value = extension = 'glTF';
  177. }
  178. }
  179. var url = sceneInfo.url;
  180. var r = eval("/" + '\%s' + "/g");
  181. url = url.replace(r, extension);
  182. if (extension === 'glTF-Binary') {
  183. url = url.replace('.gltf', '.glb');
  184. }
  185. var loadStartTime = performance.now();
  186. var status = document.getElementById("status");
  187. status.innerHTML = "Loading...";
  188. loader.load( url, function(data) {
  189. gltf = data;
  190. var object = gltf.scene;
  191. status.innerHTML = "Load time: " + ( performance.now() - loadStartTime ).toFixed( 2 ) + " ms.";
  192. if (sceneInfo.cameraPos)
  193. defaultCamera.position.copy(sceneInfo.cameraPos);
  194. if (sceneInfo.center) {
  195. orbitControls.target.copy(sceneInfo.center);
  196. }
  197. if (sceneInfo.objectPosition) {
  198. object.position.copy(sceneInfo.objectPosition);
  199. if (spot1) {
  200. spot1.position.set(sceneInfo.objectPosition.x - 100, sceneInfo.objectPosition.y + 200, sceneInfo.objectPosition.z - 100 );
  201. spot1.target.position.copy(sceneInfo.objectPosition);
  202. }
  203. }
  204. if (sceneInfo.objectRotation)
  205. object.rotation.copy(sceneInfo.objectRotation);
  206. if (sceneInfo.objectScale)
  207. object.scale.copy(sceneInfo.objectScale);
  208. if ( sceneInfo.addEnvMap ) {
  209. var envMap = getEnvMap();
  210. object.traverse( function( node ) {
  211. if ( node.material && ( node.material.isMeshStandardMaterial ||
  212. ( node.material.isShaderMaterial && node.material.envMap !== undefined ) ) ) {
  213. node.material.envMap = envMap;
  214. node.material.needsUpdate = true;
  215. }
  216. } );
  217. scene.background = envMap;
  218. }
  219. object.traverse( function ( node ) {
  220. if ( node.isMesh ) node.castShadow = true;
  221. } );
  222. cameraIndex = 0;
  223. cameras = [];
  224. cameraNames = [];
  225. if (gltf.cameras && gltf.cameras.length) {
  226. var i, len = gltf.cameras.length;
  227. for (i = 0; i < len; i++) {
  228. var addCamera = true;
  229. var cameraName = gltf.cameras[i].parent.name || ('camera_' + i);
  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. } else {
  241. updateCamerasList();
  242. switchCamera(0);
  243. }
  244. var animations = gltf.animations;
  245. if ( animations && animations.length ) {
  246. mixer = new THREE.AnimationMixer( object );
  247. for ( var i = 0; i < animations.length; i ++ ) {
  248. var animation = animations[ i ];
  249. // There's .3333 seconds junk at the tail of the Monster animation that
  250. // keeps it from looping cleanly. Clip it at 3 seconds
  251. if ( sceneInfo.animationTime )
  252. animation.duration = sceneInfo.animationTime;
  253. mixer.clipAction( animation ).play();
  254. }
  255. }
  256. scene.add( object );
  257. onWindowResize();
  258. }, undefined, function ( error ) {
  259. console.error( error );
  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. cameras[i].aspect = container.offsetWidth / container.offsetHeight;
  269. cameras[i].updateProjectionMatrix();
  270. }
  271. renderer.setSize( window.innerWidth, window.innerHeight );
  272. }
  273. function animate() {
  274. requestAnimationFrame( animate );
  275. if (mixer) mixer.update(clock.getDelta());
  276. if (cameraIndex == 0)
  277. orbitControls.update();
  278. render();
  279. }
  280. function render() {
  281. renderer.render( scene, camera );
  282. }
  283. function onKeyDown(event) {
  284. var chr = String.fromCharCode(event.keyCode);
  285. if (chr == ' ') {
  286. index = cameraIndex + 1;
  287. if (index > cameras.length)
  288. index = 0;
  289. switchCamera(index);
  290. } else {
  291. var index = parseInt(chr);
  292. if (!isNaN(index) && (index <= cameras.length)) {
  293. switchCamera(index);
  294. }
  295. }
  296. }
  297. var envMap;
  298. function getEnvMap() {
  299. if ( envMap ) {
  300. return envMap;
  301. }
  302. var path = 'textures/cube/Park2/';
  303. var format = '.jpg';
  304. var urls = [
  305. path + 'posx' + format, path + 'negx' + format,
  306. path + 'posy' + format, path + 'negy' + format,
  307. path + 'posz' + format, path + 'negz' + format
  308. ];
  309. envMap = new THREE.CubeTextureLoader().load( urls );
  310. envMap.format = THREE.RGBFormat;
  311. return envMap;
  312. }
  313. var sceneList = [
  314. {
  315. name : 'BoomBox (PBR)', url : './models/gltf/BoomBox/%s/BoomBox.gltf',
  316. cameraPos: new THREE.Vector3(0.02, 0.01, 0.03),
  317. objectRotation: new THREE.Euler(0, Math.PI, 0),
  318. addLights:true,
  319. extensions: ['glTF', 'glTF-pbrSpecularGlossiness', 'glTF-Binary'],
  320. addEnvMap: true
  321. },
  322. {
  323. name : 'MetalRoughSpheres (PBR)', url : './models/gltf/MetalRoughSpheres/%s/MetalRoughSpheres.gltf',
  324. cameraPos: new THREE.Vector3(2, 1, 15),
  325. objectRotation: new THREE.Euler(0, 0, 0),
  326. addLights:true,
  327. extensions: ['glTF', 'glTF-Embedded'],
  328. addEnvMap: true
  329. },
  330. {
  331. name : 'Duck', url : './models/gltf/Duck/%s/Duck.gltf',
  332. cameraPos: new THREE.Vector3(0, 3, 5),
  333. addLights:true,
  334. addGround:true,
  335. shadows:true,
  336. extensions: ['glTF', 'glTF-Embedded', 'glTF-pbrSpecularGlossiness', 'glTF-Binary', 'glTF-Draco']
  337. },
  338. {
  339. name : "Monster", url : "./models/gltf/Monster/%s/Monster.gltf",
  340. cameraPos: new THREE.Vector3(30, 10, 70),
  341. objectScale: new THREE.Vector3(0.4, 0.4, 0.4),
  342. objectPosition: new THREE.Vector3(2, 1, 0),
  343. objectRotation: new THREE.Euler(0, - 3 * Math.PI / 4, 0),
  344. animationTime: 3,
  345. addLights:true,
  346. shadows:true,
  347. addGround:true,
  348. extensions: ['glTF', 'glTF-Embedded', 'glTF-pbrSpecularGlossiness', 'glTF-Binary', 'glTF-Draco', 'glTF-lights']
  349. },
  350. {
  351. name : "Cesium Man", url : "./models/gltf/CesiumMan/%s/CesiumMan.gltf",
  352. cameraPos: new THREE.Vector3(0, 3, 10),
  353. objectRotation: new THREE.Euler(0, 0, 0),
  354. addLights:true,
  355. addGround:true,
  356. shadows:true,
  357. extensions: ['glTF', 'glTF-Embedded', 'glTF-pbrSpecularGlossiness', 'glTF-Binary', 'glTF-Draco']
  358. },
  359. {
  360. name : "Cesium Milk Truck",
  361. url : "./models/gltf/CesiumMilkTruck/%s/CesiumMilkTruck.gltf",
  362. cameraPos: new THREE.Vector3(0, 3, 10),
  363. addLights:true,
  364. addGround:true,
  365. shadows:true,
  366. extensions: ['glTF', 'glTF-Embedded', 'glTF-pbrSpecularGlossiness', 'glTF-Binary', 'glTF-Draco']
  367. },
  368. {
  369. name : "Rigged Simple",
  370. url : "./models/gltf/RiggedSimple/%s/RiggedSimple.gltf",
  371. cameraPos: new THREE.Vector3(0, 5, 15),
  372. objectRotation: new THREE.Euler(0, 90, 0),
  373. addLights:true,
  374. shadows:true,
  375. extensions: ['glTF', 'glTF-Embedded', 'glTF-pbrSpecularGlossiness', 'glTF-Binary', 'glTF-Draco']
  376. },
  377. {
  378. name : 'Outlined Box',
  379. url : './models/gltf/OutlinedBox/OutlinedBox.gltf',
  380. cameraPos: new THREE.Vector3(0, 5, 15),
  381. objectScale: new THREE.Vector3(0.01, 0.01, 0.01),
  382. objectRotation: new THREE.Euler(0, 90, 0),
  383. addLights:true,
  384. shadows:true,
  385. extensions: ['glTF']
  386. },
  387. ];
  388. function buildSceneList() {
  389. var elt = document.getElementById('scenes_list');
  390. while( elt.hasChildNodes() ){
  391. elt.removeChild(elt.lastChild);
  392. }
  393. var i, len = sceneList.length;
  394. for (i = 0; i < len; i++) {
  395. var option = document.createElement("option");
  396. option.text=sceneList[i].name;
  397. elt.add(option);
  398. }
  399. }
  400. function switchScene(index) {
  401. cleanup();
  402. initScene(index);
  403. var elt = document.getElementById('scenes_list');
  404. elt.selectedIndex = index;
  405. }
  406. function selectScene() {
  407. var select = document.getElementById("scenes_list");
  408. var index = select.selectedIndex;
  409. if (index >= 0) {
  410. switchScene(index);
  411. }
  412. }
  413. function switchCamera(index) {
  414. cameraIndex = index;
  415. if (cameraIndex == 0) {
  416. camera = defaultCamera;
  417. }
  418. if (cameraIndex >= 1 && cameraIndex <= cameras.length) {
  419. camera = cameras[cameraIndex - 1];
  420. }
  421. var elt = document.getElementById('cameras_list');
  422. elt.selectedIndex = cameraIndex;
  423. }
  424. function updateCamerasList() {
  425. var elt = document.getElementById('cameras_list');
  426. while( elt.hasChildNodes() ){
  427. elt.removeChild(elt.lastChild);
  428. }
  429. var option = document.createElement("option");
  430. option.text="[default]";
  431. elt.add(option);
  432. var i, len = cameraNames.length;
  433. for (i = 0; i < len; i++) {
  434. var option = document.createElement("option");
  435. option.text=cameraNames[i];
  436. elt.add(option);
  437. }
  438. }
  439. function selectCamera() {
  440. var select = document.getElementById("cameras_list");
  441. var index = select.selectedIndex;
  442. if (index >= 0) {
  443. switchCamera(index);
  444. }
  445. }
  446. function toggleAnimations() {
  447. var i, len = gltf.animations.length;
  448. for (i = 0; i < len; i++) {
  449. var clip = gltf.animations[i];
  450. var action = mixer.existingAction( clip );
  451. if (action.isRunning()) {
  452. action.stop();
  453. } else {
  454. action.play();
  455. }
  456. }
  457. }
  458. var extensionSelect = document.getElementById("extensions_list");
  459. var extension = extensionSelect.value;
  460. function selectExtension()
  461. {
  462. extension = extensionSelect.value;
  463. selectScene();
  464. }
  465. function cleanup() {
  466. if (container && renderer) {
  467. container.removeChild(renderer.domElement);
  468. }
  469. cameraIndex = 0;
  470. cameras = [];
  471. cameraNames = [];
  472. defaultCamera = null;
  473. if (!loader || !mixer)
  474. return;
  475. mixer.stopAllAction();
  476. }
  477. onload();
  478. </script>
  479. </body>
  480. </html>
粤ICP备19079148号