webgl_loader_gltf_extensions.html 12 KB

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