Last active
February 16, 2016 18:51
-
-
Save gluschenko/5e60c03453898cd26545 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From GeoEngine scripts | |
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
[RequireComponent(typeof(MeshFilter))] | |
[RequireComponent(typeof(MeshCollider))] | |
public class Landscape : MonoBehaviour { | |
public int Scale = 10; | |
public Mesh ChunkMesh; | |
public MeshCollider ChunkCollider; | |
protected List<Vector3> Verts = new List<Vector3>(); | |
protected List<int> Tris = new List<int>(); | |
protected List<Vector2> UV = new List<Vector2>(); | |
float Timer = 0; | |
float OffsetTimer = 0; | |
void Start () | |
{ | |
Init(); | |
GenerateMesh(); | |
} | |
void Update () | |
{ | |
Timer += Time.deltaTime; | |
OffsetTimer += Time.deltaTime * 1f; | |
if (Timer > 0.02f) | |
{ | |
StartCoroutine(GEN()); | |
Timer = 0; | |
} | |
} | |
void Init() | |
{ | |
ChunkMesh = new Mesh(); | |
GetComponent<MeshFilter>().mesh = ChunkMesh; | |
ChunkCollider = GetComponent<MeshCollider>(); | |
// | |
this.transform.localScale = new Vector3(Scale, 1, Scale); | |
} | |
IEnumerator GEN() | |
{ | |
GenerateMesh(); | |
yield return null; | |
} | |
void GenerateMesh() | |
{ | |
Verts.Clear(); | |
Tris.Clear(); | |
UV.Clear(); | |
// | |
int width = 15; | |
GameObject S = GameObject.Find("Sphere"); | |
for (int x = -width; x < width; x++) | |
{ | |
for (int z = -width; z < width; z++) | |
{ | |
float first = Mathf.Sin((x + z) + OffsetTimer) * Scale; | |
float second = Mathf.Sin(((x + 1) + z) + OffsetTimer) * Scale; | |
float third = Mathf.Sin((x + (z + 1)) + OffsetTimer) * Scale; | |
float fourth = Mathf.Sin(((x + 1) + (z + 1)) + OffsetTimer) * Scale; | |
/*first *= -Mathf.Pow(100 / Vector3.Distance(S.transform.position, new Vector3(Scale * x, 0, Scale * z)), 3) * 10; | |
second *= -Mathf.Pow(100 / Vector3.Distance(S.transform.position, new Vector3(Scale*x, 0, Scale* z + Scale)), 3) * 10; | |
third *= -Mathf.Pow(100 / Vector3.Distance(S.transform.position, new Vector3(Scale* x + Scale, 0, Scale*z)), 3) * 10; | |
fourth *= -Mathf.Pow(100 / Vector3.Distance(S.transform.position, new Vector3(Scale*x + Scale, 0, Scale*z + Scale)), 3) * 10;*/ | |
/*int PosX = x * Scale; | |
int PosZ = (z - 1) * Scale; | |
float first = World.GenerateHeight(PosX, PosZ) * World.WorldHeight; | |
float second = World.GenerateHeight(PosX, PosZ + Scale) * World.WorldHeight; | |
float third = World.GenerateHeight(PosX + Scale, PosZ) * World.WorldHeight; | |
float fourth = World.GenerateHeight(PosX + Scale, PosZ + Scale) * World.WorldHeight;*/ | |
DrawFace(new Vector3(x, 0, z), first, second, third, fourth, 2); | |
} | |
} | |
ChunkMesh.Clear(); | |
ChunkMesh.vertices = Verts.ToArray(); | |
ChunkMesh.triangles = Tris.ToArray(); | |
ChunkMesh.uv = UV.ToArray(); | |
Verts.Clear(); | |
Tris.Clear(); | |
UV.Clear(); | |
ChunkMesh.RecalculateNormals(); | |
ChunkCollider.sharedMesh = null; | |
ChunkCollider.sharedMesh = ChunkMesh; | |
} | |
void DrawFace(Vector3 start, float first, float second, float third, float fourth, short block) | |
{ | |
int index = Verts.Count; | |
Verts.AddRange(new Vector3[] | |
{ | |
start + new Vector3(0, first, -1), | |
start + new Vector3(0, second, 0), | |
start + new Vector3(1, third, -1), | |
start + new Vector3(1, fourth, 0), | |
}); | |
Tris.AddRange(new int[] | |
{ | |
index + 0, | |
index + 1, | |
index + 2, | |
index + 3, | |
index + 2, | |
index + 1, | |
}); | |
Vector2 UVBase = GetUVStart(block); | |
AddUVQuad(UVBase); | |
} | |
public Vector2 GetUVStart(short block) | |
{ | |
int count = 0; | |
for (int row = 0; row < 8; row++) | |
{ | |
for (int x = 1; x <= 8; x++) | |
{ | |
count++; | |
if (count == block) return new Vector2((1f / 8f) * x, -(1f / 8f) * row); | |
} | |
} | |
return Vector2.zero; | |
} | |
public void AddUVQuad(Vector2 UVBase) | |
{ | |
float a = -(3f / 2048f); | |
float b = -(250f / 2048f); | |
UV.AddRange(new Vector2[] { | |
UVBase + new Vector2(a, a), | |
UVBase + new Vector2(b, a), | |
UVBase + new Vector2(a, b), | |
UVBase + new Vector2(b, b) | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment