public Mesh(Table table) { children = new List <Mesh>(); //General name = table["Name"].StrValue; model = table["Model"].StrValue; meshType = MeshTypeEnumExtensions.TypeFromString(table["SpecialObjectName"].StrValue); systemType = table["SystemType"].StrValue; sectionName = table["SectionName"].StrValue; uiName = table["UIName"].StrValue; //Interaction PickSphereRADIUS = table["PickSphereRADIUS"].DoubleValue; //Position parentName = table["ParentTo"].StrValue; position = new Vec3(table["Position"]); rotation = new Vec3(table["Rotation"]); //Collision //Render render = new MeshRenderData(table); collision = new MeshCollisionData(table); //Light //light = new MeshLightData(table); //States states = new MeshStatesData(table); //Surfaces surfaces = new MeshSurfacesData(table); }
public static string StringFromType(this MeshTypeEnum key) { return(table[key]); }
// Creates a mesh public static Mesh CreateMesh(MeshTypeEnum _type) { // Set limits int worldSize = gm.baseSettings.worldSize; int numRows = worldSize * 2; int numQuadsPerRow = numRows; int numVertsTotal = numRows * numVertsPerQuad * numQuadsPerRow; int numTriIndicesTotal = numVertsTotal * 3 / 2; // Assign limits to arrays Mesh mesh = new Mesh(); Vector3[] verts = new Vector3[numVertsTotal]; Vector2[] uvs = new Vector2[numVertsTotal]; int[] tris = new int[numTriIndicesTotal]; // Assign all verts, uvs, and tris to arrays // Loop through rows for (int rowIndex = 0; rowIndex < numRows; rowIndex++) { // Verts // Assign starting x and z values for vertices int vx = -worldSize; int vy = 0; int vz = -worldSize + rowIndex; // Loop through quads in row for (int quadIndex = rowIndex * numQuadsPerRow; quadIndex < (rowIndex + 1) * numQuadsPerRow; quadIndex++) { // Assign vertices int vertIndex = quadIndex * numVertsPerQuad; verts[vertIndex] = new Vector3(vx, vy, vz); vertIndex++; vx += 0; vz += 1; verts[vertIndex] = new Vector3(vx, vy, vz); vertIndex++; vx += 1; vz -= 1; verts[vertIndex] = new Vector3(vx, vy, vz); vertIndex++; vx += 0; vz += 1; verts[vertIndex] = new Vector3(vx, vy, vz); vz -= 1; } // UVs // Loop through quads in row for (int quadIndex = rowIndex * numQuadsPerRow; quadIndex < (rowIndex + 1) * numQuadsPerRow; quadIndex++) { // Assign UVs int uvIndex = quadIndex * numVertsPerQuad; { Vector2Int tilePos = World.GetTilePosFromQuadIndex(quadIndex); string name; if (_type == MeshTypeEnum.Terrain) { name = World.Tiles[tilePos].sedimentTileType.ToString(); } else { name = World.Tiles[tilePos].heightmapTileType.ToString(); if (name != World.HeightmapTileTypeEnum.Shallows.ToString() && name != World.HeightmapTileTypeEnum.Ocean.ToString()) { name = "Blank"; } } Vector2[] map = UVMap.GetUVMap(name).UVMaps; uvs[uvIndex] = map[0]; uvIndex++; uvs[uvIndex] = map[1]; uvIndex++; uvs[uvIndex] = map[2]; uvIndex++; uvs[uvIndex] = map[3]; } } // Tris // Assign starting x, y, and z values for triangles int vertIndexStartForTris = rowIndex * numVertsPerQuad * numQuadsPerRow; int tx = vertIndexStartForTris; int ty = vertIndexStartForTris + 1; int tz = vertIndexStartForTris + 2; // Loop through quads in row for (int quadIndex = rowIndex * numQuadsPerRow; quadIndex < (rowIndex + 1) * numQuadsPerRow; quadIndex++) { // Assign triangles int triIndex = quadIndex * numTriIndicesPerQuad; tris[triIndex] = tx; triIndex++; tris[triIndex] = ty; triIndex++; tris[triIndex] = tz; triIndex++; tx++; ty += 2; tris[triIndex] = tx; triIndex++; tris[triIndex] = ty; triIndex++; tris[triIndex] = tz; tx += 3; ty += 2; tz += 4; } } // Assign mesh data to mesh mesh.vertices = verts; mesh.uv = uvs; mesh.triangles = tris; mesh.RecalculateBounds(); mesh.Optimize(); // Return mesh return(mesh); }