webgl_loader_gltf_extensions.html 13 KB

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