webgl_modifier_edgesplit.html 4.8 KB

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