webgl_modifier_subdivision.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - modifier - Subdivisions using Loop Subdivision Scheme</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. <style>
  9. body {
  10. background-color: #f0f0f0;
  11. color: #444;
  12. }
  13. a {
  14. color: #08f;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="info">
  20. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - subdivision modifier<br/ >
  21. Original Geometry: <span id="original-vertex-count"></span> vertices and <span id="original-face-count"></span> faces<br/ >
  22. Smooth Geometry: <span id="smooth-vertex-count"></span> vertices and <span id="smooth-face-count"></span> faces<br/ >
  23. </div>
  24. <script type="module">
  25. import * as THREE from '../build/three.module.js';
  26. import Stats from './jsm/libs/stats.module.js';
  27. import { GUI } from './jsm/libs/dat.gui.module.js';
  28. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  29. import { SubdivisionModifier } from './jsm/modifiers/SubdivisionModifier.js';
  30. var camera, scene, renderer, stats, smoothMesh, wireframe;
  31. var smoothMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, flatShading: true, vertexColors: THREE.VertexColors } );
  32. var wireframeMaterial = new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true, opacity: 0.15, transparent: true } );
  33. var faceIndices = [ 'a', 'b', 'c' ];
  34. var params = {
  35. subdivisions: 2
  36. };
  37. init();
  38. function init() {
  39. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 500 );
  40. camera.position.z = 100;
  41. scene = new THREE.Scene();
  42. scene.background = new THREE.Color( 0xf0f0f0 );
  43. var light = new THREE.PointLight( 0xffffff, 1.5 );
  44. light.position.set( 1000, 1000, 2000 );
  45. scene.add( light );
  46. var loader = new THREE.BufferGeometryLoader();
  47. loader.load( 'models/json/WaltHeadLo_buffergeometry.json', function ( bufferGeometry ) {
  48. // converting to legacy THREE.Geometry because SubdivisionModifier only returns THREE.Geometry
  49. var geometry = new THREE.Geometry().fromBufferGeometry( bufferGeometry );
  50. geometry.mergeVertices();
  51. var material = new THREE.MeshBasicMaterial( { color: 0xcccccc, wireframe: true } );
  52. var mesh = new THREE.Mesh( bufferGeometry, material );
  53. scene.add( mesh );
  54. var gui = new GUI();
  55. gui.add( params, 'subdivisions', 0, 3 ).step( 1 ).onChange( function ( subdivisions ) {
  56. subdivide( geometry, subdivisions );
  57. } );
  58. //
  59. subdivide( geometry, params.subdivisions );
  60. animate();
  61. } );
  62. renderer = new THREE.WebGLRenderer( { antialias: true } );
  63. renderer.setPixelRatio( window.devicePixelRatio );
  64. renderer.setSize( window.innerWidth, window.innerHeight );
  65. document.body.appendChild( renderer.domElement );
  66. stats = new Stats();
  67. document.body.appendChild( stats.dom );
  68. //
  69. var controls = new OrbitControls( camera, renderer.domElement );
  70. controls.minDistance = 50;
  71. controls.maxDistance = 400;
  72. window.addEventListener( 'resize', onWindowResize, false );
  73. //
  74. smoothMesh = new THREE.Mesh( undefined, smoothMaterial );
  75. wireframe = new THREE.Mesh( undefined, wireframeMaterial );
  76. scene.add( smoothMesh, wireframe );
  77. }
  78. function subdivide( geometry, subdivisions ) {
  79. var modifier = new SubdivisionModifier( subdivisions );
  80. var smoothGeometry = modifier.modify( geometry );
  81. // colorify faces
  82. for ( var i = 0; i < smoothGeometry.faces.length; i ++ ) {
  83. var face = smoothGeometry.faces[ i ];
  84. for ( var j = 0; j < 3; j ++ ) {
  85. var vertexIndex = face[ faceIndices[ j ] ];
  86. var vertex = smoothGeometry.vertices[ vertexIndex ];
  87. var hue = ( vertex.y / 200 ) + 0.5;
  88. var color = new THREE.Color().setHSL( hue, 1, 0.5 );
  89. face.vertexColors[ j ] = color;
  90. }
  91. }
  92. // convert to THREE.BufferGeometry
  93. if ( smoothMesh.geometry ) smoothMesh.geometry.dispose();
  94. smoothMesh.geometry = new THREE.BufferGeometry().fromGeometry( smoothGeometry );
  95. wireframe.geometry = smoothMesh.geometry;
  96. //
  97. updateUI( geometry, smoothGeometry );
  98. }
  99. function updateUI( originalGeometry, smoothGeometry ) {
  100. document.getElementById( 'original-vertex-count' ).textContent = originalGeometry.vertices.length;
  101. document.getElementById( 'original-face-count' ).textContent = originalGeometry.faces.length;
  102. document.getElementById( 'smooth-vertex-count' ).textContent = smoothGeometry.vertices.length;
  103. document.getElementById( 'smooth-face-count' ).textContent = smoothGeometry.faces.length;
  104. }
  105. function onWindowResize() {
  106. camera.aspect = window.innerWidth / window.innerHeight;
  107. camera.updateProjectionMatrix();
  108. renderer.setSize( window.innerWidth, window.innerHeight );
  109. }
  110. //
  111. function animate() {
  112. requestAnimationFrame( animate );
  113. render();
  114. stats.update();
  115. }
  116. function render() {
  117. renderer.render( scene, camera );
  118. }
  119. </script>
  120. </body>
  121. </html>