From c8775e01d6a485941cbd23e83e2acfdd28a90a9e Mon Sep 17 00:00:00 2001 From: Don McCurdy Date: Thu, 6 Jan 2022 17:51:53 -0800 Subject: [PATCH 1/2] Examples: Inline small utility methods for examples/js. --- examples/jsm/loaders/GLTFLoader.js | 95 +-------------------- examples/jsm/modifiers/EdgeSplitModifier.js | 10 +-- examples/jsm/modifiers/SimplifyModifier.js | 14 +-- examples/jsm/utils/BufferGeometryUtils.js | 8 +- utils/build/rollup.examples.config.js | 16 +++- 5 files changed, 23 insertions(+), 120 deletions(-) diff --git a/examples/jsm/loaders/GLTFLoader.js b/examples/jsm/loaders/GLTFLoader.js index 82e7b012bff6c0..a503ba5d19da7c 100644 --- a/examples/jsm/loaders/GLTFLoader.js +++ b/examples/jsm/loaders/GLTFLoader.js @@ -63,6 +63,7 @@ import { VectorKeyframeTrack, sRGBEncoding } from '../../../build/three.module.js'; +import { toTrianglesDrawMode } from '../utils/BufferGeometryUtils.js'; class GLTFLoader extends Loader { @@ -4251,98 +4252,4 @@ function addPrimitiveAttributes( geometry, primitiveDef, parser ) { } -/** - * @param {BufferGeometry} geometry - * @param {Number} drawMode - * @return {BufferGeometry} - */ -function toTrianglesDrawMode( geometry, drawMode ) { - - let index = geometry.getIndex(); - - // generate index if not present - - if ( index === null ) { - - const indices = []; - - const position = geometry.getAttribute( 'position' ); - - if ( position !== undefined ) { - - for ( let i = 0; i < position.count; i ++ ) { - - indices.push( i ); - - } - - geometry.setIndex( indices ); - index = geometry.getIndex(); - - } else { - - console.error( 'THREE.GLTFLoader.toTrianglesDrawMode(): Undefined position attribute. Processing not possible.' ); - return geometry; - - } - - } - - // - - const numberOfTriangles = index.count - 2; - const newIndices = []; - - if ( drawMode === TriangleFanDrawMode ) { - - // gl.TRIANGLE_FAN - - for ( let i = 1; i <= numberOfTriangles; i ++ ) { - - newIndices.push( index.getX( 0 ) ); - newIndices.push( index.getX( i ) ); - newIndices.push( index.getX( i + 1 ) ); - - } - - } else { - - // gl.TRIANGLE_STRIP - - for ( let i = 0; i < numberOfTriangles; i ++ ) { - - if ( i % 2 === 0 ) { - - newIndices.push( index.getX( i ) ); - newIndices.push( index.getX( i + 1 ) ); - newIndices.push( index.getX( i + 2 ) ); - - - } else { - - newIndices.push( index.getX( i + 2 ) ); - newIndices.push( index.getX( i + 1 ) ); - newIndices.push( index.getX( i ) ); - - } - - } - - } - - if ( ( newIndices.length / 3 ) !== numberOfTriangles ) { - - console.error( 'THREE.GLTFLoader.toTrianglesDrawMode(): Unable to generate correct amount of triangles.' ); - - } - - // build final geometry - - const newGeometry = geometry.clone(); - newGeometry.setIndex( newIndices ); - - return newGeometry; - -} - export { GLTFLoader }; diff --git a/examples/jsm/modifiers/EdgeSplitModifier.js b/examples/jsm/modifiers/EdgeSplitModifier.js index c0a129c21ab3d6..4f325c598cf210 100644 --- a/examples/jsm/modifiers/EdgeSplitModifier.js +++ b/examples/jsm/modifiers/EdgeSplitModifier.js @@ -3,7 +3,7 @@ import { BufferGeometry, Vector3 } from '../../../build/three.module.js'; -import * as BufferGeometryUtils from '../utils/BufferGeometryUtils.js'; +import { mergeVertices } from '../utils/BufferGeometryUtils.js'; const _A = new Vector3(); const _B = new Vector3(); @@ -181,13 +181,7 @@ class EdgeSplitModifier { if ( geometry.index == null ) { - if ( BufferGeometryUtils === undefined ) { - - throw new Error( 'THREE.EdgeSplitModifier relies on BufferGeometryUtils' ); - - } - - geometry = BufferGeometryUtils.mergeVertices( geometry ); + geometry = mergeVertices( geometry ); } diff --git a/examples/jsm/modifiers/SimplifyModifier.js b/examples/jsm/modifiers/SimplifyModifier.js index 4483c24d805800..0e6cdff4b1d51f 100644 --- a/examples/jsm/modifiers/SimplifyModifier.js +++ b/examples/jsm/modifiers/SimplifyModifier.js @@ -3,7 +3,7 @@ import { Float32BufferAttribute, Vector3 } from '../../../build/three.module.js'; -import * as BufferGeometryUtils from '../utils/BufferGeometryUtils.js'; +import { mergeVertices } from '../utils/BufferGeometryUtils.js'; /** * Simplification Geometry Modifier @@ -17,16 +17,6 @@ const _cb = new Vector3(), _ab = new Vector3(); class SimplifyModifier { - constructor() { - - if ( BufferGeometryUtils === undefined ) { - - throw new Error( 'THREE.SimplifyModifier relies on BufferGeometryUtils' ); - - } - - } - modify( geometry, count ) { if ( geometry.isGeometry === true ) { @@ -47,7 +37,7 @@ class SimplifyModifier { } - geometry = BufferGeometryUtils.mergeVertices( geometry ); + geometry = mergeVertices( geometry ); // // put data of original geometry in different data structures diff --git a/examples/jsm/utils/BufferGeometryUtils.js b/examples/jsm/utils/BufferGeometryUtils.js index 8f1721060347e6..e351547c2d79a5 100644 --- a/examples/jsm/utils/BufferGeometryUtils.js +++ b/examples/jsm/utils/BufferGeometryUtils.js @@ -19,10 +19,10 @@ function computeTangents( geometry ) { } /** - * @param {Array} geometries - * @param {Boolean} useGroups - * @return {BufferGeometry} - */ + * @param {Array} geometries + * @param {Boolean} useGroups + * @return {BufferGeometry} + */ function mergeBufferGeometries( geometries, useGroups = false ) { const isIndexed = geometries[ 0 ].index !== null; diff --git a/utils/build/rollup.examples.config.js b/utils/build/rollup.examples.config.js index caa708a30cd8d4..d7f77e88ec819b 100644 --- a/utils/build/rollup.examples.config.js +++ b/utils/build/rollup.examples.config.js @@ -202,8 +202,20 @@ export default files.map( file => { return { input: inputPath, - treeshake: false, - external: () => true, // don't bundle anything + treeshake: true, + external: ( id ) => { + + // Small utility methods can be individually inlined into examples, + // so users don't have to import the entire utility file. + if ( /BufferGeometryUtils/.test( id ) ) { + + return false; + + } + + return true; + + }, plugins: [ babel( { babelHelpers: 'bundled', From 247fb7ed6283e8b0041329a80bba707ca326563e Mon Sep 17 00:00:00 2001 From: Don McCurdy Date: Thu, 6 Jan 2022 18:03:27 -0800 Subject: [PATCH 2/2] Examples: Clean up rollup config. --- utils/build/rollup.examples.config.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/utils/build/rollup.examples.config.js b/utils/build/rollup.examples.config.js index d7f77e88ec819b..8fc02b47beb5f3 100644 --- a/utils/build/rollup.examples.config.js +++ b/utils/build/rollup.examples.config.js @@ -191,6 +191,11 @@ const files = glob.sync( '**/*.js', { cwd: jsmFolder, ignore: [ 'offscreen/**/*', ] } ); +// Small utilities can be individually inlined into examples. +const inline = [ + 'BufferGeometryUtils.js', + 'WorkerPool.js', +]; // Create a rollup config for each .js file export default files.map( file => { @@ -202,12 +207,10 @@ export default files.map( file => { return { input: inputPath, - treeshake: true, + treeshake: 'recommended', external: ( id ) => { - // Small utility methods can be individually inlined into examples, - // so users don't have to import the entire utility file. - if ( /BufferGeometryUtils/.test( id ) ) { + if ( inline.some( ( _id ) => id.includes( _id ) ) ) { return false;