Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Chunk : ITickable
- {
- public readonly int ChunkWidth = 20;
- public readonly int ChunkHeight = 125;
- public float _perlin;
- public Cube[,,] _Cubes;
- private int PosX;
- private int Posz;
- public Chunk(int px, int pz)
- {
- PosX = px;
- Posz = pz;
- }
- protected bool HasGenerated = false;
- public float GetHeight(float px, float pz, float py)
- {
- px += (PosX * ChunkWidth);
- pz += (Posz * ChunkWidth);
- float p1 = Mathf.PerlinNoise(px / GameManager.Sdx, pz / GameManager.Sdz) * GameManager.Smul;
- p1 *= (GameManager.Smy * py);
- return p1;
- }
- public void Start()
- {
- _Cubes = new Cube[ChunkWidth, ChunkHeight, ChunkWidth];
- for (int x = 0; x < ChunkWidth; x++)
- {
- for (int y = 0; y < ChunkHeight; y++)
- {
- System.Random m_rnd = new System.Random();
- for (int z = 0; z < ChunkWidth; z++)
- {
- float perlin = GetHeight(x, z, y);
- _perlin = perlin;
- if (perlin > GameManager.Scutoff)
- {
- _Cubes[x, y, z] = Cube.Air;
- }
- else
- {
- if (y > 100)
- { // 10% chance of a cloud.
- _Cubes[x, y, z]
- = m_rnd.NextDouble() > 0.9
- ? Cube.Cloud
- : Cube.Air;
- }
- if (perlin > GameManager.Scutoff/ 1.1f)
- {
- _Cubes[x, y, z] = Cube.Grass;
- }
- else
- {
- _Cubes[x, y, z] = Cube.Stone;
- }
- }
- }
- }
- }
- HasGenerated = true;
- }
- public void Tick()
- {
- }
- protected bool HasDrawn = false;
- private MeshData data;
- protected bool DrawnLock = false;
- public void Update()
- {
- if (!HasDrawn && HasGenerated && !DrawnLock)
- {
- DrawnLock = true;
- data = new MeshData();
- for (int x = 0; x < ChunkWidth; x++)
- {
- for (int y = 0; y < ChunkHeight; y++)
- {
- for (int z = 0; z < ChunkWidth; z++)
- {
- data.Merge(_Cubes[x, y, z].Draw(this, _Cubes, x, y, z));
- }
- }
- }
- DrawnLock = false;
- HasDrawn = true;
- }
- }
- protected bool HasRendered = false;
- private GameObject go;
- public virtual void OnUnityUpdate()
- {
- if (HasGenerated && !HasRendered && HasDrawn)
- {
- HasRendered = true;
- Mesh mesh = data.ToMesh();
- if (go == null)
- {
- go = new GameObject();
- }
- Transform t = go.transform;
- if (t.gameObject.GetComponent<MeshFilter>() == null)
- {
- t.gameObject.AddComponent<MeshFilter>();
- t.gameObject.AddComponent<MeshRenderer>();
- t.gameObject.AddComponent<MeshCollider>();
- t.gameObject.GetComponent<MeshRenderer>().material = Resources.Load<Material>("ChunkMaterial");
- t.transform.position = new Vector3(PosX * ChunkWidth / 2, 0, Posz * ChunkWidth / 2);
- t.gameObject.layer = LayerMask.NameToLayer("Ground");
- Texture2D tmp = new Texture2D(0, 0);
- tmp.LoadImage(System.IO.File.ReadAllBytes("atlas.png"));
- tmp.filterMode = FilterMode.Point;
- t.gameObject.GetComponent<MeshRenderer>().material.mainTexture = tmp;
- }
- t.transform.GetComponent<MeshFilter>().sharedMesh = mesh;
- t.transform.GetComponent<MeshCollider>().sharedMesh = mesh;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement