webgl_loader_gltf_extensions.html 11 KB

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