|
@@ -5,39 +5,24 @@
|
|
<meta charset="utf-8">
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
|
|
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
|
|
<link type="text/css" rel="stylesheet" href="main.css">
|
|
<link type="text/css" rel="stylesheet" href="main.css">
|
|
- <style>
|
|
|
|
- body {
|
|
|
|
- background-color: #f0f0f0;
|
|
|
|
- color: #444;
|
|
|
|
- }
|
|
|
|
- a {
|
|
|
|
- color: #08f;
|
|
|
|
- }
|
|
|
|
- </style>
|
|
|
|
</head>
|
|
</head>
|
|
<body>
|
|
<body>
|
|
<div id="info">
|
|
<div id="info">
|
|
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - subdivision modifier<br/ >
|
|
|
|
- Original Geometry: <span id="original-vertex-count"></span> vertices and <span id="original-face-count"></span> faces<br/ >
|
|
|
|
- Smooth Geometry: <span id="smooth-vertex-count"></span> vertices and <span id="smooth-face-count"></span> faces<br/ >
|
|
|
|
|
|
+ <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - subdivision modifier
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<script type="module">
|
|
<script type="module">
|
|
|
|
|
|
import * as THREE from '../build/three.module.js';
|
|
import * as THREE from '../build/three.module.js';
|
|
|
|
|
|
- import Stats from './jsm/libs/stats.module.js';
|
|
|
|
-
|
|
|
|
import { GUI } from './jsm/libs/dat.gui.module.js';
|
|
import { GUI } from './jsm/libs/dat.gui.module.js';
|
|
import { OrbitControls } from './jsm/controls/OrbitControls.js';
|
|
import { OrbitControls } from './jsm/controls/OrbitControls.js';
|
|
import { SubdivisionModifier } from './jsm/modifiers/SubdivisionModifier.js';
|
|
import { SubdivisionModifier } from './jsm/modifiers/SubdivisionModifier.js';
|
|
|
|
|
|
- let camera, scene, renderer, stats, smoothMesh, wireframe;
|
|
|
|
-
|
|
|
|
- const smoothMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, flatShading: true, vertexColors: true } );
|
|
|
|
- const wireframeMaterial = new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true, opacity: 0.15, transparent: true } );
|
|
|
|
|
|
+ let camera, scene, renderer, smoothMesh;
|
|
|
|
|
|
- const faceIndices = [ 'a', 'b', 'c' ];
|
|
|
|
|
|
+ const material = new THREE.MeshPhongMaterial( { color: 0xffffff, flatShading: true } );
|
|
|
|
+ const wireframeMaterial = new THREE.LineBasicMaterial();
|
|
|
|
|
|
const params = {
|
|
const params = {
|
|
subdivisions: 2
|
|
subdivisions: 2
|
|
@@ -51,36 +36,38 @@
|
|
camera.position.z = 100;
|
|
camera.position.z = 100;
|
|
|
|
|
|
scene = new THREE.Scene();
|
|
scene = new THREE.Scene();
|
|
- scene.background = new THREE.Color( 0xf0f0f0 );
|
|
|
|
|
|
|
|
- const light = new THREE.PointLight( 0xffffff, 1.5 );
|
|
|
|
- light.position.set( 1000, 1000, 2000 );
|
|
|
|
|
|
+ scene.add( new THREE.AmbientLight( 0xffffff, 0.2 ) );
|
|
|
|
+
|
|
|
|
+ const light = new THREE.PointLight( 0xffffff, 0.8 );
|
|
|
|
+ light.position.set( 0, 50, 100 );
|
|
scene.add( light );
|
|
scene.add( light );
|
|
|
|
|
|
const loader = new THREE.BufferGeometryLoader();
|
|
const loader = new THREE.BufferGeometryLoader();
|
|
- loader.load( 'models/json/WaltHeadLo_buffergeometry.json', function ( bufferGeometry ) {
|
|
|
|
-
|
|
|
|
- // converting to legacy THREE.Geometry because SubdivisionModifier only returns THREE.Geometry
|
|
|
|
|
|
+ loader.load( 'models/json/WaltHeadLo_buffergeometry.json', function ( geometry ) {
|
|
|
|
|
|
- const geometry = new THREE.Geometry().fromBufferGeometry( bufferGeometry );
|
|
|
|
- geometry.mergeVertices();
|
|
|
|
-
|
|
|
|
- const material = new THREE.MeshBasicMaterial( { color: 0xcccccc, wireframe: true } );
|
|
|
|
- const mesh = new THREE.Mesh( bufferGeometry, material );
|
|
|
|
|
|
+ const mesh = new THREE.Mesh( geometry, material );
|
|
|
|
+ mesh.position.x = 32;
|
|
scene.add( mesh );
|
|
scene.add( mesh );
|
|
|
|
|
|
|
|
+ const wireframe = new THREE.WireframeGeometry( geometry );
|
|
|
|
+ const line = new THREE.LineSegments( wireframe, wireframeMaterial );
|
|
|
|
+ mesh.add( line );
|
|
|
|
+
|
|
const gui = new GUI();
|
|
const gui = new GUI();
|
|
|
|
|
|
gui.add( params, 'subdivisions', 0, 3 ).step( 1 ).onChange( function ( subdivisions ) {
|
|
gui.add( params, 'subdivisions', 0, 3 ).step( 1 ).onChange( function ( subdivisions ) {
|
|
|
|
|
|
subdivide( geometry, subdivisions );
|
|
subdivide( geometry, subdivisions );
|
|
|
|
+ render();
|
|
|
|
|
|
} );
|
|
} );
|
|
|
|
|
|
//
|
|
//
|
|
|
|
|
|
subdivide( geometry, params.subdivisions );
|
|
subdivide( geometry, params.subdivisions );
|
|
- animate();
|
|
|
|
|
|
+
|
|
|
|
+ render();
|
|
|
|
|
|
} );
|
|
} );
|
|
|
|
|
|
@@ -89,23 +76,15 @@
|
|
renderer.setSize( window.innerWidth, window.innerHeight );
|
|
renderer.setSize( window.innerWidth, window.innerHeight );
|
|
document.body.appendChild( renderer.domElement );
|
|
document.body.appendChild( renderer.domElement );
|
|
|
|
|
|
- stats = new Stats();
|
|
|
|
- document.body.appendChild( stats.dom );
|
|
|
|
-
|
|
|
|
//
|
|
//
|
|
|
|
|
|
const controls = new OrbitControls( camera, renderer.domElement );
|
|
const controls = new OrbitControls( camera, renderer.domElement );
|
|
|
|
+ controls.addEventListener( 'change', render );
|
|
controls.minDistance = 50;
|
|
controls.minDistance = 50;
|
|
controls.maxDistance = 400;
|
|
controls.maxDistance = 400;
|
|
|
|
|
|
window.addEventListener( 'resize', onWindowResize, false );
|
|
window.addEventListener( 'resize', onWindowResize, false );
|
|
|
|
|
|
- //
|
|
|
|
-
|
|
|
|
- smoothMesh = new THREE.Mesh( undefined, smoothMaterial );
|
|
|
|
- wireframe = new THREE.Mesh( undefined, wireframeMaterial );
|
|
|
|
- scene.add( smoothMesh, wireframe );
|
|
|
|
-
|
|
|
|
}
|
|
}
|
|
|
|
|
|
function subdivide( geometry, subdivisions ) {
|
|
function subdivide( geometry, subdivisions ) {
|
|
@@ -114,45 +93,23 @@
|
|
|
|
|
|
const smoothGeometry = modifier.modify( geometry );
|
|
const smoothGeometry = modifier.modify( geometry );
|
|
|
|
|
|
- // colorify faces
|
|
|
|
-
|
|
|
|
- for ( let i = 0; i < smoothGeometry.faces.length; i ++ ) {
|
|
|
|
-
|
|
|
|
- const face = smoothGeometry.faces[ i ];
|
|
|
|
|
|
+ if ( smoothMesh !== undefined ) {
|
|
|
|
|
|
- for ( let j = 0; j < 3; j ++ ) {
|
|
|
|
|
|
+ smoothMesh.geometry.dispose();
|
|
|
|
+ smoothMesh.children[ 0 ].geometry.dispose();
|
|
|
|
|
|
- const vertexIndex = face[ faceIndices[ j ] ];
|
|
|
|
- const vertex = smoothGeometry.vertices[ vertexIndex ];
|
|
|
|
-
|
|
|
|
- const hue = ( vertex.y / 200 ) + 0.5;
|
|
|
|
- const color = new THREE.Color().setHSL( hue, 1, 0.5 );
|
|
|
|
- face.vertexColors[ j ] = color;
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
|
|
+ scene.remove( smoothMesh );
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- // convert to THREE.BufferGeometry
|
|
|
|
-
|
|
|
|
- if ( smoothMesh.geometry ) smoothMesh.geometry.dispose();
|
|
|
|
-
|
|
|
|
- smoothMesh.geometry = new THREE.BufferGeometry().fromGeometry( smoothGeometry );
|
|
|
|
- wireframe.geometry = smoothMesh.geometry;
|
|
|
|
|
|
+ smoothMesh = new THREE.Mesh( smoothGeometry, material );
|
|
|
|
+ smoothMesh.position.x = - 32;
|
|
|
|
|
|
- //
|
|
|
|
|
|
+ const wireframe = new THREE.WireframeGeometry( smoothGeometry );
|
|
|
|
+ const line = new THREE.LineSegments( wireframe, wireframeMaterial );
|
|
|
|
+ smoothMesh.add( line );
|
|
|
|
|
|
- updateUI( geometry, smoothGeometry );
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- function updateUI( originalGeometry, smoothGeometry ) {
|
|
|
|
-
|
|
|
|
- document.getElementById( 'original-vertex-count' ).textContent = originalGeometry.vertices.length;
|
|
|
|
- document.getElementById( 'original-face-count' ).textContent = originalGeometry.faces.length;
|
|
|
|
-
|
|
|
|
- document.getElementById( 'smooth-vertex-count' ).textContent = smoothGeometry.vertices.length;
|
|
|
|
- document.getElementById( 'smooth-face-count' ).textContent = smoothGeometry.faces.length;
|
|
|
|
|
|
+ scene.add( smoothMesh );
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
@@ -163,16 +120,7 @@
|
|
|
|
|
|
renderer.setSize( window.innerWidth, window.innerHeight );
|
|
renderer.setSize( window.innerWidth, window.innerHeight );
|
|
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- //
|
|
|
|
-
|
|
|
|
- function animate() {
|
|
|
|
-
|
|
|
|
- requestAnimationFrame( animate );
|
|
|
|
-
|
|
|
|
render();
|
|
render();
|
|
- stats.update();
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|