webgl_modifier_edgesplit.html 4.9 KB

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