viral-viewer-2 2.9.1 → 2.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -32903,6 +32903,1140 @@ function workerFunction() {
|
|
|
32903
32903
|
window.__THREE__ = REVISION;
|
|
32904
32904
|
}
|
|
32905
32905
|
}
|
|
32906
|
+
//#region BufferGeometry Utils
|
|
32907
|
+
function computeMikkTSpaceTangents(geometry, MikkTSpace, negateSign = true) {
|
|
32908
|
+
if (!MikkTSpace || !MikkTSpace.isReady) {
|
|
32909
|
+
throw new Error("BufferGeometryUtils: Initialized MikkTSpace library required.");
|
|
32910
|
+
}
|
|
32911
|
+
if (!geometry.hasAttribute("position") ||
|
|
32912
|
+
!geometry.hasAttribute("normal") ||
|
|
32913
|
+
!geometry.hasAttribute("uv")) {
|
|
32914
|
+
throw new Error('BufferGeometryUtils: Tangents require "position", "normal", and "uv" attributes.');
|
|
32915
|
+
}
|
|
32916
|
+
function getAttributeArray(attribute) {
|
|
32917
|
+
if (attribute.normalized || attribute.isInterleavedBufferAttribute) {
|
|
32918
|
+
const dstArray = new Float32Array(attribute.count * attribute.itemSize);
|
|
32919
|
+
for (let i = 0, j = 0; i < attribute.count; i++) {
|
|
32920
|
+
dstArray[j++] = attribute.getX(i);
|
|
32921
|
+
dstArray[j++] = attribute.getY(i);
|
|
32922
|
+
if (attribute.itemSize > 2) {
|
|
32923
|
+
dstArray[j++] = attribute.getZ(i);
|
|
32924
|
+
}
|
|
32925
|
+
}
|
|
32926
|
+
return dstArray;
|
|
32927
|
+
}
|
|
32928
|
+
if (attribute.array instanceof Float32Array) {
|
|
32929
|
+
return attribute.array;
|
|
32930
|
+
}
|
|
32931
|
+
return new Float32Array(attribute.array);
|
|
32932
|
+
}
|
|
32933
|
+
// MikkTSpace algorithm requires non-indexed input.
|
|
32934
|
+
const _geometry = geometry.index ? geometry.toNonIndexed() : geometry;
|
|
32935
|
+
// Compute vertex tangents.
|
|
32936
|
+
const tangents = MikkTSpace.generateTangents(getAttributeArray(_geometry.attributes.position), getAttributeArray(_geometry.attributes.normal), getAttributeArray(_geometry.attributes.uv));
|
|
32937
|
+
// Texture coordinate convention of glTF differs from the apparent
|
|
32938
|
+
// default of the MikkTSpace library; .w component must be flipped.
|
|
32939
|
+
if (negateSign) {
|
|
32940
|
+
for (let i = 3; i < tangents.length; i += 4) {
|
|
32941
|
+
tangents[i] *= -1;
|
|
32942
|
+
}
|
|
32943
|
+
}
|
|
32944
|
+
//
|
|
32945
|
+
_geometry.setAttribute("tangent", new BufferAttribute(tangents, 4));
|
|
32946
|
+
if (geometry !== _geometry) {
|
|
32947
|
+
geometry.copy(_geometry);
|
|
32948
|
+
}
|
|
32949
|
+
return geometry;
|
|
32950
|
+
}
|
|
32951
|
+
/**
|
|
32952
|
+
* @param {Array<BufferGeometry>} geometries
|
|
32953
|
+
* @param {Boolean} useGroups
|
|
32954
|
+
* @return {BufferGeometry}
|
|
32955
|
+
*/
|
|
32956
|
+
function mergeGeometries(geometries, useGroups = false) {
|
|
32957
|
+
const isIndexed = geometries[0].index !== null;
|
|
32958
|
+
const attributesUsed = new Set(Object.keys(geometries[0].attributes));
|
|
32959
|
+
const morphAttributesUsed = new Set(Object.keys(geometries[0].morphAttributes));
|
|
32960
|
+
const attributes = {};
|
|
32961
|
+
const morphAttributes = {};
|
|
32962
|
+
const morphTargetsRelative = geometries[0].morphTargetsRelative;
|
|
32963
|
+
const mergedGeometry = new BufferGeometry();
|
|
32964
|
+
let offset = 0;
|
|
32965
|
+
for (let i = 0; i < geometries.length; ++i) {
|
|
32966
|
+
const geometry = geometries[i];
|
|
32967
|
+
let attributesCount = 0;
|
|
32968
|
+
// ensure that all geometries are indexed, or none
|
|
32969
|
+
if (isIndexed !== (geometry.index !== null)) {
|
|
32970
|
+
console.error("THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index " +
|
|
32971
|
+
i +
|
|
32972
|
+
". All geometries must have compatible attributes; make sure index attribute exists among all geometries, or in none of them.");
|
|
32973
|
+
return null;
|
|
32974
|
+
}
|
|
32975
|
+
// gather attributes, exit early if they're different
|
|
32976
|
+
for (const name in geometry.attributes) {
|
|
32977
|
+
if (!attributesUsed.has(name)) {
|
|
32978
|
+
console.error("THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index " +
|
|
32979
|
+
i +
|
|
32980
|
+
'. All geometries must have compatible attributes; make sure "' +
|
|
32981
|
+
name +
|
|
32982
|
+
'" attribute exists among all geometries, or in none of them.');
|
|
32983
|
+
return null;
|
|
32984
|
+
}
|
|
32985
|
+
if (attributes[name] === undefined)
|
|
32986
|
+
attributes[name] = [];
|
|
32987
|
+
attributes[name].push(geometry.attributes[name]);
|
|
32988
|
+
attributesCount++;
|
|
32989
|
+
}
|
|
32990
|
+
// ensure geometries have the same number of attributes
|
|
32991
|
+
if (attributesCount !== attributesUsed.size) {
|
|
32992
|
+
console.error("THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index " +
|
|
32993
|
+
i +
|
|
32994
|
+
". Make sure all geometries have the same number of attributes.");
|
|
32995
|
+
return null;
|
|
32996
|
+
}
|
|
32997
|
+
// gather morph attributes, exit early if they're different
|
|
32998
|
+
if (morphTargetsRelative !== geometry.morphTargetsRelative) {
|
|
32999
|
+
console.error("THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index " +
|
|
33000
|
+
i +
|
|
33001
|
+
". .morphTargetsRelative must be consistent throughout all geometries.");
|
|
33002
|
+
return null;
|
|
33003
|
+
}
|
|
33004
|
+
for (const name in geometry.morphAttributes) {
|
|
33005
|
+
if (!morphAttributesUsed.has(name)) {
|
|
33006
|
+
console.error("THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index " +
|
|
33007
|
+
i +
|
|
33008
|
+
". .morphAttributes must be consistent throughout all geometries.");
|
|
33009
|
+
return null;
|
|
33010
|
+
}
|
|
33011
|
+
if (morphAttributes[name] === undefined)
|
|
33012
|
+
morphAttributes[name] = [];
|
|
33013
|
+
morphAttributes[name].push(geometry.morphAttributes[name]);
|
|
33014
|
+
}
|
|
33015
|
+
if (useGroups) {
|
|
33016
|
+
let count;
|
|
33017
|
+
if (isIndexed) {
|
|
33018
|
+
count = geometry.index.count;
|
|
33019
|
+
}
|
|
33020
|
+
else if (geometry.attributes.position !== undefined) {
|
|
33021
|
+
count = geometry.attributes.position.count;
|
|
33022
|
+
}
|
|
33023
|
+
else {
|
|
33024
|
+
console.error("THREE.BufferGeometryUtils: .mergeGeometries() failed with geometry at index " +
|
|
33025
|
+
i +
|
|
33026
|
+
". The geometry must have either an index or a position attribute");
|
|
33027
|
+
return null;
|
|
33028
|
+
}
|
|
33029
|
+
mergedGeometry.addGroup(offset, count, i);
|
|
33030
|
+
offset += count;
|
|
33031
|
+
}
|
|
33032
|
+
}
|
|
33033
|
+
// merge indices
|
|
33034
|
+
if (isIndexed) {
|
|
33035
|
+
let indexOffset = 0;
|
|
33036
|
+
const mergedIndex = [];
|
|
33037
|
+
for (let i = 0; i < geometries.length; ++i) {
|
|
33038
|
+
const index = geometries[i].index;
|
|
33039
|
+
for (let j = 0; j < index.count; ++j) {
|
|
33040
|
+
mergedIndex.push(index.getX(j) + indexOffset);
|
|
33041
|
+
}
|
|
33042
|
+
indexOffset += geometries[i].attributes.position.count;
|
|
33043
|
+
}
|
|
33044
|
+
mergedGeometry.setIndex(mergedIndex);
|
|
33045
|
+
}
|
|
33046
|
+
// merge attributes
|
|
33047
|
+
for (const name in attributes) {
|
|
33048
|
+
const mergedAttribute = mergeAttributes(attributes[name]);
|
|
33049
|
+
if (!mergedAttribute) {
|
|
33050
|
+
console.error("THREE.BufferGeometryUtils: .mergeGeometries() failed while trying to merge the " +
|
|
33051
|
+
name +
|
|
33052
|
+
" attribute.");
|
|
33053
|
+
return null;
|
|
33054
|
+
}
|
|
33055
|
+
mergedGeometry.setAttribute(name, mergedAttribute);
|
|
33056
|
+
}
|
|
33057
|
+
// merge morph attributes
|
|
33058
|
+
for (const name in morphAttributes) {
|
|
33059
|
+
const numMorphTargets = morphAttributes[name][0].length;
|
|
33060
|
+
if (numMorphTargets === 0)
|
|
33061
|
+
break;
|
|
33062
|
+
mergedGeometry.morphAttributes = mergedGeometry.morphAttributes || {};
|
|
33063
|
+
mergedGeometry.morphAttributes[name] = [];
|
|
33064
|
+
for (let i = 0; i < numMorphTargets; ++i) {
|
|
33065
|
+
const morphAttributesToMerge = [];
|
|
33066
|
+
for (let j = 0; j < morphAttributes[name].length; ++j) {
|
|
33067
|
+
morphAttributesToMerge.push(morphAttributes[name][j][i]);
|
|
33068
|
+
}
|
|
33069
|
+
const mergedMorphAttribute = mergeAttributes(morphAttributesToMerge);
|
|
33070
|
+
if (!mergedMorphAttribute) {
|
|
33071
|
+
console.error("THREE.BufferGeometryUtils: .mergeGeometries() failed while trying to merge the " +
|
|
33072
|
+
name +
|
|
33073
|
+
" morphAttribute.");
|
|
33074
|
+
return null;
|
|
33075
|
+
}
|
|
33076
|
+
mergedGeometry.morphAttributes[name].push(mergedMorphAttribute);
|
|
33077
|
+
}
|
|
33078
|
+
}
|
|
33079
|
+
return mergedGeometry;
|
|
33080
|
+
}
|
|
33081
|
+
/**
|
|
33082
|
+
* @param {Array<BufferAttribute>} attributes
|
|
33083
|
+
* @return {BufferAttribute}
|
|
33084
|
+
*/
|
|
33085
|
+
function mergeAttributes(attributes) {
|
|
33086
|
+
let TypedArray;
|
|
33087
|
+
let itemSize;
|
|
33088
|
+
let normalized;
|
|
33089
|
+
let gpuType = -1;
|
|
33090
|
+
let arrayLength = 0;
|
|
33091
|
+
for (let i = 0; i < attributes.length; ++i) {
|
|
33092
|
+
const attribute = attributes[i];
|
|
33093
|
+
if (attribute.isInterleavedBufferAttribute) {
|
|
33094
|
+
console.error("THREE.BufferGeometryUtils: .mergeAttributes() failed. InterleavedBufferAttributes are not supported.");
|
|
33095
|
+
return null;
|
|
33096
|
+
}
|
|
33097
|
+
if (TypedArray === undefined)
|
|
33098
|
+
TypedArray = attribute.array.constructor;
|
|
33099
|
+
if (TypedArray !== attribute.array.constructor) {
|
|
33100
|
+
console.error("THREE.BufferGeometryUtils: .mergeAttributes() failed. BufferAttribute.array must be of consistent array types across matching attributes.");
|
|
33101
|
+
return null;
|
|
33102
|
+
}
|
|
33103
|
+
if (itemSize === undefined)
|
|
33104
|
+
itemSize = attribute.itemSize;
|
|
33105
|
+
if (itemSize !== attribute.itemSize) {
|
|
33106
|
+
console.error("THREE.BufferGeometryUtils: .mergeAttributes() failed. BufferAttribute.itemSize must be consistent across matching attributes.");
|
|
33107
|
+
return null;
|
|
33108
|
+
}
|
|
33109
|
+
if (normalized === undefined)
|
|
33110
|
+
normalized = attribute.normalized;
|
|
33111
|
+
if (normalized !== attribute.normalized) {
|
|
33112
|
+
console.error("THREE.BufferGeometryUtils: .mergeAttributes() failed. BufferAttribute.normalized must be consistent across matching attributes.");
|
|
33113
|
+
return null;
|
|
33114
|
+
}
|
|
33115
|
+
if (gpuType === -1)
|
|
33116
|
+
gpuType = attribute.gpuType;
|
|
33117
|
+
if (gpuType !== attribute.gpuType) {
|
|
33118
|
+
console.error("THREE.BufferGeometryUtils: .mergeAttributes() failed. BufferAttribute.gpuType must be consistent across matching attributes.");
|
|
33119
|
+
return null;
|
|
33120
|
+
}
|
|
33121
|
+
arrayLength += attribute.array.length;
|
|
33122
|
+
}
|
|
33123
|
+
const array = new TypedArray(arrayLength);
|
|
33124
|
+
let offset = 0;
|
|
33125
|
+
for (let i = 0; i < attributes.length; ++i) {
|
|
33126
|
+
array.set(attributes[i].array, offset);
|
|
33127
|
+
offset += attributes[i].array.length;
|
|
33128
|
+
}
|
|
33129
|
+
const result = new BufferAttribute(array, itemSize, normalized);
|
|
33130
|
+
if (gpuType !== undefined) {
|
|
33131
|
+
result.gpuType = gpuType;
|
|
33132
|
+
}
|
|
33133
|
+
return result;
|
|
33134
|
+
}
|
|
33135
|
+
/**
|
|
33136
|
+
* @param {BufferAttribute}
|
|
33137
|
+
* @return {BufferAttribute}
|
|
33138
|
+
*/
|
|
33139
|
+
function deepCloneAttribute(attribute) {
|
|
33140
|
+
if (attribute.isInstancedInterleavedBufferAttribute ||
|
|
33141
|
+
attribute.isInterleavedBufferAttribute) {
|
|
33142
|
+
return deinterleaveAttribute(attribute);
|
|
33143
|
+
}
|
|
33144
|
+
if (attribute.isInstancedBufferAttribute) {
|
|
33145
|
+
return new InstancedBufferAttribute().copy(attribute);
|
|
33146
|
+
}
|
|
33147
|
+
return new BufferAttribute().copy(attribute);
|
|
33148
|
+
}
|
|
33149
|
+
/**
|
|
33150
|
+
* @param {Array<BufferAttribute>} attributes
|
|
33151
|
+
* @return {Array<InterleavedBufferAttribute>}
|
|
33152
|
+
*/
|
|
33153
|
+
function interleaveAttributes(attributes) {
|
|
33154
|
+
// Interleaves the provided attributes into an InterleavedBuffer and returns
|
|
33155
|
+
// a set of InterleavedBufferAttributes for each attribute
|
|
33156
|
+
let TypedArray;
|
|
33157
|
+
let arrayLength = 0;
|
|
33158
|
+
let stride = 0;
|
|
33159
|
+
// calculate the length and type of the interleavedBuffer
|
|
33160
|
+
for (let i = 0, l = attributes.length; i < l; ++i) {
|
|
33161
|
+
const attribute = attributes[i];
|
|
33162
|
+
if (TypedArray === undefined)
|
|
33163
|
+
TypedArray = attribute.array.constructor;
|
|
33164
|
+
if (TypedArray !== attribute.array.constructor) {
|
|
33165
|
+
console.error("AttributeBuffers of different types cannot be interleaved");
|
|
33166
|
+
return null;
|
|
33167
|
+
}
|
|
33168
|
+
arrayLength += attribute.array.length;
|
|
33169
|
+
stride += attribute.itemSize;
|
|
33170
|
+
}
|
|
33171
|
+
// Create the set of buffer attributes
|
|
33172
|
+
const interleavedBuffer = new InterleavedBuffer(new TypedArray(arrayLength), stride);
|
|
33173
|
+
let offset = 0;
|
|
33174
|
+
const res = [];
|
|
33175
|
+
const getters = ["getX", "getY", "getZ", "getW"];
|
|
33176
|
+
const setters = ["setX", "setY", "setZ", "setW"];
|
|
33177
|
+
for (let j = 0, l = attributes.length; j < l; j++) {
|
|
33178
|
+
const attribute = attributes[j];
|
|
33179
|
+
const itemSize = attribute.itemSize;
|
|
33180
|
+
const count = attribute.count;
|
|
33181
|
+
const iba = new InterleavedBufferAttribute(interleavedBuffer, itemSize, offset, attribute.normalized);
|
|
33182
|
+
res.push(iba);
|
|
33183
|
+
offset += itemSize;
|
|
33184
|
+
// Move the data for each attribute into the new interleavedBuffer
|
|
33185
|
+
// at the appropriate offset
|
|
33186
|
+
for (let c = 0; c < count; c++) {
|
|
33187
|
+
for (let k = 0; k < itemSize; k++) {
|
|
33188
|
+
iba[setters[k]](c, attribute[getters[k]](c));
|
|
33189
|
+
}
|
|
33190
|
+
}
|
|
33191
|
+
}
|
|
33192
|
+
return res;
|
|
33193
|
+
}
|
|
33194
|
+
// returns a new, non-interleaved version of the provided attribute
|
|
33195
|
+
function deinterleaveAttribute(attribute) {
|
|
33196
|
+
const cons = attribute.data.array.constructor;
|
|
33197
|
+
const count = attribute.count;
|
|
33198
|
+
const itemSize = attribute.itemSize;
|
|
33199
|
+
const normalized = attribute.normalized;
|
|
33200
|
+
const array = new cons(count * itemSize);
|
|
33201
|
+
let newAttribute;
|
|
33202
|
+
if (attribute.isInstancedInterleavedBufferAttribute) {
|
|
33203
|
+
newAttribute = new InstancedBufferAttribute(array, itemSize, normalized, attribute.meshPerAttribute);
|
|
33204
|
+
}
|
|
33205
|
+
else {
|
|
33206
|
+
newAttribute = new BufferAttribute(array, itemSize, normalized);
|
|
33207
|
+
}
|
|
33208
|
+
for (let i = 0; i < count; i++) {
|
|
33209
|
+
newAttribute.setX(i, attribute.getX(i));
|
|
33210
|
+
if (itemSize >= 2) {
|
|
33211
|
+
newAttribute.setY(i, attribute.getY(i));
|
|
33212
|
+
}
|
|
33213
|
+
if (itemSize >= 3) {
|
|
33214
|
+
newAttribute.setZ(i, attribute.getZ(i));
|
|
33215
|
+
}
|
|
33216
|
+
if (itemSize >= 4) {
|
|
33217
|
+
newAttribute.setW(i, attribute.getW(i));
|
|
33218
|
+
}
|
|
33219
|
+
}
|
|
33220
|
+
return newAttribute;
|
|
33221
|
+
}
|
|
33222
|
+
// deinterleaves all attributes on the geometry
|
|
33223
|
+
function deinterleaveGeometry(geometry) {
|
|
33224
|
+
const attributes = geometry.attributes;
|
|
33225
|
+
const morphTargets = geometry.morphTargets;
|
|
33226
|
+
const attrMap = new Map();
|
|
33227
|
+
for (const key in attributes) {
|
|
33228
|
+
const attr = attributes[key];
|
|
33229
|
+
if (attr.isInterleavedBufferAttribute) {
|
|
33230
|
+
if (!attrMap.has(attr)) {
|
|
33231
|
+
attrMap.set(attr, deinterleaveAttribute(attr));
|
|
33232
|
+
}
|
|
33233
|
+
attributes[key] = attrMap.get(attr);
|
|
33234
|
+
}
|
|
33235
|
+
}
|
|
33236
|
+
for (const key in morphTargets) {
|
|
33237
|
+
const attr = morphTargets[key];
|
|
33238
|
+
if (attr.isInterleavedBufferAttribute) {
|
|
33239
|
+
if (!attrMap.has(attr)) {
|
|
33240
|
+
attrMap.set(attr, deinterleaveAttribute(attr));
|
|
33241
|
+
}
|
|
33242
|
+
morphTargets[key] = attrMap.get(attr);
|
|
33243
|
+
}
|
|
33244
|
+
}
|
|
33245
|
+
}
|
|
33246
|
+
/**
|
|
33247
|
+
* @param {BufferGeometry} geometry
|
|
33248
|
+
* @return {number}
|
|
33249
|
+
*/
|
|
33250
|
+
function estimateBytesUsed(geometry) {
|
|
33251
|
+
// Return the estimated memory used by this geometry in bytes
|
|
33252
|
+
// Calculate using itemSize, count, and BYTES_PER_ELEMENT to account
|
|
33253
|
+
// for InterleavedBufferAttributes.
|
|
33254
|
+
let mem = 0;
|
|
33255
|
+
for (const name in geometry.attributes) {
|
|
33256
|
+
const attr = geometry.getAttribute(name);
|
|
33257
|
+
mem += attr.count * attr.itemSize * attr.array.BYTES_PER_ELEMENT;
|
|
33258
|
+
}
|
|
33259
|
+
const indices = geometry.getIndex();
|
|
33260
|
+
mem += indices
|
|
33261
|
+
? indices.count * indices.itemSize * indices.array.BYTES_PER_ELEMENT
|
|
33262
|
+
: 0;
|
|
33263
|
+
return mem;
|
|
33264
|
+
}
|
|
33265
|
+
/**
|
|
33266
|
+
* @param {BufferGeometry} geometry
|
|
33267
|
+
* @param {number} tolerance
|
|
33268
|
+
* @return {BufferGeometry}
|
|
33269
|
+
*/
|
|
33270
|
+
function mergeVertices(geometry, tolerance = 1e-4) {
|
|
33271
|
+
tolerance = Math.max(tolerance, Number.EPSILON);
|
|
33272
|
+
// Generate an index buffer if the geometry doesn't have one, or optimize it
|
|
33273
|
+
// if it's already available.
|
|
33274
|
+
const hashToIndex = {};
|
|
33275
|
+
const indices = geometry.getIndex();
|
|
33276
|
+
const positions = geometry.getAttribute("position");
|
|
33277
|
+
const vertexCount = indices ? indices.count : positions.count;
|
|
33278
|
+
// next value for triangle indices
|
|
33279
|
+
let nextIndex = 0;
|
|
33280
|
+
// attributes and new attribute arrays
|
|
33281
|
+
const attributeNames = Object.keys(geometry.attributes);
|
|
33282
|
+
const tmpAttributes = {};
|
|
33283
|
+
const tmpMorphAttributes = {};
|
|
33284
|
+
const newIndices = [];
|
|
33285
|
+
const getters = ["getX", "getY", "getZ", "getW"];
|
|
33286
|
+
const setters = ["setX", "setY", "setZ", "setW"];
|
|
33287
|
+
// Initialize the arrays, allocating space conservatively. Extra
|
|
33288
|
+
// space will be trimmed in the last step.
|
|
33289
|
+
for (let i = 0, l = attributeNames.length; i < l; i++) {
|
|
33290
|
+
const name = attributeNames[i];
|
|
33291
|
+
const attr = geometry.attributes[name];
|
|
33292
|
+
tmpAttributes[name] = new BufferAttribute(new attr.array.constructor(attr.count * attr.itemSize), attr.itemSize, attr.normalized);
|
|
33293
|
+
const morphAttr = geometry.morphAttributes[name];
|
|
33294
|
+
if (morphAttr) {
|
|
33295
|
+
tmpMorphAttributes[name] = new BufferAttribute(new morphAttr.array.constructor(morphAttr.count * morphAttr.itemSize), morphAttr.itemSize, morphAttr.normalized);
|
|
33296
|
+
}
|
|
33297
|
+
}
|
|
33298
|
+
// convert the error tolerance to an amount of decimal places to truncate to
|
|
33299
|
+
const decimalShift = Math.log10(1 / tolerance);
|
|
33300
|
+
const shiftMultiplier = Math.pow(10, decimalShift);
|
|
33301
|
+
for (let i = 0; i < vertexCount; i++) {
|
|
33302
|
+
const index = indices ? indices.getX(i) : i;
|
|
33303
|
+
// Generate a hash for the vertex attributes at the current index 'i'
|
|
33304
|
+
let hash = "";
|
|
33305
|
+
for (let j = 0, l = attributeNames.length; j < l; j++) {
|
|
33306
|
+
const name = attributeNames[j];
|
|
33307
|
+
const attribute = geometry.getAttribute(name);
|
|
33308
|
+
const itemSize = attribute.itemSize;
|
|
33309
|
+
for (let k = 0; k < itemSize; k++) {
|
|
33310
|
+
// double tilde truncates the decimal value
|
|
33311
|
+
hash += `${~~(attribute[getters[k]](index) * shiftMultiplier)},`;
|
|
33312
|
+
}
|
|
33313
|
+
}
|
|
33314
|
+
// Add another reference to the vertex if it's already
|
|
33315
|
+
// used by another index
|
|
33316
|
+
if (hash in hashToIndex) {
|
|
33317
|
+
newIndices.push(hashToIndex[hash]);
|
|
33318
|
+
}
|
|
33319
|
+
else {
|
|
33320
|
+
// copy data to the new index in the temporary attributes
|
|
33321
|
+
for (let j = 0, l = attributeNames.length; j < l; j++) {
|
|
33322
|
+
const name = attributeNames[j];
|
|
33323
|
+
const attribute = geometry.getAttribute(name);
|
|
33324
|
+
const morphAttr = geometry.morphAttributes[name];
|
|
33325
|
+
const itemSize = attribute.itemSize;
|
|
33326
|
+
const newarray = tmpAttributes[name];
|
|
33327
|
+
const newMorphArrays = tmpMorphAttributes[name];
|
|
33328
|
+
for (let k = 0; k < itemSize; k++) {
|
|
33329
|
+
const getterFunc = getters[k];
|
|
33330
|
+
const setterFunc = setters[k];
|
|
33331
|
+
newarray[setterFunc](nextIndex, attribute[getterFunc](index));
|
|
33332
|
+
if (morphAttr) {
|
|
33333
|
+
for (let m = 0, ml = morphAttr.length; m < ml; m++) {
|
|
33334
|
+
newMorphArrays[m][setterFunc](nextIndex, morphAttr[m][getterFunc](index));
|
|
33335
|
+
}
|
|
33336
|
+
}
|
|
33337
|
+
}
|
|
33338
|
+
}
|
|
33339
|
+
hashToIndex[hash] = nextIndex;
|
|
33340
|
+
newIndices.push(nextIndex);
|
|
33341
|
+
nextIndex++;
|
|
33342
|
+
}
|
|
33343
|
+
}
|
|
33344
|
+
// generate result BufferGeometry
|
|
33345
|
+
const result = geometry.clone();
|
|
33346
|
+
for (const name in geometry.attributes) {
|
|
33347
|
+
const tmpAttribute = tmpAttributes[name];
|
|
33348
|
+
result.setAttribute(name, new BufferAttribute(tmpAttribute.array.slice(0, nextIndex * tmpAttribute.itemSize), tmpAttribute.itemSize, tmpAttribute.normalized));
|
|
33349
|
+
if (!(name in tmpMorphAttributes))
|
|
33350
|
+
continue;
|
|
33351
|
+
for (let j = 0; j < tmpMorphAttributes[name].length; j++) {
|
|
33352
|
+
const tmpMorphAttribute = tmpMorphAttributes[name][j];
|
|
33353
|
+
result.morphAttributes[name][j] = new BufferAttribute(tmpMorphAttribute.array.slice(0, nextIndex * tmpMorphAttribute.itemSize), tmpMorphAttribute.itemSize, tmpMorphAttribute.normalized);
|
|
33354
|
+
}
|
|
33355
|
+
}
|
|
33356
|
+
// indices
|
|
33357
|
+
result.setIndex(newIndices);
|
|
33358
|
+
return result;
|
|
33359
|
+
}
|
|
33360
|
+
/**
|
|
33361
|
+
* @param {BufferGeometry} geometry
|
|
33362
|
+
* @param {number} drawMode
|
|
33363
|
+
* @return {BufferGeometry}
|
|
33364
|
+
*/
|
|
33365
|
+
function toTrianglesDrawMode(geometry, drawMode) {
|
|
33366
|
+
if (drawMode === TrianglesDrawMode) {
|
|
33367
|
+
console.warn("THREE.BufferGeometryUtils.toTrianglesDrawMode(): Geometry already defined as triangles.");
|
|
33368
|
+
return geometry;
|
|
33369
|
+
}
|
|
33370
|
+
if (drawMode === TriangleFanDrawMode ||
|
|
33371
|
+
drawMode === TriangleStripDrawMode) {
|
|
33372
|
+
let index = geometry.getIndex();
|
|
33373
|
+
// generate index if not present
|
|
33374
|
+
if (index === null) {
|
|
33375
|
+
const indices = [];
|
|
33376
|
+
const position = geometry.getAttribute("position");
|
|
33377
|
+
if (position !== undefined) {
|
|
33378
|
+
for (let i = 0; i < position.count; i++) {
|
|
33379
|
+
indices.push(i);
|
|
33380
|
+
}
|
|
33381
|
+
geometry.setIndex(indices);
|
|
33382
|
+
index = geometry.getIndex();
|
|
33383
|
+
}
|
|
33384
|
+
else {
|
|
33385
|
+
console.error("THREE.BufferGeometryUtils.toTrianglesDrawMode(): Undefined position attribute. Processing not possible.");
|
|
33386
|
+
return geometry;
|
|
33387
|
+
}
|
|
33388
|
+
}
|
|
33389
|
+
//
|
|
33390
|
+
const numberOfTriangles = index.count - 2;
|
|
33391
|
+
const newIndices = [];
|
|
33392
|
+
if (drawMode === TriangleFanDrawMode) {
|
|
33393
|
+
// gl.TRIANGLE_FAN
|
|
33394
|
+
for (let i = 1; i <= numberOfTriangles; i++) {
|
|
33395
|
+
newIndices.push(index.getX(0));
|
|
33396
|
+
newIndices.push(index.getX(i));
|
|
33397
|
+
newIndices.push(index.getX(i + 1));
|
|
33398
|
+
}
|
|
33399
|
+
}
|
|
33400
|
+
else {
|
|
33401
|
+
// gl.TRIANGLE_STRIP
|
|
33402
|
+
for (let i = 0; i < numberOfTriangles; i++) {
|
|
33403
|
+
if (i % 2 === 0) {
|
|
33404
|
+
newIndices.push(index.getX(i));
|
|
33405
|
+
newIndices.push(index.getX(i + 1));
|
|
33406
|
+
newIndices.push(index.getX(i + 2));
|
|
33407
|
+
}
|
|
33408
|
+
else {
|
|
33409
|
+
newIndices.push(index.getX(i + 2));
|
|
33410
|
+
newIndices.push(index.getX(i + 1));
|
|
33411
|
+
newIndices.push(index.getX(i));
|
|
33412
|
+
}
|
|
33413
|
+
}
|
|
33414
|
+
}
|
|
33415
|
+
if (newIndices.length / 3 !== numberOfTriangles) {
|
|
33416
|
+
console.error("THREE.BufferGeometryUtils.toTrianglesDrawMode(): Unable to generate correct amount of triangles.");
|
|
33417
|
+
}
|
|
33418
|
+
// build final geometry
|
|
33419
|
+
const newGeometry = geometry.clone();
|
|
33420
|
+
newGeometry.setIndex(newIndices);
|
|
33421
|
+
newGeometry.clearGroups();
|
|
33422
|
+
return newGeometry;
|
|
33423
|
+
}
|
|
33424
|
+
else {
|
|
33425
|
+
console.error("THREE.BufferGeometryUtils.toTrianglesDrawMode(): Unknown draw mode:", drawMode);
|
|
33426
|
+
return geometry;
|
|
33427
|
+
}
|
|
33428
|
+
}
|
|
33429
|
+
/**
|
|
33430
|
+
* Calculates the morphed attributes of a morphed/skinned BufferGeometry.
|
|
33431
|
+
* Helpful for Raytracing or Decals.
|
|
33432
|
+
* @param {Mesh | Line | Points} object An instance of Mesh, Line or Points.
|
|
33433
|
+
* @return {Object} An Object with original position/normal attributes and morphed ones.
|
|
33434
|
+
*/
|
|
33435
|
+
function computeMorphedAttributes(object) {
|
|
33436
|
+
const _vA = new Vector3();
|
|
33437
|
+
const _vB = new Vector3();
|
|
33438
|
+
const _vC = new Vector3();
|
|
33439
|
+
const _tempA = new Vector3();
|
|
33440
|
+
const _tempB = new Vector3();
|
|
33441
|
+
const _tempC = new Vector3();
|
|
33442
|
+
const _morphA = new Vector3();
|
|
33443
|
+
const _morphB = new Vector3();
|
|
33444
|
+
const _morphC = new Vector3();
|
|
33445
|
+
function _calculateMorphedAttributeData(object, attribute, morphAttribute, morphTargetsRelative, a, b, c, modifiedAttributeArray) {
|
|
33446
|
+
_vA.fromBufferAttribute(attribute, a);
|
|
33447
|
+
_vB.fromBufferAttribute(attribute, b);
|
|
33448
|
+
_vC.fromBufferAttribute(attribute, c);
|
|
33449
|
+
const morphInfluences = object.morphTargetInfluences;
|
|
33450
|
+
if (morphAttribute && morphInfluences) {
|
|
33451
|
+
_morphA.set(0, 0, 0);
|
|
33452
|
+
_morphB.set(0, 0, 0);
|
|
33453
|
+
_morphC.set(0, 0, 0);
|
|
33454
|
+
for (let i = 0, il = morphAttribute.length; i < il; i++) {
|
|
33455
|
+
const influence = morphInfluences[i];
|
|
33456
|
+
const morph = morphAttribute[i];
|
|
33457
|
+
if (influence === 0)
|
|
33458
|
+
continue;
|
|
33459
|
+
_tempA.fromBufferAttribute(morph, a);
|
|
33460
|
+
_tempB.fromBufferAttribute(morph, b);
|
|
33461
|
+
_tempC.fromBufferAttribute(morph, c);
|
|
33462
|
+
if (morphTargetsRelative) {
|
|
33463
|
+
_morphA.addScaledVector(_tempA, influence);
|
|
33464
|
+
_morphB.addScaledVector(_tempB, influence);
|
|
33465
|
+
_morphC.addScaledVector(_tempC, influence);
|
|
33466
|
+
}
|
|
33467
|
+
else {
|
|
33468
|
+
_morphA.addScaledVector(_tempA.sub(_vA), influence);
|
|
33469
|
+
_morphB.addScaledVector(_tempB.sub(_vB), influence);
|
|
33470
|
+
_morphC.addScaledVector(_tempC.sub(_vC), influence);
|
|
33471
|
+
}
|
|
33472
|
+
}
|
|
33473
|
+
_vA.add(_morphA);
|
|
33474
|
+
_vB.add(_morphB);
|
|
33475
|
+
_vC.add(_morphC);
|
|
33476
|
+
}
|
|
33477
|
+
if (object.isSkinnedMesh) {
|
|
33478
|
+
object.applyBoneTransform(a, _vA);
|
|
33479
|
+
object.applyBoneTransform(b, _vB);
|
|
33480
|
+
object.applyBoneTransform(c, _vC);
|
|
33481
|
+
}
|
|
33482
|
+
modifiedAttributeArray[a * 3 + 0] = _vA.x;
|
|
33483
|
+
modifiedAttributeArray[a * 3 + 1] = _vA.y;
|
|
33484
|
+
modifiedAttributeArray[a * 3 + 2] = _vA.z;
|
|
33485
|
+
modifiedAttributeArray[b * 3 + 0] = _vB.x;
|
|
33486
|
+
modifiedAttributeArray[b * 3 + 1] = _vB.y;
|
|
33487
|
+
modifiedAttributeArray[b * 3 + 2] = _vB.z;
|
|
33488
|
+
modifiedAttributeArray[c * 3 + 0] = _vC.x;
|
|
33489
|
+
modifiedAttributeArray[c * 3 + 1] = _vC.y;
|
|
33490
|
+
modifiedAttributeArray[c * 3 + 2] = _vC.z;
|
|
33491
|
+
}
|
|
33492
|
+
const geometry = object.geometry;
|
|
33493
|
+
const material = object.material;
|
|
33494
|
+
let a, b, c;
|
|
33495
|
+
const index = geometry.index;
|
|
33496
|
+
const positionAttribute = geometry.attributes.position;
|
|
33497
|
+
const morphPosition = geometry.morphAttributes.position;
|
|
33498
|
+
const morphTargetsRelative = geometry.morphTargetsRelative;
|
|
33499
|
+
const normalAttribute = geometry.attributes.normal;
|
|
33500
|
+
const morphNormal = geometry.morphAttributes.position;
|
|
33501
|
+
const groups = geometry.groups;
|
|
33502
|
+
const drawRange = geometry.drawRange;
|
|
33503
|
+
let i, j, il, jl;
|
|
33504
|
+
let group;
|
|
33505
|
+
let start, end;
|
|
33506
|
+
const modifiedPosition = new Float32Array(positionAttribute.count * positionAttribute.itemSize);
|
|
33507
|
+
const modifiedNormal = new Float32Array(normalAttribute.count * normalAttribute.itemSize);
|
|
33508
|
+
if (index !== null) {
|
|
33509
|
+
// indexed buffer geometry
|
|
33510
|
+
if (Array.isArray(material)) {
|
|
33511
|
+
for (i = 0, il = groups.length; i < il; i++) {
|
|
33512
|
+
group = groups[i];
|
|
33513
|
+
start = Math.max(group.start, drawRange.start);
|
|
33514
|
+
end = Math.min(group.start + group.count, drawRange.start + drawRange.count);
|
|
33515
|
+
for (j = start, jl = end; j < jl; j += 3) {
|
|
33516
|
+
a = index.getX(j);
|
|
33517
|
+
b = index.getX(j + 1);
|
|
33518
|
+
c = index.getX(j + 2);
|
|
33519
|
+
_calculateMorphedAttributeData(object, positionAttribute, morphPosition, morphTargetsRelative, a, b, c, modifiedPosition);
|
|
33520
|
+
_calculateMorphedAttributeData(object, normalAttribute, morphNormal, morphTargetsRelative, a, b, c, modifiedNormal);
|
|
33521
|
+
}
|
|
33522
|
+
}
|
|
33523
|
+
}
|
|
33524
|
+
else {
|
|
33525
|
+
start = Math.max(0, drawRange.start);
|
|
33526
|
+
end = Math.min(index.count, drawRange.start + drawRange.count);
|
|
33527
|
+
for (i = start, il = end; i < il; i += 3) {
|
|
33528
|
+
a = index.getX(i);
|
|
33529
|
+
b = index.getX(i + 1);
|
|
33530
|
+
c = index.getX(i + 2);
|
|
33531
|
+
_calculateMorphedAttributeData(object, positionAttribute, morphPosition, morphTargetsRelative, a, b, c, modifiedPosition);
|
|
33532
|
+
_calculateMorphedAttributeData(object, normalAttribute, morphNormal, morphTargetsRelative, a, b, c, modifiedNormal);
|
|
33533
|
+
}
|
|
33534
|
+
}
|
|
33535
|
+
}
|
|
33536
|
+
else {
|
|
33537
|
+
// non-indexed buffer geometry
|
|
33538
|
+
if (Array.isArray(material)) {
|
|
33539
|
+
for (i = 0, il = groups.length; i < il; i++) {
|
|
33540
|
+
group = groups[i];
|
|
33541
|
+
start = Math.max(group.start, drawRange.start);
|
|
33542
|
+
end = Math.min(group.start + group.count, drawRange.start + drawRange.count);
|
|
33543
|
+
for (j = start, jl = end; j < jl; j += 3) {
|
|
33544
|
+
a = j;
|
|
33545
|
+
b = j + 1;
|
|
33546
|
+
c = j + 2;
|
|
33547
|
+
_calculateMorphedAttributeData(object, positionAttribute, morphPosition, morphTargetsRelative, a, b, c, modifiedPosition);
|
|
33548
|
+
_calculateMorphedAttributeData(object, normalAttribute, morphNormal, morphTargetsRelative, a, b, c, modifiedNormal);
|
|
33549
|
+
}
|
|
33550
|
+
}
|
|
33551
|
+
}
|
|
33552
|
+
else {
|
|
33553
|
+
start = Math.max(0, drawRange.start);
|
|
33554
|
+
end = Math.min(positionAttribute.count, drawRange.start + drawRange.count);
|
|
33555
|
+
for (i = start, il = end; i < il; i += 3) {
|
|
33556
|
+
a = i;
|
|
33557
|
+
b = i + 1;
|
|
33558
|
+
c = i + 2;
|
|
33559
|
+
_calculateMorphedAttributeData(object, positionAttribute, morphPosition, morphTargetsRelative, a, b, c, modifiedPosition);
|
|
33560
|
+
_calculateMorphedAttributeData(object, normalAttribute, morphNormal, morphTargetsRelative, a, b, c, modifiedNormal);
|
|
33561
|
+
}
|
|
33562
|
+
}
|
|
33563
|
+
}
|
|
33564
|
+
const morphedPositionAttribute = new Float32BufferAttribute(modifiedPosition, 3);
|
|
33565
|
+
const morphedNormalAttribute = new Float32BufferAttribute(modifiedNormal, 3);
|
|
33566
|
+
return {
|
|
33567
|
+
positionAttribute: positionAttribute,
|
|
33568
|
+
normalAttribute: normalAttribute,
|
|
33569
|
+
morphedPositionAttribute: morphedPositionAttribute,
|
|
33570
|
+
morphedNormalAttribute: morphedNormalAttribute,
|
|
33571
|
+
};
|
|
33572
|
+
}
|
|
33573
|
+
function mergeGroups(geometry) {
|
|
33574
|
+
if (geometry.groups.length === 0) {
|
|
33575
|
+
console.warn("THREE.BufferGeometryUtils.mergeGroups(): No groups are defined. Nothing to merge.");
|
|
33576
|
+
return geometry;
|
|
33577
|
+
}
|
|
33578
|
+
let groups = geometry.groups;
|
|
33579
|
+
// sort groups by material index
|
|
33580
|
+
groups = groups.sort((a, b) => {
|
|
33581
|
+
if (a.materialIndex !== b.materialIndex)
|
|
33582
|
+
return a.materialIndex - b.materialIndex;
|
|
33583
|
+
return a.start - b.start;
|
|
33584
|
+
});
|
|
33585
|
+
// create index for non-indexed geometries
|
|
33586
|
+
if (geometry.getIndex() === null) {
|
|
33587
|
+
const positionAttribute = geometry.getAttribute("position");
|
|
33588
|
+
const indices = [];
|
|
33589
|
+
for (let i = 0; i < positionAttribute.count; i += 3) {
|
|
33590
|
+
indices.push(i, i + 1, i + 2);
|
|
33591
|
+
}
|
|
33592
|
+
geometry.setIndex(indices);
|
|
33593
|
+
}
|
|
33594
|
+
// sort index
|
|
33595
|
+
const index = geometry.getIndex();
|
|
33596
|
+
const newIndices = [];
|
|
33597
|
+
for (let i = 0; i < groups.length; i++) {
|
|
33598
|
+
const group = groups[i];
|
|
33599
|
+
const groupStart = group.start;
|
|
33600
|
+
const groupLength = groupStart + group.count;
|
|
33601
|
+
for (let j = groupStart; j < groupLength; j++) {
|
|
33602
|
+
newIndices.push(index.getX(j));
|
|
33603
|
+
}
|
|
33604
|
+
}
|
|
33605
|
+
geometry.dispose(); // Required to force buffer recreation
|
|
33606
|
+
geometry.setIndex(newIndices);
|
|
33607
|
+
// update groups indices
|
|
33608
|
+
let start = 0;
|
|
33609
|
+
for (let i = 0; i < groups.length; i++) {
|
|
33610
|
+
const group = groups[i];
|
|
33611
|
+
group.start = start;
|
|
33612
|
+
start += group.count;
|
|
33613
|
+
}
|
|
33614
|
+
// merge groups
|
|
33615
|
+
let currentGroup = groups[0];
|
|
33616
|
+
geometry.groups = [currentGroup];
|
|
33617
|
+
for (let i = 1; i < groups.length; i++) {
|
|
33618
|
+
const group = groups[i];
|
|
33619
|
+
if (currentGroup.materialIndex === group.materialIndex) {
|
|
33620
|
+
currentGroup.count += group.count;
|
|
33621
|
+
}
|
|
33622
|
+
else {
|
|
33623
|
+
currentGroup = group;
|
|
33624
|
+
geometry.groups.push(currentGroup);
|
|
33625
|
+
}
|
|
33626
|
+
}
|
|
33627
|
+
return geometry;
|
|
33628
|
+
}
|
|
33629
|
+
/**
|
|
33630
|
+
* Modifies the supplied geometry if it is non-indexed, otherwise creates a new,
|
|
33631
|
+
* non-indexed geometry. Returns the geometry with smooth normals everywhere except
|
|
33632
|
+
* faces that meet at an angle greater than the crease angle.
|
|
33633
|
+
*
|
|
33634
|
+
* @param {BufferGeometry} geometry
|
|
33635
|
+
* @param {number} [creaseAngle]
|
|
33636
|
+
* @return {BufferGeometry}
|
|
33637
|
+
*/
|
|
33638
|
+
function toCreasedNormals(geometry, creaseAngle = Math.PI / 3 /* 60 degrees */) {
|
|
33639
|
+
const creaseDot = Math.cos(creaseAngle);
|
|
33640
|
+
const hashMultiplier = (1 + 1e-10) * 1e2;
|
|
33641
|
+
// reusable vectors
|
|
33642
|
+
const verts = [new Vector3(), new Vector3(), new Vector3()];
|
|
33643
|
+
const tempVec1 = new Vector3();
|
|
33644
|
+
const tempVec2 = new Vector3();
|
|
33645
|
+
const tempNorm = new Vector3();
|
|
33646
|
+
const tempNorm2 = new Vector3();
|
|
33647
|
+
// hashes a vector
|
|
33648
|
+
function hashVertex(v) {
|
|
33649
|
+
const x = ~~(v.x * hashMultiplier);
|
|
33650
|
+
const y = ~~(v.y * hashMultiplier);
|
|
33651
|
+
const z = ~~(v.z * hashMultiplier);
|
|
33652
|
+
return `${x},${y},${z}`;
|
|
33653
|
+
}
|
|
33654
|
+
// BufferGeometry.toNonIndexed() warns if the geometry is non-indexed
|
|
33655
|
+
// and returns the original geometry
|
|
33656
|
+
const resultGeometry = geometry.index ? geometry.toNonIndexed() : geometry;
|
|
33657
|
+
const posAttr = resultGeometry.attributes.position;
|
|
33658
|
+
const vertexMap = {};
|
|
33659
|
+
// find all the normals shared by commonly located vertices
|
|
33660
|
+
for (let i = 0, l = posAttr.count / 3; i < l; i++) {
|
|
33661
|
+
const i3 = 3 * i;
|
|
33662
|
+
const a = verts[0].fromBufferAttribute(posAttr, i3 + 0);
|
|
33663
|
+
const b = verts[1].fromBufferAttribute(posAttr, i3 + 1);
|
|
33664
|
+
const c = verts[2].fromBufferAttribute(posAttr, i3 + 2);
|
|
33665
|
+
tempVec1.subVectors(c, b);
|
|
33666
|
+
tempVec2.subVectors(a, b);
|
|
33667
|
+
// add the normal to the map for all vertices
|
|
33668
|
+
const normal = new Vector3().crossVectors(tempVec1, tempVec2).normalize();
|
|
33669
|
+
for (let n = 0; n < 3; n++) {
|
|
33670
|
+
const vert = verts[n];
|
|
33671
|
+
const hash = hashVertex(vert);
|
|
33672
|
+
if (!(hash in vertexMap)) {
|
|
33673
|
+
vertexMap[hash] = [];
|
|
33674
|
+
}
|
|
33675
|
+
vertexMap[hash].push(normal);
|
|
33676
|
+
}
|
|
33677
|
+
}
|
|
33678
|
+
// average normals from all vertices that share a common location if they are within the
|
|
33679
|
+
// provided crease threshold
|
|
33680
|
+
const normalArray = new Float32Array(posAttr.count * 3);
|
|
33681
|
+
const normAttr = new BufferAttribute(normalArray, 3, false);
|
|
33682
|
+
for (let i = 0, l = posAttr.count / 3; i < l; i++) {
|
|
33683
|
+
// get the face normal for this vertex
|
|
33684
|
+
const i3 = 3 * i;
|
|
33685
|
+
const a = verts[0].fromBufferAttribute(posAttr, i3 + 0);
|
|
33686
|
+
const b = verts[1].fromBufferAttribute(posAttr, i3 + 1);
|
|
33687
|
+
const c = verts[2].fromBufferAttribute(posAttr, i3 + 2);
|
|
33688
|
+
tempVec1.subVectors(c, b);
|
|
33689
|
+
tempVec2.subVectors(a, b);
|
|
33690
|
+
tempNorm.crossVectors(tempVec1, tempVec2).normalize();
|
|
33691
|
+
// average all normals that meet the threshold and set the normal value
|
|
33692
|
+
for (let n = 0; n < 3; n++) {
|
|
33693
|
+
const vert = verts[n];
|
|
33694
|
+
const hash = hashVertex(vert);
|
|
33695
|
+
const otherNormals = vertexMap[hash];
|
|
33696
|
+
tempNorm2.set(0, 0, 0);
|
|
33697
|
+
for (let k = 0, lk = otherNormals.length; k < lk; k++) {
|
|
33698
|
+
const otherNorm = otherNormals[k];
|
|
33699
|
+
if (tempNorm.dot(otherNorm) > creaseDot) {
|
|
33700
|
+
tempNorm2.add(otherNorm);
|
|
33701
|
+
}
|
|
33702
|
+
}
|
|
33703
|
+
tempNorm2.normalize();
|
|
33704
|
+
normAttr.setXYZ(i3 + n, tempNorm2.x, tempNorm2.y, tempNorm2.z);
|
|
33705
|
+
}
|
|
33706
|
+
}
|
|
33707
|
+
resultGeometry.setAttribute("normal", normAttr);
|
|
33708
|
+
return resultGeometry;
|
|
33709
|
+
}
|
|
33710
|
+
function mergeBufferGeometries(geometries, useGroups = false) {
|
|
33711
|
+
console.warn("THREE.BufferGeometryUtils: mergeBufferGeometries() has been renamed to mergeGeometries()."); // @deprecated, r151
|
|
33712
|
+
return mergeGeometries(geometries, useGroups);
|
|
33713
|
+
}
|
|
33714
|
+
function mergeBufferAttributes(attributes) {
|
|
33715
|
+
console.warn("THREE.BufferGeometryUtils: mergeBufferAttributes() has been renamed to mergeAttributes()."); // @deprecated, r151
|
|
33716
|
+
return mergeAttributes(attributes);
|
|
33717
|
+
}
|
|
33718
|
+
//#endregion
|
|
33719
|
+
//#region Simplify Modifier
|
|
33720
|
+
/**
|
|
33721
|
+
* Simplification Geometry Modifier
|
|
33722
|
+
* - based on code and technique
|
|
33723
|
+
* - by Stan Melax in 1998
|
|
33724
|
+
* - Progressive Mesh type Polygon Reduction Algorithm
|
|
33725
|
+
* - http://www.melax.com/polychop/
|
|
33726
|
+
*/
|
|
33727
|
+
const _cb = new Vector3(), _ab = new Vector3();
|
|
33728
|
+
class SimplifyModifier {
|
|
33729
|
+
modify(geometry, count) {
|
|
33730
|
+
geometry = geometry.clone();
|
|
33731
|
+
const attributes = geometry.attributes;
|
|
33732
|
+
// this modifier can only process indexed and non-indexed geomtries with a position attribute
|
|
33733
|
+
for (const name in attributes) {
|
|
33734
|
+
if (name !== "position")
|
|
33735
|
+
geometry.deleteAttribute(name);
|
|
33736
|
+
}
|
|
33737
|
+
geometry = mergeVertices(geometry);
|
|
33738
|
+
//
|
|
33739
|
+
// put data of original geometry in different data structures
|
|
33740
|
+
//
|
|
33741
|
+
const vertices = [];
|
|
33742
|
+
const faces = [];
|
|
33743
|
+
// add vertices
|
|
33744
|
+
const positionAttribute = geometry.getAttribute("position");
|
|
33745
|
+
for (let i = 0; i < positionAttribute.count; i++) {
|
|
33746
|
+
const v = new Vector3().fromBufferAttribute(positionAttribute, i);
|
|
33747
|
+
const vertex = new Vertex(v);
|
|
33748
|
+
vertices.push(vertex);
|
|
33749
|
+
}
|
|
33750
|
+
// add faces
|
|
33751
|
+
let index = geometry.getIndex();
|
|
33752
|
+
if (index !== null) {
|
|
33753
|
+
for (let i = 0; i < index.count; i += 3) {
|
|
33754
|
+
const a = index.getX(i);
|
|
33755
|
+
const b = index.getX(i + 1);
|
|
33756
|
+
const c = index.getX(i + 2);
|
|
33757
|
+
const triangle = new Triangle(vertices[a], vertices[b], vertices[c], a, b, c);
|
|
33758
|
+
faces.push(triangle);
|
|
33759
|
+
}
|
|
33760
|
+
}
|
|
33761
|
+
else {
|
|
33762
|
+
for (let i = 0; i < positionAttribute.count; i += 3) {
|
|
33763
|
+
const a = i;
|
|
33764
|
+
const b = i + 1;
|
|
33765
|
+
const c = i + 2;
|
|
33766
|
+
const triangle = new Triangle(vertices[a], vertices[b], vertices[c], a, b, c);
|
|
33767
|
+
faces.push(triangle);
|
|
33768
|
+
}
|
|
33769
|
+
}
|
|
33770
|
+
// compute all edge collapse costs
|
|
33771
|
+
for (let i = 0, il = vertices.length; i < il; i++) {
|
|
33772
|
+
computeEdgeCostAtVertex(vertices[i]);
|
|
33773
|
+
}
|
|
33774
|
+
let nextVertex;
|
|
33775
|
+
let z = count;
|
|
33776
|
+
if (count > vertices.length) {
|
|
33777
|
+
z = length;
|
|
33778
|
+
}
|
|
33779
|
+
while (z--) {
|
|
33780
|
+
nextVertex = minimumCostEdge(vertices);
|
|
33781
|
+
if (!nextVertex) {
|
|
33782
|
+
console.log("THREE.SimplifyModifier: No next vertex");
|
|
33783
|
+
break;
|
|
33784
|
+
}
|
|
33785
|
+
collapse(vertices, faces, nextVertex, nextVertex.collapseNeighbor);
|
|
33786
|
+
}
|
|
33787
|
+
//
|
|
33788
|
+
const simplifiedGeometry = new BufferGeometry();
|
|
33789
|
+
const position = [];
|
|
33790
|
+
index = [];
|
|
33791
|
+
//
|
|
33792
|
+
for (let i = 0; i < vertices.length; i++) {
|
|
33793
|
+
const vertex = vertices[i].position;
|
|
33794
|
+
position.push(vertex.x, vertex.y, vertex.z);
|
|
33795
|
+
// cache final index to GREATLY speed up faces reconstruction
|
|
33796
|
+
vertices[i].id = i;
|
|
33797
|
+
}
|
|
33798
|
+
//
|
|
33799
|
+
for (let i = 0; i < faces.length; i++) {
|
|
33800
|
+
const face = faces[i];
|
|
33801
|
+
index.push(face.v1.id, face.v2.id, face.v3.id);
|
|
33802
|
+
}
|
|
33803
|
+
//
|
|
33804
|
+
simplifiedGeometry.setAttribute("position", new Float32BufferAttribute(position, 3));
|
|
33805
|
+
simplifiedGeometry.setIndex(index);
|
|
33806
|
+
return simplifiedGeometry;
|
|
33807
|
+
}
|
|
33808
|
+
}
|
|
33809
|
+
function pushIfUnique(array, object) {
|
|
33810
|
+
if (array.indexOf(object) === -1)
|
|
33811
|
+
array.push(object);
|
|
33812
|
+
}
|
|
33813
|
+
function removeFromArray(array, object) {
|
|
33814
|
+
const k = array.indexOf(object);
|
|
33815
|
+
if (k > -1)
|
|
33816
|
+
array.splice(k, 1);
|
|
33817
|
+
}
|
|
33818
|
+
function computeEdgeCollapseCost(u, v) {
|
|
33819
|
+
// if we collapse edge uv by moving u to v then how
|
|
33820
|
+
// much different will the model change, i.e. the "error".
|
|
33821
|
+
const edgelength = v.position.distanceTo(u.position);
|
|
33822
|
+
let curvature = 0;
|
|
33823
|
+
const sideFaces = [];
|
|
33824
|
+
// find the "sides" triangles that are on the edge uv
|
|
33825
|
+
for (let i = 0, il = u.faces.length; i < il; i++) {
|
|
33826
|
+
const face = u.faces[i];
|
|
33827
|
+
if (face.hasVertex(v)) {
|
|
33828
|
+
sideFaces.push(face);
|
|
33829
|
+
}
|
|
33830
|
+
}
|
|
33831
|
+
// use the triangle facing most away from the sides
|
|
33832
|
+
// to determine our curvature term
|
|
33833
|
+
for (let i = 0, il = u.faces.length; i < il; i++) {
|
|
33834
|
+
let minCurvature = 1;
|
|
33835
|
+
const face = u.faces[i];
|
|
33836
|
+
for (let j = 0; j < sideFaces.length; j++) {
|
|
33837
|
+
const sideFace = sideFaces[j];
|
|
33838
|
+
// use dot product of face normals.
|
|
33839
|
+
const dotProd = face.normal.dot(sideFace.normal);
|
|
33840
|
+
minCurvature = Math.min(minCurvature, (1.001 - dotProd) / 2);
|
|
33841
|
+
}
|
|
33842
|
+
curvature = Math.max(curvature, minCurvature);
|
|
33843
|
+
}
|
|
33844
|
+
// crude approach in attempt to preserve borders
|
|
33845
|
+
// though it seems not to be totally correct
|
|
33846
|
+
const borders = 0;
|
|
33847
|
+
if (sideFaces.length < 2) {
|
|
33848
|
+
// we add some arbitrary cost for borders,
|
|
33849
|
+
// borders += 10;
|
|
33850
|
+
curvature = 1;
|
|
33851
|
+
}
|
|
33852
|
+
const amt = edgelength * curvature + borders;
|
|
33853
|
+
return amt;
|
|
33854
|
+
}
|
|
33855
|
+
function computeEdgeCostAtVertex(v) {
|
|
33856
|
+
// compute the edge collapse cost for all edges that start
|
|
33857
|
+
// from vertex v. Since we are only interested in reducing
|
|
33858
|
+
// the object by selecting the min cost edge at each step, we
|
|
33859
|
+
// only cache the cost of the least cost edge at this vertex
|
|
33860
|
+
// (in member variable collapse) as well as the value of the
|
|
33861
|
+
// cost (in member variable collapseCost).
|
|
33862
|
+
if (v.neighbors.length === 0) {
|
|
33863
|
+
// collapse if no neighbors.
|
|
33864
|
+
v.collapseNeighbor = null;
|
|
33865
|
+
v.collapseCost = -0.01;
|
|
33866
|
+
return;
|
|
33867
|
+
}
|
|
33868
|
+
v.collapseCost = 100000;
|
|
33869
|
+
v.collapseNeighbor = null;
|
|
33870
|
+
// search all neighboring edges for "least cost" edge
|
|
33871
|
+
for (let i = 0; i < v.neighbors.length; i++) {
|
|
33872
|
+
const collapseCost = computeEdgeCollapseCost(v, v.neighbors[i]);
|
|
33873
|
+
if (!v.collapseNeighbor) {
|
|
33874
|
+
v.collapseNeighbor = v.neighbors[i];
|
|
33875
|
+
v.collapseCost = collapseCost;
|
|
33876
|
+
v.minCost = collapseCost;
|
|
33877
|
+
v.totalCost = 0;
|
|
33878
|
+
v.costCount = 0;
|
|
33879
|
+
}
|
|
33880
|
+
v.costCount++;
|
|
33881
|
+
v.totalCost += collapseCost;
|
|
33882
|
+
if (collapseCost < v.minCost) {
|
|
33883
|
+
v.collapseNeighbor = v.neighbors[i];
|
|
33884
|
+
v.minCost = collapseCost;
|
|
33885
|
+
}
|
|
33886
|
+
}
|
|
33887
|
+
// we average the cost of collapsing at this vertex
|
|
33888
|
+
v.collapseCost = v.totalCost / v.costCount;
|
|
33889
|
+
// v.collapseCost = v.minCost;
|
|
33890
|
+
}
|
|
33891
|
+
function removeVertex(v, vertices) {
|
|
33892
|
+
console.assert(v.faces.length === 0);
|
|
33893
|
+
while (v.neighbors.length) {
|
|
33894
|
+
const n = v.neighbors.pop();
|
|
33895
|
+
removeFromArray(n.neighbors, v);
|
|
33896
|
+
}
|
|
33897
|
+
removeFromArray(vertices, v);
|
|
33898
|
+
}
|
|
33899
|
+
function removeFace(f, faces) {
|
|
33900
|
+
removeFromArray(faces, f);
|
|
33901
|
+
if (f.v1)
|
|
33902
|
+
removeFromArray(f.v1.faces, f);
|
|
33903
|
+
if (f.v2)
|
|
33904
|
+
removeFromArray(f.v2.faces, f);
|
|
33905
|
+
if (f.v3)
|
|
33906
|
+
removeFromArray(f.v3.faces, f);
|
|
33907
|
+
// TODO optimize this!
|
|
33908
|
+
const vs = [f.v1, f.v2, f.v3];
|
|
33909
|
+
for (let i = 0; i < 3; i++) {
|
|
33910
|
+
const v1 = vs[i];
|
|
33911
|
+
const v2 = vs[(i + 1) % 3];
|
|
33912
|
+
if (!v1 || !v2)
|
|
33913
|
+
continue;
|
|
33914
|
+
v1.removeIfNonNeighbor(v2);
|
|
33915
|
+
v2.removeIfNonNeighbor(v1);
|
|
33916
|
+
}
|
|
33917
|
+
}
|
|
33918
|
+
function collapse(vertices, faces, u, v) {
|
|
33919
|
+
// u and v are pointers to vertices of an edge
|
|
33920
|
+
// Collapse the edge uv by moving vertex u onto v
|
|
33921
|
+
if (!v) {
|
|
33922
|
+
// u is a vertex all by itself so just delete it..
|
|
33923
|
+
removeVertex(u, vertices);
|
|
33924
|
+
return;
|
|
33925
|
+
}
|
|
33926
|
+
const tmpVertices = [];
|
|
33927
|
+
for (let i = 0; i < u.neighbors.length; i++) {
|
|
33928
|
+
tmpVertices.push(u.neighbors[i]);
|
|
33929
|
+
}
|
|
33930
|
+
// delete triangles on edge uv:
|
|
33931
|
+
for (let i = u.faces.length - 1; i >= 0; i--) {
|
|
33932
|
+
if (u.faces[i] && u.faces[i].hasVertex(v)) {
|
|
33933
|
+
removeFace(u.faces[i], faces);
|
|
33934
|
+
}
|
|
33935
|
+
}
|
|
33936
|
+
// update remaining triangles to have v instead of u
|
|
33937
|
+
for (let i = u.faces.length - 1; i >= 0; i--) {
|
|
33938
|
+
u.faces[i].replaceVertex(u, v);
|
|
33939
|
+
}
|
|
33940
|
+
removeVertex(u, vertices);
|
|
33941
|
+
// recompute the edge collapse costs in neighborhood
|
|
33942
|
+
for (let i = 0; i < tmpVertices.length; i++) {
|
|
33943
|
+
computeEdgeCostAtVertex(tmpVertices[i]);
|
|
33944
|
+
}
|
|
33945
|
+
}
|
|
33946
|
+
function minimumCostEdge(vertices) {
|
|
33947
|
+
// O(n * n) approach. TODO optimize this
|
|
33948
|
+
let least = vertices[0];
|
|
33949
|
+
for (let i = 0; i < vertices.length; i++) {
|
|
33950
|
+
if (vertices[i].collapseCost < least.collapseCost) {
|
|
33951
|
+
least = vertices[i];
|
|
33952
|
+
}
|
|
33953
|
+
}
|
|
33954
|
+
return least;
|
|
33955
|
+
}
|
|
33956
|
+
// we use a triangle class to represent structure of face slightly differently
|
|
33957
|
+
class Triangle {
|
|
33958
|
+
constructor(v1, v2, v3, a, b, c) {
|
|
33959
|
+
this.a = a;
|
|
33960
|
+
this.b = b;
|
|
33961
|
+
this.c = c;
|
|
33962
|
+
this.v1 = v1;
|
|
33963
|
+
this.v2 = v2;
|
|
33964
|
+
this.v3 = v3;
|
|
33965
|
+
this.normal = new Vector3();
|
|
33966
|
+
this.computeNormal();
|
|
33967
|
+
v1.faces.push(this);
|
|
33968
|
+
v1.addUniqueNeighbor(v2);
|
|
33969
|
+
v1.addUniqueNeighbor(v3);
|
|
33970
|
+
v2.faces.push(this);
|
|
33971
|
+
v2.addUniqueNeighbor(v1);
|
|
33972
|
+
v2.addUniqueNeighbor(v3);
|
|
33973
|
+
v3.faces.push(this);
|
|
33974
|
+
v3.addUniqueNeighbor(v1);
|
|
33975
|
+
v3.addUniqueNeighbor(v2);
|
|
33976
|
+
}
|
|
33977
|
+
computeNormal() {
|
|
33978
|
+
const vA = this.v1.position;
|
|
33979
|
+
const vB = this.v2.position;
|
|
33980
|
+
const vC = this.v3.position;
|
|
33981
|
+
_cb.subVectors(vC, vB);
|
|
33982
|
+
_ab.subVectors(vA, vB);
|
|
33983
|
+
_cb.cross(_ab).normalize();
|
|
33984
|
+
this.normal.copy(_cb);
|
|
33985
|
+
}
|
|
33986
|
+
hasVertex(v) {
|
|
33987
|
+
return v === this.v1 || v === this.v2 || v === this.v3;
|
|
33988
|
+
}
|
|
33989
|
+
replaceVertex(oldv, newv) {
|
|
33990
|
+
if (oldv === this.v1)
|
|
33991
|
+
this.v1 = newv;
|
|
33992
|
+
else if (oldv === this.v2)
|
|
33993
|
+
this.v2 = newv;
|
|
33994
|
+
else if (oldv === this.v3)
|
|
33995
|
+
this.v3 = newv;
|
|
33996
|
+
removeFromArray(oldv.faces, this);
|
|
33997
|
+
newv.faces.push(this);
|
|
33998
|
+
oldv.removeIfNonNeighbor(this.v1);
|
|
33999
|
+
this.v1.removeIfNonNeighbor(oldv);
|
|
34000
|
+
oldv.removeIfNonNeighbor(this.v2);
|
|
34001
|
+
this.v2.removeIfNonNeighbor(oldv);
|
|
34002
|
+
oldv.removeIfNonNeighbor(this.v3);
|
|
34003
|
+
this.v3.removeIfNonNeighbor(oldv);
|
|
34004
|
+
this.v1.addUniqueNeighbor(this.v2);
|
|
34005
|
+
this.v1.addUniqueNeighbor(this.v3);
|
|
34006
|
+
this.v2.addUniqueNeighbor(this.v1);
|
|
34007
|
+
this.v2.addUniqueNeighbor(this.v3);
|
|
34008
|
+
this.v3.addUniqueNeighbor(this.v1);
|
|
34009
|
+
this.v3.addUniqueNeighbor(this.v2);
|
|
34010
|
+
this.computeNormal();
|
|
34011
|
+
}
|
|
34012
|
+
}
|
|
34013
|
+
class Vertex {
|
|
34014
|
+
constructor(v) {
|
|
34015
|
+
this.position = v;
|
|
34016
|
+
this.id = -1; // external use position in vertices list (for e.g. face generation)
|
|
34017
|
+
this.faces = []; // faces vertex is connected
|
|
34018
|
+
this.neighbors = []; // neighbouring vertices aka "adjacentVertices"
|
|
34019
|
+
// these will be computed in computeEdgeCostAtVertex()
|
|
34020
|
+
this.collapseCost = 0; // cost of collapsing this vertex, the less the better. aka objdist
|
|
34021
|
+
this.collapseNeighbor = null; // best candinate for collapsing
|
|
34022
|
+
}
|
|
34023
|
+
addUniqueNeighbor(vertex) {
|
|
34024
|
+
pushIfUnique(this.neighbors, vertex);
|
|
34025
|
+
}
|
|
34026
|
+
removeIfNonNeighbor(n) {
|
|
34027
|
+
const neighbors = this.neighbors;
|
|
34028
|
+
const faces = this.faces;
|
|
34029
|
+
const offset = neighbors.indexOf(n);
|
|
34030
|
+
if (offset === -1)
|
|
34031
|
+
return;
|
|
34032
|
+
for (let i = 0; i < faces.length; i++) {
|
|
34033
|
+
if (faces[i].hasVertex(n))
|
|
34034
|
+
return;
|
|
34035
|
+
}
|
|
34036
|
+
neighbors.splice(offset, 1);
|
|
34037
|
+
}
|
|
34038
|
+
}
|
|
34039
|
+
//#endregion
|
|
32906
34040
|
//My code
|
|
32907
34041
|
//////////////////
|
|
32908
34042
|
const maxPolygonPerObject = 1000;
|
|
@@ -32971,7 +34105,20 @@ function workerFunction() {
|
|
|
32971
34105
|
dummyGeometry.setAttribute("position", new BufferAttribute(buffer, 3));
|
|
32972
34106
|
dummyGeometry.computeVertexNormals();
|
|
32973
34107
|
if (data.Instances > 0) {
|
|
32974
|
-
|
|
34108
|
+
let geometry = new BufferGeometry();
|
|
34109
|
+
// In case number of polygon larger than expect, merge them to reduce polygon first, for optimize purpose
|
|
34110
|
+
if (dummyGeometry.attributes.position.count > maxPolygonPerObject) {
|
|
34111
|
+
const modifier = new SimplifyModifier();
|
|
34112
|
+
const ratio = maxPolygonPerObject /
|
|
34113
|
+
dummyGeometry.attributes.position.count;
|
|
34114
|
+
const count = Math.floor(dummyGeometry.attributes.position.count * ratio); // number of polygon to remove
|
|
34115
|
+
geometry = modifier.modify(dummyGeometry, count);
|
|
34116
|
+
console.log(dummyGeometry.attributes.position.count, geometry.attributes.position.count);
|
|
34117
|
+
}
|
|
34118
|
+
else {
|
|
34119
|
+
geometry = dummyGeometry;
|
|
34120
|
+
}
|
|
34121
|
+
const childMesh = new InstancedMesh(geometry, null, data.Instances.length);
|
|
32975
34122
|
childMesh.castShadow = true;
|
|
32976
34123
|
childMesh.receiveShadow = true;
|
|
32977
34124
|
for (let index = 0; index < data.Instances.length; index++) {
|
|
@@ -32999,8 +34146,8 @@ function workerFunction() {
|
|
|
32999
34146
|
matrix4.fromArray(numbers);
|
|
33000
34147
|
childMesh.setMatrixAt(index, matrix4);
|
|
33001
34148
|
//if number of polygon smaller than expect, generate edges
|
|
33002
|
-
if (
|
|
33003
|
-
const edges = new EdgesGeometry(
|
|
34149
|
+
if (geometry.attributes.position.count < maxPolygonForEdge) {
|
|
34150
|
+
const edges = new EdgesGeometry(geometry, 90);
|
|
33004
34151
|
const positions = edges.attributes.position.array;
|
|
33005
34152
|
const indices = [];
|
|
33006
34153
|
for (let i = 0; i < positions.length / 3; i += 2) {
|