webgl_loader_gltf_extensions.html 12 KB

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