123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - modifier - Edge Split modifier</title>
- <meta charset="utf-8">
- <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">
- </head>
- <body>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.module.js",
- "three/addons/": "./jsm/"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three';
- import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
- import { OBJLoader } from 'three/addons/loaders/OBJLoader.js';
- import { EdgeSplitModifier } from 'three/addons/modifiers/EdgeSplitModifier.js';
- import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js';
- import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
- let renderer, scene, camera;
- let modifier, mesh, baseGeometry;
- let map;
- const params = {
- smoothShading: true,
- edgeSplit: true,
- cutOffAngle: 20,
- showMap: false,
- tryKeepNormals: true,
- };
- init();
- function init() {
- const info = document.createElement( 'div' );
- info.style.position = 'absolute';
- info.style.top = '10px';
- info.style.width = '100%';
- info.style.textAlign = 'center';
- info.innerHTML = '<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Edge Split modifier';
- document.body.appendChild( info );
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- document.body.appendChild( renderer.domElement );
- scene = new THREE.Scene();
- camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight );
- const controls = new OrbitControls( camera, renderer.domElement );
- controls.addEventListener( 'change', render ); // use if there is no animation loop
- controls.enableDamping = true;
- controls.dampingFactor = 0.25;
- controls.rotateSpeed = 0.35;
- controls.minZoom = 1;
- camera.position.set( 0, 0, 4 );
- scene.add( new THREE.HemisphereLight( 0xffffff, 0x444444, 3 ) );
- new OBJLoader().load(
- './models/obj/cerberus/Cerberus.obj',
- function ( group ) {
- const cerberus = group.children[ 0 ];
- const modelGeometry = cerberus.geometry;
- modifier = new EdgeSplitModifier();
- baseGeometry = BufferGeometryUtils.mergeVertices( modelGeometry );
- mesh = new THREE.Mesh( getGeometry(), new THREE.MeshStandardMaterial() );
- mesh.material.flatShading = ! params.smoothShading;
- mesh.rotateY( - Math.PI / 2 );
- mesh.scale.set( 3.5, 3.5, 3.5 );
- mesh.translateZ( 1.5 );
- scene.add( mesh );
- if ( map !== undefined && params.showMap ) {
- mesh.material.map = map;
- mesh.material.needsUpdate = true;
- }
- render();
- }
- );
- window.addEventListener( 'resize', onWindowResize );
- new THREE.TextureLoader().load( './models/obj/cerberus/Cerberus_A.jpg', function ( texture ) {
- map = texture;
- map.colorSpace = THREE.SRGBColorSpace;
- if ( mesh !== undefined && params.showMap ) {
- mesh.material.map = map;
- mesh.material.needsUpdate = true;
- }
- } );
- const gui = new GUI( { title: 'Edge split modifier parameters' } );
- gui.add( params, 'showMap' ).onFinishChange( updateMesh );
- gui.add( params, 'smoothShading' ).onFinishChange( updateMesh );
- gui.add( params, 'edgeSplit' ).onFinishChange( updateMesh );
- gui.add( params, 'cutOffAngle' ).min( 0 ).max( 180 ).onFinishChange( updateMesh );
- gui.add( params, 'tryKeepNormals' ).onFinishChange( updateMesh );
- }
- function onWindowResize() {
- renderer.setSize( window.innerWidth, window.innerHeight );
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- render();
- }
- function getGeometry() {
- let geometry;
- if ( params.edgeSplit ) {
- geometry = modifier.modify(
- baseGeometry,
- params.cutOffAngle * Math.PI / 180,
- params.tryKeepNormals
- );
- } else {
- geometry = baseGeometry;
- }
- return geometry;
- }
- function updateMesh() {
- if ( mesh !== undefined ) {
- mesh.geometry = getGeometry();
- let needsUpdate = mesh.material.flatShading === params.smoothShading;
- mesh.material.flatShading = params.smoothShading === false;
- if ( map !== undefined ) {
- needsUpdate = needsUpdate || mesh.material.map !== ( params.showMap ? map : null );
- mesh.material.map = params.showMap ? map : null;
- }
- mesh.material.needsUpdate = needsUpdate;
- render();
- }
- }
- function render() {
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|