webgl_loader_gltf_extensions.html 12 KB

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