public QuadTree(NiTriShapeData data, int quadLevel) { this.sampleSize = Game.sampleSize; // water if (data.GetBSNumUVSets() == 0) { this.sampleSize = 1; this.isLand = false; } else { isLand = true; } this.vertices = data.GetVertices(); this.boundingBox = new BBox(); this.boundingBox.Set(float.MaxValue, float.MinValue, float.MaxValue, float.MinValue, float.MaxValue, float.MinValue); this.segments = new QuadTreeLeaf[(int)(this.sampleSize * quadLevel * this.sampleSize * quadLevel)]; for (int index = 0; index < (this.sampleSize * quadLevel) * (this.sampleSize * quadLevel); index++) { this.segments[index] = this.CreateSegment(index, quadLevel, data.GetTriangles(), data.GetVertices()); boundingBox.GrowByBox(this.segments[index].boundingBox); } QuadTreeLeaf quadTreeLeaf = new QuadTreeLeaf(); List<Triangle> triangles = data.GetTriangles(); quadTreeLeaf.boundingBox.GrowByBox(boundingBox); quadTreeLeaf.triangles = triangles; this.entirequad = quadTreeLeaf; }
private void RemoveUnseenFaces(QuadDesc quad, NiTriShapeData data, ShapeDesc shape) { List<Triangle> triangles = data.GetTriangles(); List<Vector3> vertices = data.GetVertices(); int count = triangles.Count; quad.outValues.totalTriCount += count; int loops = 0; if (this.removeUnderwaterFaces) { loops = 1; } for (int loop = 0; loop <= loops; loop++) { for (int index = 0; index < triangles.Count; ++index) { QuadDesc quadCurrent = new QuadDesc(); List<Triangle> trianglesCompare = new List<Triangle>(); List<Vector3> verticesCompare = new List<Vector3>(); bool[] vertexBelow = new bool[3]; for (int index1 = 0; index1 < 3; index1++) { Vector3 vertex = vertices[triangles[index][index1]]; float x = vertex[0]; float y = vertex[1]; int vertexQuadx = quad.x + cellquad(x * quadLevel, southWestX); int vertexQuady = quad.y + cellquad(y * quadLevel, southWestY); if (quad.x != vertexQuadx || quad.y != vertexQuady) { for (int index3 = 0; index3 < this.quadList.Count; ++index3) { if (vertexQuadx == this.quadList[index3].x && vertexQuady == this.quadList[index3].y) { quadCurrent = this.quadList[index3]; x -= (vertexQuadx - quad.x) / quadLevel * 4096; y -= (vertexQuady - quad.y) / quadLevel * 4096; break; } } } else { quadCurrent = quad; } if (!quadCurrent.hasTerrainVertices) { continue; } Vector3 vertex1 = new Vector3(x, y, vertex[2]); List<Triangle> trianglesTerrain = new List<Triangle>(); List<Vector3> verticesTerrain = new List<Vector3>(); if (this.removeUnderwaterFaces && loop == 0) { if (quadCurrent.waterQuadTree != null) { trianglesTerrain = quadCurrent.waterQuadTree.entirequad.triangles; verticesTerrain = quadCurrent.waterQuadTree.vertices; } } else { trianglesTerrain = quadCurrent.terrainQuadTree.GetSegment(vertex1, quadLevel); verticesTerrain = quadCurrent.terrainQuadTree.vertices; } if (trianglesTerrain == null) { vertexBelow[index1] = true; } else { if (trianglesTerrain.Count != 0) { float num1 = float.MaxValue; num1 = GetTriangleHeight(verticesTerrain, trianglesTerrain, vertex1); if (vertex1[2] < num1) { vertexBelow[index1] = true; } } } } if (vertexBelow[0] && vertexBelow[1] && vertexBelow[2]) { triangles.RemoveAt(index); --index; } } } // remove elements from other lists too if (triangles.Count != 0 && triangles.Count != count) { Dictionary<ushort, ushort> dictionary = new Dictionary<ushort, ushort>(); List<Vector3> vertices2 = new List<Vector3>(); List<UVCoord> uvcoords = data.GetUVCoords(); List<UVCoord> uvcoords2 = new List<UVCoord>(); List<Color4> vertexcolors = data.GetVertexColors(); List<Color4> vertexcolors2 = new List<Color4>(); List<Vector3> normals = data.GetNormals(); List<Vector3> normals2 = new List<Vector3>(); List<Vector3> tangents = data.GetTangents(); List<Vector3> tangents2 = new List<Vector3>(); List<Vector3> bitangents = data.GetBitangents(); List<Vector3> bitangents2 = new List<Vector3>(); ushort index2 = 0; bool vertexcolors_allwhite = true; for (int index = 0; index < triangles.Count; ++index) { for (int index3 = 0; index3 < 3; ++index3) { ushort v1 = triangles[index][index3]; if (!dictionary.ContainsKey(v1)) { dictionary.Add(v1, index2); ++index2; if (vertices.Count != 0) { vertices2.Add(vertices[v1]); } if (uvcoords.Count != 0) { uvcoords2.Add(uvcoords[v1]); } if (vertexcolors.Count != 0) { float r = vertexcolors[v1][0]; float g = vertexcolors[v1][1]; float b = vertexcolors[v1][2]; if (r != 1f || b != 1f || g != 1f) { vertexcolors_allwhite = false; } vertexcolors2.Add(vertexcolors[v1]); } if (normals.Count != 0) { normals2.Add(normals[v1]); } if (tangents.Count != 0) { tangents2.Add(tangents[v1]); } if (bitangents.Count != 0) { bitangents2.Add(bitangents[v1]); } } } } // update indexes of triangles for (int index = 0; index < triangles.Count; ++index) { for (int index3 = 0; index3 < 3; ++index3) { ushort key = triangles[index][index3]; if (!dictionary.ContainsKey(key)) { this.logFile.WriteLog("no index for " + key); } ushort v1 = dictionary[key]; Triangle triangle; (triangle = triangles[index])[index3] = (v1); } } if (vertices2.Count != 0) { data.SetVertices(vertices2); } if (uvcoords2.Count != 0) { data.SetUVCoords(uvcoords2); } if (vertexcolors2.Count != 0) { if (vertexcolors_allwhite) { data.SetHasVertexColors(false); shape.hasVertexColor = false; } else { data.SetHasVertexColors(true); shape.hasVertexColor = true; data.SetVertexColors(vertexcolors2); } } if (normals2.Count != 0) { data.SetNormals(normals2); } if (tangents2.Count != 0) { data.SetTangents(tangents2); } if (bitangents2.Count != 0) { data.SetBitangents(bitangents2); } } quad.outValues.reducedTriCount += triangles.Count; data.SetTriangles(triangles); }