webgl_modifier_edgesplit.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. renderer.useLegacyLights = false;
  51. document.body.appendChild( renderer.domElement );
  52. scene = new THREE.Scene();
  53. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight );
  54. const controls = new OrbitControls( camera, renderer.domElement );
  55. controls.addEventListener( 'change', render ); // use if there is no animation loop
  56. controls.enableDamping = true;
  57. controls.dampingFactor = 0.25;
  58. controls.rotateSpeed = 0.35;
  59. controls.minZoom = 1;
  60. camera.position.set( 0, 0, 4 );
  61. scene.add( new THREE.HemisphereLight( 0xffffff, 0x444444, 3 ) );
  62. new OBJLoader().load(
  63. './models/obj/cerberus/Cerberus.obj',
  64. function ( group ) {
  65. const cerberus = group.children[ 0 ];
  66. const modelGeometry = cerberus.geometry;
  67. modifier = new EdgeSplitModifier();
  68. baseGeometry = BufferGeometryUtils.mergeVertices( modelGeometry );
  69. mesh = new THREE.Mesh( getGeometry(), new THREE.MeshStandardMaterial() );
  70. mesh.material.flatShading = ! params.smoothShading;
  71. mesh.rotateY( - Math.PI / 2 );
  72. mesh.scale.set( 3.5, 3.5, 3.5 );
  73. mesh.translateZ( 1.5 );
  74. scene.add( mesh );
  75. if ( map !== undefined && params.showMap ) {
  76. mesh.material.map = map;
  77. mesh.material.needsUpdate = true;
  78. }
  79. render();
  80. }
  81. );
  82. window.addEventListener( 'resize', onWindowResize );
  83. new THREE.TextureLoader().load( './models/obj/cerberus/Cerberus_A.jpg', function ( texture ) {
  84. map = texture;
  85. map.colorSpace = THREE.SRGBColorSpace;
  86. if ( mesh !== undefined && params.showMap ) {
  87. mesh.material.map = map;
  88. mesh.material.needsUpdate = true;
  89. }
  90. } );
  91. const gui = new GUI( { name: 'Edge split modifier parameters' } );
  92. gui.add( params, 'showMap' ).onFinishChange( updateMesh );
  93. gui.add( params, 'smoothShading' ).onFinishChange( updateMesh );
  94. gui.add( params, 'edgeSplit' ).onFinishChange( updateMesh );
  95. gui.add( params, 'cutOffAngle' ).min( 0 ).max( 180 ).onFinishChange( updateMesh );
  96. gui.add( params, 'tryKeepNormals' ).onFinishChange( updateMesh );
  97. }
  98. function onWindowResize() {
  99. renderer.setSize( window.innerWidth, window.innerHeight );
  100. camera.aspect = window.innerWidth / window.innerHeight;
  101. camera.updateProjectionMatrix();
  102. render();
  103. }
  104. function getGeometry() {
  105. let geometry;
  106. if ( params.edgeSplit ) {
  107. geometry = modifier.modify(
  108. baseGeometry,
  109. params.cutOffAngle * Math.PI / 180,
  110. params.tryKeepNormals
  111. );
  112. } else {
  113. geometry = baseGeometry;
  114. }
  115. return geometry;
  116. }
  117. function updateMesh() {
  118. if ( mesh !== undefined ) {
  119. mesh.geometry = getGeometry();
  120. let needsUpdate = mesh.material.flatShading === params.smoothShading;
  121. mesh.material.flatShading = params.smoothShading === false;
  122. if ( map !== undefined ) {
  123. needsUpdate = needsUpdate || mesh.material.map !== ( params.showMap ? map : null );
  124. mesh.material.map = params.showMap ? map : null;
  125. }
  126. mesh.material.needsUpdate = needsUpdate;
  127. render();
  128. }
  129. }
  130. function render() {
  131. renderer.render( scene, camera );
  132. }
  133. </script>
  134. </body>
  135. </html>