|
@@ -17,9 +17,10 @@
|
|
|
import * as THREE from '../build/three.module.js';
|
|
|
|
|
|
import Stats from './jsm/libs/stats.module.js';
|
|
|
-
|
|
|
import { OrbitControls } from './jsm/controls/OrbitControls.js';
|
|
|
import { GeometryCompressionUtils } from './jsm/utils/GeometryCompressionUtils.js';
|
|
|
+ import { BufferGeometryUtils } from './jsm/utils/BufferGeometryUtils.js';
|
|
|
+ import { TeapotBufferGeometry } from './jsm/geometries/TeapotBufferGeometry.js';
|
|
|
import { GUI } from './jsm/libs/dat.gui.module.js';
|
|
|
|
|
|
var statsEnabled = true;
|
|
@@ -32,19 +33,15 @@
|
|
|
|
|
|
// options
|
|
|
var data = {
|
|
|
+ "model": "Icosahedron",
|
|
|
"wireframe": false,
|
|
|
"texture": false,
|
|
|
"detail": 4,
|
|
|
"rotationSpeed": 0.1,
|
|
|
|
|
|
- "quantizeEncodePos": false,
|
|
|
-
|
|
|
- "defaultEncodeNormal": false,
|
|
|
- "anglesEncodeNormal": false,
|
|
|
- "oct1bytesEncode": false,
|
|
|
- "oct2bytesEncode": false,
|
|
|
-
|
|
|
- "defaultEncodeUV": false,
|
|
|
+ "QuantizePosEncoding": false,
|
|
|
+ "NormEncodingMethods": "None", // for normal encodings
|
|
|
+ "DefaultUVEncoding": false,
|
|
|
|
|
|
"totalGPUMemory": "0 bytes"
|
|
|
};
|
|
@@ -109,36 +106,43 @@
|
|
|
|
|
|
//
|
|
|
|
|
|
- var ballGeom = newGeometry();
|
|
|
+ var geom = newGeometry(data);
|
|
|
|
|
|
- var ballMesh = new THREE.Mesh( ballGeom, meshMaterial );
|
|
|
- var ballLineSegments = new THREE.LineSegments( new THREE.WireframeGeometry( ballGeom ), lineMaterial );
|
|
|
- ballLineSegments.visible = data.wireframe;
|
|
|
+ var mesh = new THREE.Mesh( geom, meshMaterial );
|
|
|
+ var lineSegments = new THREE.LineSegments( new THREE.WireframeGeometry( geom ), lineMaterial );
|
|
|
+ lineSegments.visible = data.wireframe;
|
|
|
|
|
|
- scene.add( ballMesh );
|
|
|
- scene.add( ballLineSegments );
|
|
|
+ scene.add( mesh );
|
|
|
+ scene.add( lineSegments );
|
|
|
|
|
|
//
|
|
|
|
|
|
gui = new GUI();
|
|
|
gui.width = 350;
|
|
|
|
|
|
- function newGeometry() {
|
|
|
+ function newGeometry(data) {
|
|
|
|
|
|
- var geom = new THREE.IcosahedronBufferGeometry( radius, data.detail );
|
|
|
- // var geom = new THREE.CylinderBufferGeometry( 5, 5, 20, 32 );
|
|
|
- // var geom = new THREE.OctahedronBufferGeometry( radius, data.detail );
|
|
|
- // var geom = new THREE.BoxBufferGeometry(radius, radius, radius, data.detail, data.detail, data.detail);
|
|
|
- return geom;
|
|
|
+ switch (data.model) {
|
|
|
+ case "Icosahedron":
|
|
|
+ return new THREE.IcosahedronBufferGeometry( radius, data.detail );
|
|
|
+ case "Cylinder":
|
|
|
+ return new THREE.CylinderBufferGeometry( radius, radius, radius * 2, data.detail * 6 );
|
|
|
+ case "Teapot":
|
|
|
+ return new TeapotBufferGeometry( radius, data.detail * 3, true, true, true, true, true );
|
|
|
+ case "TorusKnot":
|
|
|
+ return new THREE.TorusKnotBufferGeometry( radius, 10, data.detail * 20, data.detail * 6, 3, 4 );
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|
|
|
function generateGeometry() {
|
|
|
|
|
|
+ geom = newGeometry(data);
|
|
|
+
|
|
|
updateGroupGeometry(
|
|
|
- ballMesh,
|
|
|
- ballLineSegments,
|
|
|
- newGeometry(),
|
|
|
+ mesh,
|
|
|
+ lineSegments,
|
|
|
+ geom,
|
|
|
data );
|
|
|
|
|
|
}
|
|
@@ -146,36 +150,34 @@
|
|
|
// updateLineSegments
|
|
|
function updateLineSegments() {
|
|
|
|
|
|
- ballLineSegments.visible = data.wireframe;
|
|
|
+ lineSegments.visible = data.wireframe;
|
|
|
|
|
|
}
|
|
|
|
|
|
var folder = gui.addFolder( 'Scene' );
|
|
|
- folder.open();
|
|
|
+ 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', 0, 6, 1 ).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.add( data, 'quantizeEncodePos', false ).onChange( generateGeometry );
|
|
|
|
|
|
folder = gui.addFolder( 'Normal Compression' );
|
|
|
+ folder.add( data, 'NormEncodingMethods', ["None", "DEFAULT", "OCT1Byte", "OCT2Byte", "ANGLES"] ).onChange( generateGeometry );
|
|
|
folder.open();
|
|
|
- folder.add( data, 'defaultEncodeNormal', false ).onChange( generateGeometry );
|
|
|
- folder.add( data, 'anglesEncodeNormal', false ).onChange( generateGeometry );
|
|
|
- folder.add( data, 'oct1bytesEncode', false ).onChange( generateGeometry );
|
|
|
- folder.add( data, 'oct2bytesEncode', false ).onChange( generateGeometry );
|
|
|
|
|
|
folder = gui.addFolder( 'UV Compression' );
|
|
|
+ folder.add( data, 'DefaultUVEncoding', false ).onChange( generateGeometry );
|
|
|
folder.open();
|
|
|
- folder.add( data, 'defaultEncodeUV', false ).onChange( generateGeometry );
|
|
|
|
|
|
folder = gui.addFolder( 'Memory Info' );
|
|
|
folder.open();
|
|
|
memoryDisplay = folder.add( data, 'totalGPUMemory', "0 bytes" );
|
|
|
- computeGPUMemory( ballMesh );
|
|
|
+ computeGPUMemory( mesh );
|
|
|
|
|
|
//
|
|
|
|
|
@@ -237,10 +239,11 @@
|
|
|
|
|
|
geometry = new THREE.BufferGeometry().fromGeometry( geometry );
|
|
|
|
|
|
- console.warn( 'THREE.GeometryBrowser: Converted Geometry to BufferGeometry.' );
|
|
|
+ console.log( 'THREE.GeometryCompression: Converted Geometry to BufferGeometry.' );
|
|
|
|
|
|
}
|
|
|
|
|
|
+ // dispose first
|
|
|
lineSegments.geometry.dispose();
|
|
|
mesh.geometry.dispose();
|
|
|
|
|
@@ -249,44 +252,19 @@
|
|
|
mesh.material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x111111 } );
|
|
|
mesh.material.map = data.texture ? texture : null;
|
|
|
|
|
|
- var normalEncode = "";
|
|
|
-
|
|
|
- if ( data.oct1bytesEncode ) {
|
|
|
-
|
|
|
- /**
|
|
|
- * It is not recommended to use 1-byte octahedron normals encoding unless you want to extremely reduce the memory usage
|
|
|
- * As it makes vertex data not aligned to a 4 byte boundary which may harm some WebGL implementations and sometimes the normal distortion is visible
|
|
|
- * Please refer to @zeux 's comments in https://github.com/mrdoob/three.js/pull/18208
|
|
|
- */
|
|
|
- normalEncode = "OCT1Byte";
|
|
|
-
|
|
|
- } else if ( data.oct2bytesEncode ) {
|
|
|
+ if ( data["QuantizePosEncoding"] ) {
|
|
|
|
|
|
- normalEncode = "OCT2Byte";
|
|
|
-
|
|
|
- } else if ( data.anglesEncodeNormal ) {
|
|
|
-
|
|
|
- normalEncode = "ANGLES";
|
|
|
-
|
|
|
- } else if ( data.defaultEncodeNormal ) {
|
|
|
-
|
|
|
- normalEncode = "DEFAULT";
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- if ( normalEncode != "" ) {
|
|
|
-
|
|
|
- GeometryCompressionUtils.compressNormals( mesh, normalEncode );
|
|
|
+ GeometryCompressionUtils.compressPositions( mesh );
|
|
|
|
|
|
}
|
|
|
|
|
|
- if ( data.quantizeEncodePos ) {
|
|
|
+ if ( data["NormEncodingMethods"] !== "None" ) {
|
|
|
|
|
|
- GeometryCompressionUtils.compressPositions( mesh );
|
|
|
+ GeometryCompressionUtils.compressNormals( mesh, data["NormEncodingMethods"] );
|
|
|
|
|
|
}
|
|
|
|
|
|
- if ( data.defaultEncodeUV ) {
|
|
|
+ if ( data["DefaultUVEncoding"] ) {
|
|
|
|
|
|
GeometryCompressionUtils.compressUvs( mesh );
|
|
|
|
|
@@ -299,10 +277,8 @@
|
|
|
|
|
|
function computeGPUMemory( mesh ) {
|
|
|
|
|
|
- let posBytes = mesh.geometry.attributes.position.bytes || mesh.geometry.attributes.position.array.length * 4;
|
|
|
- let normBytes = mesh.geometry.attributes.normal.bytes || mesh.geometry.attributes.normal.array.length * 4;
|
|
|
- let uvBytes = mesh.geometry.attributes.uv.bytes || mesh.geometry.attributes.uv.array.length * 4;
|
|
|
- memoryDisplay.setValue( posBytes + normBytes + uvBytes + " bytes" );
|
|
|
+ // Use BufferGeometryUtils to do memory calculation
|
|
|
+ memoryDisplay.setValue( BufferGeometryUtils.estimateBytesUsed(mesh.geometry) + " bytes");
|
|
|
|
|
|
}
|
|
|
|