Chunk.cs

mail@pastecode.io avatar
unknown
csharp
a month ago
4.3 kB
2
Indexable
Never
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Reflection;
using UnityEngine;
// using UnityEngine.Rendering.HighDefinition;

public class Chunk
{
    World world;

    public GameObject chunkObject;

    public Vector2Int coord;

    MeshRenderer meshRenderer;

    MeshFilter meshFilter;

    MeshCollider meshCollider;


    // GameObject waterExcluderObject;
    // GameObject waterExcluderRender;
    // WaterExcluder waterExcluder;


    bool generate = false;

    public Chunk(World world, Vector2Int coord)
    {
        this.world = world;
        this.coord = coord;
        world.StartCoroutine(Init());
    }

    IEnumerator Init()
    {
        Vector3Int pos = new Vector3Int(coord.x, 0, coord.y);
        this.chunkObject = new GameObject("Chunk", new System.Type[]{
            typeof(MeshRenderer),
            typeof(MeshFilter),
            typeof(MeshCollider)
        });
        chunkObject.transform.position = new Vector3(coord.x * 16, 0f, coord.y * 16);
        chunkObject.name = $"Chunk {coord.x} {coord.y}";
        chunkObject.transform.SetParent(world.gameObject.transform);

        WorldData worldData = world.GetWorldDataFromVector3(pos);
        int[,,] dataToApply = worldData != null ? worldData.data : null;
        Mesh meshToUse = null;

        if (dataToApply == null)
        {
            world.dataCreator.QueueDataToGenerate(new DataGenerator.GenData
            {
                GenerationPoint = pos,
                OnComplete = x => dataToApply = x
            });

            yield return new WaitUntil(() => dataToApply != null);
        }

        world.meshCreator.QueueDataToDraw(new ChunkMeshCreator.CreateMesh
        {
            DataToDraw = dataToApply,
            OnComplete = x => meshToUse = x
        });

        yield return new WaitUntil(() => meshToUse != null);

        this.meshRenderer = chunkObject.GetComponent<MeshRenderer>();
        this.meshFilter = chunkObject.GetComponent<MeshFilter>();
        this.meshCollider = chunkObject.GetComponent<MeshCollider>();

        meshFilter.mesh = meshToUse;
        meshRenderer.material = world.ChunkMaterial;
        meshCollider.sharedMesh = meshFilter.mesh;

        // UpdateWaterExcluder();

        generate = true;
    }

    public bool generated
    {
        get { return generate; }
    }

    public bool IsActive()
    {
        return chunkObject != null && chunkObject.activeSelf;
    }

    public void SetActive(bool value)
    {
        if (chunkObject != null)
            chunkObject.SetActive(value);
    }

    public void Update()
    {
        // UpdateWaterExcluder();
    }

    // void UpdateWaterExcluder()
    // {
    //     if (waterExcluderObject == null)
    //     {
    //         waterExcluderObject = new GameObject("Water Excluder");
    //         waterExcluderObject.transform.position = chunkObject.transform.position;
    //         waterExcluderObject.transform.SetParent(chunkObject.transform);
    //         waterExcluder = waterExcluderObject.AddComponent<WaterExcluder>();

    //         waterExcluderRender = new GameObject("Water Excluder Render", new System.Type[]{
    //             typeof(MeshRenderer),
    //             typeof(MeshFilter)
    //         });

    //         MeshRenderer waterRender = waterExcluderRender.GetComponent<MeshRenderer>();
    //         MeshFilter waterFilter = waterExcluderRender.GetComponent<MeshFilter>();
    //         waterRender.material = world.waterExclusionMaterial;

    //         waterExcluderRender.transform.position = chunkObject.transform.position;
    //         waterExcluderRender.transform.SetParent(waterExcluderObject.transform);

    //         var type = typeof(WaterExcluder);
    //         FieldInfo field = type.GetField("m_ExclusionRenderer", BindingFlags.NonPublic | BindingFlags.Instance);
    //         field.SetValue(waterExcluder, waterExcluderRender);
    //     }
    //     if (waterExcluderRender.TryGetComponent(out MeshFilter filter))
    //     {
    //         filter.mesh = meshFilter.mesh;
    //     }

    //     waterExcluder.SetExclusionMesh(meshFilter.mesh);
    // }



}

Leave a Comment