123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - geometry - normals</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>
- <div id="container"></div>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - geometry - normals</a>
- </div>
- <script type="module">
- 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 { OrbitControls } from './jsm/controls/OrbitControls.js';
- import { VertexNormalsHelper } from './jsm/helpers/VertexNormalsHelper.js';
- var container, stats, gui;
- var camera, scene, renderer;
- var mesh, geometry;
- var geometries = [
- new THREE.BoxBufferGeometry( 200, 200, 200, 2, 2, 2 ),
- new THREE.CircleBufferGeometry( 200, 32 ),
- new THREE.CylinderBufferGeometry( 75, 75, 200, 8, 8 ),
- new THREE.IcosahedronBufferGeometry( 100, 1 ),
- new THREE.OctahedronBufferGeometry( 200, 0 ),
- new THREE.PlaneBufferGeometry( 200, 200, 4, 4 ),
- new THREE.RingBufferGeometry( 32, 64, 16 ),
- new THREE.SphereBufferGeometry( 100, 12, 12 ),
- new THREE.TorusBufferGeometry( 64, 16, 12, 12 ),
- new THREE.TorusKnotBufferGeometry( 64, 16 )
- ];
- var options = {
- Geometry: 0
- };
- var material = new THREE.MeshBasicMaterial( { color: 0xfefefe, wireframe: true, opacity: 0.5 } );
- init();
- animate();
- function addMesh() {
- if ( mesh !== undefined ) {
- scene.remove( mesh );
- geometry.dispose();
- }
- geometry = geometries[ options.Geometry ];
- // scale geometry to a uniform size
- geometry.computeBoundingSphere();
- var scaleFactor = 160 / geometry.boundingSphere.radius;
- geometry.scale( scaleFactor, scaleFactor, scaleFactor );
- mesh = new THREE.Mesh( geometry, material );
- scene.add( mesh );
- var vertexNormalsHelper = new VertexNormalsHelper( mesh, 10 );
- mesh.add( vertexNormalsHelper );
- }
- function init() {
- container = document.getElementById( 'container' );
- camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
- camera.position.z = 500;
- scene = new THREE.Scene();
- addMesh();
- //
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- container.appendChild( renderer.domElement );
- //
- stats = new Stats();
- container.appendChild( stats.dom );
- //
- var geometries = {
- BoxBufferGeometry: 0,
- CircleBufferGeometry: 1,
- CylinderBufferGeometry: 2,
- IcosahedronBufferGeometry: 3,
- OctahedronBufferGeometry: 4,
- PlaneBufferGeometry: 5,
- RingBufferGeometry: 6,
- SphereBufferGeometry: 7,
- TorusBufferGeometry: 8,
- TorusKnotBufferGeometry: 9
- };
- gui = new GUI( { width: 350 } );
- gui.add( options, 'Geometry', geometries ).onChange( function () {
- addMesh();
- } );
- //
- var controls = new OrbitControls( camera, renderer.domElement );
- //
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- //
- function animate() {
- requestAnimationFrame( animate );
- render();
- stats.update();
- }
- function render() {
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|