mirror of
https://github.com/DanielnetoDotCom/YouPHPTube
synced 2025-10-05 10:49:36 +02:00
This commit is contained in:
parent
f0f62670c5
commit
7e26256cac
4563 changed files with 1246712 additions and 17558 deletions
285
node_modules/three/examples/js/modifiers/EdgeSplitModifier.js
generated
vendored
Normal file
285
node_modules/three/examples/js/modifiers/EdgeSplitModifier.js
generated
vendored
Normal file
|
@ -0,0 +1,285 @@
|
|||
THREE.EdgeSplitModifier = function () {
|
||||
|
||||
var A = new THREE.Vector3();
|
||||
var B = new THREE.Vector3();
|
||||
var C = new THREE.Vector3();
|
||||
|
||||
var positions, normals;
|
||||
var indexes;
|
||||
var pointToIndexMap, splitIndexes;
|
||||
let oldNormals;
|
||||
|
||||
|
||||
function computeNormals() {
|
||||
|
||||
normals = new Float32Array( indexes.length * 3 );
|
||||
|
||||
for ( var i = 0; i < indexes.length; i += 3 ) {
|
||||
|
||||
var index = indexes[ i ];
|
||||
|
||||
A.set(
|
||||
positions[ 3 * index ],
|
||||
positions[ 3 * index + 1 ],
|
||||
positions[ 3 * index + 2 ] );
|
||||
|
||||
index = indexes[ i + 1 ];
|
||||
B.set(
|
||||
positions[ 3 * index ],
|
||||
positions[ 3 * index + 1 ],
|
||||
positions[ 3 * index + 2 ] );
|
||||
|
||||
index = indexes[ i + 2 ];
|
||||
C.set(
|
||||
positions[ 3 * index ],
|
||||
positions[ 3 * index + 1 ],
|
||||
positions[ 3 * index + 2 ] );
|
||||
|
||||
C.sub( B );
|
||||
A.sub( B );
|
||||
|
||||
var normal = C.cross( A ).normalize();
|
||||
|
||||
for ( var j = 0; j < 3; j ++ ) {
|
||||
|
||||
normals[ 3 * ( i + j ) ] = normal.x;
|
||||
normals[ 3 * ( i + j ) + 1 ] = normal.y;
|
||||
normals[ 3 * ( i + j ) + 2 ] = normal.z;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
function mapPositionsToIndexes() {
|
||||
|
||||
pointToIndexMap = Array( positions.length / 3 );
|
||||
|
||||
for ( var i = 0; i < indexes.length; i ++ ) {
|
||||
|
||||
var index = indexes[ i ];
|
||||
|
||||
if ( pointToIndexMap[ index ] == null ) {
|
||||
|
||||
pointToIndexMap[ index ] = [];
|
||||
|
||||
}
|
||||
|
||||
pointToIndexMap[ index ].push( i );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
function edgeSplitToGroups( indexes, cutOff, firstIndex ) {
|
||||
|
||||
A.set( normals[ 3 * firstIndex ], normals[ 3 * firstIndex + 1 ], normals[ 3 * firstIndex + 2 ] ).normalize();
|
||||
|
||||
var result = {
|
||||
splitGroup: [],
|
||||
currentGroup: [ firstIndex ]
|
||||
};
|
||||
|
||||
for ( var j of indexes ) {
|
||||
|
||||
if ( j !== firstIndex ) {
|
||||
|
||||
B.set( normals[ 3 * j ], normals[ 3 * j + 1 ], normals[ 3 * j + 2 ] ).normalize();
|
||||
|
||||
if ( B.dot( A ) < cutOff ) {
|
||||
|
||||
result.splitGroup.push( j );
|
||||
|
||||
} else {
|
||||
|
||||
result.currentGroup.push( j );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
|
||||
function edgeSplit( indexes, cutOff, original = null ) {
|
||||
|
||||
if ( indexes.length === 0 ) return;
|
||||
|
||||
var groupResults = [];
|
||||
|
||||
for ( var index of indexes ) {
|
||||
|
||||
groupResults.push( edgeSplitToGroups( indexes, cutOff, index ) );
|
||||
|
||||
}
|
||||
|
||||
var result = groupResults[ 0 ];
|
||||
|
||||
for ( var groupResult of groupResults ) {
|
||||
|
||||
if ( groupResult.currentGroup.length > result.currentGroup.length ) {
|
||||
|
||||
result = groupResult;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
if ( original != null ) {
|
||||
|
||||
splitIndexes.push( {
|
||||
original: original,
|
||||
indexes: result.currentGroup
|
||||
} );
|
||||
|
||||
}
|
||||
|
||||
if ( result.splitGroup.length ) {
|
||||
|
||||
edgeSplit( result.splitGroup, cutOff, original || result.currentGroup[ 0 ] );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
this.modify = function ( geometry, cutOffAngle, tryKeepNormals = true ) {
|
||||
|
||||
if ( geometry.isGeometry === true ) {
|
||||
|
||||
console.error( 'THREE.EdgeSplitModifier no longer supports THREE.Geometry. Use THREE.BufferGeometry instead.' );
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
let hadNormals = false;
|
||||
oldNormals = null;
|
||||
|
||||
if ( geometry.attributes.normal ) {
|
||||
|
||||
hadNormals = true;
|
||||
|
||||
geometry = geometry.clone();
|
||||
|
||||
if ( tryKeepNormals === true && geometry.index !== null ) {
|
||||
|
||||
oldNormals = geometry.attributes.normal.array;
|
||||
|
||||
}
|
||||
|
||||
geometry.deleteAttribute( 'normal' );
|
||||
|
||||
}
|
||||
|
||||
if ( geometry.index == null ) {
|
||||
|
||||
if ( THREE.BufferGeometryUtils === undefined ) {
|
||||
|
||||
throw 'THREE.EdgeSplitModifier relies on THREE.BufferGeometryUtils';
|
||||
|
||||
}
|
||||
|
||||
geometry = THREE.BufferGeometryUtils.mergeVertices( geometry );
|
||||
|
||||
}
|
||||
|
||||
indexes = geometry.index.array;
|
||||
positions = geometry.getAttribute( 'position' ).array;
|
||||
|
||||
computeNormals();
|
||||
mapPositionsToIndexes();
|
||||
|
||||
splitIndexes = [];
|
||||
|
||||
for ( var vertexIndexes of pointToIndexMap ) {
|
||||
|
||||
edgeSplit( vertexIndexes, Math.cos( cutOffAngle ) - 0.001 );
|
||||
|
||||
}
|
||||
|
||||
const newAttributes = {};
|
||||
for ( const name of Object.keys( geometry.attributes ) ) {
|
||||
|
||||
const oldAttribute = geometry.attributes[ name ];
|
||||
const newArray = new oldAttribute.array.constructor( ( indexes.length + splitIndexes.length ) * oldAttribute.itemSize );
|
||||
newArray.set( oldAttribute.array );
|
||||
newAttributes[ name ] = new THREE.BufferAttribute( newArray, oldAttribute.itemSize, oldAttribute.normalized );
|
||||
|
||||
}
|
||||
|
||||
var newIndexes = new Uint32Array( indexes.length );
|
||||
newIndexes.set( indexes );
|
||||
|
||||
for ( var i = 0; i < splitIndexes.length; i ++ ) {
|
||||
|
||||
var split = splitIndexes[ i ];
|
||||
var index = indexes[ split.original ];
|
||||
|
||||
for ( const attribute of Object.values( newAttributes ) ) {
|
||||
|
||||
for ( let j = 0; j < attribute.itemSize; j ++ ) {
|
||||
|
||||
attribute.array[ ( indexes.length + i ) * attribute.itemSize + j ] =
|
||||
attribute.array[ index * attribute.itemSize + j ];
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
for ( var j of split.indexes ) {
|
||||
|
||||
newIndexes[ j ] = indexes.length + i;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
geometry = new THREE.BufferGeometry();
|
||||
geometry.setIndex( new THREE.BufferAttribute( newIndexes, 1 ) );
|
||||
|
||||
for ( const name of Object.keys( newAttributes ) ) {
|
||||
|
||||
geometry.setAttribute( name, newAttributes[ name ] );
|
||||
|
||||
}
|
||||
|
||||
if ( hadNormals ) {
|
||||
|
||||
geometry.computeVertexNormals();
|
||||
|
||||
if ( oldNormals !== null ) {
|
||||
|
||||
const changedNormals = new Array( oldNormals.length / 3 ).fill( false );
|
||||
|
||||
for ( const splitData of splitIndexes )
|
||||
changedNormals[ splitData.original ] = true;
|
||||
|
||||
for ( let i = 0; i < changedNormals.length; i ++ ) {
|
||||
|
||||
if ( changedNormals[ i ] === false ) {
|
||||
|
||||
for ( let j = 0; j < 3; j ++ )
|
||||
geometry.attributes.normal.array[ 3 * i + j ] = oldNormals[ 3 * i + j ];
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return geometry;
|
||||
|
||||
};
|
||||
|
||||
};
|
527
node_modules/three/examples/js/modifiers/SimplifyModifier.js
generated
vendored
Normal file
527
node_modules/three/examples/js/modifiers/SimplifyModifier.js
generated
vendored
Normal file
|
@ -0,0 +1,527 @@
|
|||
/**
|
||||
* Simplification Geometry Modifier
|
||||
* - based on code and technique
|
||||
* - by Stan Melax in 1998
|
||||
* - Progressive Mesh type Polygon Reduction Algorithm
|
||||
* - http://www.melax.com/polychop/
|
||||
*/
|
||||
|
||||
THREE.SimplifyModifier = function () {
|
||||
|
||||
if ( THREE.BufferGeometryUtils === undefined ) {
|
||||
|
||||
throw 'THREE.SimplifyModifier relies on THREE.BufferGeometryUtils';
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
( function () {
|
||||
|
||||
var cb = new THREE.Vector3(), ab = new THREE.Vector3();
|
||||
|
||||
function pushIfUnique( array, object ) {
|
||||
|
||||
if ( array.indexOf( object ) === - 1 ) array.push( object );
|
||||
|
||||
}
|
||||
|
||||
function removeFromArray( array, object ) {
|
||||
|
||||
var k = array.indexOf( object );
|
||||
if ( k > - 1 ) array.splice( k, 1 );
|
||||
|
||||
}
|
||||
|
||||
function computeEdgeCollapseCost( u, v ) {
|
||||
|
||||
// if we collapse edge uv by moving u to v then how
|
||||
// much different will the model change, i.e. the "error".
|
||||
|
||||
var edgelength = v.position.distanceTo( u.position );
|
||||
var curvature = 0;
|
||||
|
||||
var sideFaces = [];
|
||||
var i, il = u.faces.length, face, sideFace;
|
||||
|
||||
// find the "sides" triangles that are on the edge uv
|
||||
for ( i = 0; i < il; i ++ ) {
|
||||
|
||||
face = u.faces[ i ];
|
||||
|
||||
if ( face.hasVertex( v ) ) {
|
||||
|
||||
sideFaces.push( face );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// use the triangle facing most away from the sides
|
||||
// to determine our curvature term
|
||||
for ( i = 0; i < il; i ++ ) {
|
||||
|
||||
var minCurvature = 1;
|
||||
face = u.faces[ i ];
|
||||
|
||||
for ( var j = 0; j < sideFaces.length; j ++ ) {
|
||||
|
||||
sideFace = sideFaces[ j ];
|
||||
// use dot product of face normals.
|
||||
var dotProd = face.normal.dot( sideFace.normal );
|
||||
minCurvature = Math.min( minCurvature, ( 1.001 - dotProd ) / 2 );
|
||||
|
||||
}
|
||||
|
||||
curvature = Math.max( curvature, minCurvature );
|
||||
|
||||
}
|
||||
|
||||
// crude approach in attempt to preserve borders
|
||||
// though it seems not to be totally correct
|
||||
var borders = 0;
|
||||
if ( sideFaces.length < 2 ) {
|
||||
|
||||
// we add some arbitrary cost for borders,
|
||||
// borders += 10;
|
||||
curvature = 1;
|
||||
|
||||
}
|
||||
|
||||
var amt = edgelength * curvature + borders;
|
||||
|
||||
return amt;
|
||||
|
||||
}
|
||||
|
||||
function computeEdgeCostAtVertex( v ) {
|
||||
|
||||
// compute the edge collapse cost for all edges that start
|
||||
// from vertex v. Since we are only interested in reducing
|
||||
// the object by selecting the min cost edge at each step, we
|
||||
// only cache the cost of the least cost edge at this vertex
|
||||
// (in member variable collapse) as well as the value of the
|
||||
// cost (in member variable collapseCost).
|
||||
|
||||
if ( v.neighbors.length === 0 ) {
|
||||
|
||||
// collapse if no neighbors.
|
||||
v.collapseNeighbor = null;
|
||||
v.collapseCost = - 0.01;
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
v.collapseCost = 100000;
|
||||
v.collapseNeighbor = null;
|
||||
|
||||
// search all neighboring edges for "least cost" edge
|
||||
for ( var i = 0; i < v.neighbors.length; i ++ ) {
|
||||
|
||||
var collapseCost = computeEdgeCollapseCost( v, v.neighbors[ i ] );
|
||||
|
||||
if ( ! v.collapseNeighbor ) {
|
||||
|
||||
v.collapseNeighbor = v.neighbors[ i ];
|
||||
v.collapseCost = collapseCost;
|
||||
v.minCost = collapseCost;
|
||||
v.totalCost = 0;
|
||||
v.costCount = 0;
|
||||
|
||||
}
|
||||
|
||||
v.costCount ++;
|
||||
v.totalCost += collapseCost;
|
||||
|
||||
if ( collapseCost < v.minCost ) {
|
||||
|
||||
v.collapseNeighbor = v.neighbors[ i ];
|
||||
v.minCost = collapseCost;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// we average the cost of collapsing at this vertex
|
||||
v.collapseCost = v.totalCost / v.costCount;
|
||||
// v.collapseCost = v.minCost;
|
||||
|
||||
}
|
||||
|
||||
function removeVertex( v, vertices ) {
|
||||
|
||||
console.assert( v.faces.length === 0 );
|
||||
|
||||
while ( v.neighbors.length ) {
|
||||
|
||||
var n = v.neighbors.pop();
|
||||
removeFromArray( n.neighbors, v );
|
||||
|
||||
}
|
||||
|
||||
removeFromArray( vertices, v );
|
||||
|
||||
}
|
||||
|
||||
function removeFace( f, faces ) {
|
||||
|
||||
removeFromArray( faces, f );
|
||||
|
||||
if ( f.v1 ) removeFromArray( f.v1.faces, f );
|
||||
if ( f.v2 ) removeFromArray( f.v2.faces, f );
|
||||
if ( f.v3 ) removeFromArray( f.v3.faces, f );
|
||||
|
||||
// TODO optimize this!
|
||||
var vs = [ f.v1, f.v2, f.v3 ];
|
||||
var v1, v2;
|
||||
|
||||
for ( var i = 0; i < 3; i ++ ) {
|
||||
|
||||
v1 = vs[ i ];
|
||||
v2 = vs[ ( i + 1 ) % 3 ];
|
||||
|
||||
if ( ! v1 || ! v2 ) continue;
|
||||
|
||||
v1.removeIfNonNeighbor( v2 );
|
||||
v2.removeIfNonNeighbor( v1 );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function collapse( vertices, faces, u, v ) { // u and v are pointers to vertices of an edge
|
||||
|
||||
// Collapse the edge uv by moving vertex u onto v
|
||||
|
||||
if ( ! v ) {
|
||||
|
||||
// u is a vertex all by itself so just delete it..
|
||||
removeVertex( u, vertices );
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
var i;
|
||||
var tmpVertices = [];
|
||||
|
||||
for ( i = 0; i < u.neighbors.length; i ++ ) {
|
||||
|
||||
tmpVertices.push( u.neighbors[ i ] );
|
||||
|
||||
}
|
||||
|
||||
|
||||
// delete triangles on edge uv:
|
||||
for ( i = u.faces.length - 1; i >= 0; i -- ) {
|
||||
|
||||
if ( u.faces[ i ].hasVertex( v ) ) {
|
||||
|
||||
removeFace( u.faces[ i ], faces );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// update remaining triangles to have v instead of u
|
||||
for ( i = u.faces.length - 1; i >= 0; i -- ) {
|
||||
|
||||
u.faces[ i ].replaceVertex( u, v );
|
||||
|
||||
}
|
||||
|
||||
|
||||
removeVertex( u, vertices );
|
||||
|
||||
// recompute the edge collapse costs in neighborhood
|
||||
for ( i = 0; i < tmpVertices.length; i ++ ) {
|
||||
|
||||
computeEdgeCostAtVertex( tmpVertices[ i ] );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
function minimumCostEdge( vertices ) {
|
||||
|
||||
// O(n * n) approach. TODO optimize this
|
||||
|
||||
var least = vertices[ 0 ];
|
||||
|
||||
for ( var i = 0; i < vertices.length; i ++ ) {
|
||||
|
||||
if ( vertices[ i ].collapseCost < least.collapseCost ) {
|
||||
|
||||
least = vertices[ i ];
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return least;
|
||||
|
||||
}
|
||||
|
||||
// we use a triangle class to represent structure of face slightly differently
|
||||
|
||||
function Triangle( v1, v2, v3, a, b, c ) {
|
||||
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
this.c = c;
|
||||
|
||||
this.v1 = v1;
|
||||
this.v2 = v2;
|
||||
this.v3 = v3;
|
||||
|
||||
this.normal = new THREE.Vector3();
|
||||
|
||||
this.computeNormal();
|
||||
|
||||
v1.faces.push( this );
|
||||
v1.addUniqueNeighbor( v2 );
|
||||
v1.addUniqueNeighbor( v3 );
|
||||
|
||||
v2.faces.push( this );
|
||||
v2.addUniqueNeighbor( v1 );
|
||||
v2.addUniqueNeighbor( v3 );
|
||||
|
||||
|
||||
v3.faces.push( this );
|
||||
v3.addUniqueNeighbor( v1 );
|
||||
v3.addUniqueNeighbor( v2 );
|
||||
|
||||
}
|
||||
|
||||
Triangle.prototype.computeNormal = function () {
|
||||
|
||||
var vA = this.v1.position;
|
||||
var vB = this.v2.position;
|
||||
var vC = this.v3.position;
|
||||
|
||||
cb.subVectors( vC, vB );
|
||||
ab.subVectors( vA, vB );
|
||||
cb.cross( ab ).normalize();
|
||||
|
||||
this.normal.copy( cb );
|
||||
|
||||
};
|
||||
|
||||
Triangle.prototype.hasVertex = function ( v ) {
|
||||
|
||||
return v === this.v1 || v === this.v2 || v === this.v3;
|
||||
|
||||
};
|
||||
|
||||
Triangle.prototype.replaceVertex = function ( oldv, newv ) {
|
||||
|
||||
if ( oldv === this.v1 ) this.v1 = newv;
|
||||
else if ( oldv === this.v2 ) this.v2 = newv;
|
||||
else if ( oldv === this.v3 ) this.v3 = newv;
|
||||
|
||||
removeFromArray( oldv.faces, this );
|
||||
newv.faces.push( this );
|
||||
|
||||
|
||||
oldv.removeIfNonNeighbor( this.v1 );
|
||||
this.v1.removeIfNonNeighbor( oldv );
|
||||
|
||||
oldv.removeIfNonNeighbor( this.v2 );
|
||||
this.v2.removeIfNonNeighbor( oldv );
|
||||
|
||||
oldv.removeIfNonNeighbor( this.v3 );
|
||||
this.v3.removeIfNonNeighbor( oldv );
|
||||
|
||||
this.v1.addUniqueNeighbor( this.v2 );
|
||||
this.v1.addUniqueNeighbor( this.v3 );
|
||||
|
||||
this.v2.addUniqueNeighbor( this.v1 );
|
||||
this.v2.addUniqueNeighbor( this.v3 );
|
||||
|
||||
this.v3.addUniqueNeighbor( this.v1 );
|
||||
this.v3.addUniqueNeighbor( this.v2 );
|
||||
|
||||
this.computeNormal();
|
||||
|
||||
};
|
||||
|
||||
function Vertex( v, id ) {
|
||||
|
||||
this.position = v;
|
||||
|
||||
this.id = id; // old index id
|
||||
|
||||
this.faces = []; // faces vertex is connected
|
||||
this.neighbors = []; // neighbouring vertices aka "adjacentVertices"
|
||||
|
||||
// these will be computed in computeEdgeCostAtVertex()
|
||||
this.collapseCost = 0; // cost of collapsing this vertex, the less the better. aka objdist
|
||||
this.collapseNeighbor = null; // best candinate for collapsing
|
||||
|
||||
}
|
||||
|
||||
Vertex.prototype.addUniqueNeighbor = function ( vertex ) {
|
||||
|
||||
pushIfUnique( this.neighbors, vertex );
|
||||
|
||||
};
|
||||
|
||||
Vertex.prototype.removeIfNonNeighbor = function ( n ) {
|
||||
|
||||
var neighbors = this.neighbors;
|
||||
var faces = this.faces;
|
||||
|
||||
var offset = neighbors.indexOf( n );
|
||||
if ( offset === - 1 ) return;
|
||||
for ( var i = 0; i < faces.length; i ++ ) {
|
||||
|
||||
if ( faces[ i ].hasVertex( n ) ) return;
|
||||
|
||||
}
|
||||
|
||||
neighbors.splice( offset, 1 );
|
||||
|
||||
};
|
||||
|
||||
THREE.SimplifyModifier.prototype.modify = function ( geometry, count ) {
|
||||
|
||||
if ( geometry.isGeometry === true ) {
|
||||
|
||||
console.error( 'THREE.SimplifyModifier no longer supports THREE.Geometry. Use THREE.BufferGeometry instead.' );
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
geometry = geometry.clone();
|
||||
var attributes = geometry.attributes;
|
||||
|
||||
// this modifier can only process indexed and non-indexed geomtries with a position attribute
|
||||
|
||||
for ( var name in attributes ) {
|
||||
|
||||
if ( name !== 'position' ) geometry.deleteAttribute( name );
|
||||
|
||||
}
|
||||
|
||||
geometry = THREE.BufferGeometryUtils.mergeVertices( geometry );
|
||||
|
||||
//
|
||||
// put data of original geometry in different data structures
|
||||
//
|
||||
|
||||
var vertices = [];
|
||||
var faces = [];
|
||||
|
||||
// add vertices
|
||||
|
||||
var positionAttribute = geometry.getAttribute( 'position' );
|
||||
|
||||
for ( var i = 0; i < positionAttribute.count; i ++ ) {
|
||||
|
||||
var v = new THREE.Vector3().fromBufferAttribute( positionAttribute, i );
|
||||
|
||||
var vertex = new Vertex( v, i );
|
||||
vertices.push( vertex );
|
||||
|
||||
}
|
||||
|
||||
// add faces
|
||||
|
||||
var index = geometry.getIndex();
|
||||
|
||||
if ( index !== null ) {
|
||||
|
||||
for ( var i = 0; i < index.count; i += 3 ) {
|
||||
|
||||
var a = index.getX( i );
|
||||
var b = index.getX( i + 1 );
|
||||
var c = index.getX( i + 2 );
|
||||
|
||||
var triangle = new Triangle( vertices[ a ], vertices[ b ], vertices[ c ], a, b, c );
|
||||
faces.push( triangle );
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
for ( var i = 0; i < positionAttribute.count; i += 3 ) {
|
||||
|
||||
var a = i;
|
||||
var b = i + 1;
|
||||
var c = i + 2;
|
||||
|
||||
var triangle = new Triangle( vertices[ a ], vertices[ b ], vertices[ c ], a, b, c );
|
||||
faces.push( triangle );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// compute all edge collapse costs
|
||||
|
||||
for ( var i = 0, il = vertices.length; i < il; i ++ ) {
|
||||
|
||||
computeEdgeCostAtVertex( vertices[ i ] );
|
||||
|
||||
}
|
||||
|
||||
var nextVertex;
|
||||
|
||||
var z = count;
|
||||
|
||||
while ( z -- ) {
|
||||
|
||||
nextVertex = minimumCostEdge( vertices );
|
||||
|
||||
if ( ! nextVertex ) {
|
||||
|
||||
console.log( 'THREE.SimplifyModifier: No next vertex' );
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
collapse( vertices, faces, nextVertex, nextVertex.collapseNeighbor );
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
var simplifiedGeometry = new THREE.BufferGeometry();
|
||||
var position = [];
|
||||
var index = [];
|
||||
|
||||
//
|
||||
|
||||
for ( var i = 0; i < vertices.length; i ++ ) {
|
||||
|
||||
var vertex = vertices[ i ].position;
|
||||
position.push( vertex.x, vertex.y, vertex.z );
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
for ( var i = 0; i < faces.length; i ++ ) {
|
||||
|
||||
var face = faces[ i ];
|
||||
|
||||
var a = vertices.indexOf( face.v1 );
|
||||
var b = vertices.indexOf( face.v2 );
|
||||
var c = vertices.indexOf( face.v3 );
|
||||
|
||||
index.push( a, b, c );
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
simplifiedGeometry.setAttribute( 'position', new THREE.Float32BufferAttribute( position, 3 ) );
|
||||
simplifiedGeometry.setIndex( index );
|
||||
|
||||
return simplifiedGeometry;
|
||||
|
||||
};
|
||||
|
||||
} )();
|
300
node_modules/three/examples/js/modifiers/TessellateModifier.js
generated
vendored
Normal file
300
node_modules/three/examples/js/modifiers/TessellateModifier.js
generated
vendored
Normal file
|
@ -0,0 +1,300 @@
|
|||
/**
|
||||
* Break faces with edges longer than maxEdgeLength
|
||||
*/
|
||||
|
||||
THREE.TessellateModifier = function ( maxEdgeLength = 0.1, maxIterations = 6 ) {
|
||||
|
||||
this.maxEdgeLength = maxEdgeLength;
|
||||
this.maxIterations = maxIterations;
|
||||
|
||||
};
|
||||
|
||||
THREE.TessellateModifier.prototype.modify = function ( geometry ) {
|
||||
|
||||
if ( geometry.isGeometry === true ) {
|
||||
|
||||
console.error( 'THREE.TessellateModifier no longer supports THREE.Geometry. Use THREE.BufferGeometry instead.' );
|
||||
return geometry;
|
||||
|
||||
}
|
||||
|
||||
if ( geometry.index !== null ) {
|
||||
|
||||
geometry = geometry.toNonIndexed();
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
const maxIterations = this.maxIterations;
|
||||
const maxEdgeLengthSquared = this.maxEdgeLength * this.maxEdgeLength;
|
||||
|
||||
const va = new THREE.Vector3();
|
||||
const vb = new THREE.Vector3();
|
||||
const vc = new THREE.Vector3();
|
||||
const vm = new THREE.Vector3();
|
||||
const vs = [ va, vb, vc, vm ];
|
||||
|
||||
const na = new THREE.Vector3();
|
||||
const nb = new THREE.Vector3();
|
||||
const nc = new THREE.Vector3();
|
||||
const nm = new THREE.Vector3();
|
||||
const ns = [ na, nb, nc, nm ];
|
||||
|
||||
const ca = new THREE.Color();
|
||||
const cb = new THREE.Color();
|
||||
const cc = new THREE.Color();
|
||||
const cm = new THREE.Color();
|
||||
const cs = [ ca, cb, cc, cm ];
|
||||
|
||||
const ua = new THREE.Vector2();
|
||||
const ub = new THREE.Vector2();
|
||||
const uc = new THREE.Vector2();
|
||||
const um = new THREE.Vector2();
|
||||
const us = [ ua, ub, uc, um ];
|
||||
|
||||
const u2a = new THREE.Vector2();
|
||||
const u2b = new THREE.Vector2();
|
||||
const u2c = new THREE.Vector2();
|
||||
const u2m = new THREE.Vector2();
|
||||
const u2s = [ u2a, u2b, u2c, u2m ];
|
||||
|
||||
const attributes = geometry.attributes;
|
||||
const hasNormals = attributes.normal !== undefined;
|
||||
const hasColors = attributes.color !== undefined;
|
||||
const hasUVs = attributes.uv !== undefined;
|
||||
const hasUV2s = attributes.uv2 !== undefined;
|
||||
|
||||
let positions = attributes.position.array;
|
||||
let normals = hasNormals ? attributes.normal.array : null;
|
||||
let colors = hasColors ? attributes.color.array : null;
|
||||
let uvs = hasUVs ? attributes.uv.array : null;
|
||||
let uv2s = hasUV2s ? attributes.uv2.array : null;
|
||||
|
||||
let positions2 = positions;
|
||||
let normals2 = normals;
|
||||
let colors2 = colors;
|
||||
let uvs2 = uvs;
|
||||
let uv2s2 = uv2s;
|
||||
|
||||
let iteration = 0;
|
||||
let tessellating = true;
|
||||
|
||||
function addTriangle( a, b, c ) {
|
||||
|
||||
const v1 = vs[ a ];
|
||||
const v2 = vs[ b ];
|
||||
const v3 = vs[ c ];
|
||||
|
||||
positions2.push( v1.x, v1.y, v1.z );
|
||||
positions2.push( v2.x, v2.y, v2.z );
|
||||
positions2.push( v3.x, v3.y, v3.z );
|
||||
|
||||
if ( hasNormals ) {
|
||||
|
||||
const n1 = ns[ a ];
|
||||
const n2 = ns[ b ];
|
||||
const n3 = ns[ c ];
|
||||
|
||||
normals2.push( n1.x, n1.y, n1.z );
|
||||
normals2.push( n2.x, n2.y, n2.z );
|
||||
normals2.push( n3.x, n3.y, n3.z );
|
||||
|
||||
}
|
||||
|
||||
if ( hasColors ) {
|
||||
|
||||
const c1 = cs[ a ];
|
||||
const c2 = cs[ b ];
|
||||
const c3 = cs[ c ];
|
||||
|
||||
colors2.push( c1.x, c1.y, c1.z );
|
||||
colors2.push( c2.x, c2.y, c2.z );
|
||||
colors2.push( c3.x, c3.y, c3.z );
|
||||
|
||||
}
|
||||
|
||||
if ( hasUVs ) {
|
||||
|
||||
const u1 = us[ a ];
|
||||
const u2 = us[ b ];
|
||||
const u3 = us[ c ];
|
||||
|
||||
uvs2.push( u1.x, u1.y );
|
||||
uvs2.push( u2.x, u2.y );
|
||||
uvs2.push( u3.x, u3.y );
|
||||
|
||||
}
|
||||
|
||||
if ( hasUV2s ) {
|
||||
|
||||
const u21 = u2s[ a ];
|
||||
const u22 = u2s[ b ];
|
||||
const u23 = u2s[ c ];
|
||||
|
||||
uv2s2.push( u21.x, u21.y );
|
||||
uv2s2.push( u22.x, u22.y );
|
||||
uv2s2.push( u23.x, u23.y );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
while ( tessellating && iteration < maxIterations ) {
|
||||
|
||||
iteration ++;
|
||||
tessellating = false;
|
||||
|
||||
positions = positions2;
|
||||
positions2 = [];
|
||||
|
||||
if ( hasNormals ) {
|
||||
|
||||
normals = normals2;
|
||||
normals2 = [];
|
||||
|
||||
}
|
||||
|
||||
if ( hasColors ) {
|
||||
|
||||
colors = colors2;
|
||||
colors2 = [];
|
||||
|
||||
}
|
||||
|
||||
if ( hasUVs ) {
|
||||
|
||||
uvs = uvs2;
|
||||
uvs2 = [];
|
||||
|
||||
}
|
||||
|
||||
if ( hasUV2s ) {
|
||||
|
||||
uv2s = uv2s2;
|
||||
uv2s2 = [];
|
||||
|
||||
}
|
||||
|
||||
for ( var i = 0, i2 = 0, il = positions.length; i < il; i += 9, i2 += 6 ) {
|
||||
|
||||
va.fromArray( positions, i + 0 );
|
||||
vb.fromArray( positions, i + 3 );
|
||||
vc.fromArray( positions, i + 6 );
|
||||
|
||||
if ( hasNormals ) {
|
||||
|
||||
na.fromArray( normals, i + 0 );
|
||||
nb.fromArray( normals, i + 3 );
|
||||
nc.fromArray( normals, i + 6 );
|
||||
|
||||
}
|
||||
|
||||
if ( hasColors ) {
|
||||
|
||||
ca.fromArray( colors, i + 0 );
|
||||
cb.fromArray( colors, i + 3 );
|
||||
cc.fromArray( colors, i + 6 );
|
||||
|
||||
}
|
||||
|
||||
if ( hasUVs ) {
|
||||
|
||||
ua.fromArray( uvs, i2 + 0 );
|
||||
ub.fromArray( uvs, i2 + 2 );
|
||||
uc.fromArray( uvs, i2 + 4 );
|
||||
|
||||
}
|
||||
|
||||
if ( hasUV2s ) {
|
||||
|
||||
u2a.fromArray( uv2s, i2 + 0 );
|
||||
u2b.fromArray( uv2s, i2 + 2 );
|
||||
u2c.fromArray( uv2s, i2 + 4 );
|
||||
|
||||
}
|
||||
|
||||
const dab = va.distanceToSquared( vb );
|
||||
const dbc = vb.distanceToSquared( vc );
|
||||
const dac = va.distanceToSquared( vc );
|
||||
|
||||
if ( dab > maxEdgeLengthSquared || dbc > maxEdgeLengthSquared || dac > maxEdgeLengthSquared ) {
|
||||
|
||||
tessellating = true;
|
||||
|
||||
if ( dab >= dbc && dab >= dac ) {
|
||||
|
||||
vm.lerpVectors( va, vb, 0.5 );
|
||||
if ( hasNormals ) nm.lerpVectors( na, nb, 0.5 );
|
||||
if ( hasColors ) cm.lerpColors( ca, cb, 0.5 );
|
||||
if ( hasUVs ) um.lerpVectors( ua, ub, 0.5 );
|
||||
if ( hasUV2s ) u2m.lerpVectors( u2a, u2b, 0.5 );
|
||||
|
||||
addTriangle( 0, 3, 2 );
|
||||
addTriangle( 3, 1, 2 );
|
||||
|
||||
} else if ( dbc >= dab && dbc >= dac ) {
|
||||
|
||||
vm.lerpVectors( vb, vc, 0.5 );
|
||||
if ( hasNormals ) nm.lerpVectors( nb, nc, 0.5 );
|
||||
if ( hasColors ) cm.lerpColors( cb, cc, 0.5 );
|
||||
if ( hasUVs ) um.lerpVectors( ub, uc, 0.5 );
|
||||
if ( hasUV2s ) u2m.lerpVectors( u2b, u2c, 0.5 );
|
||||
|
||||
addTriangle( 0, 1, 3 );
|
||||
addTriangle( 3, 2, 0 );
|
||||
|
||||
} else {
|
||||
|
||||
vm.lerpVectors( va, vc, 0.5 );
|
||||
if ( hasNormals ) nm.lerpVectors( na, nc, 0.5 );
|
||||
if ( hasColors ) cm.lerpColors( ca, cc, 0.5 );
|
||||
if ( hasUVs ) um.lerpVectors( ua, uc, 0.5 );
|
||||
if ( hasUV2s ) u2m.lerpVectors( u2a, u2c, 0.5 );
|
||||
|
||||
addTriangle( 0, 1, 3 );
|
||||
addTriangle( 3, 1, 2 );
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
addTriangle( 0, 1, 2 );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const geometry2 = new THREE.BufferGeometry();
|
||||
|
||||
geometry2.setAttribute( 'position', new THREE.Float32BufferAttribute( positions2, 3 ) );
|
||||
|
||||
if ( hasNormals ) {
|
||||
|
||||
geometry2.setAttribute( 'normal', new THREE.Float32BufferAttribute( normals2, 3 ) );
|
||||
|
||||
}
|
||||
|
||||
if ( hasColors ) {
|
||||
|
||||
geometry2.setAttribute( 'color', new THREE.Float32BufferAttribute( colors2, 3 ) );
|
||||
|
||||
}
|
||||
|
||||
if ( hasUVs ) {
|
||||
|
||||
geometry2.setAttribute( 'uv', new THREE.Float32BufferAttribute( uvs2, 2 ) );
|
||||
|
||||
}
|
||||
|
||||
if ( hasUV2s ) {
|
||||
|
||||
geometry2.setAttribute( 'uv2', new THREE.Float32BufferAttribute( uv2s2, 2 ) );
|
||||
|
||||
}
|
||||
|
||||
return geometry2;
|
||||
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue