private QuadTreeLeaf CreateSegment(int segmentnumber, int quadLevel, List<Triangle> triangles, List<Vector3> vertices)
 {
     quadLevel = (int)(this.sampleSize * quadLevel);
     QuadTreeLeaf quadTreeLeaf = new QuadTreeLeaf();
     List<Triangle> triangles1 = new List<Triangle>();
     int segmentx = segmentnumber / quadLevel;
     int segmenty = segmentnumber - (segmentx * quadLevel);
     quadTreeLeaf.boundingBox.Set(float.MaxValue, float.MinValue, float.MaxValue, float.MinValue, float.MaxValue, float.MinValue);
     BBox bbox = new BBox();
     bbox.Set((float)segmentx / quadLevel * 4096, ((float)segmentx + 1) / quadLevel * 4096, (float)segmenty / quadLevel * 4096, ((float)segmenty + 1) / quadLevel * 4096, 0f, 0f);
     for (int index = 0; index < triangles.Count; index++)
     {
         BBox tbox = new BBox(float.MaxValue, float.MinValue, float.MaxValue, float.MinValue, float.MaxValue, float.MinValue);
         for (int index2 = 0; index2 < 3; index2++)
         {
             Vector3 vertex = vertices[triangles[index][index2]];
             tbox.GrowByVertex(vertex);
         }
         if (tbox.px2 >= bbox.px1 && tbox.px1 <= bbox.px2 && tbox.py2 >= bbox.py1 && tbox.py1 <= bbox.py2)
         {
             triangles1.Add(triangles[index]);
             quadTreeLeaf.boundingBox.GrowByBox(tbox);
         }
     }
     quadTreeLeaf.triangles = triangles1;
     return quadTreeLeaf;
 }
 private ShapeDesc TransformShape(QuadDesc quad, StaticDesc stat, NiFile file, NiTriBasedGeom geom, Matrix44 parentTransform, float parentScale)
 {
     BBox bbox = new BBox(float.MaxValue, float.MinValue, float.MaxValue, float.MinValue, float.MaxValue, float.MinValue);
     ShapeDesc shape1 = new ShapeDesc();
     NiTriShape shape2;
     NiTriShapeData data = new NiTriShapeData();
     //Console.WriteLine("Reading block #" + geom.GetData());
     if (geom.GetClassName() == "NiTriStrips")
     {
         shape2 = new NiTriShape(geom);
         data = new NiTriShapeData((NiTriStripsData)file.GetBlockAtIndex(geom.GetData()));
     }
     else if (geom.GetClassName() == "BSLODTriShape")
     {
         shape2 = new NiTriShape(geom);
         data = (NiTriShapeData)file.GetBlockAtIndex(geom.GetData());
     }
     else
     {
         shape2 = new NiTriShape(geom);
         data = (NiTriShapeData)file.GetBlockAtIndex(geom.GetData());
     }
     if (verbose && !data.HasVertexColors() && ((this.GetShaderFlags2(file, shape2) & 32) == 32))
     {
         if (!stat.staticModels[this.quadIndex].Contains("glacierrubbletrim0"))
         {
             logFile.WriteLog("Vertex Colors Flag, but no vertex colors in " + stat.staticModels[this.quadIndex]);
         }
     }
     if (((int)data.GetBSNumUVSets() & 1) == 0)
         return shape1;
     float _x = stat.x - (float)quad.x * 4096f;
     float _y = stat.y - (float)quad.y * 4096f;
     Matrix33 matrix33_1 = new Matrix33(true);
     Matrix33 matrix33_2 = new Matrix33(true);
     Matrix33 matrix33_3 = new Matrix33(true);
     matrix33_1.SetRotationX(Utils.ToRadians(-stat.rotX));
     matrix33_2.SetRotationY(Utils.ToRadians(-stat.rotY));
     matrix33_3.SetRotationZ(Utils.ToRadians(-stat.rotZ));
     Matrix44 matrix44 = new Matrix44(new Matrix33(true) * matrix33_1 * matrix33_2 * matrix33_3, new Vector3(_x, _y, stat.z), 1f);
     List<Vector3> vertices = new List<Vector3>(data.GetVertices());
     List<Vector3> normals = new List<Vector3>(data.GetNormals());
     List<Vector3> tangents = new List<Vector3>(data.GetTangents());
     List<Vector3> bitangents = new List<Vector3>(data.GetBitangents());
     // generate tangents independent of fix tangents setting
     bool newtangents = false;
     bool newbitangents = false;
     if (this.generateTangents && data.HasNormals())
     {
         if (tangents.Count == 0)
         {
             newtangents = true;
             for (int index = 0; index < vertices.Count; ++index)
                 tangents.Add(new Vector3(0.0f, 0.0f, 0.0f));
         }
         if (bitangents.Count == 0)
         {
             newbitangents = true;
             for (int index = 0; index < vertices.Count; ++index)
                 bitangents.Add(new Vector3(0.0f, 0.0f, 0.0f));
         }
     }
     for (int index = 0; index < vertices.Count; ++index)
     {
         vertices[index] *= shape2.GetScale() * parentScale;
         vertices[index] *= shape2.GetTransform() * parentTransform;
         vertices[index] *= stat.scale;
         vertices[index] *= matrix44;
         if (data.HasNormals())
         {
             normals[index] *= parentTransform.RemoveTranslation() * shape2.GetTransform().RemoveTranslation();
             normals[index] *= matrix44.RemoveTranslation();
             // adjust tangents as well
             if (tangents.Count != 0)
             {
                 tangents[index] *= parentTransform.RemoveTranslation() * shape2.GetTransform().RemoveTranslation();
                 tangents[index] *= matrix44.RemoveTranslation();
             }
             // adjust bitangents as well
             if (bitangents.Count != 0)
             {
                 bitangents[index] *= parentTransform.RemoveTranslation() * shape2.GetTransform().RemoveTranslation();
                 bitangents[index] *= matrix44.RemoveTranslation();
             }
             // fix tangents when they were newly generated
             if (newtangents || newbitangents || this.fixTangents)
             {
                 Vector3 vector3_1 = Vector3.Cross(normals[index], new Vector3(0.0f, 0.0f, 1f));
                 Vector3 vector3_2 = Vector3.Cross(normals[index], new Vector3(0.0f, 1f, 0.0f));
                 if (newtangents)
                 {
                     tangents[index] = (double)vector3_1.Length > (double)vector3_2.Length ? vector3_1 : vector3_2;
                     tangents[index].Normalize();
                 }
                 if (newbitangents)
                 {
                     bitangents[index] = Vector3.Cross(normals[index], tangents[index]);
                     bitangents[index].Normalize();
                 }
             }
         }
         bbox.GrowByVertex(vertices[index]);
         vertices[index] /= (float)this.quadLevel;
     }
     data.SetVertices(vertices);
     if (data.HasNormals())
     {
         data.SetNormals(normals);
         data.SetHasTangents(false);
     }
     data.SetCenter(new Vector3((float)(((double)bbox.px1 + (double)bbox.px2) / 2.0), (float)(((double)bbox.py1 + (double)bbox.py2) / 2.0), (float)(((double)bbox.pz1 + (double)bbox.pz2) / 2.0)) / (float)this.quadLevel);
     data.SetRadius(this.CalcRadius(data));
     data.SetConsistencyFlags((ushort)0);
     data.SetKeepFlags((byte)51);
     data.SetSkyrimMaterial(0U);
     shape1.shape = shape2;
     shape1.data = data;
     // relative x, y for segment
     shape1.x = _x;
     shape1.y = _y;
     shape1.boundingBox = bbox;
     //Console.WriteLine(stat.staticName + " 2 " + stat.refID + " " + file + " " + shape2.GetName() + " ");
     shape1.textures = this.GetMeshTextures(file, shape2);
     shape1.material = stat.materialName;
     shape1.isHighDetail = false;
     if (useHDFlag)
     {
         if ((stat.refFlags & 131072) == 131072)
         {
             if (HDMeshList.Any(stat.staticModels[this.quadIndex].ToLower().Contains))
             {
                 shape1.isHighDetail = true;
             }
             else if (notHDMeshList.Any(stat.staticModels[this.quadIndex].ToLower().Contains))
             {
                 shape1.isHighDetail = false;
             }
             else
             {
                 shape1.isHighDetail = true;
             }
         }
     }
     // Shader Flags 2 SLSF2_Double_Sided
     shape1.isDoubleSided = (this.GetShaderFlags2(file, shape2) & 16) == 16;
     // clamp mode for atlas
     shape1.TextureClampMode = this.GetTextureClampMode(file, shape2);
     /*if ((this.GetShaderFlags1(file, shape2) & 1) == 1)
     {
         //logFile.WriteLog("Specular" + stat.staticModels[this.quadIndex]);
     }*/
     if (AtlasList.Contains(shape1.textures[0]))
     {
         if (this.UVAtlas(data, shape1.textures[0], stat))
         {
             string[] strArray = new string[2];
             strArray[0] = AtlasList.Get(shape1.textures[0]).AtlasTexture;
             strArray[1] = AtlasList.Get(shape1.textures[0]).AtlasTextureN;
             shape1.textures = strArray;
             shape1.TextureClampMode = 0U;
             shape1.isHighDetail = false;
         }
     }
     else
     {
         if (useOptimizer && quadLevel != 4 && shape1.textures[0].ToLower().Contains("mountainslab01"))
         {
             string[] strArray = new string[2];
             strArray[0] = "textures\\landscape\\mountains\\mountainslab02.dds";
             strArray[1] = "textures\\landscape\\mountains\\mountainslab02_n.dds";
             shape1.textures = strArray;
         }
         if (notHDTextureList.Any(shape1.textures[0].Contains))
         {
             if (this.verbose)
             {
                 logFile.WriteLog("No atlas for " + shape1.textures[0] + " in " + stat.staticModels[this.quadIndex]);
             }
         }
         else
         {
             if (!useHDFlag)
             {
                 shape1.isHighDetail = true;
             }
         }
     }
     if (notHDTextureList.Any(shape1.textures[0].Contains))
     {
         shape1.isHighDetail = false;
     }
     if (HDTextureList.Any(shape1.textures[0].Contains))
     {
         shape1.isHighDetail = true;
     }
     if (shape1.textures[0].Contains("dyndolod\\lod"))
     {
         shape1.TextureClampMode = 0U;
         shape1.isHighDetail = false;
     }
     if (!this.generateVertexColors || (this.quadLevel != 4 && !shape1.isHighDetail))
     {
         data.SetHasVertexColors(false);
         shape1.hasVertexColor = false;
     }
     if (this.generateTangents)
     {
         //shape1.material.Length != 0
         if (!useOptimizer || (useOptimizer && (this.quadLevel == 4 || shape1.isHighDetail)))
         {
             data.SetTangents(tangents);
             data.SetBitangents(bitangents);
         }
     }
     if (this.removeUnseenFaces && quad.hasTerrainVertices)
     {
         this.RemoveUnseenFaces(quad, data, shape1);
         if ((int)data.GetNumTriangles() == 0)
             return (ShapeDesc)null;
     }
     else
     {
         quad.outValues.totalTriCount += data.GetNumTriangles();
         quad.outValues.reducedTriCount += data.GetNumTriangles();
     }
     this.GenerateSegments(quad, ref shape1);
     return shape1;
 }