﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;



[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class MakeEgg : MonoBehaviour
{

    public enum VFXTYPE

    {
        egg,
        confetti
    };

    public VFXTYPE vfxType = VFXTYPE.confetti;
    
    public Mesh mesh;

    public int u = 20;

    public int v = 20;

    private MeshFilter mf;

    private MeshRenderer mr;

    public Material mat;
    public 
    // Start is called before the first frame update
    void Start()
    {
        if (!mf) mf = GetComponent<MeshFilter>();
        if (!mr) mr = GetComponent<MeshRenderer>();
        if (!mesh) createMesh();
    }

    private void createMesh()
    {
        if (vfxType == VFXTYPE.egg)
        {
            createMeshEgg();
        }
        if (vfxType == VFXTYPE.confetti)
        {
            createMeshConfetti();
        }
    }
 
    private void createMeshConfetti()
    {
         // quads are not connected
        int num = u * v * 4;
        Vector3[] pos = new Vector3[num];
        Vector4[] uvs = new Vector4[num];
        Vector3[] normals = new Vector3[num];
        int[] indices = new int[num];
        Color[] colors = new Color[num];
        mesh = new Mesh();
        int quadIndex = 0;
        for (int iu = 0; iu < u; iu++)
        {
            for (int iv = 0; iv < v; iv++)
            {
                // this quad
                int ind = quadIndex * 4;
                pos[ind].x = 0;
                pos[ind].z = 0;
                pos[ind].y = (quadIndex) * 0.1f;

                // these get override by the vertex shader
                pos[ind+1].x = 1;
                pos[ind+1].z = 0;
                pos[ind+1].y = (quadIndex) * 0.1f;
                
                pos[ind+2].x = 1;
                pos[ind+2].z = 1;
                pos[ind+2].y = (quadIndex) * 0.1f;
                
                pos[ind+3].x = 0;
                pos[ind+3].z = 1;
                pos[ind+3].y = (quadIndex) * 0.1f;
                
                
                // shader overrides this
                Color32 randomColor = Random.ColorHSV();
                
                for (int c = 0; c < 4; c++)
                {
                    normals[ind + c] = Vector3.up;
                    
                    // the uv's are storing the original position basically
                    uvs[ind + c].x = pos[ind + c].x;
                    uvs[ind + c].y = pos[ind + c].y;
                    
                    // w stores quad id number
                    uvs[ind + c].w = quadIndex;
                    
                    // indices are easy since each quad is not connected
                    indices[ind+c] = ind+c;
                    
                    // solid but different color for each quad
                    colors[ind + c] = randomColor;
                    //colors[ind + c].a = 1; //quadIndex;
                }
                
                quadIndex++;
            }
        }
        mesh.SetVertices(pos.ToList());
        mesh.SetUVs(0,uvs.ToList());
        mesh.SetIndices(indices,MeshTopology.Quads,0);
        mesh.SetColors(colors);
        mf.sharedMesh = mesh;
      //  mr.sharedMaterial = default;
      // PrefabUtility.SaveAsPrefabAsset(transform.gameObject, "Eggulator/egg_mesh.asset");
    }

    private void createMeshEgg()
    {
        // quads are not connected
        int num = u * v * 4;
        Vector3[] pos = new Vector3[num];
        Vector4[] uvs = new Vector4[num];
        int[] indices = new int[num];
        Color[] colors = new Color[num];
        mesh = new Mesh();
        int quadIndex = 0;
        for (int iu = 0; iu < u; iu++)
        {
            for (int iv = 0; iv < v; iv++)
            {
                // this quad
                int ind = quadIndex * 4;
                pos[ind].x = iu;
                pos[ind].y = iv;
                
                // these get override by the vertex shader
                pos[ind+1].x = iu+1;
                pos[ind+1].y = iv;
                
                pos[ind+2].x = iu+1;
                pos[ind+2].y = iv+1;
                
                pos[ind+3].x = iu;
                pos[ind+3].y = iv+1;

                // shader overrides this
                Color randomColor = Random.ColorHSV();
                
                for (int c = 0; c < 4; c++)
                {
                    // the uv's are storing the original position basically
                    uvs[ind + c].x = pos[ind + c].x;
                    uvs[ind + c].y = pos[ind + c].y;
                    if (c == 0)
                    {
                        uvs[ind + c].z = 0.1f;
                        uvs[ind + c].w = 0.1f;
                    } else if (c == 1)
                    {
                        uvs[ind + c].z = -0.1f;
                        uvs[ind + c].w = 0.1f;
                    }
                    else if (c == 2)
                    {
                        uvs[ind + c].z = -0.1f;
                        uvs[ind + c].w = -0.1f;
                    }
                    else if (c == 3)
                    {
                        uvs[ind + c].z = 0.1f;
                        uvs[ind + c].w = -0.1f;
                    }
                    // indices are easy since each quad is not connected
                    indices[ind+c] = ind+c;
                    
                    // solid but different color for each quad
                    colors[ind + c] = randomColor;
                }
                quadIndex++;
            }
        }
        mesh.SetVertices(pos.ToList());
        mesh.SetUVs(0,uvs.ToList());
        mesh.SetIndices(indices,MeshTopology.Quads,0);
        mesh.SetColors(colors);
        mf.sharedMesh = mesh;
      //  mr.sharedMaterial = default;
      // PrefabUtility.SaveAsPrefabAsset(transform.gameObject, "Eggulator/egg_mesh.asset");
    }
    // Update is called once per frame
    void Update()
    {
        
    }
}
