webgl_modifier_edgesplit.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - modifier - Edge Split modifier</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. <!-- Import maps polyfill -->
  11. <!-- Remove this when import maps will be widely supported -->
  12. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.module.js"
  17. }
  18. }
  19. </script>
  20. <script type="module">
  21. import * as THREE from 'three';
  22. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  23. import { OBJLoader } from './jsm/loaders/OBJLoader.js';
  24. import { EdgeSplitModifier } from './jsm/modifiers/EdgeSplitModifier.js';
  25. import * as BufferGeometryUtils from './jsm/utils/BufferGeometryUtils.js';
  26. import { GUI } from './jsm/libs/lil-gui.module.min.js';
  27. let renderer, scene, camera;
  28. let modifier, mesh, baseGeometry;
  29. let map;
  30. const params = {
  31. smoothShading: true,
  32. edgeSplit: true,
  33. cutOffAngle: 20,
  34. showMap: false,
  35. tryKeepNormals: true,
  36. };
  37. init();
  38. function init() {
  39. const info = document.createElement( 'div' );
  40. info.style.position = 'absolute';
  41. info.style.top = '10px';
  42. info.style.width = '100%';
  43. info.style.textAlign = 'center';
  44. info.innerHTML = '<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Edge Split modifier';
  45. document.body.appendChild( info );
  46. renderer = new THREE.WebGLRenderer( { antialias: true } );
  47. renderer.setPixelRatio( window.devicePixelRatio );
  48. renderer.setSize( window.innerWidth, window.innerHeight );
  49. document.body.appendChild( renderer.domElement );
  50. scene = new THREE.Scene();
  51. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight );
  52. const controls = new OrbitControls( camera, renderer.domElement );
  53. controls.addEventListener( 'change', render ); // use if there is no animation loop
  54. controls.enableDamping = true;
  55. controls.dampingFactor = 0.25;
  56. controls.rotateSpeed = 0.35;
  57. controls.minZoom = 1;
  58. camera.position.set( 0, 0, 4 );
  59. scene.add( new THREE.HemisphereLight( 0xffffff, 0x444444 ) );
  60. new OBJLoader().load(
  61. './models/obj/cerberus/Cerberus.obj',
  62. function ( group ) {
  63. const cerberus = group.children[ 0 ];
  64. const modelGeometry = cerberus.geometry;
  65. modifier = new EdgeSplitModifier();
  66. baseGeometry = BufferGeometryUtils.mergeVertices( modelGeometry );
  67. mesh = new THREE.Mesh( getGeometry(), new THREE.MeshStandardMaterial() );
  68. mesh.material.flatShading = ! params.smoothShading;
  69. mesh.rotateY( - Math.PI / 2 );
  70. mesh.scale.set( 3.5, 3.5, 3.5 );
  71. mesh.translateZ( 1.5 );
  72. scene.add( mesh );
  73. if ( map !== undefined && params.showMap ) {
  74. mesh.material.map = map;
  75. mesh.material.needsUpdate = true;
  76. }
  77. render();
  78. }
  79. );
  80. window.addEventListener( 'resize', onWindowResize );
  81. new THREE.TextureLoader().load( './models/obj/cerberus/Cerberus_A.jpg', function ( texture ) {
  82. map = texture;
  83. if ( mesh !== undefined && params.showMap ) {
  84. mesh.material.map = map;
  85. mesh.material.needsUpdate = true;
  86. }
  87. } );
  88. const gui = new GUI( { name: 'Edge split modifier parameters' } );
  89. gui.add( params, 'showMap' ).onFinishChange( updateMesh );
  90. gui.add( params, 'smoothShading' ).onFinishChange( updateMesh );
  91. gui.add( params, 'edgeSplit' ).onFinishChange( updateMesh );
  92. gui.add( params, 'cutOffAngle' ).min( 0 ).max( 180 ).onFinishChange( updateMesh );
  93. gui.add( params, 'tryKeepNormals' ).onFinishChange( updateMesh );
  94. }
  95. function onWindowResize() {
  96. renderer.setSize( window.innerWidth, window.innerHeight );
  97. camera.aspect = window.innerWidth / window.innerHeight;
  98. camera.updateProjectionMatrix();
  99. render();
  100. }
  101. function getGeometry() {
  102. let geometry;
  103. if ( params.edgeSplit ) {
  104. geometry = modifier.modify(
  105. baseGeometry,
  106. params.cutOffAngle * Math.PI / 180,
  107. params.tryKeepNormals
  108. );
  109. } else {
  110. geometry = baseGeometry;
  111. }
  112. return geometry;
  113. }
  114. function updateMesh() {
  115. if ( mesh !== undefined ) {
  116. mesh.geometry = getGeometry();
  117. let needsUpdate = mesh.material.flatShading === params.smoothShading;
  118. mesh.material.flatShading = params.smoothShading === false;
  119. if ( map !== undefined ) {
  120. needsUpdate = needsUpdate || mesh.material.map !== ( params.showMap ? map : null );
  121. mesh.material.map = params.showMap ? map : null;
  122. }
  123. mesh.material.needsUpdate = needsUpdate;
  124. render();
  125. }
  126. }
  127. function render() {
  128. renderer.render( scene, camera );
  129. }
  130. </script>
  131. </body>
  132. </html>