public VertexPositionColorTextureNormal[] GenerateVertexArray() { for (int slice = 0; slice <= slices; ++slice) { for (int stack = 0; stack <= stacks; ++stack) { VertexPositionColorTextureNormal v = new VertexPositionColorTextureNormal(); v.Position.Y = (float)System.Math.Sin(((double)stack / stacks - 0.5) * System.Math.PI); if (v.Position.Y < -1) { v.Position.Y = -1; } else if (v.Position.Y > 1) { v.Position.Y = 1; } float l = (float)System.Math.Sqrt(1 - v.Position.Y * v.Position.Y); v.Position.X = l * (float)System.Math.Sin((double)slice / slices * System.Math.PI * 2); v.Position.Z = l * (float)System.Math.Cos((double)slice / slices * System.Math.PI * 2); v.Normal = v.Position; v.Texture = new Vector2((float)slice / slices, (float)stack / stacks); verts.Add(v); } } vertexCount = verts.Count; return(verts.ToArray()); }
public VertexPositionColorTextureNormal[] GenerateVertexArray(float xOffset = 0, float yOffset = 0, float zOffset = 0) { int heightMapSize = heights.GetLength(0); var vertArray = new VertexPositionColorTextureNormal[heightMapSize * heightMapSize]; int vertIndex = 0; for (int i = 0; i < heightMapSize; i++) { for (int j = 0; j < heightMapSize; j++) { var vert = new VertexPositionColorTextureNormal(); vert.Position = new Vector3(i * scale + xOffset, (heights[i, j] * scale * 0.2f) + yOffset, j * scale + zOffset); vertices.Add(vert.Position); vert.Color = Color.DarkOrange; vert.Texture = new Vector2(i * 2f / heightMapSize * scale, j * 2f / heightMapSize * scale); vert.Normal = Vector3.Up; vertArray[vertIndex] = vert; vertIndex++; } } GenerateNormals(ref vertArray); return(vertArray); }
public ProceduralPlane() { // Create the box data buffers. Indices = new short[6]; Vertices = new VertexPositionColorTextureNormal[4]; Vertices[0].Position = new Vector3(-0.5f, 0, -0.5f); Vertices[1].Position = new Vector3(-0.5f, 0, 0.5f); Vertices[2].Position = new Vector3(0.5f, 0, 0.5f); Vertices[3].Position = new Vector3(0.5f, 0, -0.5f); Vertices[0].Normal = Vector3.Up; Vertices[1].Normal = Vector3.Up; Vertices[2].Normal = Vector3.Up; Vertices[3].Normal = Vector3.Up; Vertices[0].Texture = new Vector2(0, 0); Vertices[1].Texture = new Vector2(0, 1); Vertices[2].Texture = new Vector2(1, 1); Vertices[3].Texture = new Vector2(1, 0); Indices[0] = (byte)(0); Indices[1] = (byte)(3); Indices[2] = (byte)(1); Indices[3] = (byte)(1); Indices[4] = (byte)(3); Indices[5] = (byte)(2); PrimitiveCount = 2; SetColor(Color.White); }
public VertexPositionColorTextureNormal[] GenerateVertexArray(IModule module, float radius) { for (int slice = 0; slice <= slices; ++slice) { for (int stack = 0; stack <= stacks; ++stack) { VertexPositionColorTextureNormal v = new VertexPositionColorTextureNormal(); v.Position.Y = (float)System.Math.Sin(((double)stack / stacks - 0.5) * System.Math.PI); if (v.Position.Y < -1) { v.Position.Y = -1; } else if (v.Position.Y > 1) { v.Position.Y = 1; } float l = (float)System.Math.Sqrt(1 - v.Position.Y * v.Position.Y); v.Position.X = l * (float)System.Math.Sin((double)slice / slices * System.Math.PI * 2); v.Position.Z = l * (float)System.Math.Cos((double)slice / slices * System.Math.PI * 2); v.Normal = v.Position; v.Texture = new Vector2((float)slice / slices, (float)stack / stacks); v.Position *= radius; double heightValue = module.GetValue(v.Position.X, v.Position.Y, v.Position.Z); v.Position = Vector3.Normalize(v.Position) * (float)(radius + heightValue); verts.Add(v); } } vertexCount = verts.Count; return(verts.ToArray()); }
public static VertexPositionColorTextureNormal[] CreateIndexedQuadVerts(float scale = 1.0f) { var rv = new VertexPositionColorTextureNormal[4]; rv[0].Position = new Vector3(0.5f * scale, 0, 0.5f * scale); // Top Right rv[1].Position = new Vector3(0.5f * scale, 0, -0.5f * scale); // Bottom Right rv[2].Position = new Vector3(-0.5f * scale, 0, -0.5f * scale); // Bottom Left rv[3].Position = new Vector3(-0.5f * scale, 0, 0.5f * scale); // Top Left rv[0].Color = new Vector4(1f, 1f, 0.0f, 1.0f); rv[1].Color = new Vector4(0.5f, 1f, 0.0f, 1.0f); rv[2].Color = new Vector4(1f, 1f, 0.0f, 1.0f); rv[3].Color = new Vector4(1f, 0.5f, 0.0f, 1.0f); rv[0].Texture = new Vector2(1f * scale, 1f * scale); rv[1].Texture = new Vector2(1f * scale, 0f); rv[2].Texture = new Vector2(0f, 0f); rv[3].Texture = new Vector2(0f, 1f * scale); rv[0].Normal = new Vector3(0.0f, 1f, 0.0f); rv[1].Normal = new Vector3(0.0f, 1f, 0.0f); rv[2].Normal = new Vector3(0.0f, 1f, 0.0f); rv[3].Normal = new Vector3(0.0f, 1f, 0.0f); return(rv); }
private void GenerateVertexArray() { Vertices = new VertexPositionColorTextureNormal[(slices + 1) * (stacks + 1)]; int currentIndex = 0; for (int slice = 0; slice <= slices; ++slice) { for (int stack = 0; stack <= stacks; ++stack) { VertexPositionColorTextureNormal v = new VertexPositionColorTextureNormal(); v.Position.Y = (float)System.Math.Sin(((double)stack / stacks - 0.5) * System.Math.PI); if (v.Position.Y < -1) { v.Position.Y = -1; } else if (v.Position.Y > 1) { v.Position.Y = 1; } float l = (float)System.Math.Sqrt(1 - v.Position.Y * v.Position.Y); v.Position.X = l * (float)System.Math.Sin((double)slice / slices * System.Math.PI * 2); v.Position.Z = l * (float)System.Math.Cos((double)slice / slices * System.Math.PI * 2); v.Normal = Vector3.Normalize(v.Position); v.Texture = new Vector2((float)slice / slices, (float)stack / stacks); v.Color = Color.White; Vertices[currentIndex] = v; currentIndex++; } } }
public ProceduralTriangle(Vector3 a, Vector3 b, Vector3 c, Vector3 normal, Vector4 col) { Indices = new short[3]; Vertices = new VertexPositionColorTextureNormal[3]; Indices[0] = 0; Indices[1] = 1; Indices[2] = 2; Vertices[0].Position = a; Vertices[0].Normal = normal; Vertices[0].Texture = GenerateBoxMap(Vertices[0].Position, normal); Vertices[1].Position = b; Vertices[1].Normal = normal; Vertices[1].Texture = GenerateBoxMap(Vertices[1].Position, normal); Vertices[2].Position = c; Vertices[2].Normal = normal; Vertices[2].Texture = GenerateBoxMap(Vertices[0].Position, normal); PrimitiveCount = 1; }
public ProceduralShape(VertexPositionColorTextureNormal[] vertices, short[] indices) { Vertices = new VertexPositionColorTextureNormal[vertices.Length]; Indices = new short[indices.Length]; Array.Copy(vertices, Vertices, vertices.Length); Array.Copy(indices, Indices, indices.Length); PrimitiveCount = Indices.Count() / 3; }
/// <summary> /// Creates a new BoxInstanceSourceData instance. /// </summary> public ProceduralCuboid(float width, float depth, float height) { _BoxCorners = new Vector3[] { new Vector3(width, height, depth), new Vector3(-width, height, depth), new Vector3(width, -height, depth), new Vector3(-width, -height, depth), new Vector3(width, height, -depth), new Vector3(-width, height, -depth), new Vector3(width, -height, -depth), new Vector3(-width, -height, -depth), }; // Create the box data buffers. Indices = new short[36]; Vertices = new VertexPositionColorTextureNormal[24]; // Create and fill the box data. int vertindex = 0; int indexoffset = 0; for (int f = 0; f < 6; f++) { // Get the static box builder data. Vector3 vert0 = _BoxCorners[_BoxIndices[vertindex]]; Vector3 vert1 = _BoxCorners[_BoxIndices[vertindex + 1]]; Vector3 vert2 = _BoxCorners[_BoxIndices[vertindex + 2]]; Vector3 vert3 = _BoxCorners[_BoxIndices[vertindex + 3]]; // Create and set the vertex positions, normals, uvs, and indices. Vertices[vertindex].Position = vert0; Vertices[vertindex + 1].Position = vert1; Vertices[vertindex + 2].Position = vert2; Vertices[vertindex + 3].Position = vert3; Plane plane = new Plane(vert0, vert2, vert1); Vertices[vertindex].Normal = plane.Normal; Vertices[vertindex + 1].Normal = plane.Normal; Vertices[vertindex + 2].Normal = plane.Normal; Vertices[vertindex + 3].Normal = plane.Normal; Vertices[vertindex].Texture = GenerateBoxMap(vert0, plane.Normal); Vertices[vertindex + 1].Texture = GenerateBoxMap(vert1, plane.Normal); Vertices[vertindex + 2].Texture = GenerateBoxMap(vert2, plane.Normal); Vertices[vertindex + 3].Texture = GenerateBoxMap(vert3, plane.Normal); Indices[indexoffset++] = (byte)(vertindex); Indices[indexoffset++] = (byte)(vertindex + 1); Indices[indexoffset++] = (byte)(vertindex + 2); Indices[indexoffset++] = (byte)(vertindex + 3); Indices[indexoffset++] = (byte)(vertindex + 2); Indices[indexoffset++] = (byte)(vertindex + 1); vertindex += 4; } //Indices = Indices.Reverse().ToArray(); PrimitiveCount = 12; }
public void GenerateGeometry(Effect testEffect, IModule module) { this.effect = testEffect; this.module = module; vertices = new VertexPositionColorTextureNormal[heightMapSize * heightMapSize]; int vertIndex = 0; for (float i = 0; i < heightMapSize; i++) { for (float j = 0; j < heightMapSize; j++) { var vert = new VertexPositionColorTextureNormal(); vert.Position = CalculateVertexPosition(i, j); vert.Texture = new Vector2(i * 2f / heightMapSize, j * 2f / heightMapSize); vert.Normal = normal; vert.Color = NodeColor; vertices[vertIndex] = vert; vertIndex++; } } GenerateIndices(); if (normal == Vector3.Down || normal == Vector3.Backward || normal == Vector3.Right) { indices = indices.Reverse().ToArray(); } indices = indices.Reverse().ToArray(); Sphereify(sphereSize); GenerateNormals(ref vertices); short[] ind = indices; var p = vertices.Select(x => x.Position).ToList(); var s = BoundingSphere.CreateFromPoints(p); ProceduralShape spherePatch = new ProceduralShape(vertices, indices); spherePatch.Translate(positionOffset); gameObject = GameObjectFactory.CreateRenderableGameObjectFromShape(spherePatch, effect); SystemCore.GameObjectManager.AddAndInitialiseGameObject(gameObject); isLeaf = true; }
/// <summary> /// Creates a new BoxInstanceSourceData instance. /// </summary> public ProceduralCube() { // Create the box data buffers. Indices = new short[36]; Vertices = new VertexPositionColorTextureNormal[24]; // Create and fill the box data. int vertindex = 0; int indexoffset = 0; for (int f = 0; f < 6; f++) { // Get the static box builder data. Vector3 vert0 = _BoxCorners[_BoxIndices[vertindex]]; Vector3 vert1 = _BoxCorners[_BoxIndices[vertindex + 1]]; Vector3 vert2 = _BoxCorners[_BoxIndices[vertindex + 2]]; Vector3 vert3 = _BoxCorners[_BoxIndices[vertindex + 3]]; // Create and set the vertex positions, normals, uvs, and indices. Vertices[vertindex].Position = vert0; Vertices[vertindex + 1].Position = vert1; Vertices[vertindex + 2].Position = vert2; Vertices[vertindex + 3].Position = vert3; Vertices[vertindex].Color = Color.White; Vertices[vertindex + 1].Color = Color.White;; Vertices[vertindex + 2].Color = Color.White;; Vertices[vertindex + 3].Color = Color.White;; Plane plane = new Plane(vert0, vert2, vert1); Vertices[vertindex].Normal = plane.Normal; Vertices[vertindex + 1].Normal = plane.Normal; Vertices[vertindex + 2].Normal = plane.Normal; Vertices[vertindex + 3].Normal = plane.Normal; Vertices[vertindex].Texture = GenerateBoxMap(vert0, plane.Normal); Vertices[vertindex + 1].Texture = GenerateBoxMap(vert1, plane.Normal); Vertices[vertindex + 2].Texture = GenerateBoxMap(vert2, plane.Normal); Vertices[vertindex + 3].Texture = GenerateBoxMap(vert3, plane.Normal); Indices[indexoffset++] = (byte)(vertindex); Indices[indexoffset++] = (byte)(vertindex + 1); Indices[indexoffset++] = (byte)(vertindex + 2); Indices[indexoffset++] = (byte)(vertindex + 3); Indices[indexoffset++] = (byte)(vertindex + 2); Indices[indexoffset++] = (byte)(vertindex + 1); vertindex += 4; } PrimitiveCount = 12; }
public void SetColour(Color colour) { var list = new VertexPositionColorTextureNormal[VertexBuffer.VertexCount]; VertexBuffer.GetData(list); for (int i = 0; i < list.Length; i++) { list[i].Color = colour; } VertexBuffer.SetData(list); }
internal void TransformVerts(Matrix transform) { var list = new VertexPositionColorTextureNormal[VertexBuffer.VertexCount]; VertexBuffer.GetData(list); for (int i = 0; i < list.Length; i++) { list[i].Position = Vector3.Transform(list[i].Position, transform); } VertexBuffer.SetData(list); }
internal List <Vector3> GetVertices() { var list = new VertexPositionColorTextureNormal[VertexBuffer.VertexCount]; VertexBuffer.GetData(list); List <Vector3> vertices = new List <Vector3>(VertexBuffer.VertexCount); for (int i = 0; i < VertexBuffer.VertexCount; i++) { vertices.Add(list[i].Position); } return(vertices); }
private void GenerateVertexArray() { Vertices = new VertexPositionColorTextureNormal[detail * detail]; Vector3 center = new Vector3(0, 0, 0); Vector3 rad = new Vector3((float)Math.Abs(1), 0, 0); for (int x = 0; x < detail; x++) //90 circles, difference between each is 4 degrees { float difx = 360.0f / detail; for (int y = 0; y < detail; y++) //90 veritces, difference between each is 4 degrees { float dify = 360.0f / detail; Matrix zrot = Matrix.CreateRotationZ(MathHelper.ToRadians(y * dify)); Matrix yrot = Matrix.CreateRotationY(MathHelper.ToRadians(x * difx)); Vector3 point = Vector3.Transform(Vector3.Transform(rad, zrot), yrot);//transformation Vertices[x + y * detail] = new VertexPositionColorTextureNormal(point, Color.White, Vector2.Zero, Vector3.Normalize(point)); } } }
public ProceduralDiamond() { //simple diamond (from center) float height = 0.35f; float width = 0.75f; //four points temp store List <Vector3> vertices = new List <Vector3>(); vertices.Add(new Vector3(height, 0f, 0f)); vertices.Add(new Vector3(0f, 0f, width)); vertices.Add(new Vector3(-height, 0f, 0f)); vertices.Add(new Vector3(0f, 0f, -width)); // Create the box data buffers. Indices = new short[6]; Vertices = new VertexPositionColorTextureNormal[4]; //triangle vertices for (int i = 0; i < vertices.Count; i++) { Vertices[i].Position = vertices[i]; Vertices[i].Normal = Vector3.Up; Vertices[i].Texture = GenerateBoxMap(Vertices[i].Position, Vertices[i].Normal); } Indices[0] = (byte)(0); Indices[1] = (byte)(3); Indices[2] = (byte)(1); Indices[3] = (byte)(1); Indices[4] = (byte)(3); Indices[5] = (byte)(2); Indices = Indices.Reverse().ToArray(); PrimitiveCount = 2; }
public LineBatch(params Vector3 [] linePoints) { Indices = new short[linePoints.Length * 2 - 2]; Vertices = new VertexPositionColorTextureNormal[linePoints.Length]; for (int i = 0; i < linePoints.Length; i++) { Vertices[i] = new VertexPositionColorTextureNormal(linePoints[i], Color.Black, Vector2.Zero, Vector3.Zero); if (i < linePoints.Length - 1) { Indices[i * 2] = (short)(i); Indices[(i * 2) + 1] = (short)(i + 1); } } PrimitiveCount = linePoints.Length - 1; }
public ProceduralCylinder(float bottomRadius, float topRadius, float length, int slices, int stacks) { float sliceStep = MathHelper.TwoPi / slices; float heightStep = length / stacks; float radiusStep = (topRadius - bottomRadius) / stacks; float currentHeight = -length / 2; int vertexCount = (stacks + 1) * slices + 2; //cone = stacks * slices + 1 int triangleCount = (stacks + 1) * slices * 2; //cone = stacks * slices * 2 + slices int indexCount = triangleCount * 3; float currentRadius = bottomRadius; Vertices = new VertexPositionColorTextureNormal[vertexCount]; // Start at the bottom of the cylinder int currentVertex = 0; Vertices[currentVertex++] = new VertexPositionColorTextureNormal(new Vector3(0, currentHeight, 0), Color.White, Vector2.Zero, Vector3.Down); for (int i = 0; i <= stacks; i++) { float sliceAngle = 0; for (int j = 0; j < slices; j++) { float x = currentRadius * (float)Math.Cos(sliceAngle); float y = currentHeight; float z = currentRadius * (float)Math.Sin(sliceAngle); Vector3 position = new Vector3(x, y, z); Vertices[currentVertex++] = new VertexPositionColorTextureNormal(position, Color.White, Vector2.Zero, Vector3.Normalize(position)); sliceAngle += sliceStep; } currentHeight += heightStep; currentRadius += radiusStep; } Vertices[currentVertex++] = new VertexPositionColorTextureNormal(new Vector3(0, length / 2, 0), Color.White, Vector2.Zero, Vector3.Up); // Create the actual vertex buffer object CreateIndexBuffer(vertexCount, indexCount, slices); }
public void AddTriangle(Vector3 a, Vector3 b, Vector3 c) { Plane p = new Plane(a, b, c); VertexPositionColorTextureNormal v1 = new VertexPositionColorTextureNormal(); v1.Position = a; v1.Normal = p.Normal; v1.Color = Color.White; v1.Texture = new Vector2(0, 0); VertexPositionColorTextureNormal v2 = new VertexPositionColorTextureNormal(); v2.Position = b; v2.Normal = p.Normal; v2.Color = Color.White; v2.Texture = new Vector2(0, 0); VertexPositionColorTextureNormal v3 = new VertexPositionColorTextureNormal(); v3.Position = c; v3.Normal = p.Normal; v3.Color = Color.White; v3.Texture = new Vector2(0, 0); vertices.Add(v1); vertices.Add(v2); vertices.Add(v3); indices.Add(currentIndex); currentIndex++; indices.Add(currentIndex); currentIndex++; indices.Add(currentIndex); currentIndex++; primCount++; }
public void Update() { if (NeedRecalculation) { NeedRecalculation = false; Matrix m = Matrix.Translation(position); VerticeArray = new VertexPositionColorTextureNormal[VertexCount]; for (int i = VertexCount; --i >= 0;) { VertexPositionColorTextureNormal vpc = Vertices[i]; Vector4 v4; Vector3.Transform(ref vpc.Position, ref m, out v4); vpc.Position = new Vector3(v4.X, v4.Y, v4.Z); VerticeArray[i] = vpc; } //VerticeArray = Vertices.ToArray(); IndiceArray = Indices.ToArray(); } }
public void RegenerateNormals() { //If you have three vertices, V1, V2 and V3, ordered in counterclockwise order, you can obtain the direction of the normal by computing //(V2 - V1) x (V3 - V1), where x is the cross product of the two vectors. for (int i = 0; i < Indices.Length - 3; i += 3) { VertexPositionColorTextureNormal v1 = Vertices[i]; VertexPositionColorTextureNormal v2 = Vertices[i + 1]; VertexPositionColorTextureNormal v3 = Vertices[i + 2]; Vector3 v1v2 = v2.Position - v1.Position; Vector3 v1v3 = v3.Position - v1.Position; // Vector3 triMidPoint = (v1.Position + v2.Position + v3.Position) / 3; Vector3 potentialNormal = Vector3.Cross(v1v2, v1v3); potentialNormal.Normalize(); Vertices[i].Normal = potentialNormal; Vertices[i + 1].Normal = potentialNormal; Vertices[i + 2].Normal = potentialNormal; } }
/// <summary> /// /// </summary> /// <param name="heightMap"></param> private void InitializeVertices(Texture2D heightMap) { for (int zIndex = 0; zIndex < mHeight; ++zIndex) { for (int xIndex = 0; xIndex < mWidth; ++xIndex) { float vertexHeight = RetrieveHeight(heightMap, xIndex, zIndex); VertexPositionColorTextureNormal vertex = new VertexPositionColorTextureNormal(); vertex.Position = new Vector3(xIndex - mWidth / 2, vertexHeight, zIndex - mHeight / 2); vertex.Color = Color.BurlyWood; vertex.TexCoord = new Vector2((float)xIndex / (float)mWidth, (float)zIndex / (float)mHeight); vertex.Normal = Vector3.Zero; mVertices[xIndex + zIndex * mWidth] = vertex; FillLookUpTable(xIndex, zIndex); } } int topSideBaseIndex = 0; int bottomSideBaseIndex = topSideBaseIndex + mWidth; // Add top and bottom sides. for (int xIndex = 0; xIndex < mWidth; ++xIndex) { VertexPositionColorTextureNormal vertex = new VertexPositionColorTextureNormal(); vertex.Position = new Vector3(xIndex - mWidth / 2, 0.0f, 0.0f - mHeight / 2); vertex.Color = Color.BurlyWood; vertex.TexCoord = new Vector2((float)xIndex / (float)mWidth, 0.0f); vertex.Normal = Vector3.Zero; mEdgeVertices[topSideBaseIndex + xIndex] = vertex; vertex.Position.Z = mHeight - 1 - mHeight / 2; vertex.TexCoord.Y = 1.0f; mEdgeVertices[bottomSideBaseIndex + xIndex] = vertex; } int leftSideBaseIndex = bottomSideBaseIndex + mWidth; int rightSideBaseIndex = leftSideBaseIndex + mHeight; // Add left and right sides. for (int zIndex = 0; zIndex < mHeight; ++zIndex) { VertexPositionColorTextureNormal vertex = new VertexPositionColorTextureNormal(); vertex.Position = new Vector3(0.0f - mWidth / 2, 0.0f, zIndex - mHeight / 2); vertex.Color = Color.BurlyWood; vertex.TexCoord = new Vector2(0.0f, (float)zIndex / (float)mHeight); vertex.Normal = Vector3.Zero; mEdgeVertices[leftSideBaseIndex + zIndex] = vertex; vertex.Position.X = mWidth - 1 - mWidth / 2; vertex.TexCoord.X = 1.0f; mEdgeVertices[rightSideBaseIndex + zIndex] = vertex; } }
private VertexPositionColorTextureNormal[] ConstructEdgeChunk(int edgeIndex) { int edgeHeight = 2; int[] edgeLengths = { mWidth, mWidth, mHeight, mHeight }; VertexPositionColorTextureNormal[] result = new VertexPositionColorTextureNormal[edgeLengths[edgeIndex] * edgeHeight]; switch (edgeIndex) { // Top Edge or Bottom Edge. case 0: case 1: { int verticesOffset = edgeIndex == 0 ? 0 : mWidth * (mHeight - 1); int edgeVerticesOffset = edgeIndex == 0 ? 0 : mWidth; Vector3 normal = edgeIndex == 0 ? new Vector3(0.0f, 0.0f, -1.0f) : new Vector3(0.0f, 0.0f, 1.0f); for (int x = 0; x < mWidth; ++x) { result[x] = mVertices[x + verticesOffset]; result[x].Normal = normal; result[x + mWidth] = mEdgeVertices[x + edgeVerticesOffset]; result[x + mWidth].Normal = normal; } break; } // Left Edge or Right Edge. case 2: case 3: { int verticesOffset = edgeIndex == 2 ? 0 : mWidth - 1; int edgeVerticesOffset = edgeIndex == 2 ? 2 * mWidth : 2 * mWidth + mHeight; Vector3 normal = edgeIndex == 2 ? new Vector3(-1.0f, 0.0f, 0.0f) : new Vector3(1.0f, 0.0f, 0.0f); for (int z = 0; z < mHeight; ++z) { result[z] = mVertices[z * mWidth + verticesOffset]; result[z].Normal = normal; result[z + mHeight] = mEdgeVertices[z + edgeVerticesOffset]; result[z + mHeight].Normal = normal; if (result[z].Position == Vector3.Zero || result[z + mHeight].Position == Vector3.Zero) { Console.WriteLine(); } } break; } } return result; }
/// <summary> /// Copies vertices from total collection in to a chunk. /// </summary> /// <param name="verticalIndex">Y index in to chunk array.</param> /// <param name="horizontalIndex">X index in to chunk array.</param> private VertexPositionColorTextureNormal[] ConstructChunk(int verticalIndex, int horizontalIndex) { int chunkHeight = mChunkHeight; int remainingHeight = mHeight - verticalIndex * (mChunkHeight - 1); if (remainingHeight < mChunkHeight) { chunkHeight = remainingHeight; } int chunkWidth = mChunkWidth; int remainingWidth = mWidth - horizontalIndex * (mChunkWidth - 1); if (remainingWidth < mChunkWidth) { chunkWidth = remainingWidth; } VertexPositionColorTextureNormal[] result = new VertexPositionColorTextureNormal[chunkHeight * chunkWidth]; for (int zIndex = 0; zIndex < chunkHeight; ++zIndex) { for (int xIndex = 0; xIndex < chunkWidth; ++xIndex) { int z = verticalIndex * (mChunkHeight - 1) + zIndex; int x = horizontalIndex * (mChunkWidth - 1) + xIndex; result[xIndex + zIndex * chunkWidth] = mVertices[x + z * mWidth]; result[xIndex + zIndex * chunkWidth].TexCoord = new Vector2((float)xIndex / (float)(chunkWidth - 1), (float)zIndex / (float)(chunkHeight - 1)); } } return result; }
public void BuildGeometry() { var vertices = new VertexPositionColorTextureNormal[(heightMapSize * heightMapSize)]; int vertIndex = 0; List <int> topEdges = new List <int>(); List <int> bottomEdges = new List <int>(); List <int> leftEdges = new List <int>(); List <int> rightEdges = new List <int>(); for (float i = 0; i < heightMapSize; i++) { for (float j = 0; j < heightMapSize; j++) { var vert = new VertexPositionColorTextureNormal(); vert.Position = CalculateVertexPosition(i, j); vert.Texture = new Vector2(i * 2f / heightMapSize, j * 2f / heightMapSize); vert.Normal = normal; vert.Color = NodeColor; vertices[vertIndex] = vert; if (i == 0) { rightEdges.Add(vertIndex); } if (i == heightMapSize - 1) { leftEdges.Add(vertIndex); } if (j == 0) { bottomEdges.Add(vertIndex); } if (j == heightMapSize - 1) { topEdges.Add(vertIndex); } vertIndex++; } } var indices = GenerateIndices(); if (normal == Vector3.Up || normal == Vector3.Forward || normal == Vector3.Left) { indices = indices.Reverse().ToArray(); } List <NeighbourTracker.Connection> connections = this.Planet.GetNeighbours(this); if (connections != null && SystemWarGlobalSettings.RepairSeams) { var northConn = connections.Find(x => x.direction == NeighbourTracker.ConnectionDirection.north); var southConn = connections.Find(x => x.direction == NeighbourTracker.ConnectionDirection.south); var westConn = connections.Find(x => x.direction == NeighbourTracker.ConnectionDirection.west); var eastConn = connections.Find(x => x.direction == NeighbourTracker.ConnectionDirection.east); Sphereify(sphereSize, ref vertices); if (northConn != null) { CalculateEdgeAdjustment(northConn, ref vertices, topEdges, NeighbourTracker.ConnectionDirection.north); } if (southConn != null) { CalculateEdgeAdjustment(southConn, ref vertices, bottomEdges, NeighbourTracker.ConnectionDirection.south); } if (westConn != null) { CalculateEdgeAdjustment(westConn, ref vertices, leftEdges, NeighbourTracker.ConnectionDirection.west); } if (eastConn != null) { CalculateEdgeAdjustment(eastConn, ref vertices, rightEdges, NeighbourTracker.ConnectionDirection.east); } } else { Sphereify(sphereSize, ref vertices); } GenerateNormals(ref vertices, ref indices); var p = vertices.Select(x => x.Position).ToList(); boundingSphere = BoundingSphere.CreateFromPoints(p); ProceduralShape spherePatch = new ProceduralShape(vertices, indices); this.AddComponent(new RenderGeometryComponent(spherePatch)); meshCollider = new MobileMeshColliderComponent(this, spherePatch.GetVertices(), spherePatch.GetIndicesAsInt().ToArray()); AddComponent(meshCollider); renderComponent = new EffectRenderComponent(effect); renderComponent.DrawOrder = Planet.DrawOrder; this.AddComponent(renderComponent); SetHighPrecisionPosition(this); built = true; }
public void Update() { if( NeedRecalculation ) { NeedRecalculation = false; Matrix m = Matrix.Translation( position ); VerticeArray = new VertexPositionColorTextureNormal[ VertexCount ]; for( int i = VertexCount; --i >= 0; ) { VertexPositionColorTextureNormal vpc = Vertices[ i ]; Vector4 v4; Vector3.Transform( ref vpc.Position, ref m, out v4 ); vpc.Position = new Vector3( v4.X, v4.Y, v4.Z ); VerticeArray[ i ] = vpc; } //VerticeArray = Vertices.ToArray(); IndiceArray = Indices.ToArray(); } }