| |
| |
| |
| |
|
|
| import { truncateValues } from "./Math"; |
| import { mergeVertices, updateIndices } from "./MergeVerticesByDistance"; |
| import { parseVRML, replaceVerticesAndIndicesInVRML } from "./ParseVRML"; |
| import { simplifyMesh } from "./QEM"; |
|
|
| |
| |
| |
| |
| |
| |
| |
| function countFaces(indices: number[]): number { |
| return indices.filter((index) => index === -1 && index !== undefined) |
| .length; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export async function processVRML( |
| content: string, |
| mergeCutoff: number, |
| reductionFraction: number |
| ): Promise<string> { |
| const { chunkDatas, firstChunkContent } = parseVRML(content); |
|
|
| const newVRMLContents: string[] = []; |
|
|
| for (const chunkDataIdx in chunkDatas) { |
| const chunkData = chunkDatas[chunkDataIdx]; |
| const { vertices, indices, colors, normals, shapeChunkContent } = |
| chunkData; |
|
|
| console.log("Original mesh:"); |
| console.log(` Vertices: ${vertices.length}`); |
| console.log(` Faces: ${countFaces(indices)}`); |
|
|
| |
| const { mergedVertices, mergedColors, mapping } = mergeVertices( |
| vertices, |
| colors, |
| mergeCutoff |
| ); |
| const updatedIndices = updateIndices(indices, mapping); |
| console.log("After vertex merging:"); |
| console.log(` Vertices: ${mergedVertices.length}`); |
| console.log(` Faces: ${countFaces(updatedIndices)}`); |
|
|
| |
| let targetVertexCount = null; |
| if (reductionFraction !== null) { |
| targetVertexCount = Math.round( |
| mergedVertices.length * reductionFraction |
| ); |
| |
| } |
|
|
| |
| let simplifiedVertices = mergedVertices; |
| let simplifiedIndices = updatedIndices; |
| let simplifiedColors = mergedColors; |
|
|
| if (targetVertexCount) { |
| const simplifiedMesh = simplifyMesh( |
| mergedVertices, |
| updatedIndices, |
| mergedColors, |
| targetVertexCount |
| ); |
| simplifiedVertices = simplifiedMesh.vertices.map(truncateValues); |
| simplifiedIndices = simplifiedMesh.indices; |
| simplifiedColors = simplifiedMesh.colors.map(truncateValues); |
| console.log("After Vertex Clustering simplification:"); |
| console.log(` Vertices: ${simplifiedVertices.length}`); |
| console.log(` Faces: ${countFaces(simplifiedIndices)}`); |
| } |
|
|
| |
| const mergedNormals = |
| normals.length > 0 ? normals.map(truncateValues) : normals; |
|
|
| |
| const newVRMLContent = replaceVerticesAndIndicesInVRML( |
| shapeChunkContent, |
| simplifiedVertices, |
| simplifiedColors, |
| mergedNormals, |
| simplifiedIndices |
| ); |
|
|
| |
|
|
| newVRMLContents.push(newVRMLContent); |
|
|
| |
| |
|
|
| |
| console.log(""); |
| } |
|
|
| const newVRMLContent = |
| firstChunkContent + "Shape {" + newVRMLContents.join("Shape {"); |
|
|
| console.log(newVRMLContents.length); |
|
|
| console.log(newVRMLContent); |
|
|
| return newVRMLContent; |
| } |
|
|