webgl_loader_gltf_extensions.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. #info a, .button {
  31. color: #f00;
  32. font-weight: bold;
  33. text-decoration: underline;
  34. cursor: pointer
  35. }
  36. </style>
  37. </head>
  38. <body>
  39. <div id="info">
  40. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> -
  41. <a href="https://github.com/KhronosGroup/glTF" target="_blank" rel="noopener">glTF</a> 2.0 loader
  42. <br>
  43. <div id="description"></div>
  44. </div>
  45. <div id="container"></div>
  46. <script src="../build/three.js"></script>
  47. <script src="js/libs/dat.gui.min.js"></script>
  48. <script src="js/controls/OrbitControls.js"></script>
  49. <script src="js/loaders/DRACOLoader.js"></script>
  50. <script src="js/loaders/GLTFLoader.js"></script>
  51. <script>
  52. var orbitControls = null;
  53. var container, camera, scene, renderer, loader;
  54. var gltf = null;
  55. var mixer = null;
  56. var gui = null;
  57. var extensionCtrl = null;
  58. var clock = new THREE.Clock();
  59. var scenes = {
  60. Boombox: {
  61. name: 'BoomBox (PBR)',
  62. url: './models/gltf/BoomBox/%s/BoomBox.gltf',
  63. author: 'Microsoft',
  64. authorURL: 'https://www.microsoft.com/',
  65. cameraPos: new THREE.Vector3(0.02, 0.01, 0.03),
  66. objectRotation: new THREE.Euler(0, Math.PI, 0),
  67. addLights: true,
  68. extensions: ['glTF', 'glTF-pbrSpecularGlossiness', 'glTF-Binary'],
  69. addEnvMap: true
  70. },
  71. 'Bot Skinned': {
  72. name: 'Bot Skinned',
  73. url: './models/gltf/BotSkinned/%s/Bot_Skinned.gltf',
  74. author: 'MozillaVR',
  75. authorURL: 'https://vr.mozilla.org/',
  76. cameraPos: new THREE.Vector3(0.5, 2, 2),
  77. center: new THREE.Vector3(0, 1.2, 0),
  78. objectRotation: new THREE.Euler(0, 0, 0),
  79. addLights: true,
  80. addGround: true,
  81. shadows: true,
  82. extensions: ['glTF-MaterialsUnlit']
  83. },
  84. MetalRoughSpheres: {
  85. name: 'MetalRoughSpheres (PBR)',
  86. url: './models/gltf/MetalRoughSpheres/%s/MetalRoughSpheres.gltf',
  87. author: '@emackey',
  88. authorURL: 'https://twitter.com/emackey',
  89. cameraPos: new THREE.Vector3(2, 1, 15),
  90. objectRotation: new THREE.Euler(0, 0, 0),
  91. addLights: true,
  92. extensions: ['glTF', 'glTF-Embedded'],
  93. addEnvMap: true
  94. },
  95. Duck: {
  96. name: 'Duck',
  97. url: './models/gltf/Duck/%s/Duck.gltf',
  98. author: 'Sony',
  99. authorURL: 'https://www.playstation.com/en-us/corporate/about/',
  100. cameraPos: new THREE.Vector3(0, 3, 5),
  101. addLights: true,
  102. addGround: true,
  103. shadows: true,
  104. extensions: ['glTF', 'glTF-Embedded', 'glTF-pbrSpecularGlossiness', 'glTF-Binary', 'glTF-Draco']
  105. },
  106. Monster: {
  107. name: 'Monster',
  108. url: './models/gltf/Monster/%s/Monster.gltf',
  109. author: '3drt.com',
  110. authorURL: 'http://www.3drt.com/downloads.htm',
  111. cameraPos: new THREE.Vector3(30, 10, 70),
  112. objectScale: new THREE.Vector3(0.4, 0.4, 0.4),
  113. objectPosition: new THREE.Vector3(2, 1, 0),
  114. objectRotation: new THREE.Euler(0, - 3 * Math.PI / 4, 0),
  115. animationTime: 3,
  116. addLights: true,
  117. shadows: true,
  118. addGround: true,
  119. extensions: ['glTF', 'glTF-Embedded', 'glTF-Binary', 'glTF-Draco']
  120. },
  121. 'Cesium Man': {
  122. name: 'Cesium Man',
  123. url: './models/gltf/CesiumMan/%s/CesiumMan.gltf',
  124. author: 'Cesium',
  125. authorURL: 'https://cesiumjs.org/',
  126. cameraPos: new THREE.Vector3(0, 3, 10),
  127. objectRotation: new THREE.Euler(0, 0, 0),
  128. addLights: true,
  129. addGround: true,
  130. shadows: true,
  131. extensions: ['glTF', 'glTF-Embedded', 'glTF-Binary', 'glTF-Draco']
  132. },
  133. 'Cesium Milk Truck': {
  134. name: 'Cesium Milk Truck',
  135. url: './models/gltf/CesiumMilkTruck/%s/CesiumMilkTruck.gltf',
  136. author: 'Cesium',
  137. authorURL: 'https://cesiumjs.org/',
  138. cameraPos: new THREE.Vector3(0, 3, 10),
  139. addLights: true,
  140. addGround: true,
  141. shadows: true,
  142. extensions: ['glTF', 'glTF-Embedded', 'glTF-Binary', 'glTF-Draco']
  143. },
  144. 'Outlined Box': {
  145. name: 'Outlined Box',
  146. url: './models/gltf/OutlinedBox/OutlinedBox.gltf',
  147. author: '@twittmann',
  148. authorURL: 'https://github.com/twittmann',
  149. cameraPos: new THREE.Vector3(0, 5, 15),
  150. objectScale: new THREE.Vector3(0.01, 0.01, 0.01),
  151. objectRotation: new THREE.Euler(0, 90, 0),
  152. addLights: true,
  153. shadows: true,
  154. extensions: ['glTF']
  155. },
  156. };
  157. var state = {
  158. scene: Object.keys( scenes )[ 0 ],
  159. extension: scenes[ Object.keys( scenes )[ 0 ] ].extensions[ 0 ],
  160. playAnimation: true
  161. };
  162. function onload() {
  163. window.addEventListener( 'resize', onWindowResize, false );
  164. buildGUI();
  165. initScene( scenes[ state.scene ] );
  166. animate();
  167. }
  168. function initScene( sceneInfo ) {
  169. container = document.getElementById( 'container' );
  170. descriptionEl = document.getElementById( 'description' );
  171. if ( sceneInfo.author && sceneInfo.authorURL ) {
  172. descriptionEl.innerHTML = sceneInfo.name + ' by <a href="' + sceneInfo.authorURL + '" target="_blank" rel="noopener">' + sceneInfo.author + '</a>';
  173. }
  174. sceneInfo.descriptionHTML;
  175. scene = new THREE.Scene();
  176. scene.background = new THREE.Color( 0x222222 );
  177. camera = new THREE.PerspectiveCamera( 45, container.offsetWidth / container.offsetHeight, 0.001, 1000 );
  178. scene.add( camera );
  179. var spot1 = null;
  180. if (sceneInfo.addLights) {
  181. var ambient = new THREE.AmbientLight( 0x222222 );
  182. scene.add( ambient );
  183. var directionalLight = new THREE.DirectionalLight( 0xdddddd );
  184. directionalLight.position.set( 0, 0, 1 ).normalize();
  185. scene.add( directionalLight );
  186. spot1 = new THREE.SpotLight( 0xffffff, 1 );
  187. spot1.position.set( 10, 20, 10 );
  188. spot1.angle = 0.25;
  189. spot1.distance = 1024;
  190. spot1.penumbra = 0.75;
  191. if ( sceneInfo.shadows ) {
  192. spot1.castShadow = true;
  193. spot1.shadow.bias = 0.0001;
  194. spot1.shadow.mapSize.width = 2048;
  195. spot1.shadow.mapSize.height = 2048;
  196. }
  197. scene.add( spot1 );
  198. }
  199. // RENDERER
  200. renderer = new THREE.WebGLRenderer( { antialias: true } );
  201. renderer.setPixelRatio( window.devicePixelRatio );
  202. renderer.setSize( window.innerWidth, window.innerHeight );
  203. renderer.gammaOutput = true;
  204. if (sceneInfo.shadows) {
  205. renderer.shadowMap.enabled = true;
  206. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  207. }
  208. container.appendChild( renderer.domElement );
  209. orbitControls = new THREE.OrbitControls( camera, renderer.domElement );
  210. var ground = null;
  211. if ( sceneInfo.addGround ) {
  212. var groundMaterial = new THREE.MeshPhongMaterial( { color: 0xFFFFFF } );
  213. ground = new THREE.Mesh( new THREE.PlaneBufferGeometry(512, 512), groundMaterial);
  214. ground.receiveShadow = !!sceneInfo.shadows;
  215. if (sceneInfo.groundPos) {
  216. ground.position.copy(sceneInfo.groundPos);
  217. } else {
  218. ground.position.z = -70;
  219. }
  220. ground.rotation.x = -Math.PI / 2;
  221. scene.add(ground);
  222. }
  223. loader = new THREE.GLTFLoader();
  224. THREE.DRACOLoader.setDecoderPath( 'js/libs/draco/gltf/' );
  225. loader.setDRACOLoader( new THREE.DRACOLoader() );
  226. var url = sceneInfo.url;
  227. var r = eval( '/' + '\%s' + '/g' );
  228. url = url.replace( r, state.extension );
  229. if ( state.extension === 'glTF-Binary' ) {
  230. url = url.replace( '.gltf', '.glb' );
  231. }
  232. var loadStartTime = performance.now();
  233. loader.load( url, function(data) {
  234. gltf = data;
  235. var object = gltf.scene;
  236. console.info( 'Load time: ' + ( performance.now() - loadStartTime ).toFixed( 2 ) + ' ms.' );
  237. if (sceneInfo.cameraPos)
  238. camera.position.copy(sceneInfo.cameraPos);
  239. if (sceneInfo.center) {
  240. orbitControls.target.copy(sceneInfo.center);
  241. }
  242. if (sceneInfo.objectPosition) {
  243. object.position.copy(sceneInfo.objectPosition);
  244. if (spot1) {
  245. spot1.position.set(sceneInfo.objectPosition.x - 100, sceneInfo.objectPosition.y + 200, sceneInfo.objectPosition.z - 100 );
  246. spot1.target.position.copy(sceneInfo.objectPosition);
  247. }
  248. }
  249. if (sceneInfo.objectRotation)
  250. object.rotation.copy(sceneInfo.objectRotation);
  251. if (sceneInfo.objectScale)
  252. object.scale.copy(sceneInfo.objectScale);
  253. if ( sceneInfo.addEnvMap ) {
  254. var envMap = getEnvMap();
  255. object.traverse( function( node ) {
  256. if ( node.material && ( node.material.isMeshStandardMaterial ||
  257. ( node.material.isShaderMaterial && node.material.envMap !== undefined ) ) ) {
  258. node.material.envMap = envMap;
  259. node.material.needsUpdate = true;
  260. }
  261. } );
  262. scene.background = envMap;
  263. }
  264. object.traverse( function ( node ) {
  265. if ( node.isMesh ) node.castShadow = true;
  266. } );
  267. var animations = gltf.animations;
  268. if ( animations && animations.length ) {
  269. mixer = new THREE.AnimationMixer( object );
  270. for ( var i = 0; i < animations.length; i ++ ) {
  271. var animation = animations[ i ];
  272. // There's .3333 seconds junk at the tail of the Monster animation that
  273. // keeps it from looping cleanly. Clip it at 3 seconds
  274. if ( sceneInfo.animationTime )
  275. animation.duration = sceneInfo.animationTime;
  276. var action = mixer.clipAction( animation );
  277. if ( state.playAnimation ) action.play();
  278. }
  279. }
  280. scene.add( object );
  281. onWindowResize();
  282. }, undefined, function ( error ) {
  283. console.error( error );
  284. } );
  285. }
  286. function onWindowResize() {
  287. camera.aspect = container.offsetWidth / container.offsetHeight;
  288. camera.updateProjectionMatrix();
  289. renderer.setSize( window.innerWidth, window.innerHeight );
  290. }
  291. function animate() {
  292. requestAnimationFrame( animate );
  293. if ( mixer ) mixer.update( clock.getDelta() );
  294. orbitControls.update();
  295. render();
  296. }
  297. function render() {
  298. renderer.render( scene, camera );
  299. }
  300. var envMap;
  301. function getEnvMap() {
  302. if ( envMap ) {
  303. return envMap;
  304. }
  305. var path = 'textures/cube/Park2/';
  306. var format = '.jpg';
  307. var urls = [
  308. path + 'posx' + format, path + 'negx' + format,
  309. path + 'posy' + format, path + 'negy' + format,
  310. path + 'posz' + format, path + 'negz' + format
  311. ];
  312. envMap = new THREE.CubeTextureLoader().load( urls );
  313. envMap.format = THREE.RGBFormat;
  314. return envMap;
  315. }
  316. function buildGUI() {
  317. gui = new dat.GUI( { width: 330 } );
  318. gui.domElement.parentElement.style.zIndex = 101;
  319. var sceneCtrl = gui.add( state, 'scene', Object.keys( scenes ) );
  320. sceneCtrl.onChange( reload );
  321. var animCtrl = gui.add( state, 'playAnimation' );
  322. animCtrl.onChange( toggleAnimations );
  323. updateGUI();
  324. }
  325. function updateGUI() {
  326. if ( extensionCtrl ) extensionCtrl.remove();
  327. var sceneInfo = scenes[ state.scene ];
  328. if ( sceneInfo.extensions.indexOf( state.extension ) === -1 ) {
  329. state.extension = sceneInfo.extensions[ 0 ];
  330. }
  331. extensionCtrl = gui.add( state, 'extension', sceneInfo.extensions );
  332. extensionCtrl.onChange( reload );
  333. }
  334. function toggleAnimations() {
  335. for ( var i = 0; i < gltf.animations.length; ++ i ) {
  336. var clip = gltf.animations[i];
  337. var action = mixer.existingAction( clip );
  338. state.playAnimation ? action.play() : action.stop();
  339. }
  340. }
  341. function reload() {
  342. if ( container && renderer ) {
  343. container.removeChild( renderer.domElement );
  344. }
  345. if ( loader && mixer ) mixer.stopAllAction();
  346. updateGUI();
  347. initScene( scenes[ state.scene ] );
  348. }
  349. onload();
  350. </script>
  351. </body>
  352. </html>