webgl_loader_gltf_extensions.html 12 KB

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