private void CreateFrontQuad(ref ChunkStruct c, int x, int y, int x2, int y2, int z) { //The Z-1 is more of a hack because the face previously was shown on the back side rather than the front Vector3 voxPosition1 = new Vector3((x) * VOX_SIZE, (y) * VOX_SIZE, (z - 1) * VOX_SIZE); voxPosition1.x = voxPosition1.x + (c.Dx * CHUNK_SIZE * VOX_SIZE); voxPosition1.y = voxPosition1.y + (c.Dy * CHUNK_SIZE * VOX_SIZE); voxPosition1.z = voxPosition1.z + (c.Dz * CHUNK_SIZE * VOX_SIZE); Vector3 voxPosition2 = new Vector3((x2) * VOX_SIZE, (y2) * VOX_SIZE, (z) * VOX_SIZE); voxPosition2.x = voxPosition2.x + (c.Dx * CHUNK_SIZE * VOX_SIZE); voxPosition2.y = voxPosition2.y + (c.Dy * CHUNK_SIZE * VOX_SIZE); voxPosition2.z = voxPosition2.z + (c.Dz * CHUNK_SIZE * VOX_SIZE); SurfaceTool surfaceTool = c.surfaceTool; surfaceTool.AddNormal(new Vector3(0.0f, 0.0f, -1.0f)); surfaceTool.AddVertex(new Vector3(voxPosition1.x, voxPosition1.y, voxPosition1.z)); surfaceTool.AddVertex(new Vector3(voxPosition2.x, voxPosition1.y, voxPosition1.z)); surfaceTool.AddVertex(new Vector3(voxPosition2.x, voxPosition2.y, voxPosition1.z)); surfaceTool.AddVertex(new Vector3(voxPosition2.x, voxPosition2.y, voxPosition1.z)); surfaceTool.AddVertex(new Vector3(voxPosition1.x, voxPosition2.y, voxPosition1.z)); surfaceTool.AddVertex(new Vector3(voxPosition1.x, voxPosition1.y, voxPosition1.z)); // addQuad(ref c, // new Vector3(x,y,z), // new Vector3(x2,y,z), // new Vector3(x2,y2,z), // new Vector3(x,y2,z)); }
private void AddVertex(Vector3 vertex, Vector2 uv, Vector3 normal) { Builder.AddUv(uv); Builder.AddNormal(normal); Builder.AddVertex(vertex); collisionShapeFaces.Add(vertex); }
private void CreateTopQuad(ref ChunkStruct c, int x, int z, int x2, int z2, int y) { Vector3 voxPosition1 = new Vector3((x) * VOX_SIZE, (y) * VOX_SIZE, (z) * VOX_SIZE); voxPosition1.x = voxPosition1.x + (c.Dx * CHUNK_SIZE * VOX_SIZE); voxPosition1.y = voxPosition1.y + (c.Dy * CHUNK_SIZE * VOX_SIZE); voxPosition1.z = voxPosition1.z + (c.Dz * CHUNK_SIZE * VOX_SIZE); Vector3 voxPosition2 = new Vector3((x2) * VOX_SIZE, (y) * VOX_SIZE, (z2) * VOX_SIZE); voxPosition2.x = voxPosition2.x + (c.Dx * CHUNK_SIZE * VOX_SIZE); voxPosition2.y = voxPosition2.y + (c.Dy * CHUNK_SIZE * VOX_SIZE); voxPosition2.z = voxPosition2.z + (c.Dz * CHUNK_SIZE * VOX_SIZE); SurfaceTool surfaceTool = c.surfaceTool; surfaceTool.AddNormal(new Vector3(0.0f, 1.0f, 0.0f)); surfaceTool.AddVertex(new Vector3(voxPosition1.x, voxPosition2.y, voxPosition1.z)); surfaceTool.AddVertex(new Vector3(voxPosition2.x, voxPosition2.y, voxPosition1.z)); surfaceTool.AddVertex(new Vector3(voxPosition1.x, voxPosition2.y, voxPosition2.z)); surfaceTool.AddVertex(new Vector3(voxPosition2.x, voxPosition2.y, voxPosition1.z)); surfaceTool.AddVertex(new Vector3(voxPosition2.x, voxPosition2.y, voxPosition2.z)); surfaceTool.AddVertex(new Vector3(voxPosition1.x, voxPosition2.y, voxPosition2.z)); // this.addQuad(ref c, // new Vector3(x, y, z2), // new Vector3(x2, y, z2), // new Vector3(x2, y, z), // new Vector3(x, y, z)); }
public void createCube(int x, int y, int z, BlockState data, bool renderFront, bool renderBack, bool renderRight, bool renderLeft, bool renderTop, bool renderBot) { var p001 = new Vector3(x - CUBE_SIZE, y - CUBE_SIZE, z + CUBE_SIZE); var p101 = new Vector3(x + CUBE_SIZE, y - CUBE_SIZE, z + CUBE_SIZE); var p111 = new Vector3(x + CUBE_SIZE, y + CUBE_SIZE, z + CUBE_SIZE); var p011 = new Vector3(x - CUBE_SIZE, y + CUBE_SIZE, z + CUBE_SIZE); var p100 = new Vector3(x + CUBE_SIZE, y - CUBE_SIZE, z - CUBE_SIZE); var p000 = new Vector3(x - CUBE_SIZE, y - CUBE_SIZE, z - CUBE_SIZE); var p010 = new Vector3(x - CUBE_SIZE, y + CUBE_SIZE, z - CUBE_SIZE); var p110 = new Vector3(x + CUBE_SIZE, y + CUBE_SIZE, z - CUBE_SIZE); if (renderFront) { tool.AddNormal(new Vector3(0, 0, -1)); addTri(p001, p101, p111, p011, data); } if (renderBack) { tool.AddNormal(new Vector3(0, 0, 1)); addTri(p100, p000, p010, p110, data); } if (renderRight) { tool.AddNormal(new Vector3(1, 0, 0)); addTri(p101, p100, p110, p111, data); } if (renderLeft) { tool.AddNormal(new Vector3(-1, 0, 0)); addTri(p000, p001, p011, p010, data); } if (renderTop) { tool.AddNormal(new Vector3(0, 1, 0)); addTri(p011, p111, p110, p010, data); } if (renderBot) { tool.AddNormal(new Vector3(0, -1, 0)); addTri(p000, p100, p101, p001, data); } }
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 void CreateFaces(int x, int y, int z) { 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)) { surfaceTool.AddNormal(new Vector3(0.0f, -1.0f, 0.0f)); 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)) { surfaceTool.AddNormal(new Vector3(0.0f, 1.0f, 0.0f)); 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)) { surfaceTool.AddNormal(new Vector3(1.0f, 0.0f, 0.0f)); 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)) { surfaceTool.AddNormal(new Vector3(-1.0f, 0.0f, 0.0f)); 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)) { surfaceTool.AddNormal(new Vector3(0.0f, 0.0f, 1.0f)); 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)) { surfaceTool.AddNormal(new Vector3(0.0f, 0.0f, -1.0f)); 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); } }
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 void AddNormal(RR_Godot.Core.Geometry.Vector3Cp normal) { Surface.AddNormal(new Godot.Vector3(normal.x, normal.y, normal.z)); }
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); } }
public override void _Ready() { var surfTool = new SurfaceTool(); surfTool.Begin(Mesh.PrimitiveType.Triangles); Vector3[] vertices = new Vector3[] { new Vector3(0, 0, 0), new Vector3(length, 0, 0), new Vector3(length, 0, width), new Vector3(0, 0, width), new Vector3(0, height, 0), new Vector3(length, height, 0), new Vector3(length, height, width), new Vector3(0, height, width) }; // surfTool.AddNormal(new Vector3(0.0f,0.0f,-1.0f)); // surfTool.AddVertex(new Vector3(length, -height, -width)); // surfTool.AddVertex(new Vector3(-length, -height, -width)); // surfTool.AddVertex(new Vector3(-length, height, -width)); // surfTool.AddVertex(new Vector3(length, height, -width)); // // surfTool.AddNormal(new Vector3(0.0f, 0.0f, 1.0f)); // surfTool.AddVertex(new Vector3(-length, -height, width)); // surfTool.AddVertex(new Vector3(length, -height, width)); // surfTool.AddVertex(new Vector3(length, height, width)); // surfTool.AddVertex(new Vector3(-length, height, width)); // // surfTool.AddNormal(new Vector3(1.0f, 0.0f, 0.0f)); // surfTool.AddVertex(new Vector3(length, -height, width)); // surfTool.AddVertex(new Vector3(length, -height, -width)); // surfTool.AddVertex(new Vector3(length, height, -width)); // surfTool.AddVertex(new Vector3(length, height, width)); // surfTool.AddNormal(new Vector3(-1.0f, 0.0f, 0.0f)); // surfTool.AddVertex(new Vector3(-length, -height, -width)); // surfTool.AddVertex(new Vector3(-length, -height, width)); // surfTool.AddVertex(new Vector3(-length, height, width)); // surfTool.AddVertex(new Vector3(-length, height, -width)); //Bottom Face surfTool.AddNormal(new Vector3(0.0f, -1.0f, 0.0f)); surfTool.AddVertex(vertices[1]); surfTool.AddVertex(vertices[3]); surfTool.AddVertex(vertices[2]); surfTool.AddVertex(vertices[1]); surfTool.AddVertex(vertices[0]); surfTool.AddVertex(vertices[3]); surfTool.AddNormal(new Vector3(0.0f, 1.0f, 0.0f)); surfTool.AddVertex(vertices[4]); surfTool.AddVertex(vertices[5]); surfTool.AddVertex(vertices[7]); surfTool.AddVertex(vertices[5]); surfTool.AddVertex(vertices[6]); surfTool.AddVertex(vertices[7]); surfTool.AddNormal(new Vector3(0.0f, 0.0f, 1.0f)); surfTool.AddVertex(vertices[0]); surfTool.AddVertex(vertices[1]); surfTool.AddVertex(vertices[5]); surfTool.AddVertex(vertices[5]); surfTool.AddVertex(vertices[4]); surfTool.AddVertex(vertices[0]); surfTool.AddNormal(new Vector3(0.0f, 0.0f, -1.0f)); surfTool.AddVertex(vertices[3]); surfTool.AddVertex(vertices[6]); surfTool.AddVertex(vertices[2]); surfTool.AddVertex(vertices[3]); surfTool.AddVertex(vertices[7]); surfTool.AddVertex(vertices[6]); surfTool.AddNormal(new Vector3(-1.0f, 0.0f, 0.0f)); surfTool.AddVertex(vertices[0]); surfTool.AddVertex(vertices[7]); surfTool.AddVertex(vertices[3]); surfTool.AddVertex(vertices[0]); surfTool.AddVertex(vertices[4]); surfTool.AddVertex(vertices[7]); surfTool.AddNormal(new Vector3(1.0f, 0.0f, 0.0f)); surfTool.AddVertex(vertices[2]); surfTool.AddVertex(vertices[5]); surfTool.AddVertex(vertices[1]); surfTool.AddVertex(vertices[2]); surfTool.AddVertex(vertices[6]); surfTool.AddVertex(vertices[5]); // // surfTool.AddNormal(new Vector3(0.0f, 1.0f, 0.0f)); // surfTool.AddVertex(new Vector3(length, height, -width)); // surfTool.AddVertex(new Vector3(-length, height, -width)); // surfTool.AddVertex(new Vector3(-length, height, width)); // surfTool.AddVertex(new Vector3(length, height, width)); surfTool.Index(); // surfTool.GenerateNormals(); var mesh = surfTool.Commit(); this.SetMesh(mesh); }