webgl_modifier_edgesplit.html 4.3 KB

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