misc_exporter_gltf_normals.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - exporter - gltf - normals</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. <meta property="og:title" content="three.js webgl - exporter - gltf - normals">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/misc_exporter_gltf_normals.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/misc_exporter_gltf_normals.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - exporter - gltf - normals<br/><br/>
  16. <button id="export_objects">Export</button><br/>
  17. </div>
  18. <script type="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.module.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. import { GLTFExporter } from 'three/addons/exporters/GLTFExporter.js';
  30. //
  31. let container;
  32. let camera, scene, renderer;
  33. let mesh1, mesh2;
  34. init();
  35. render();
  36. function init() {
  37. container = document.createElement( 'div' );
  38. document.body.appendChild( container );
  39. // renderer
  40. renderer = new THREE.WebGLRenderer( { antialias: true } );
  41. renderer.setPixelRatio( window.devicePixelRatio );
  42. renderer.setSize( window.innerWidth, window.innerHeight );
  43. container.appendChild( renderer.domElement );
  44. scene = new THREE.Scene();
  45. scene.name = 'Scene';
  46. // camera
  47. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  48. camera.position.set( 0, 0, 150 );
  49. camera.name = 'PerspectiveCamera';
  50. scene.add( camera );
  51. // controls
  52. const controls = new OrbitControls( camera, renderer.domElement );
  53. controls.addEventListener( 'change', render ); // use if there is no animation loop
  54. controls.minDistance = 100;
  55. controls.maxDistance = 200;
  56. // lights
  57. const light = new THREE.DirectionalLight( 0xffffff, 4 );
  58. light.position.set( 0, 1, 1 );
  59. light.name = 'DirectionalLight';
  60. scene.add( light );
  61. const light2 = new THREE.DirectionalLight( 0xffffff, 4 ); // for back-sided objects
  62. light2.position.set( 0, 1, - 1 );
  63. light2.name = 'DirectionalLight2';
  64. scene.add( light2 );
  65. // geometry1 - three.js convention: uv (0,0) bottom left
  66. const geometry1 = new THREE.PlaneGeometry( 50, 50 );
  67. // geometry2 - glTF convention: uv (0,0) top left
  68. const geometry2 = new THREE.PlaneGeometry( 50, 50 );
  69. geometry2.setAttribute( 'uv', new THREE.BufferAttribute( new Float32Array( [ 0, 0, 1, 0, 0, 1, 1, 1 ] ), 2 ) );
  70. // textures
  71. const normalMapOpenGL = new THREE.TextureLoader().load( 'textures/NormalMapOpenGL.png', render );
  72. const normalMapDirectX = new THREE.TextureLoader().load( 'textures/NormalMapDirectX.png', render );
  73. // material
  74. const material = new THREE.MeshStandardMaterial( {
  75. color: 0x636389,
  76. roughness: 0.3,
  77. normalMap: null,
  78. normalScale: new THREE.Vector2( 0.5, 0.5 ),
  79. side: THREE.DoubleSide
  80. } );
  81. // pick one
  82. // ---------------------------------------------------
  83. const GEOMETRY = geometry1; // geometry1 or geometry2
  84. // ---------------------------------------------------
  85. // mesh1
  86. mesh1 = new THREE.Mesh( GEOMETRY, material.clone() );
  87. mesh1.position.set( - 30, 0, 0 );
  88. mesh1.material.normalMap = normalMapOpenGL;
  89. //mesh1.material.normalMap.flipY = false; // default true
  90. //mesh1.material.normalScale.y *= - 1; // exporter ignores negative normalScale.y
  91. mesh1.name = 'Mesh1';
  92. scene.add( mesh1 );
  93. // mesh2
  94. mesh2 = new THREE.Mesh( GEOMETRY, material.clone() );
  95. mesh2.position.set( 30, 0, 0 );
  96. mesh2.material.normalMap = normalMapDirectX;
  97. //mesh2.material.normalMap.flipY = false; // default true
  98. mesh2.material.normalScale.y *= - 1; // exporter ignores negative normalScale.y
  99. mesh2.name = 'Mesh2';
  100. scene.add( mesh2 );
  101. //
  102. window.addEventListener( 'resize', onWindowResize );
  103. }
  104. function onWindowResize() {
  105. camera.aspect = window.innerWidth / window.innerHeight;
  106. camera.updateProjectionMatrix();
  107. renderer.setSize( window.innerWidth, window.innerHeight );
  108. }
  109. function render() {
  110. renderer.render( scene, camera );
  111. }
  112. //
  113. function exportGLTF( input ) {
  114. const gltfExporter = new GLTFExporter();
  115. const options = {
  116. binary: true
  117. };
  118. gltfExporter.parse( input, function ( result ) {
  119. if ( result instanceof ArrayBuffer ) {
  120. saveArrayBuffer( result, 'scene.glb' );
  121. } else {
  122. const output = JSON.stringify( result, null, 2 );
  123. console.log( output );
  124. saveString( output, 'scene.gltf' );
  125. }
  126. }, null, options );
  127. }
  128. document.getElementById( 'export_objects' ).addEventListener( 'click', function () {
  129. exportGLTF( [ mesh1, mesh2 ] );
  130. } );
  131. const link = document.createElement( 'a' );
  132. link.style.display = 'none';
  133. document.body.appendChild( link ); // Firefox workaround, see #6594
  134. function save( blob, filename ) {
  135. link.href = URL.createObjectURL( blob );
  136. link.download = filename;
  137. link.click();
  138. // URL.revokeObjectURL( url ); breaks Firefox...
  139. }
  140. function saveString( text, filename ) {
  141. save( new Blob( [ text ], { type: 'text/plain' } ), filename );
  142. }
  143. function saveArrayBuffer( buffer, filename ) {
  144. save( new Blob( [ buffer ], { type: 'application/octet-stream' } ), filename );
  145. }
  146. </script>
  147. </body>
  148. </html>
粤ICP备19079148号