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