misc_exporter_collada.html 12 KB

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