misc_exporter_collada.html 13 KB

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