You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

296 lines
7.6 KiB

2 years ago
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - buffergeometry - compression</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="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - BufferGeometry Compression<br />
Octahedron and Quantization encoding methods from Tarek Sherif
</div>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import Stats from 'three/addons/libs/stats.module.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import * as GeometryCompressionUtils from 'three/addons/utils/GeometryCompressionUtils.js';
import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js';
import { TeapotGeometry } from 'three/addons/geometries/TeapotGeometry.js';
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
const statsEnabled = true;
let container, stats, gui;
let camera, scene, renderer, controls;
const lights = [];
// options
const data = {
'model': 'Icosahedron',
'wireframe': false,
'texture': false,
'detail': 4,
'rotationSpeed': 0.1,
'QuantizePosEncoding': false,
'NormEncodingMethods': 'None', // for normal encodings
'DefaultUVEncoding': false,
'totalGPUMemory': '0 bytes'
};
let memoryDisplay;
// geometry params
const radius = 100;
// materials
const lineMaterial = new THREE.LineBasicMaterial( { color: 0xaaaaaa, transparent: true, opacity: 0.8 } );
const meshMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x111111 } );
// texture
const texture = new THREE.TextureLoader().load( 'textures/uv_grid_opengl.jpg' );
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
//
init();
animate();
function init() {
//
container = document.createElement( 'div' );
document.body.appendChild( container );
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
//
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 10000000 );
camera.position.x = 2 * radius;
camera.position.y = 2 * radius;
camera.position.z = 2 * radius;
controls = new OrbitControls( camera, renderer.domElement );
//
lights[ 0 ] = new THREE.PointLight( 0xffffff, 1, 0 );
lights[ 1 ] = new THREE.PointLight( 0xffffff, 1, 0 );
lights[ 2 ] = new THREE.PointLight( 0xffffff, 1, 0 );
lights[ 0 ].position.set( 0, 2 * radius, 0 );
lights[ 1 ].position.set( 2 * radius, - 2 * radius, 2 * radius );
lights[ 2 ].position.set( - 2 * radius, - 2 * radius, - 2 * radius );
scene.add( lights[ 0 ] );
scene.add( lights[ 1 ] );
scene.add( lights[ 2 ] );
//
scene.add( new THREE.AxesHelper( radius * 5 ) );
//
let geom = newGeometry( data );
const mesh = new THREE.Mesh( geom, meshMaterial );
const lineSegments = new THREE.LineSegments( new THREE.WireframeGeometry( geom ), lineMaterial );
lineSegments.visible = data.wireframe;
scene.add( mesh );
scene.add( lineSegments );
//
gui = new GUI();
gui.width = 350;
function newGeometry( data ) {
switch ( data.model ) {
case 'Icosahedron':
return new THREE.IcosahedronGeometry( radius, data.detail );
case 'Cylinder':
return new THREE.CylinderGeometry( radius, radius, radius * 2, data.detail * 6 );
case 'Teapot':
return new TeapotGeometry( radius, data.detail * 3, true, true, true, true, true );
case 'TorusKnot':
return new THREE.TorusKnotGeometry( radius, 10, data.detail * 20, data.detail * 6, 3, 4 );
}
}
function generateGeometry() {
geom = newGeometry( data );
updateGroupGeometry(
mesh,
lineSegments,
geom,
data );
}
// updateLineSegments
function updateLineSegments() {
lineSegments.visible = data.wireframe;
}
let folder = gui.addFolder( 'Scene' );
folder.add( data, 'model', [ 'Icosahedron', 'Cylinder', 'TorusKnot', 'Teapot' ] ).onChange( generateGeometry );
folder.add( data, 'wireframe', false ).onChange( updateLineSegments );
folder.add( data, 'texture', false ).onChange( generateGeometry );
folder.add( data, 'detail', 1, 8, 1 ).onChange( generateGeometry );
folder.add( data, 'rotationSpeed', 0, 0.5, 0.1 );
folder.open();
folder = gui.addFolder( 'Position Compression' );
folder.add( data, 'QuantizePosEncoding', false ).onChange( generateGeometry );
folder.open();
folder = gui.addFolder( 'Normal Compression' );
folder.add( data, 'NormEncodingMethods', [ 'None', 'DEFAULT', 'OCT1Byte', 'OCT2Byte', 'ANGLES' ] ).onChange( generateGeometry );
folder.open();
folder = gui.addFolder( 'UV Compression' );
folder.add( data, 'DefaultUVEncoding', false ).onChange( generateGeometry );
folder.open();
folder = gui.addFolder( 'Memory Info' );
folder.open();
memoryDisplay = folder.add( data, 'totalGPUMemory', '0 bytes' );
computeGPUMemory( mesh );
//
if ( statsEnabled ) {
stats = new Stats();
container.appendChild( stats.dom );
}
window.addEventListener( 'resize', onWindowResize );
}
//
function onWindowResize() {
renderer.setSize( window.innerWidth, window.innerHeight );
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
}
//
function updateLightsPossition() {
lights.forEach( light => {
const direction = light.position.clone();
direction.applyAxisAngle( new THREE.Vector3( 1, 1, 0 ), data.rotationSpeed / 180 * Math.PI );
light.position.add( direction.sub( light.position ) );
} );
}
//
function animate() {
requestAnimationFrame( animate );
controls.update();
updateLightsPossition();
renderer.render( scene, camera );
if ( statsEnabled ) stats.update();
}
//
function updateGroupGeometry( mesh, lineSegments, geometry, data ) {
// dispose first
lineSegments.geometry.dispose();
mesh.geometry.dispose();
lineSegments.geometry = new THREE.WireframeGeometry( geometry );
mesh.geometry = geometry;
mesh.material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x111111 } );
mesh.material.map = data.texture ? texture : null;
if ( data[ 'QuantizePosEncoding' ] ) {
GeometryCompressionUtils.compressPositions( mesh );
}
if ( data[ 'NormEncodingMethods' ] !== 'None' ) {
GeometryCompressionUtils.compressNormals( mesh, data[ 'NormEncodingMethods' ] );
}
if ( data[ 'DefaultUVEncoding' ] ) {
GeometryCompressionUtils.compressUvs( mesh );
}
computeGPUMemory( mesh );
}
function computeGPUMemory( mesh ) {
// Use BufferGeometryUtils to do memory calculation
memoryDisplay.setValue( BufferGeometryUtils.estimateBytesUsed( mesh.geometry ) + ' bytes' );
}
</script>
</body>
</html>