misc_exporter_collada.html 12 KB

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