public float[] GetHeights(int xstart, int zstart) { int size = chunkSize; height = size - 1; width = size - 1; heights = new float[size * size]; biomes = new Biome[heights.Length]; vertexArray = new Vertex[heights.Length]; indexArray = new int[width * height * 6]; for (int xx = 0; xx < size; xx++) { for (int yy = 0; yy < size; yy++) { float _x = yy + ((int)x * (size - 1)); float _y = xx + ((int)z * (size - 1)); float terrainHeight = (float)parent.getNoise(_x, _y); float biomeHeight = 1f, temperature = 1f; int heightIndex = HeightIndexAt(yy, xx); if (this.generateBiomes) { biomeHeight = (float)(parent.getNoise(((double)_y) * 0.1, ((double)_x) * 0.1)); temperature = (float)(parent.getNoise(((double)_y), terrainHeight, ((double)_x))); Biome biome = new Biome(); biome.AverageTemperature = temperature; if (System.Math.Abs(biomeHeight) < 0.3f) { biome.Topography = Biome.BiomeTopography.Plains; } else { biome.Topography = Biome.BiomeTopography.Hills; } biomeHeight *= 6 * (float)System.Math.Abs(biomeHeight); biomes[heightIndex] = biome; } heights[heightIndex] = terrainHeight * biomeHeight * 25f; } } return heights; }
public Vertex(Vertex other) { this.position = other.position; this.normal = other.normal; this.texCoord0 = other.texCoord0; this.texCoord1 = other.texCoord1; this.tangent = other.tangent; this.bitangent = other.bitangent; for (int i = 0; i < 4; i++) { this.boneIndices[i] = other.boneIndices[i]; this.boneWeights[i] = other.boneWeights[i]; } }
public void BuildVertices() { int heightPitch = height + 1; int widthPitch = width + 1; int idx = 0; int hIdx = 0; // int strength = 10; // multiplier for height map for (int z = 0; z < heightPitch; z++) { for (int x = 0; x < widthPitch; x++) { // POSITION vertexArray[idx++] = new Vertex(new Vector3f(scale.x * (x-(widthPitch/2)), heights[hIdx++] * scale.y, scale.z * (z - (heightPitch / 2))), new Vector2f((float)-x / (float)widthPitch, (float)-z / (float)heightPitch), new Vector3f()); /* vertexArray[idx++] = new Vertex(new Vector3f(scale.x * x, heights[hIdx++] * scale.y, scale.z * z), new Vector2f((float)-x / (float)widthPitch, (float)-z / (float)heightPitch), new Vector3f());*/ } } }
protected void AddNormal(int vertIndex, Vertex[] verts, float x, float y, float z) { int i = vertIndex; verts[i].GetNormal().x += x; verts[i].GetNormal().y += y; verts[i].GetNormal().z += z; }
protected void NormalizeNormal(int vertIndex, Vertex[] verts) { int i = vertIndex; float x = verts[i].GetNormal().x; float y = verts[i].GetNormal().y; float z = verts[i].GetNormal().z; float num2 = ((x * x) + (y * y)) + (z * z); float num = 1f / (float)System.Math.Sqrt(num2); x *= num; y *= num; z *= num; verts[i].GetNormal().x = x; verts[i].GetNormal().y = y; verts[i].GetNormal().z = z; }
protected void CalcNormals(int[] idc, Vertex[] verts) { for (int i = 0; i < idc.Length; i += 3) { int i1 = idc[i]; int i2 = idc[i + 1]; int i3 = idc[i + 2]; // p1 float x1 = verts[i1].GetPosition().x; float y1 = verts[i1].GetPosition().y; float z1 = verts[i1].GetPosition().z; // p2 float x2 = verts[i2].GetPosition().x; float y2 = verts[i2].GetPosition().y; float z2 = verts[i2].GetPosition().z; // p3 float x3 = verts[i3].GetPosition().x; float y3 = verts[i3].GetPosition().y; float z3 = verts[i3].GetPosition().z; // u = p3 - p1 float ux = x3 - x1; float uy = y3 - y1; float uz = z3 - z1; // v = p2 - p1 float vx = x2 - x1; float vy = y2 - y1; float vz = z2 - z1; // n = cross(v, u) float nx = (vy * uz) - (vz * uy); float ny = (vz * ux) - (vx * uz); float nz = (vx * uy) - (vy * ux); // normalize(n) float num2 = ((nx * nx) + (ny * ny)) + (nz * nz); float num = 1f / (float)System.Math.Sqrt(num2); nx *= num; ny *= num; nz *= num; AddNormal(idc[i], verts, nx, ny, nz); AddNormal(idc[i + 1], verts, nx, ny, nz); AddNormal(idc[i + 2], verts, nx, ny, nz); } for (int i = 0; i < (verts.Length); i++) { NormalizeNormal(i, verts); } }
private void EndModel() { if (skeletons.Count > 0) { Skeleton skeleton = skeletons[skeletons.Count - 1]; if (skeleton.GetNumBones() > 0) { for (int i = 0; i < skeleton.GetNumBones(); i++) skeleton.GetBone(i).SetToBindingPose(); skeleton.GetBone(0).CalculateBindingRotation(); skeleton.GetBone(0).CalculateBindingTranslation(); for (int i = 0; i < skeleton.GetNumBones(); i++) { skeleton.GetBone(i).StoreBindingPose(); skeleton.GetBone(i).ClearPose(); } skeleton.GetBone(0).UpdateTransform(); } if (hasAnimations) { for (int j = 0; j < animations.Count; j++) { skeletons[0].AddAnimation(animations[j]); } nodes[0].AddController(new AnimationController(skeletons[0])); } } for (int i = 0; i < meshes.Count; i++) { List<Vertex> cVerts = vertices[i]; List<int> cFaces = faces[i]; List<Vector3f> cPos = positions[i]; List<Vector3f> cNorm = normals[i]; List<Vector2f> tc0 = texcoords0[i]; List<Vector2f> tc1 = texcoords1[i]; List<BoneAssign> ba = null; Mesh mesh = meshes[i]; if (boneAssigns.Count > 0) { ba = boneAssigns[i]; } int stride = 3; if (tc1.Count > 0) stride++; for (int j = 0; j < cFaces.Count; j += stride) { Vertex v = new Vertex(cPos[cFaces[j]]); if (cNorm.Count > 0) { mesh.GetAttributes().SetAttribute(VertexAttributes.NORMALS); v.SetNormal(cNorm[cFaces[j + 1]]); } if (tc0.Count > 0) { mesh.GetAttributes().SetAttribute(VertexAttributes.TEXCOORDS0); v.SetTexCoord1(tc0[cFaces[j + 2]]); } if (tc1.Count > 0) { mesh.GetAttributes().SetAttribute(VertexAttributes.TEXCOORDS1); v.SetTexCoord1(tc1[cFaces[j + 3]]); } cVerts.Add(v); } List<int> indexData = new List<int>(); if (skeletons.Count > 0) { mesh.SetSkeleton(skeletons[0]); if (boneAssigns.Count > 0) { for (int j = 0; j < ba.Count; j++) { Vertex v = cVerts[ba[j].GetVertexIndex()]; v.AddBoneIndex(ba[j].GetBoneIndex()); v.AddBoneWeight(ba[j].GetBoneWeight()); } } } mesh.SetVertices(cVerts); if (geoms.Count > 0) { Geometry parent = geoms[i]; Material material = null; if (geomMats.ContainsKey(parent)) material = geomMats[parent]; else material = new Material(); parent.Mesh = mesh; parent.Material = material; } } }
public float[] GetHeights(int xstart, int zstart) { int size = chunkSize; height = size - 1; width = size - 1; heights = new float[size * size]; biomes = new Biome[heights.Length]; vertexArray = new Vertex[heights.Length]; indexArray = new int[width * height * 6]; for (int xx = 0; xx < size; xx++) { for (int yy = 0; yy < size; yy++) { float _x = yy + ((int)x * (size - 1)); float _y = xx + ((int)z * (size - 1)); float biomeHeight = 1f, temperature = 1f; int heightIndex = HeightIndexAt(yy, xx); float terrainHeight; if (this.generateBiomes) { biomeHeight = (float)(parent.getSimplexNoise(((double)_y*0.6), ((double)_x*0.6))); temperature = (float)(parent.getSimplexNoise(((double)_y), ((double)_x))); terrainHeight = (float)parent.getSimplexNoise(_x, _y); float mountainHeight = (float)parent.getNoise(_x*0.05f, _y*0.05f) * parent.GetWorleyNoise(_x*0.008f, _y*0.008f); Biome biome = new Biome(); biome.AverageTemperature = temperature; if (biomeHeight < 0.3f) { biome.Topography = Biome.BiomeTopography.Plains; } else { biome.Topography = Biome.BiomeTopography.Hills; } biomeHeight = (float)System.Math.Abs(biomeHeight) * 2; biomeHeight *= biomeHeight; biomes[heightIndex] = biome; heights[heightIndex] = MathUtil.Lerp(terrainHeight * 10, mountainHeight * 150f, MathUtil.Clamp(biomeHeight, 0.0f, 1.0f)); } else { terrainHeight = (float)parent.getNoise(_x, _y); heights[heightIndex] = terrainHeight * 25f; } } } return heights; }
private void LoopThrough(List<int> faces, ref List<Vertex> outVerts) { for (int i = 0; i < faces.Count; i++) { Vertex v = new Vertex(positions[faces[i]], texCoords.Count > 0 ? texCoords[faces[i]] : null, normals.Count > 0 ? normals[faces[i]] : null); if (boneAssigns.ContainsKey(faces[i])) { BoneAssign[] vertBoneAssigns = boneAssigns[faces[i]]; for (int j = 0; j < vertBoneAssigns.Length; j++) { if (vertBoneAssigns[j] != null) { v.AddBoneIndex(vertBoneAssigns[j].GetBoneIndex()); v.AddBoneWeight(vertBoneAssigns[j].GetBoneWeight()); } } } outVerts.Add(v); } }