2
0

misc_exporter_collada.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. 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.colorSpace = THREE.SRGBColorSpace;
  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.colorSpace = THREE.SRGBColorSpace;
  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. vertexColors: false,
  135. // bizarrely, if you initialize these with negative numbers, the sliders
  136. // will not show any decimal places.
  137. lx: 0.32,
  138. ly: 0.39,
  139. lz: 0.7,
  140. newTess: 15,
  141. bottom: true,
  142. lid: true,
  143. body: true,
  144. fitLid: false,
  145. nonblinn: false,
  146. newShading: 'glossy',
  147. export: exportCollada
  148. };
  149. let h;
  150. const gui = new GUI();
  151. // material (attributes)
  152. h = gui.addFolder( 'Material control' );
  153. h.add( effectController, 'shininess', 1.0, 400.0, 1.0 ).name( 'shininess' ).onChange( render );
  154. h.add( effectController, 'kd', 0.0, 1.0, 0.025 ).name( 'diffuse strength' ).onChange( render );
  155. h.add( effectController, 'ks', 0.0, 1.0, 0.025 ).name( 'specular strength' ).onChange( render );
  156. h.add( effectController, 'metallic' ).onChange( render );
  157. // material (color)
  158. h = gui.addFolder( 'Material color' );
  159. h.add( effectController, 'hue', 0.0, 1.0, 0.025 ).name( 'hue' ).onChange( render );
  160. h.add( effectController, 'saturation', 0.0, 1.0, 0.025 ).name( 'saturation' ).onChange( render );
  161. h.add( effectController, 'lightness', 0.0, 1.0, 0.025 ).name( 'lightness' ).onChange( render );
  162. h.add( effectController, 'vertexColors' ).onChange( render );
  163. // light (point)
  164. h = gui.addFolder( 'Lighting' );
  165. h.add( effectController, 'lhue', 0.0, 1.0, 0.025 ).name( 'hue' ).onChange( render );
  166. h.add( effectController, 'lsaturation', 0.0, 1.0, 0.025 ).name( 'saturation' ).onChange( render );
  167. h.add( effectController, 'llightness', 0.0, 1.0, 0.025 ).name( 'lightness' ).onChange( render );
  168. h.add( effectController, 'ka', 0.0, 1.0, 0.025 ).name( 'ambient' ).onChange( render );
  169. // light (directional)
  170. h = gui.addFolder( 'Light direction' );
  171. h.add( effectController, 'lx', - 1.0, 1.0, 0.025 ).name( 'x' ).onChange( render );
  172. h.add( effectController, 'ly', - 1.0, 1.0, 0.025 ).name( 'y' ).onChange( render );
  173. h.add( effectController, 'lz', - 1.0, 1.0, 0.025 ).name( 'z' ).onChange( render );
  174. h = gui.addFolder( 'Tessellation control' );
  175. h.add( effectController, 'newTess', [ 2, 3, 4, 5, 6, 8, 10, 15, 20, 30, 40, 50 ] ).name( 'Tessellation Level' ).onChange( render );
  176. h.add( effectController, 'lid' ).name( 'display lid' ).onChange( render );
  177. h.add( effectController, 'body' ).name( 'display body' ).onChange( render );
  178. h.add( effectController, 'bottom' ).name( 'display bottom' ).onChange( render );
  179. h.add( effectController, 'fitLid' ).name( 'snug lid' ).onChange( render );
  180. h.add( effectController, 'nonblinn' ).name( 'original scale' ).onChange( render );
  181. // shading
  182. gui.add( effectController, 'newShading', [ 'wireframe', 'flat', 'smooth', 'glossy', 'textured', 'normal', 'reflective' ] ).name( 'Shading' ).onChange( render );
  183. h = gui.addFolder( 'Export' );
  184. h.add( effectController, 'export' ).name( 'Export Collada' );
  185. }
  186. //
  187. function render() {
  188. if ( effectController.newTess !== tess ||
  189. effectController.bottom !== bBottom ||
  190. effectController.lid !== bLid ||
  191. effectController.body !== bBody ||
  192. effectController.fitLid !== bFitLid ||
  193. effectController.nonblinn !== bNonBlinn ||
  194. effectController.newShading !== shading ||
  195. effectController.vertexColors !== vertexColors ) {
  196. tess = effectController.newTess;
  197. bBottom = effectController.bottom;
  198. bLid = effectController.lid;
  199. bBody = effectController.body;
  200. bFitLid = effectController.fitLid;
  201. bNonBlinn = effectController.nonblinn;
  202. shading = effectController.newShading;
  203. vertexColors = effectController.vertexColors;
  204. createNewTeapot();
  205. }
  206. // We're a bit lazy here. We could check to see if any material attributes changed and update
  207. // only if they have. But, these calls are cheap enough and this is just a demo.
  208. phongMaterial.shininess = effectController.shininess;
  209. texturedMaterial.shininess = effectController.shininess;
  210. normalMaterial.shininess = effectController.shininess;
  211. diffuseColor.setHSL( effectController.hue, effectController.saturation, effectController.lightness );
  212. if ( effectController.metallic ) {
  213. // make colors match to give a more metallic look
  214. specularColor.copy( diffuseColor );
  215. } else {
  216. // more of a plastic look
  217. specularColor.setRGB( 1, 1, 1 );
  218. }
  219. diffuseColor.multiplyScalar( effectController.kd );
  220. flatMaterial.color.copy( diffuseColor );
  221. gouraudMaterial.color.copy( diffuseColor );
  222. phongMaterial.color.copy( diffuseColor );
  223. texturedMaterial.color.copy( diffuseColor );
  224. normalMaterial.color.copy( diffuseColor );
  225. specularColor.multiplyScalar( effectController.ks );
  226. phongMaterial.specular.copy( specularColor );
  227. texturedMaterial.specular.copy( specularColor );
  228. normalMaterial.specular.copy( specularColor );
  229. // Ambient's actually controlled by the light for this demo
  230. ambientLight.color.setHSL( effectController.hue, effectController.saturation, effectController.lightness * effectController.ka );
  231. light.position.set( effectController.lx, effectController.ly, effectController.lz );
  232. light.color.setHSL( effectController.lhue, effectController.lsaturation, effectController.llightness );
  233. // skybox is rendered separately, so that it is always behind the teapot.
  234. if ( shading === 'reflective' ) {
  235. scene.background = textureCube;
  236. } else {
  237. scene.background = null;
  238. }
  239. renderer.render( scene, camera );
  240. }
  241. // Whenever the teapot changes, the scene is rebuilt from scratch (not much to it).
  242. function createNewTeapot() {
  243. if ( teapot !== undefined ) {
  244. teapot.geometry.dispose();
  245. scene.remove( teapot );
  246. }
  247. const teapotGeometry = new TeapotGeometry( teapotSize,
  248. tess,
  249. effectController.bottom,
  250. effectController.lid,
  251. effectController.body,
  252. effectController.fitLid,
  253. ! effectController.nonblinn );
  254. teapot = new THREE.Mesh(
  255. teapotGeometry,
  256. shading === 'wireframe' ? wireMaterial : (
  257. shading === 'flat' ? flatMaterial : (
  258. shading === 'smooth' ? gouraudMaterial : (
  259. shading === 'glossy' ? phongMaterial : (
  260. shading === 'textured' ? texturedMaterial : (
  261. shading === 'normal' ? normalMaterial : reflectiveMaterial ) ) ) ) ) ); // if no match, pick Phong
  262. if ( effectController.vertexColors ) {
  263. teapot.geometry.computeBoundingBox();
  264. const minY = teapot.geometry.boundingBox.min.y;
  265. const maxY = teapot.geometry.boundingBox.max.y;
  266. const sizeY = maxY - minY;
  267. const colors = [];
  268. const position = teapot.geometry.getAttribute( 'position' );
  269. for ( let i = 0, l = position.count; i < l; i ++ ) {
  270. const y = position.getY( i );
  271. const r = ( y - minY ) / sizeY;
  272. const b = 1.0 - r;
  273. colors.push( r * 128, 0, b * 128 );
  274. }
  275. teapot.geometry.setAttribute( 'color', new THREE.Uint8BufferAttribute( colors, 3, true ) );
  276. teapot.material.vertexColors = true;
  277. teapot.material.needsUpdate = true;
  278. } else {
  279. teapot.geometry.deleteAttribute( 'color' );
  280. teapot.material.vertexColors = false;
  281. teapot.material.needsUpdate = true;
  282. }
  283. scene.add( teapot );
  284. }
  285. const exporter = new ColladaExporter();
  286. function exportCollada() {
  287. const result = exporter.parse( teapot, undefined, { upAxis: 'Y_UP', unitName: 'millimeter', unitMeter: 0.001 } );
  288. let materialType = 'Phong';
  289. if ( shading === 'wireframe' ) {
  290. materialType = 'Constant';
  291. }
  292. if ( shading === 'smooth' ) {
  293. materialType = 'Lambert';
  294. }
  295. saveString( result.data, 'teapot_' + shading + '_' + materialType + '.dae' );
  296. result.textures.forEach( tex => {
  297. saveArrayBuffer( tex.data, `${ tex.name }.${ tex.ext }` );
  298. } );
  299. }
  300. function save( blob, filename ) {
  301. link.href = URL.createObjectURL( blob );
  302. link.download = filename;
  303. link.click();
  304. }
  305. function saveString( text, filename ) {
  306. save( new Blob( [ text ], { type: 'text/plain' } ), filename );
  307. }
  308. function saveArrayBuffer( buffer, filename ) {
  309. save( new Blob( [ buffer ], { type: 'application/octet-stream' } ), filename );
  310. }
  311. const link = document.createElement( 'a' );
  312. link.style.display = 'none';
  313. document.body.appendChild( link );
  314. </script>
  315. </body>
  316. </html>