webgl_loader_gltf_extensions.html 12 KB

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