using System;
using System.Collections.Generic;
using Core.BRG;
using Unity.Collections;
using Unity.Mathematics;
using UnityEngine;
using Random = UnityEngine.Random;
public class BRG_backgrond : MonoBehaviour
{
    public BRGSamples samples;
    // public Material mat;
    // public Mesh mesh;
    public bool m_castShadows;
    public int w;
    public int h;
    /// 
    /// BRG容器对象
    /// 
    private BRGRender _mBrgRenderBasic;
    private int m_itemCount;
    public bool isUpdate = true;
    /// 
    /// GPU项目大小(2个4x3矩阵加1个颜色,每个float4占16字节)
    /// 
    private const int kGpuItemSize = (3 * 2 + 1) * 16;
    public List m_gameObjectInfos = new List();
    private void Start()
    {
        m_itemCount = w * h;
        for (int i = 0; i < w; i++)
        {
            for (int j = 0; j < h; j++)
            {
                BGRGameObjectInfo info = new BGRGameObjectInfo();
                info.pos = new Vector3(i * 2, j , 0); // 增加间距,使物体更容易看到
                info.rot = Vector3.zero;
                info.scale = Vector3.one; // 调整尺寸
                m_gameObjectInfos.Add(info);
            }
        }
        _mBrgRenderBasic = new BRGRender();
        _mBrgRenderBasic.InitRender(samples, m_gameObjectInfos,512);
    }
    private void OnDestroy()
    {
        if (_mBrgRenderBasic != null)
        {
            _mBrgRenderBasic.Shutdown();
            _mBrgRenderBasic = null;
        }
    }
    private void Update()
    {
        UpdatePos();
    }
    private void UpdatePos()
    {
        if (!isUpdate)
        {
            return;
        }
        if (Input.GetMouseButtonDown(0))
        {
            BGRGameObjectInfo gameObjectInfo= new BGRGameObjectInfo();
            gameObjectInfo.pos = new Vector3(Random.Range(0f, 100f), Random.Range(0f, 100f), 0);
            _mBrgRenderBasic.AddBGRGameObjectInfo(gameObjectInfo);
        }
        if (Input.GetMouseButtonDown(1))
        {
            int index = Random.Range(0, _mBrgRenderBasic.m_gameObjectInfos.Count);
            _mBrgRenderBasic.RemoveBGRGameObjectInfo(index);
        }
        _mBrgRenderBasic.UpdatePos();
    }
}