2
0

misc_exporter_collada.html 12 KB

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