mirror of
https://git.mirrors.martin98.com/https://github.com/google/draco
synced 2025-07-05 19:45:12 +08:00
Updated DracoMeshLoader.cs (#387)
* Updated DracoMeshLoader.cs Improved formatting and variable naming for DracoMeshLoader.cs - added back tabs - renamed some variables to better reflect their intended use
This commit is contained in:
parent
9edbac7a86
commit
b4f63c5b13
@ -1,4 +1,4 @@
|
|||||||
// Copyright 2017 The Draco Authors.
|
// Copyright 2017 The Draco Authors.
|
||||||
//
|
//
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
// you may not use this file except in compliance with the License.
|
// you may not use this file except in compliance with the License.
|
||||||
@ -37,7 +37,7 @@ public unsafe class DracoMeshLoader
|
|||||||
|
|
||||||
private struct DecodedMesh
|
private struct DecodedMesh
|
||||||
{
|
{
|
||||||
public int[] faces;
|
public int[] indices;
|
||||||
public Vector3[] vertices;
|
public Vector3[] vertices;
|
||||||
public Vector3[] normals;
|
public Vector3[] normals;
|
||||||
public Vector2[] uvs;
|
public Vector2[] uvs;
|
||||||
@ -53,98 +53,122 @@ public unsafe class DracoMeshLoader
|
|||||||
// to be splitted.
|
// to be splitted.
|
||||||
private void SplitMesh (DecodedMesh mesh, ref List<DecodedMesh> splittedMeshes)
|
private void SplitMesh (DecodedMesh mesh, ref List<DecodedMesh> splittedMeshes)
|
||||||
{
|
{
|
||||||
int[] IndexNew = new int[maxNumVerticesPerMesh];
|
// Map between new indices on a splitted mesh and old indices on the
|
||||||
int BaseIndex = 0;
|
// original mesh.
|
||||||
int FaceCount = mesh.faces.Length;
|
int[] newToOldIndexMap = new int[maxNumVerticesPerMesh];
|
||||||
int[] FaceIndex = new int[FaceCount];
|
|
||||||
int[] NewFace = new int[FaceCount];
|
// Index of the first unprocessed corner.
|
||||||
|
int baseCorner = 0;
|
||||||
|
int indicesCount = mesh.indices.Length;
|
||||||
|
|
||||||
|
// Map between old indices of the original mesh and indices on the currently
|
||||||
|
// processed sub-mesh. Inverse of |newToOldIndexMap|.
|
||||||
|
int[] oldToNewIndexMap = new int[indicesCount];
|
||||||
|
int[] newIndices = new int[indicesCount];
|
||||||
|
|
||||||
|
|
||||||
for (int i = 0; i < FaceCount; i++)
|
// Set mapping between existing vertex indices and new vertex indices to
|
||||||
|
// a default value.
|
||||||
|
for (int i = 0; i < indicesCount; i++)
|
||||||
{
|
{
|
||||||
FaceIndex[i] = -1;
|
oldToNewIndexMap[i] = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (BaseIndex < FaceCount)
|
// Number of added vertices for the currently processed sub-mesh.
|
||||||
|
int numAddedVertices = 0;
|
||||||
|
|
||||||
|
// Process all corners (faces) of the original mesh.
|
||||||
|
while (baseCorner < indicesCount)
|
||||||
{
|
{
|
||||||
int uniqueCornerId = 0;
|
// Reset the old to new indices map that may have been set by previously
|
||||||
int UseIndex = 0;
|
// processed sub-meshes.
|
||||||
int AddNew = 0;
|
for (int i = 0; i < numAddedVertices; i++)
|
||||||
int[] NewCorner = new int[3];
|
|
||||||
for (; BaseIndex + UseIndex < FaceCount;)
|
|
||||||
{
|
{
|
||||||
AddNew = 0;
|
oldToNewIndexMap[newToOldIndexMap[i]] = -1;
|
||||||
|
}
|
||||||
|
numAddedVertices = 0;
|
||||||
|
|
||||||
|
// Number of processed corners on the current sub-mesh.
|
||||||
|
int numProcessedCorners = 0;
|
||||||
|
|
||||||
|
// Local storage for indices added to the new sub-mesh for a currently
|
||||||
|
// processed face.
|
||||||
|
int[] newlyAddedIndices = new int[3];
|
||||||
|
|
||||||
|
// Sub-mesh processing starts here.
|
||||||
|
for (; baseCorner + numProcessedCorners < indicesCount;)
|
||||||
|
{
|
||||||
|
// Number of vertices that we need to add to the current sub-mesh.
|
||||||
|
int verticesAdded = 0;
|
||||||
for (int i = 0; i < 3; i++)
|
for (int i = 0; i < 3; i++)
|
||||||
{
|
{
|
||||||
if (FaceIndex[mesh.faces[BaseIndex + UseIndex + i]] == -1)
|
if (oldToNewIndexMap[mesh.indices[baseCorner + numProcessedCorners + i]] == -1)
|
||||||
{
|
{
|
||||||
NewCorner[AddNew] = mesh.faces[BaseIndex + UseIndex + i];
|
newlyAddedIndices[verticesAdded] = mesh.indices[baseCorner + numProcessedCorners + i];
|
||||||
AddNew++;
|
verticesAdded++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (uniqueCornerId + AddNew > maxNumVerticesPerMesh)
|
// If the number of new vertices that we need to add is larger than the
|
||||||
|
// allowed limit, we need to stop processing the current sub-mesh.
|
||||||
|
// The current face will be processed again for the next sub-mesh.
|
||||||
|
if (numAddedVertices + verticesAdded > maxNumVerticesPerMesh)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < AddNew; i++)
|
// Update mapping between old an new vertex indices.
|
||||||
|
for (int i = 0; i < verticesAdded; i++)
|
||||||
{
|
{
|
||||||
FaceIndex[NewCorner[i]] = uniqueCornerId;
|
oldToNewIndexMap[newlyAddedIndices[i]] = numAddedVertices;
|
||||||
IndexNew[uniqueCornerId] = NewCorner[i];
|
newToOldIndexMap[numAddedVertices] = newlyAddedIndices[i];
|
||||||
uniqueCornerId++;
|
numAddedVertices++;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < 3; i++)
|
for (int i = 0; i < 3; i++)
|
||||||
{
|
{
|
||||||
NewFace[UseIndex] = FaceIndex[mesh.faces[BaseIndex + UseIndex]];
|
newIndices[numProcessedCorners] = oldToNewIndexMap[mesh.indices[baseCorner + numProcessedCorners]];
|
||||||
UseIndex++;
|
numProcessedCorners++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Sub-mesh processing done.
|
||||||
for (int i = 0; i < uniqueCornerId; i++)
|
|
||||||
{
|
|
||||||
FaceIndex[IndexNew[i]] = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
DecodedMesh subMesh = new DecodedMesh();
|
DecodedMesh subMesh = new DecodedMesh();
|
||||||
subMesh.faces = new int[UseIndex];
|
subMesh.indices = new int[numProcessedCorners];
|
||||||
Array.Copy(NewFace, subMesh.faces, UseIndex);
|
Array.Copy(newIndices, subMesh.indices, numProcessedCorners);
|
||||||
subMesh.vertices = new Vector3[uniqueCornerId];
|
subMesh.vertices = new Vector3[numAddedVertices];
|
||||||
for (int i = 0; i < uniqueCornerId; i++)
|
for (int i = 0; i < numAddedVertices; i++)
|
||||||
{
|
{
|
||||||
subMesh.vertices[i] = mesh.vertices[IndexNew[i]];
|
subMesh.vertices[i] = mesh.vertices[newToOldIndexMap[i]];
|
||||||
}
|
}
|
||||||
if (mesh.normals != null)
|
if (mesh.normals != null)
|
||||||
{
|
{
|
||||||
subMesh.normals = new Vector3[uniqueCornerId];
|
subMesh.normals = new Vector3[numAddedVertices];
|
||||||
for (int i = 0; i < uniqueCornerId; i++)
|
for (int i = 0; i < numAddedVertices; i++)
|
||||||
{
|
{
|
||||||
subMesh.normals[i] = mesh.normals[IndexNew[i]];
|
subMesh.normals[i] = mesh.normals[newToOldIndexMap[i]];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mesh.colors != null)
|
if (mesh.colors != null)
|
||||||
{
|
{
|
||||||
subMesh.colors = new Color[uniqueCornerId];
|
subMesh.colors = new Color[numAddedVertices];
|
||||||
for (int i = 0; i < uniqueCornerId; i++)
|
for (int i = 0; i < numAddedVertices; i++)
|
||||||
{
|
{
|
||||||
subMesh.colors[i] = mesh.colors[IndexNew[i]];
|
subMesh.colors[i] = mesh.colors[newToOldIndexMap[i]];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mesh.uvs != null)
|
if (mesh.uvs != null)
|
||||||
{
|
{
|
||||||
subMesh.uvs = new Vector2[uniqueCornerId];
|
subMesh.uvs = new Vector2[numAddedVertices];
|
||||||
for (int i = 0; i < uniqueCornerId; i++)
|
for (int i = 0; i < numAddedVertices; i++)
|
||||||
{
|
{
|
||||||
subMesh.uvs[i] = mesh.uvs[IndexNew[i]];
|
subMesh.uvs[i] = mesh.uvs[newToOldIndexMap[i]];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
splittedMeshes.Add(subMesh);
|
splittedMeshes.Add(subMesh);
|
||||||
|
baseCorner += numProcessedCorners;
|
||||||
BaseIndex += UseIndex;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -214,16 +238,6 @@ public unsafe class DracoMeshLoader
|
|||||||
if (tmpMesh->hasColor)
|
if (tmpMesh->hasColor)
|
||||||
newColors = new Color[tmpMesh->numVertices];
|
newColors = new Color[tmpMesh->numVertices];
|
||||||
int byteStridePerValue = 4;
|
int byteStridePerValue = 4;
|
||||||
/*
|
|
||||||
* TODO(zhafang): Change to:
|
|
||||||
* float[] pos = new float[3];
|
|
||||||
* for (int i = 0; i < tmpMesh -> numVertices; ++i) {
|
|
||||||
* Marshal.Copy(tmpMesh->position, pos, 3 * i, 3);
|
|
||||||
* for (int j = 0; j < 3; ++j) {
|
|
||||||
* newVertices[i][j] = pos[j];
|
|
||||||
* }
|
|
||||||
* }
|
|
||||||
*/
|
|
||||||
|
|
||||||
byte* posaddr = (byte*)tmpMesh->position;
|
byte* posaddr = (byte*)tmpMesh->position;
|
||||||
byte* normaladdr = (byte*)tmpMesh->normal;
|
byte* normaladdr = (byte*)tmpMesh->normal;
|
||||||
@ -276,7 +290,7 @@ public unsafe class DracoMeshLoader
|
|||||||
|
|
||||||
DecodedMesh decodedMesh = new DecodedMesh ();
|
DecodedMesh decodedMesh = new DecodedMesh ();
|
||||||
decodedMesh.vertices = newVertices;
|
decodedMesh.vertices = newVertices;
|
||||||
decodedMesh.faces = newTriangles;
|
decodedMesh.indices = newTriangles;
|
||||||
if (newUVs.Length != 0)
|
if (newUVs.Length != 0)
|
||||||
decodedMesh.uvs = newUVs;
|
decodedMesh.uvs = newUVs;
|
||||||
if (newNormals.Length != 0)
|
if (newNormals.Length != 0)
|
||||||
@ -289,7 +303,7 @@ public unsafe class DracoMeshLoader
|
|||||||
for (int i = 0; i < splittedMeshes.Count; ++i) {
|
for (int i = 0; i < splittedMeshes.Count; ++i) {
|
||||||
Mesh mesh = new Mesh ();
|
Mesh mesh = new Mesh ();
|
||||||
mesh.vertices = splittedMeshes [i].vertices;
|
mesh.vertices = splittedMeshes [i].vertices;
|
||||||
mesh.triangles = splittedMeshes [i].faces;
|
mesh.triangles = splittedMeshes [i].indices;
|
||||||
if (splittedMeshes [i].uvs != null)
|
if (splittedMeshes [i].uvs != null)
|
||||||
mesh.uv = splittedMeshes [i].uvs;
|
mesh.uv = splittedMeshes [i].uvs;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user