webgl_loader_gltf_extensions.html 12 KB

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