private void _plane(SurfaceTool st, float posX, float posY) { int xOffset = (int)posX; int yOffset = (int)posY; var z = 0; GD.Print(posX, " ", posY); st.AddColor(get_color(xOffset, yOffset)); st.AddUv(new Vector2(0, 1)); st.AddVertex(new Vector3(-1 + xOffset * 2, 1 + yOffset * 2, z)); st.AddUv(new Vector2(1, 1)); st.AddVertex(new Vector3(1 + xOffset * 2, 1 + yOffset * 2, z)); st.AddUv(new Vector2(1, 0)); st.AddVertex(new Vector3(1 + xOffset * 2, -1 + yOffset * 2, z)); st.AddUv(new Vector2(1, 0)); st.AddVertex(new Vector3(1 + xOffset * 2, -1 + yOffset * 2, z)); st.AddUv(new Vector2(0, 0)); st.AddVertex(new Vector3(-1 + xOffset * 2, -1 + yOffset * 2, z)); st.AddUv(new Vector2(0, 1)); st.AddVertex(new Vector3(-1 + xOffset * 2, 1 + yOffset * 2, z)); }
// public void SetPolygons() { // this.a.AddPolygon(this) // this.b.AddPolygon(this) // this.c.AddPolygon(this) // } public void AddToSurfaceTool(SurfaceTool surfaceTool) { // System.Diagnostics.Debug.WriteLine("This vertex A-B-C: [" + a.x + ", " + a.y + ", " + a.z + "] - [" + b.x + ", " + b.y + ", " + b.z + "] - [" + c.x + ", " + c.y + ", " + c.z + "]"); const float minLimit = -0.05f; const float maxLimit = 0.069f; surfaceTool.AddColor(a.determineVertexColor(minLimit, maxLimit)); surfaceTool.AddColor(b.determineVertexColor(minLimit, maxLimit)); surfaceTool.AddColor(c.determineVertexColor(minLimit, maxLimit)); // Add UV coords surfaceTool.AddUv( new Vector2( Mathf.Atan2(a.z, a.x) / (2f * Mathf.Pi), Mathf.Asin(a.y) / Mathf.Pi + 0.5f ) ); surfaceTool.AddUv( new Vector2( Mathf.Atan2(b.z, b.x) / (2f * Mathf.Pi), Mathf.Asin(b.y) / Mathf.Pi + 0.5f ) ); surfaceTool.AddUv( new Vector2( Mathf.Atan2(c.z, c.x) / (2f * Mathf.Pi), Mathf.Asin(c.y) / Mathf.Pi + 0.5f ) ); // add vertices surfaceTool.AddVertex(new Vector3(a.x, a.y, a.z)); surfaceTool.AddVertex(new Vector3(b.x, b.y, b.z)); surfaceTool.AddVertex(new Vector3(c.x, c.y, c.z)); }
private SurfaceTool createQuad(SurfaceTool st, Vector3 pos, Quat q) { //1Quad = 4 points = 2 triangles Vector3 v1 = new Vector3(0, q.x, -1) + pos; Vector3 v2 = new Vector3(1, q.y, -1) + pos; Vector3 v3 = new Vector3(1, q.w, 0) + pos; Vector3 v4 = new Vector3(0, q.z, 0) + pos; //tri1 st.AddUv(new Vector2(0, 0)); st.AddColor(heightToColor(v1.y));// active albedoVertexColors on material st.AddVertex(v1); st.AddUv(new Vector2(0, 1)); st.AddColor(heightToColor(v2.y)); st.AddVertex(v2); st.AddUv(new Vector2(1, 1)); st.AddColor(heightToColor(v4.y)); st.AddVertex(v4); //tri2 st.AddUv(new Vector2(0, 0)); st.AddColor(heightToColor(v2.y)); st.AddVertex(v2); st.AddUv(new Vector2(0, 1)); st.AddColor(heightToColor(v3.y)); st.AddVertex(v3); st.AddUv(new Vector2(1, 1)); st.AddColor(heightToColor(v4.y)); st.AddVertex(v4); return(st); }
public static void CreateCube(SurfaceTool st, float size, Vector3 position, Sides sides) { if (sides.HasFlag(Sides.North) || sides.HasFlag(Sides.East) || sides.HasFlag(Sides.South) || sides.HasFlag(Sides.West)) { Color white = new Color(1f, 1f, 1f, 1f); Vector3[] vertices = GetVerticesForCell(size, position); if (sides.HasFlag(Sides.North)) { st.AddUv(new Vector2(1, 1)); st.AddNormal(North); st.AddColor(white); st.AddVertex(vertices[(int)CellVertexIndex.Bot_NW]); st.AddUv(new Vector2(0, 0)); st.AddNormal(North); st.AddColor(white); st.AddVertex(vertices[(int)CellVertexIndex.Top_NE]); st.AddUv(new Vector2(1, 0)); st.AddNormal(North); st.AddColor(white); st.AddVertex(vertices[(int)CellVertexIndex.Top_NW]); st.AddUv(new Vector2(1, 1)); st.AddNormal(North); st.AddColor(white); st.AddVertex(vertices[(int)CellVertexIndex.Bot_NW]); st.AddUv(new Vector2(0, 1)); st.AddNormal(North); st.AddColor(white); st.AddVertex(vertices[(int)CellVertexIndex.Bot_NE]); st.AddUv(new Vector2(0, 0)); st.AddNormal(North); st.AddColor(white); st.AddVertex(vertices[(int)CellVertexIndex.Top_NE]); } if (sides.HasFlag(Sides.East)) { st.AddUv(new Vector2(0, 0)); st.AddNormal(East); st.AddColor(white); st.AddVertex(vertices[(int)CellVertexIndex.Top_SE]); st.AddUv(new Vector2(1, 0)); st.AddNormal(East); st.AddColor(white); st.AddVertex(vertices[(int)CellVertexIndex.Top_NE]); st.AddUv(new Vector2(0, 1)); st.AddNormal(East); st.AddColor(white); st.AddVertex(vertices[(int)CellVertexIndex.Bot_SE]); st.AddUv(new Vector2(1, 0)); st.AddNormal(East); st.AddColor(white); st.AddVertex(vertices[(int)CellVertexIndex.Top_NE]); st.AddUv(new Vector2(1, 1)); st.AddNormal(East); st.AddColor(white); st.AddVertex(vertices[(int)CellVertexIndex.Bot_NE]); st.AddUv(new Vector2(0, 1)); st.AddNormal(East); st.AddColor(white); st.AddVertex(vertices[(int)CellVertexIndex.Bot_SE]); } if (sides.HasFlag(Sides.South)) { st.AddUv(new Vector2(0, 0)); st.AddNormal(South); st.AddColor(white); st.AddVertex(vertices[(int)CellVertexIndex.Top_SW]); st.AddUv(new Vector2(1, 0)); st.AddNormal(South); st.AddColor(white); st.AddVertex(vertices[(int)CellVertexIndex.Top_SE]); st.AddUv(new Vector2(0, 1)); st.AddNormal(South); st.AddColor(white); st.AddVertex(vertices[(int)CellVertexIndex.Bot_SW]); st.AddUv(new Vector2(1, 0)); st.AddNormal(South); st.AddColor(white); st.AddVertex(vertices[(int)CellVertexIndex.Top_SE]); st.AddUv(new Vector2(1, 1)); st.AddNormal(South); st.AddColor(white); st.AddVertex(vertices[(int)CellVertexIndex.Bot_SE]); st.AddUv(new Vector2(0, 1)); st.AddNormal(South); st.AddColor(white); st.AddVertex(vertices[(int)CellVertexIndex.Bot_SW]); } if (sides.HasFlag(Sides.West)) { st.AddUv(new Vector2(1, 1)); st.AddNormal(West); st.AddColor(white); st.AddVertex(vertices[(int)CellVertexIndex.Bot_SW]); st.AddUv(new Vector2(0, 0)); st.AddNormal(West); st.AddColor(white); st.AddVertex(vertices[(int)CellVertexIndex.Top_NW]); st.AddUv(new Vector2(1, 0)); st.AddNormal(West); st.AddColor(white); st.AddVertex(vertices[(int)CellVertexIndex.Top_SW]); st.AddUv(new Vector2(1, 1)); st.AddNormal(West); st.AddColor(white); st.AddVertex(vertices[(int)CellVertexIndex.Bot_SW]); st.AddUv(new Vector2(0, 1)); st.AddNormal(West); st.AddColor(white); st.AddVertex(vertices[(int)CellVertexIndex.Bot_NW]); st.AddUv(new Vector2(0, 0)); st.AddNormal(West); st.AddColor(white); st.AddVertex(vertices[(int)CellVertexIndex.Top_NW]); } } }
private void CreatePerceptionPyramidMesh() { var material = new SpatialMaterial(); material.FlagsTransparent = true; material.AlbedoColor = new Color(0.0f, 1.0f, 0.0f, 0.02f); material.SetCullMode(SpatialMaterial.CullMode.Disabled); // Pyramid var surfaceTool = new SurfaceTool(); surfaceTool.Begin(Mesh.PrimitiveType.Triangles); // Top face surfaceTool.AddNormal(new Vector3(0.0f, 0.5f, 0.5f)); surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f)); surfaceTool.AddVertex(new Vector3(-perceptionRadius / 2, perceptionRadius / 2, -perceptionRadius)); // top left surfaceTool.AddNormal(new Vector3(0.0f, 0.5f, 0.5f)); surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f)); surfaceTool.AddVertex(new Vector3(perceptionRadius / 2, perceptionRadius / 2, -perceptionRadius)); // top right surfaceTool.AddNormal(new Vector3(0.0f, 0.5f, 0.5f)); surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f)); surfaceTool.AddVertex(new Vector3(0, 0, 2.0f)); // home // Right face surfaceTool.AddNormal(new Vector3(0.5f, 0.0f, 0.5f)); surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f)); surfaceTool.AddVertex(new Vector3(perceptionRadius / 2, perceptionRadius / 2, -perceptionRadius)); // top right surfaceTool.AddNormal(new Vector3(0.5f, 0.0f, 0.5f)); surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f)); surfaceTool.AddVertex(new Vector3(perceptionRadius / 2, -perceptionRadius / 2, -perceptionRadius)); // bottom right surfaceTool.AddNormal(new Vector3(0.5f, 0.0f, 0.5f)); surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f)); surfaceTool.AddVertex(new Vector3(0, 0, 2.0f)); // home // Bottom face surfaceTool.AddNormal(new Vector3(0.0f, -0.5f, 0.5f)); surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f)); surfaceTool.AddVertex(new Vector3(perceptionRadius / 2, -perceptionRadius / 2, -perceptionRadius)); // bottom right surfaceTool.AddNormal(new Vector3(0.0f, -0.5f, 0.5f)); surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f)); surfaceTool.AddVertex(new Vector3(-perceptionRadius / 2, -perceptionRadius / 2, -perceptionRadius)); // bottom left surfaceTool.AddNormal(new Vector3(0.0f, -0.5f, 0.5f)); surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f)); surfaceTool.AddVertex(new Vector3(0, 0, 2.0f)); // home // Left face surfaceTool.AddNormal(new Vector3(-0.5f, 0.0f, 0.5f)); surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f)); surfaceTool.AddVertex(new Vector3(-perceptionRadius / 2, -perceptionRadius / 2, -perceptionRadius)); // bottom left surfaceTool.AddNormal(new Vector3(-0.5f, 0.0f, 0.5f)); surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f)); surfaceTool.AddVertex(new Vector3(-perceptionRadius / 2, perceptionRadius / 2, -perceptionRadius)); // top left surfaceTool.AddNormal(new Vector3(-0.5f, 0.0f, 0.5f)); surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f)); surfaceTool.AddVertex(new Vector3(0, 0, 2.0f)); // home // Base face1 surfaceTool.AddNormal(new Vector3(0.0f, 0.0f, -1.0f)); surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f)); surfaceTool.AddVertex(new Vector3(perceptionRadius / 2, perceptionRadius / 2, -perceptionRadius)); // top right surfaceTool.AddNormal(new Vector3(0.0f, 0.0f, -1.0f)); surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f)); surfaceTool.AddVertex(new Vector3(-perceptionRadius / 2, perceptionRadius / 2, -perceptionRadius)); // top left surfaceTool.AddNormal(new Vector3(0.0f, 0.0f, -1.0f)); surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f)); surfaceTool.AddVertex(new Vector3(-perceptionRadius / 2, -perceptionRadius / 2, -perceptionRadius)); // bottom left // Base face2 surfaceTool.AddNormal(new Vector3(0.0f, 0.0f, -1.0f)); surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f)); surfaceTool.AddVertex(new Vector3(-perceptionRadius / 2, -perceptionRadius / 2, -perceptionRadius)); // bottom left surfaceTool.AddNormal(new Vector3(0.0f, 0.0f, -1.0f)); surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f)); surfaceTool.AddVertex(new Vector3(perceptionRadius / 2, -perceptionRadius / 2, -perceptionRadius)); // bottom right surfaceTool.AddNormal(new Vector3(0.0f, 0.0f, -1.0f)); surfaceTool.AddColor(new Color(0.0f, 1.0f, 0.0f, 0.02f)); surfaceTool.AddVertex(new Vector3(perceptionRadius / 2, perceptionRadius / 2, -perceptionRadius)); // top right surfaceTool.Index(); var mesh = surfaceTool.Commit(); mesh.SurfaceSetMaterial(0, material); var meshInstance = new MeshInstance(); meshInstance.Mesh = mesh; meshInstance.SetName("perceptionMesh"); meshInstance.SetVisible(false); this.AddChild(meshInstance); }
private Godot.Collections.Array createSurfaceByBones(ArrayMesh mesh, int surface, Skin newSkin, List <UMAReciepeBindPose> origBindPoses) { var mdt = new MeshDataTool(); mdt.CreateFromSurface(mesh, surface); var st = new SurfaceTool(); st.Begin(Mesh.PrimitiveType.Triangles); var newBindPoses = new List <UMAReciepeBindPose>(); if (newSkin != null) { for (int i = 0; i < newSkin.GetBindCount(); i++) { newBindPoses.Add(new UMAReciepeBindPose { boneName = newSkin.GetBindName(i), transform = newSkin.GetBindPose(i), boneIndex = newSkin.GetBindBone(i) }); } } var boneAmount = 0; for (int i = 0; i < mdt.GetVertexCount(); i++) { var oldVer = mdt.GetVertex(i); var oldNorm = mdt.GetVertexNormal(i); var newVer = new Vector3(); var newNorm = new Vector3(); var indexes = mdt.GetVertexBones(i); // st.AddTangent(mdt.GetVertexTangent(i)); st.AddBones(mdt.GetVertexBones(i)); st.AddWeights(mdt.GetVertexWeights(i)); int boneId = 0; foreach (var weight in mdt.GetVertexWeights(i)) { if (newBindPoses.Count >= indexes[boneId] && origBindPoses.Count >= indexes[boneId]) { var restBoneNew = newBindPoses[indexes[boneId]]; var restBoneTemplate = origBindPoses[indexes[boneId]]; var dataup = restBoneNew.transform.Xform(Vector3.Up); var dataright = restBoneNew.transform.Xform(Vector3.Right); var templateup = restBoneTemplate.transform.Xform(Vector3.Up); var templateright = restBoneTemplate.transform.Xform(Vector3.Right); if (Mathf.Abs(dataup.AngleTo(templateup)) > 1 || Mathf.Abs(dataright.AngleTo(templateright)) > 1) { Transform convertMatrix = restBoneTemplate.transform.Inverse() * restBoneNew.transform; newVer += convertMatrix.Xform(oldVer) * weight; newNorm += convertMatrix.basis.Xform(oldNorm) * weight; } else { newVer += oldVer * weight; newNorm += oldNorm * weight; } } else { newVer += oldVer * weight; newNorm += oldNorm * weight; } boneId++; } st.AddUv(mdt.GetVertexUv(i)); if (mdt.GetVertexColor(i) != null) { st.AddColor(mdt.GetVertexColor(i)); } if (mdt.GetVertexUv2(i) != null) { st.AddUv2(mdt.GetVertexUv2(i)); } st.AddNormal(newNorm); st.AddVertex(newVer); boneAmount += mdt.GetVertexBones(i).Length; } //creating indexes for (int face = 0; face < mdt.GetFaceCount(); face++) { for (int faceI = 0; faceI < 3; faceI++) { var ind = mdt.GetFaceVertex(face, faceI); st.AddIndex(ind); } } st.GenerateTangents(); return(st.CommitToArrays()); }
private void CreateVoxel(Color color, Vector3 position) { bool left = !Voxels.ContainsKey(position - new Vector3(1, 0, 0)); bool right = !Voxels.ContainsKey(position + new Vector3(1, 0, 0)); bool back = !Voxels.ContainsKey(position - new Vector3(0, 0, 1)); bool front = !Voxels.ContainsKey(position + new Vector3(0, 0, 1)); bool top = !Voxels.ContainsKey(position + new Vector3(0, 1, 0)); bool bottom = !Voxels.ContainsKey(position - new Vector3(0, 1, 0)); if (left && right && front && back && top && bottom) { return; } SurfaceTool.AddColor(color); void addVertex(Vector3 pos) => SurfaceTool.AddVertex(pos * VoxelSize); Vector3 vertexOffset = position; if (top) // Above { SurfaceTool.AddNormal(new Vector3(0, -1, 0)); addVertex(Vertices[4] + vertexOffset); addVertex(Vertices[5] + vertexOffset); addVertex(Vertices[7] + vertexOffset); addVertex(Vertices[5] + vertexOffset); addVertex(Vertices[6] + vertexOffset); addVertex(Vertices[7] + vertexOffset); } if (right) // Right { SurfaceTool.AddNormal(new Vector3(1, 0, 0)); addVertex(Vertices[2] + vertexOffset); addVertex(Vertices[5] + vertexOffset); addVertex(Vertices[1] + vertexOffset); addVertex(Vertices[2] + vertexOffset); addVertex(Vertices[6] + vertexOffset); addVertex(Vertices[5] + vertexOffset); } if (left) // Left { SurfaceTool.AddNormal(new Vector3(-1, 0, 0)); addVertex(Vertices[0] + vertexOffset); addVertex(Vertices[7] + vertexOffset); addVertex(Vertices[3] + vertexOffset); addVertex(Vertices[0] + vertexOffset); addVertex(Vertices[4] + vertexOffset); addVertex(Vertices[7] + vertexOffset); } if (front) // Front { SurfaceTool.AddNormal(new Vector3(0, 0, 1)); addVertex(Vertices[3] + vertexOffset); addVertex(Vertices[6] + vertexOffset); addVertex(Vertices[2] + vertexOffset); addVertex(Vertices[3] + vertexOffset); addVertex(Vertices[7] + vertexOffset); addVertex(Vertices[6] + vertexOffset); } if (back) // Above { SurfaceTool.AddNormal(new Vector3(0, 0, -1)); addVertex(Vertices[0] + vertexOffset); addVertex(Vertices[1] + vertexOffset); addVertex(Vertices[5] + vertexOffset); addVertex(Vertices[5] + vertexOffset); addVertex(Vertices[4] + vertexOffset); addVertex(Vertices[0] + vertexOffset); } if (bottom) { SurfaceTool.AddNormal(new Vector3(0, 1, 0)); addVertex(Vertices[1] + vertexOffset); addVertex(Vertices[3] + vertexOffset); addVertex(Vertices[2] + vertexOffset); addVertex(Vertices[1] + vertexOffset); addVertex(Vertices[0] + vertexOffset); addVertex(Vertices[3] + vertexOffset); } }
private static void BuildDoorMesh() { if (_doorMesh == null) { Color white = new Color(1f, 1f, 1f, 1f); using (SurfaceTool st = new SurfaceTool()) { Vector3[] cellVerts = Level.GetVerticesForCell(); Vector3[] verts = new Vector3[] { Lerp(cellVerts[(int)Level.CellVertexIndex.Top_SW], cellVerts[(int)Level.CellVertexIndex.Top_NW], 0.5f), Lerp(cellVerts[(int)Level.CellVertexIndex.Top_SE], cellVerts[(int)Level.CellVertexIndex.Top_NE], 0.5f), Lerp(cellVerts[(int)Level.CellVertexIndex.Bot_SW], cellVerts[(int)Level.CellVertexIndex.Bot_NW], 0.5f), Lerp(cellVerts[(int)Level.CellVertexIndex.Bot_SE], cellVerts[(int)Level.CellVertexIndex.Bot_NE], 0.5f) }; st.Begin(Godot.Mesh.PrimitiveType.Triangles); st.AddUv(new Vector2(0f, 1f)); st.AddColor(white); st.AddNormal(Level.South); st.AddVertex(verts[3]); st.AddUv(new Vector2(0f, 0f)); st.AddColor(white); st.AddNormal(Level.South); st.AddVertex(verts[1]); st.AddUv(new Vector2(1f, 0f)); st.AddColor(white); st.AddNormal(Level.South); st.AddVertex(verts[0]); st.AddUv(new Vector2(1f, 1f)); st.AddColor(white); st.AddNormal(Level.South); st.AddVertex(verts[2]); st.AddUv(new Vector2(0f, 1f)); st.AddColor(white); st.AddNormal(Level.South); st.AddVertex(verts[3]); st.AddUv(new Vector2(1f, 0f)); st.AddColor(white); st.AddNormal(Level.South); st.AddVertex(verts[0]); st.AddUv(new Vector2(1f, 0f)); st.AddColor(white); st.AddNormal(Level.North); st.AddVertex(verts[0]); st.AddUv(new Vector2(0f, 0f)); st.AddColor(white); st.AddNormal(Level.North); st.AddVertex(verts[1]); st.AddUv(new Vector2(0f, 1f)); st.AddColor(white); st.AddNormal(Level.North); st.AddVertex(verts[3]); st.AddUv(new Vector2(1f, 0f)); st.AddColor(white); st.AddNormal(Level.North); st.AddVertex(verts[0]); st.AddUv(new Vector2(0f, 1f)); st.AddColor(white); st.AddNormal(Level.North); st.AddVertex(verts[3]); st.AddUv(new Vector2(1f, 1f)); st.AddColor(white); st.AddNormal(Level.North); st.AddVertex(verts[2]); _doorMesh = st.Commit(); } } }
private void createFaces(int x, int y, int z, int Dx, int Dy, int Dz, ref SurfaceTool surfaceTool, ref ChunkStruct c) { Vector3 voxPosition = new Vector3((x) * VOX_SIZE, (y) * VOX_SIZE, (z) * VOX_SIZE); voxPosition.x = voxPosition.x + (Dx * CHUNK_SIZE * VOX_SIZE); voxPosition.y = voxPosition.y + (Dy * CHUNK_SIZE * VOX_SIZE); voxPosition.z = voxPosition.z + (Dz * CHUNK_SIZE * VOX_SIZE); if (canCreateFace(x, y - 1, z, ref c)) { surfaceTool.AddNormal(new Vector3(0.0f, -1.0f, 0.0f)); surfaceTool.AddColor(new Color(1.0f, 1.0f, .7f, 1f)); surfaceTool.AddVertex(vertices[1] + voxPosition); surfaceTool.AddVertex(vertices[3] + voxPosition); surfaceTool.AddVertex(vertices[2] + voxPosition); surfaceTool.AddVertex(vertices[1] + voxPosition); surfaceTool.AddVertex(vertices[0] + voxPosition); surfaceTool.AddVertex(vertices[3] + voxPosition); } if (canCreateFace(x, y + 1, z, ref c)) { surfaceTool.AddNormal(new Vector3(0.0f, 1.0f, 0.0f)); surfaceTool.AddColor(new Color(0.7f, 0.0f, .7f, 1f)); surfaceTool.AddVertex(vertices[4] + voxPosition); surfaceTool.AddVertex(vertices[5] + voxPosition); surfaceTool.AddVertex(vertices[7] + voxPosition); surfaceTool.AddVertex(vertices[5] + voxPosition); surfaceTool.AddVertex(vertices[6] + voxPosition); surfaceTool.AddVertex(vertices[7] + voxPosition); } if (canCreateFace(x + 1, y, z, ref c)) { surfaceTool.AddNormal(new Vector3(1.0f, 0.0f, 0.0f)); surfaceTool.AddColor(new Color(1f, 1f, 1f, 1f)); surfaceTool.AddVertex(vertices[2] + voxPosition); surfaceTool.AddVertex(vertices[5] + voxPosition); surfaceTool.AddVertex(vertices[1] + voxPosition); surfaceTool.AddVertex(vertices[2] + voxPosition); surfaceTool.AddVertex(vertices[6] + voxPosition); surfaceTool.AddVertex(vertices[5] + voxPosition); } if (canCreateFace(x - 1, y, z, ref c)) { surfaceTool.AddNormal(new Vector3(-1.0f, 0.0f, 0.0f)); surfaceTool.AddColor(new Color(1f, 1f, 1f, 1f)); surfaceTool.AddVertex(vertices[0] + voxPosition); surfaceTool.AddVertex(vertices[7] + voxPosition); surfaceTool.AddVertex(vertices[3] + voxPosition); surfaceTool.AddVertex(vertices[0] + voxPosition); surfaceTool.AddVertex(vertices[4] + voxPosition); surfaceTool.AddVertex(vertices[7] + voxPosition); } if (canCreateFace(x, y, z + 1, ref c)) { surfaceTool.AddNormal(new Vector3(0.0f, 0.0f, 1.0f)); surfaceTool.AddColor(new Color(1f, 1f, 1f, 1f)); surfaceTool.AddVertex(vertices[3] + voxPosition); surfaceTool.AddVertex(vertices[6] + voxPosition); surfaceTool.AddVertex(vertices[2] + voxPosition); surfaceTool.AddVertex(vertices[3] + voxPosition); surfaceTool.AddVertex(vertices[7] + voxPosition); surfaceTool.AddVertex(vertices[6] + voxPosition); } if (canCreateFace(x, y, z - 1, ref c)) { surfaceTool.AddNormal(new Vector3(0.0f, 0.0f, -1.0f)); surfaceTool.AddColor(new Color(1f, 1f, 1f, 1f)); surfaceTool.AddVertex(vertices[0] + voxPosition); surfaceTool.AddVertex(vertices[1] + voxPosition); surfaceTool.AddVertex(vertices[5] + voxPosition); surfaceTool.AddVertex(vertices[5] + voxPosition); surfaceTool.AddVertex(vertices[4] + voxPosition); surfaceTool.AddVertex(vertices[0] + voxPosition); } }
public MeshInstance CreateChunkMesh(Chunk chunk) { MeshInstance instance = new MeshInstance(); SurfaceTool surfacetool = new SurfaceTool(); surfacetool.Begin(Mesh.PrimitiveType.Points); surfacetool.SetMaterial(chunkMaterial); int count = 0; for (int i = 0; i < Constants.CHUNK_SIZE3D / chunk.Materials; i++) { Run run = chunk.Voxels[i]; int objectID = run.value; TerraObject terraObject = registry.SelectByID((int)objectID); var x = i % CHUNK_SIZE; var y = (i / CHUNK_SIZE) % CHUNK_SIZE; var z = i / (CHUNK_SIZE * CHUNK_SIZE); if (chunk.Voxels[i].value != 0) { int face = 0b000000; //Left if (x == 0 || chunk.Voxels[i - 1].value != objectID) { face = 0b000001; } //Right else if (x == 63 || chunk.Voxels[i + 1].value != objectID) { face = 0b000010; } //Top else if (y == 63 || chunk.Voxels[i + 64].value != objectID) { face = 0b000100; } //Bottom else if (y == 0 || chunk.Voxels[i - 64].value != objectID) { face = 0b001000; } //Back else if (z == 63 || chunk.Voxels[i + 4096].value != objectID) { face = 0b010000; } //Front else if (z == 0 || chunk.Voxels[i - 4096].value != objectID) { face = 0b100000; } if (face != 0b000000) { int counter = 0; Vector3 normalAvg = Vector3.Zero; for (int j = 0; j < 6; j++) { int bitFlagN = (face >> j) & 1; if (bitFlagN == 1) { normalAvg = normalAvg + Normals[j]; counter += 1; } } count += 1; surfacetool.AddColor(new Color(1f, 1f, 1f, 1f)); Vector3 voxPosition = new Vector3((x) * VOX_SIZE, (y) * VOX_SIZE, (z) * VOX_SIZE); voxPosition.x = voxPosition.x + (chunk.x * CHUNK_SIZE * VOX_SIZE); voxPosition.y = voxPosition.y + (chunk.y * CHUNK_SIZE * VOX_SIZE); voxPosition.z = voxPosition.z + (chunk.z * CHUNK_SIZE * VOX_SIZE); if (counter > 0) { normalAvg = normalAvg / counter; surfacetool.AddNormal(normalAvg); } surfacetool.AddVertex(voxPosition); } } } surfacetool.Index(); instance.Mesh = surfacetool.Commit(); surfacetool.Clear(); instance.MaterialOverride = chunkMaterial.Duplicate() as ShaderMaterial; // Console.WriteLine("Mesh AABB Pos: {0} , Size: {1}, End: {2}",bb.Position,bb.Size,bb.End); return(instance); }
// Create each faces of a single voxel at pPositon in cPosition chunk. private void CreateVoxel(SurfaceTool pSurfaceTool, int x, int y, int z, Chunk pChunk) { // If no voxel is at position, skip if (!pChunk.Voxels[x, y, z].Active) { return; } // If block is next to each faces bool left = x != 0 ? !pChunk.Voxels[x - 1, y, z].Active : true; bool right = x != 15 ? !pChunk.Voxels[x + 1, y, z].Active : true; bool front = z != 15 ? !pChunk.Voxels[x, y, z + 1].Active : true; bool back = z != 0 ? !pChunk.Voxels[x, y, z - 1].Active : true; bool top = y != 254 ? !pChunk.Voxels[x, y + 1, z].Active : true; bool bottom = y > 0 ? !pChunk.Voxels[x, y - 1, z].Active : true; // If voxel is completly surrounded if (left && right && front && back && top && bottom) { return; } // If the voxel is on the side of a chunk. Check in the neighbor chunk. bool left2 = (x == 0 && PreloadedChunks[new Vector2(pChunk.Offset - new Vector2(1, 0))].Voxels[15, y, z].Active); bool right2 = (x == 15 && PreloadedChunks[new Vector2(pChunk.Offset + new Vector2(1, 0))].Voxels[0, y, z].Active); bool back2 = (z == 0 && PreloadedChunks[new Vector2(pChunk.Offset - new Vector2(0, 1))].Voxels[x, y, 15].Active); bool front2 = (z == 15 && PreloadedChunks[new Vector2(pChunk.Offset + new Vector2(0, 1))].Voxels[x, y, 0].Active); // Set right type var type = pChunk.Voxels[x, y, z].Type; // Local position of the voxel. Vector3 vertextOffset = new Vector3(x, y, z); // Get colors var color = GetVoxelColor(pChunk, vertextOffset, type); pSurfaceTool.AddColor(color); if (top) // Above { pSurfaceTool.AddNormal(new Vector3(0, 1, 0)); pSurfaceTool.AddVertex(Vertices[4] + vertextOffset); pSurfaceTool.AddVertex(Vertices[5] + vertextOffset); pSurfaceTool.AddVertex(Vertices[7] + vertextOffset); pSurfaceTool.AddVertex(Vertices[5] + vertextOffset); pSurfaceTool.AddVertex(Vertices[6] + vertextOffset); pSurfaceTool.AddVertex(Vertices[7] + vertextOffset); } if (right && !right2) // Right { pSurfaceTool.AddNormal(new Vector3(1, 0, 0)); pSurfaceTool.AddVertex(Vertices[2] + vertextOffset); pSurfaceTool.AddVertex(Vertices[5] + vertextOffset); pSurfaceTool.AddVertex(Vertices[1] + vertextOffset); pSurfaceTool.AddVertex(Vertices[2] + vertextOffset); pSurfaceTool.AddVertex(Vertices[6] + vertextOffset); pSurfaceTool.AddVertex(Vertices[5] + vertextOffset); } if (left && !left2) // Left { pSurfaceTool.AddNormal(new Vector3(-1, 0, 0)); pSurfaceTool.AddVertex(Vertices[0] + vertextOffset); pSurfaceTool.AddVertex(Vertices[7] + vertextOffset); pSurfaceTool.AddVertex(Vertices[3] + vertextOffset); pSurfaceTool.AddVertex(Vertices[0] + vertextOffset); pSurfaceTool.AddVertex(Vertices[4] + vertextOffset); pSurfaceTool.AddVertex(Vertices[7] + vertextOffset); } if (front && !front2) // Front { pSurfaceTool.AddNormal(new Vector3(0, 0, 1)); pSurfaceTool.AddVertex(Vertices[3] + vertextOffset); pSurfaceTool.AddVertex(Vertices[6] + vertextOffset); pSurfaceTool.AddVertex(Vertices[2] + vertextOffset); pSurfaceTool.AddVertex(Vertices[3] + vertextOffset); pSurfaceTool.AddVertex(Vertices[7] + vertextOffset); pSurfaceTool.AddVertex(Vertices[6] + vertextOffset); } if (back && !back2) // Above { pSurfaceTool.AddNormal(new Vector3(0, 0, -1)); pSurfaceTool.AddVertex(Vertices[0] + vertextOffset); pSurfaceTool.AddVertex(Vertices[1] + vertextOffset); pSurfaceTool.AddVertex(Vertices[5] + vertextOffset); pSurfaceTool.AddVertex(Vertices[5] + vertextOffset); pSurfaceTool.AddVertex(Vertices[4] + vertextOffset); pSurfaceTool.AddVertex(Vertices[0] + vertextOffset); } if (bottom && y != 0) { pSurfaceTool.AddNormal(new Vector3(0, 1, 0)); pSurfaceTool.AddVertex(Vertices[1] + vertextOffset); pSurfaceTool.AddVertex(Vertices[3] + vertextOffset); pSurfaceTool.AddVertex(Vertices[2] + vertextOffset); pSurfaceTool.AddVertex(Vertices[1] + vertextOffset); pSurfaceTool.AddVertex(Vertices[0] + vertextOffset); pSurfaceTool.AddVertex(Vertices[3] + vertextOffset); } }