using System.Collections; using System.Collections.Generic; using UnityEngine;

RequireComponent(typeof(MeshFilter))

public class LowPoly : MonoBehaviour {

static Mesh mesh;
static Vector3[] vertici;
static int[] triangle;
public MeshFilter filter;
static public GameObject dupLowPoly, duplicate, duplicate1, duplicate2, duplicate3;
static Vector3 sDP, pDP;

// Start is called before the first frame update
static internal void CostruisciData(Vector3[] vert)
{
    vertici = new Vector3[]{ vert[0], vert[1], vert[2]};
    triangle = new int[]{ 0,1,2 };
}

static internal void CreaMesh(GameObject dup)
{
    mesh = dup.GetComponent<MeshFilter>().mesh;
    mesh.Clear();
    mesh.vertices = vertici;
    mesh.triangles = triangle;
}

static int i = 0;
static internal void Duplica()
{
    i++;
    if (i <= 3)
    {
      dupLowPoly = GameObject.Find("LowPoly");
      duplicate = Instantiate(dupLowPoly);
      duplicate.name = $"duplicate {i}";
    }
    Debug.LogFormat($"ok {duplicate.name}");
    return;
}

void Awake()
{
    mesh = GetComponent<MeshFilter>().mesh;
}

void Start()
{
    Vector3 x, y, z;
    GameObject cameretta = GameObject.Find("Cameretta");
    duplicate = GameObject.Find("LowPoly");
    x = new Vector3(0, 0, 0);
    y = new Vector3(0, 0, 1);
    z = new Vector3(2, 0, 0);

    CostruisciData(new Vector3[]{x, y, z});
    CreaMesh(duplicate);
    Duplica();

    pDP = duplicate.transform.position = new Vector3(0, (cameretta.transform.localScale.y / 2), 0);
    sDP = duplicate.transform.localScale = new Vector3(1, 1, 1);

    duplicate1 = GameObject.Find("duplicate 1");
    duplicate1.transform.parent = duplicate.transform;

    x = new Vector3(0, 0, 0);   // vertice sinistro 
    y = new Vector3(0, 3, 0);   // vertice su
    z = new Vector3(4, 3, 0);   // vertice destro

    CostruisciData(new Vector3[]{x, y, z});
    CreaMesh(duplicate1);

    pDP = duplicate1.transform.position = new Vector3(1, (cameretta.transform.localScale.y / 2) + 300, 0);
    sDP = duplicate1.transform.localScale = new Vector3(10, 10, 10);

    duplicate2 = GameObject.Find("duplicate 2");
    duplicate2.transform.parent = duplicate.transform;

    x = new Vector3(0, 0, 0);
    y = new Vector3(4, 3, 0);
    z = new Vector3(4, 0, 0);

    CostruisciData(new Vector3[]{x, y, z});
    CreaMesh(duplicate2);

    pDP = duplicate2.transform.position = new Vector3(1, (cameretta.transform.localScale.y / 2) + 300, 0);
    sDP = duplicate2.transform.localScale = new Vector3(10, 10, 10);

    duplicate3 = GameObject.Find("duplicate 3");
    duplicate3.transform.parent = duplicate.transform;

    x = new Vector3(4, 0, 0);
    y = new Vector3(4, 3, 0);
    z = new Vector3(6, 0, 0.5f);

    CostruisciData(new Vector3[]{x, y, z});
    CreaMesh(duplicate3);

    pDP = duplicate3.transform.position = new Vector3(1, (cameretta.transform.localScale.y / 2) + 300, 0);
    sDP = duplicate3.transform.localScale = new Vector3(10, 10, 10);
}

// Update is called once per frame
void Update()
{

}

}