webgl_loader_gltf_extensions.html 12 KB

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