- Home /
Simple LOD not working correctly.
I have created a simple LOD method here:
public static void SphereLOD(Vector3 camPos, Mesh mesh)
{
newVectices = new Dictionary<uint,int>();
List<Vector3> meshverts = new List<Vector3>(mesh.vertices);
normals = new List<Vector3>(mesh.normals);
uvs = new List<Vector2>(mesh.uv);
indices = new List<int>();
Vector3[] closest = new Vector3[9];
closest = mesh.vertices.OrderBy(v=>(v - camPos).sqrMagnitude).Take(9).ToArray();
Vector3 originate = (closest[0] + closest[1] + closest[2] + closest[3] + closest[4] + closest[5]
+ closest[6] + closest[7] + closest[8]) / 9;
float length = ((closest[0] - originate).magnitude + (closest[1] - originate).magnitude +
(closest[2] - originate).magnitude + (closest[3] - originate).magnitude +
(closest[4] - originate).magnitude + (closest[5] - originate).magnitude +
(closest[6] - originate).magnitude + (closest[7] - originate).magnitude +
(closest[8] - originate).magnitude) / 9;
closestlist = new List<Vector3>(closest);
vertices = new List<Vector3>(closest);
oldclose = closestlist;
int[] triangles = mesh.triangles;
if ((closest[1] - camPos).magnitude > (2 * length))
{
for (int i = 0; i < triangles.Length; i += 3)
{
int i1 = triangles[i + 0];
int i2 = triangles[i + 1];
int i3 = triangles[i + 2];
int a = GetNewVertex(i1, i2);
int b = GetNewVertex(i2, i3);
int c = GetNewVertex(i3, i1);
indices.Add(i1); indices.Add(a); indices.Add(c);
indices.Add(i2); indices.Add(b); indices.Add(a);
indices.Add(i3); indices.Add(c); indices.Add(b);
indices.Add(a ); indices.Add(b); indices.Add(c); // center triangle
}
}
for(int i = 1; i < vertices.Count; i++)
{
meshverts.Add (vertices[i]);
}
mesh.vertices = meshverts.ToArray();
//mesh.normals = normals.ToArray();
//mesh.uv = uvs.ToArray();
mesh.triangles = indices.ToArray();
newVectices = null;
vertices = null;
normals = null;
indices = null;
}
From here, I go into the a script and call MeshHelper.SphereLOD(camPos, mesh); and it should subdivide the closes 9 vertices to the camera when the camera gets close enough. The problem is that when I apply this to any object, strange things happen. The object's triangles are thrown about and the mesh is destroyed. Can anyone help me isolate the problem?
Edit: Here is the GetNewVertices code:
static int GetNewVertex(int i1, int i2)
{
uint t1 = ((uint)i1 << 16) | (uint)i2;
uint t2 = ((uint)i2 << 16) | (uint)i1;
if (newVectices.ContainsKey(t2))
return newVectices[t2];
if (newVectices.ContainsKey(t1))
return newVectices[t1];
int newIndex = vertices.Count;
newVectices.Add(t1,newIndex);
vertices.Add((vertices[i1] + vertices[i2]) * 0.5f);
normals.Add((normals[i1] + normals[i2]).normalized);
uvs.Add((uvs[i1] + uvs[i2]) * 0.5f);
return newIndex;
}
These images show that the object is responding to the camera position (see triangles pulling toward it) but not in the intended manner.
Your answer
![](https://koobas.hobune.stream/wayback/20220613102449im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rivers on procedurally terrain 0 Answers
How to load/save 'Chunks' near cam for mesh terrain? 0 Answers
Mesh based on noise 1 Answer
Huge world without huge lag? 1 Answer
Discrete divisions on terrain? 0 Answers