misc_exporter_collada.html 12 KB

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