webgl_modifier_edgesplit.html 4.9 KB

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