Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples: Inline small utility methods for examples/js #23167

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 1 addition & 94 deletions examples/jsm/loaders/GLTFLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import {
VectorKeyframeTrack,
sRGBEncoding
} from '../../../build/three.module.js';
import { toTrianglesDrawMode } from '../utils/BufferGeometryUtils.js';

class GLTFLoader extends Loader {

Expand Down Expand Up @@ -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 };
10 changes: 2 additions & 8 deletions examples/jsm/modifiers/EdgeSplitModifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 );

}

Expand Down
14 changes: 2 additions & 12 deletions examples/jsm/modifiers/SimplifyModifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ) {
Expand All @@ -47,7 +37,7 @@ class SimplifyModifier {

}

geometry = BufferGeometryUtils.mergeVertices( geometry );
geometry = mergeVertices( geometry );

//
// put data of original geometry in different data structures
Expand Down
8 changes: 4 additions & 4 deletions examples/jsm/utils/BufferGeometryUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ function computeTangents( geometry ) {
}

/**
* @param {Array<BufferGeometry>} geometries
* @param {Boolean} useGroups
* @return {BufferGeometry}
*/
* @param {Array<BufferGeometry>} geometries
* @param {Boolean} useGroups
* @return {BufferGeometry}
*/
function mergeBufferGeometries( geometries, useGroups = false ) {

const isIndexed = geometries[ 0 ].index !== null;
Expand Down
19 changes: 17 additions & 2 deletions utils/build/rollup.examples.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -202,8 +207,18 @@ export default files.map( file => {
return {

input: inputPath,
treeshake: false,
external: () => true, // don't bundle anything
treeshake: 'recommended',
external: ( id ) => {

if ( inline.some( ( _id ) => id.includes( _id ) ) ) {

return false;

}

return true;

},
plugins: [
babel( {
babelHelpers: 'bundled',
Expand Down