webgl_modifier_edgesplit.html 4.4 KB

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