webgl_modifier_edgesplit.html 4.6 KB

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