webgl_loader_gltf_extensions.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 { PMREMGenerator } from './jsm/pmrem/PMREMGenerator.js';
  25. var orbitControls;
  26. var container, camera, scene, renderer, loader;
  27. var gltf, background, envMap, mixer, gui, extensionControls;
  28. var clock = new THREE.Clock();
  29. var scenes = {
  30. Boombox: {
  31. name: 'BoomBox (PBR)',
  32. url: './models/gltf/BoomBox/%s/BoomBox.gltf',
  33. author: 'Microsoft',
  34. authorURL: 'https://www.microsoft.com/',
  35. cameraPos: new THREE.Vector3( 0.02, 0.01, 0.03 ),
  36. objectRotation: new THREE.Euler( 0, Math.PI, 0 ),
  37. extensions: [ 'glTF', 'glTF-pbrSpecularGlossiness', 'glTF-Binary', 'glTF-dds' ],
  38. addEnvMap: true
  39. },
  40. 'Bot Skinned': {
  41. name: 'Bot Skinned',
  42. url: './models/gltf/BotSkinned/%s/Bot_Skinned.gltf',
  43. author: 'MozillaVR',
  44. authorURL: 'https://vr.mozilla.org/',
  45. cameraPos: new THREE.Vector3( 0.5, 2, 2 ),
  46. center: new THREE.Vector3( 0, 1.2, 0 ),
  47. objectRotation: new THREE.Euler( 0, 0, 0 ),
  48. addLights: true,
  49. addGround: true,
  50. shadows: true,
  51. extensions: [ 'glTF-MaterialsUnlit' ]
  52. },
  53. MetalRoughSpheres: {
  54. name: 'MetalRoughSpheres (PBR)',
  55. url: './models/gltf/MetalRoughSpheres/%s/MetalRoughSpheres.gltf',
  56. author: '@emackey',
  57. authorURL: 'https://twitter.com/emackey',
  58. cameraPos: new THREE.Vector3( 2, 1, 15 ),
  59. objectRotation: new THREE.Euler( 0, 0, 0 ),
  60. extensions: [ 'glTF', 'glTF-Embedded' ],
  61. addEnvMap: true
  62. },
  63. Duck: {
  64. name: 'Duck',
  65. url: './models/gltf/Duck/%s/Duck.gltf',
  66. author: 'Sony',
  67. authorURL: 'https://www.playstation.com/en-us/corporate/about/',
  68. cameraPos: new THREE.Vector3( 0, 3, 5 ),
  69. addLights: true,
  70. addGround: true,
  71. shadows: true,
  72. extensions: [ 'glTF', 'glTF-Embedded', 'glTF-pbrSpecularGlossiness', 'glTF-Binary', 'glTF-Draco' ]
  73. },
  74. Monster: {
  75. name: 'Monster',
  76. url: './models/gltf/Monster/%s/Monster.gltf',
  77. author: '3drt.com',
  78. authorURL: 'http://www.3drt.com/downloads.htm',
  79. cameraPos: new THREE.Vector3( 3, 1, 7 ),
  80. objectScale: new THREE.Vector3( 0.04, 0.04, 0.04 ),
  81. objectPosition: new THREE.Vector3( 0.2, 0.1, 0 ),
  82. objectRotation: new THREE.Euler( 0, - 3 * Math.PI / 4, 0 ),
  83. animationTime: 3,
  84. addLights: true,
  85. shadows: true,
  86. addGround: true,
  87. extensions: [ 'glTF', 'glTF-Embedded', 'glTF-Binary', 'glTF-Draco', 'glTF-lights' ]
  88. },
  89. 'Cesium Man': {
  90. name: 'Cesium Man',
  91. url: './models/gltf/CesiumMan/%s/CesiumMan.gltf',
  92. author: 'Cesium',
  93. authorURL: 'https://cesiumjs.org/',
  94. cameraPos: new THREE.Vector3( 0, 3, 10 ),
  95. objectRotation: new THREE.Euler( 0, 0, 0 ),
  96. addLights: true,
  97. addGround: true,
  98. shadows: true,
  99. extensions: [ 'glTF', 'glTF-Embedded', 'glTF-Binary', 'glTF-Draco' ]
  100. },
  101. 'Cesium Milk Truck': {
  102. name: 'Cesium Milk Truck',
  103. url: './models/gltf/CesiumMilkTruck/%s/CesiumMilkTruck.gltf',
  104. author: 'Cesium',
  105. authorURL: 'https://cesiumjs.org/',
  106. cameraPos: new THREE.Vector3( 0, 3, 10 ),
  107. addLights: true,
  108. addGround: true,
  109. shadows: true,
  110. extensions: [ 'glTF', 'glTF-Embedded', 'glTF-Binary', 'glTF-Draco' ]
  111. },
  112. 'Outlined Box': {
  113. name: 'Outlined Box',
  114. url: './models/gltf/OutlinedBox/OutlinedBox.gltf',
  115. author: '@twittmann',
  116. authorURL: 'https://github.com/twittmann',
  117. cameraPos: new THREE.Vector3( 0, 5, 15 ),
  118. objectScale: new THREE.Vector3( 0.01, 0.01, 0.01 ),
  119. objectRotation: new THREE.Euler( 0, 90, 0 ),
  120. addLights: true,
  121. shadows: true,
  122. extensions: [ 'glTF' ]
  123. },
  124. };
  125. var state = {
  126. scene: Object.keys( scenes )[ 0 ],
  127. extension: scenes[ Object.keys( scenes )[ 0 ] ].extensions[ 0 ],
  128. playAnimation: true
  129. };
  130. function onload() {
  131. container = document.getElementById( 'container' );
  132. renderer = new THREE.WebGLRenderer( { antialias: true } );
  133. renderer.setPixelRatio( window.devicePixelRatio );
  134. renderer.setSize( window.innerWidth, window.innerHeight );
  135. renderer.gammaOutput = true;
  136. renderer.physicallyCorrectLights = true;
  137. container.appendChild( renderer.domElement );
  138. window.addEventListener( 'resize', onWindowResize, false );
  139. // Load background and generate envMap
  140. new RGBELoader()
  141. .setDataType( THREE.UnsignedByteType )
  142. .setPath( 'textures/equirectangular/' )
  143. .load( 'venice_sunset_1k.hdr', function ( texture ) {
  144. var pmremGenerator = new PMREMGenerator( renderer );
  145. envMap = pmremGenerator.fromEquirectangular( texture ).texture;
  146. pmremGenerator.dispose();
  147. background = envMap;
  148. //
  149. buildGUI();
  150. initScene( scenes[ state.scene ] );
  151. animate();
  152. } );
  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>