misc_exporter_collada.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - exporter - collada</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> webgl - exporter - collada
  12. </div>
  13. <!-- Import maps polyfill -->
  14. <!-- Remove this when import maps will be widely supported -->
  15. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.module.js",
  20. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  27. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  28. import { ColladaExporter } from 'three/addons/exporters/ColladaExporter.js';
  29. import { TeapotGeometry } from 'three/addons/geometries/TeapotGeometry.js';
  30. ////////////////////////////////////////////////////////////////////////////////
  31. // Utah/Newell Teapot demo
  32. ////////////////////////////////////////////////////////////////////////////////
  33. /*global window */
  34. let camera, scene, renderer;
  35. let cameraControls;
  36. let effectController;
  37. const teapotSize = 100; // vertical height of the teapot is about 2x this, we scale it to millimeters.
  38. let ambientLight, light;
  39. let tess = - 1; // force initialization
  40. let bBottom;
  41. let bLid;
  42. let bBody;
  43. let bFitLid;
  44. let bNonBlinn;
  45. let shading;
  46. let vertexColors;
  47. let wireMaterial, flatMaterial, gouraudMaterial, phongMaterial, texturedMaterial, normalMaterial, reflectiveMaterial;
  48. let teapot, textureCube;
  49. // allocate these just once
  50. const diffuseColor = new THREE.Color();
  51. const specularColor = new THREE.Color();
  52. init();
  53. render();
  54. function init() {
  55. const container = document.createElement( 'div' );
  56. document.body.appendChild( container );
  57. const canvasWidth = window.innerWidth;
  58. const canvasHeight = window.innerHeight;
  59. // CAMERA
  60. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 20000 );
  61. camera.position.set( - 150, 137.5, 325 );
  62. // LIGHTS
  63. ambientLight = new THREE.AmbientLight( 0x333333 ); // 0.2
  64. light = new THREE.DirectionalLight( 0xFFFFFF, 1.0 );
  65. // direction is set in GUI
  66. // RENDERER
  67. renderer = new THREE.WebGLRenderer( { antialias: true } );
  68. renderer.setPixelRatio( window.devicePixelRatio );
  69. renderer.setSize( canvasWidth, canvasHeight );
  70. renderer.outputEncoding = THREE.sRGBEncoding;
  71. container.appendChild( renderer.domElement );
  72. // EVENTS
  73. window.addEventListener( 'resize', onWindowResize );
  74. // CONTROLS
  75. cameraControls = new OrbitControls( camera, renderer.domElement );
  76. cameraControls.addEventListener( 'change', render );
  77. // TEXTURE MAP
  78. const loader = new THREE.TextureLoader();
  79. const textureMap = loader.load( 'textures/uv_grid_opengl.jpg' );
  80. textureMap.wrapS = textureMap.wrapT = THREE.RepeatWrapping;
  81. textureMap.anisotropy = 16;
  82. textureMap.encoding = THREE.sRGBEncoding;
  83. // NORMAL MAP
  84. const diffuseMap = loader.load( 'textures/floors/FloorsCheckerboard_S_Diffuse.jpg' );
  85. const normalMap = loader.load( 'textures/floors/FloorsCheckerboard_S_Normal.jpg' );
  86. // REFLECTION MAP
  87. const path = 'textures/cube/pisa/';
  88. const urls = [
  89. path + 'px.png', path + 'nx.png',
  90. path + 'py.png', path + 'ny.png',
  91. path + 'pz.png', path + 'nz.png'
  92. ];
  93. textureCube = new THREE.CubeTextureLoader().load( urls );
  94. textureCube.encoding = THREE.sRGBEncoding;
  95. // MATERIALS
  96. const materialColor = new THREE.Color();
  97. materialColor.setRGB( 1.0, 1.0, 1.0 );
  98. wireMaterial = new THREE.MeshBasicMaterial( { color: 0xFFFFFF, wireframe: true } );
  99. flatMaterial = new THREE.MeshPhongMaterial( { color: materialColor, specular: 0x000000, flatShading: true, side: THREE.DoubleSide } );
  100. gouraudMaterial = new THREE.MeshLambertMaterial( { color: materialColor, side: THREE.DoubleSide } );
  101. phongMaterial = new THREE.MeshPhongMaterial( { color: materialColor, side: THREE.DoubleSide } );
  102. texturedMaterial = new THREE.MeshPhongMaterial( { color: materialColor, map: textureMap, side: THREE.DoubleSide } );
  103. normalMaterial = new THREE.MeshPhongMaterial( { color: materialColor, map: diffuseMap, normalMap: normalMap, side: THREE.DoubleSide } );
  104. reflectiveMaterial = new THREE.MeshPhongMaterial( { color: materialColor, envMap: textureCube, side: THREE.DoubleSide } );
  105. // scene itself
  106. scene = new THREE.Scene();
  107. scene.background = new THREE.Color( 0xAAAAAA );
  108. scene.add( ambientLight );
  109. scene.add( light );
  110. // GUI
  111. setupGui();
  112. }
  113. // EVENT HANDLERS
  114. function onWindowResize() {
  115. const canvasWidth = window.innerWidth;
  116. const canvasHeight = window.innerHeight;
  117. renderer.setSize( canvasWidth, canvasHeight );
  118. camera.aspect = canvasWidth / canvasHeight;
  119. camera.updateProjectionMatrix();
  120. render();
  121. }
  122. function setupGui() {
  123. effectController = {
  124. shininess: 40.0,
  125. ka: 0.17,
  126. kd: 0.51,
  127. ks: 0.2,
  128. metallic: true,
  129. hue: 0.121,
  130. saturation: 0.73,
  131. lightness: 0.66,
  132. lhue: 0.04,
  133. lsaturation: 0.01, // non-zero so that fractions will be shown
  134. llightness: 1.0,
  135. vertexColors: false,
  136. // bizarrely, if you initialize these with negative numbers, the sliders
  137. // will not show any decimal places.
  138. lx: 0.32,
  139. ly: 0.39,
  140. lz: 0.7,
  141. newTess: 15,
  142. bottom: true,
  143. lid: true,
  144. body: true,
  145. fitLid: false,
  146. nonblinn: false,
  147. newShading: 'glossy',
  148. export: exportCollada
  149. };
  150. let h;
  151. const gui = new GUI();
  152. // material (attributes)
  153. h = gui.addFolder( 'Material control' );
  154. h.add( effectController, 'shininess', 1.0, 400.0, 1.0 ).name( 'shininess' ).onChange( render );
  155. h.add( effectController, 'kd', 0.0, 1.0, 0.025 ).name( 'diffuse strength' ).onChange( render );
  156. h.add( effectController, 'ks', 0.0, 1.0, 0.025 ).name( 'specular strength' ).onChange( render );
  157. h.add( effectController, 'metallic' ).onChange( render );
  158. // material (color)
  159. h = gui.addFolder( 'Material color' );
  160. h.add( effectController, 'hue', 0.0, 1.0, 0.025 ).name( 'hue' ).onChange( render );
  161. h.add( effectController, 'saturation', 0.0, 1.0, 0.025 ).name( 'saturation' ).onChange( render );
  162. h.add( effectController, 'lightness', 0.0, 1.0, 0.025 ).name( 'lightness' ).onChange( render );
  163. h.add( effectController, 'vertexColors' ).onChange( render );
  164. // light (point)
  165. h = gui.addFolder( 'Lighting' );
  166. h.add( effectController, 'lhue', 0.0, 1.0, 0.025 ).name( 'hue' ).onChange( render );
  167. h.add( effectController, 'lsaturation', 0.0, 1.0, 0.025 ).name( 'saturation' ).onChange( render );
  168. h.add( effectController, 'llightness', 0.0, 1.0, 0.025 ).name( 'lightness' ).onChange( render );
  169. h.add( effectController, 'ka', 0.0, 1.0, 0.025 ).name( 'ambient' ).onChange( render );
  170. // light (directional)
  171. h = gui.addFolder( 'Light direction' );
  172. h.add( effectController, 'lx', - 1.0, 1.0, 0.025 ).name( 'x' ).onChange( render );
  173. h.add( effectController, 'ly', - 1.0, 1.0, 0.025 ).name( 'y' ).onChange( render );
  174. h.add( effectController, 'lz', - 1.0, 1.0, 0.025 ).name( 'z' ).onChange( render );
  175. h = gui.addFolder( 'Tessellation control' );
  176. h.add( effectController, 'newTess', [ 2, 3, 4, 5, 6, 8, 10, 15, 20, 30, 40, 50 ] ).name( 'Tessellation Level' ).onChange( render );
  177. h.add( effectController, 'lid' ).name( 'display lid' ).onChange( render );
  178. h.add( effectController, 'body' ).name( 'display body' ).onChange( render );
  179. h.add( effectController, 'bottom' ).name( 'display bottom' ).onChange( render );
  180. h.add( effectController, 'fitLid' ).name( 'snug lid' ).onChange( render );
  181. h.add( effectController, 'nonblinn' ).name( 'original scale' ).onChange( render );
  182. // shading
  183. gui.add( effectController, 'newShading', [ 'wireframe', 'flat', 'smooth', 'glossy', 'textured', 'normal', 'reflective' ] ).name( 'Shading' ).onChange( render );
  184. h = gui.addFolder( 'Export' );
  185. h.add( effectController, 'export' ).name( 'Export Collada' );
  186. }
  187. //
  188. function render() {
  189. if ( effectController.newTess !== tess ||
  190. effectController.bottom !== bBottom ||
  191. effectController.lid !== bLid ||
  192. effectController.body !== bBody ||
  193. effectController.fitLid !== bFitLid ||
  194. effectController.nonblinn !== bNonBlinn ||
  195. effectController.newShading !== shading ||
  196. effectController.vertexColors !== vertexColors ) {
  197. tess = effectController.newTess;
  198. bBottom = effectController.bottom;
  199. bLid = effectController.lid;
  200. bBody = effectController.body;
  201. bFitLid = effectController.fitLid;
  202. bNonBlinn = effectController.nonblinn;
  203. shading = effectController.newShading;
  204. vertexColors = effectController.vertexColors;
  205. createNewTeapot();
  206. }
  207. // We're a bit lazy here. We could check to see if any material attributes changed and update
  208. // only if they have. But, these calls are cheap enough and this is just a demo.
  209. phongMaterial.shininess = effectController.shininess;
  210. texturedMaterial.shininess = effectController.shininess;
  211. normalMaterial.shininess = effectController.shininess;
  212. diffuseColor.setHSL( effectController.hue, effectController.saturation, effectController.lightness );
  213. if ( effectController.metallic ) {
  214. // make colors match to give a more metallic look
  215. specularColor.copy( diffuseColor );
  216. } else {
  217. // more of a plastic look
  218. specularColor.setRGB( 1, 1, 1 );
  219. }
  220. diffuseColor.multiplyScalar( effectController.kd );
  221. flatMaterial.color.copy( diffuseColor );
  222. gouraudMaterial.color.copy( diffuseColor );
  223. phongMaterial.color.copy( diffuseColor );
  224. texturedMaterial.color.copy( diffuseColor );
  225. normalMaterial.color.copy( diffuseColor );
  226. specularColor.multiplyScalar( effectController.ks );
  227. phongMaterial.specular.copy( specularColor );
  228. texturedMaterial.specular.copy( specularColor );
  229. normalMaterial.specular.copy( specularColor );
  230. // Ambient's actually controlled by the light for this demo
  231. ambientLight.color.setHSL( effectController.hue, effectController.saturation, effectController.lightness * effectController.ka );
  232. light.position.set( effectController.lx, effectController.ly, effectController.lz );
  233. light.color.setHSL( effectController.lhue, effectController.lsaturation, effectController.llightness );
  234. // skybox is rendered separately, so that it is always behind the teapot.
  235. if ( shading === 'reflective' ) {
  236. scene.background = textureCube;
  237. } else {
  238. scene.background = null;
  239. }
  240. renderer.render( scene, camera );
  241. }
  242. // Whenever the teapot changes, the scene is rebuilt from scratch (not much to it).
  243. function createNewTeapot() {
  244. if ( teapot !== undefined ) {
  245. teapot.geometry.dispose();
  246. scene.remove( teapot );
  247. }
  248. const teapotGeometry = new TeapotGeometry( teapotSize,
  249. tess,
  250. effectController.bottom,
  251. effectController.lid,
  252. effectController.body,
  253. effectController.fitLid,
  254. ! effectController.nonblinn );
  255. teapot = new THREE.Mesh(
  256. teapotGeometry,
  257. shading === 'wireframe' ? wireMaterial : (
  258. shading === 'flat' ? flatMaterial : (
  259. shading === 'smooth' ? gouraudMaterial : (
  260. shading === 'glossy' ? phongMaterial : (
  261. shading === 'textured' ? texturedMaterial : (
  262. shading === 'normal' ? normalMaterial : reflectiveMaterial ) ) ) ) ) ); // if no match, pick Phong
  263. if ( effectController.vertexColors ) {
  264. teapot.geometry.computeBoundingBox();
  265. const minY = teapot.geometry.boundingBox.min.y;
  266. const maxY = teapot.geometry.boundingBox.max.y;
  267. const sizeY = maxY - minY;
  268. const colors = [];
  269. const position = teapot.geometry.getAttribute( 'position' );
  270. for ( let i = 0, l = position.count; i < l; i ++ ) {
  271. const y = position.getY( i );
  272. const r = ( y - minY ) / sizeY;
  273. const b = 1.0 - r;
  274. colors.push( r * 128, 0, b * 128 );
  275. }
  276. teapot.geometry.setAttribute( 'color', new THREE.Uint8BufferAttribute( colors, 3, true ) );
  277. teapot.material.vertexColors = true;
  278. teapot.material.needsUpdate = true;
  279. } else {
  280. teapot.geometry.deleteAttribute( 'color' );
  281. teapot.material.vertexColors = false;
  282. teapot.material.needsUpdate = true;
  283. }
  284. scene.add( teapot );
  285. }
  286. const exporter = new ColladaExporter();
  287. function exportCollada() {
  288. const result = exporter.parse( teapot, undefined, { upAxis: 'Y_UP', unitName: 'millimeter', unitMeter: 0.001 } );
  289. let materialType = 'Phong';
  290. if ( shading === 'wireframe' ) {
  291. materialType = 'Constant';
  292. }
  293. if ( shading === 'smooth' ) {
  294. materialType = 'Lambert';
  295. }
  296. saveString( result.data, 'teapot_' + shading + '_' + materialType + '.dae' );
  297. result.textures.forEach( tex => {
  298. saveArrayBuffer( tex.data, `${ tex.name }.${ tex.ext }` );
  299. } );
  300. }
  301. function save( blob, filename ) {
  302. link.href = URL.createObjectURL( blob );
  303. link.download = filename;
  304. link.click();
  305. }
  306. function saveString( text, filename ) {
  307. save( new Blob( [ text ], { type: 'text/plain' } ), filename );
  308. }
  309. function saveArrayBuffer( buffer, filename ) {
  310. save( new Blob( [ buffer ], { type: 'application/octet-stream' } ), filename );
  311. }
  312. const link = document.createElement( 'a' );
  313. link.style.display = 'none';
  314. document.body.appendChild( link );
  315. </script>
  316. </body>
  317. </html>