using UnityEngine;
using System.Collections;
using ProceduralToolkit;
using UnityEngine.UI;
using System.Collections.Generic;
using VRTK;
public class Argos_Hex_Brush : MonoBehaviour
{
Mesh mesh;
[SerializeField]
Material _material;
public VRTK_ControllerEvents ctrl_events;
public AnimationCurve scaleCurve; //let's say you edit from inspector, but you can built at runtime if you prefer
MaterialPropertyBlock _block;
public GameObject newHex_Loc_Prefab;
private ArgosMeshDraft amDraft;
public ArgosMeshDraft AmDraft
{
get { return amDraft; }
}
public ArgosSphere_Indexing argosSphere;
private int lastHexIdx = -1;
public Color color;
public Text dbgTex;
private UI_Control ui_Control;
List<Vector3> CatmullRom_Point_List = new List<Vector3>();
List<Vector3> Equi_Distant_Points = new List<Vector3>();
private Vector3 vLast_Point;
private Vector3 vCursPos;
private Vector3 vUp_Last;
void Start ()
{
MeshFilter mf = GetComponent<MeshFilter>();
if (mf.mesh == null)
mf.mesh = new Mesh();
mesh = mf.mesh;
amDraft = new ArgosMeshDraft();
_block = new MaterialPropertyBlock();
amDraft.initQuadPool_AnimationCurve(5000, scaleCurve);
ui_Control = GameObject.Find("UI_Control_Canvas").GetComponent < UI_Control>();
ctrl_events.TriggerPressed += new ControllerInteractionEventHandler(Triggr_StartCollectingPoints);
ctrl_events.TriggerReleased += new ControllerInteractionEventHandler(Triggr_StopCollectingPoints);
}
public void SetCursPos(Vector3 vPos)
{
vCursPos = vPos;
}
public void Triggr_StartCollectingPoints(object sender, ControllerInteractionEventArgs e)
{
vLast_Point = vCursPos;
CatmullRom_Point_List.Clear();
CatmullRom_Point_List.Add(vCursPos);
}
public void Triggr_StopCollectingPoints(object sender, ControllerInteractionEventArgs e)
{
}
float accumTime = 0f;
void Update ()
{
accumTime += Time.deltaTime;
amDraft.QuadListUpdate();
GetComponent<MeshFilter>().mesh = amDraft.ToMeshInternal();//
//Vector3 vH0 = a12horns.v12HORN_Norms[0];
Quaternion q;
//for (int i = 0; i < 12; i++)
//{
//q = Quaternion.FromToRotation(vH0, a12horns.v12HORN_Norms[i]);
q = Quaternion.identity;
//print(q.ToString());
Graphics.DrawMesh(GetComponent<MeshFilter>().mesh, Vector3.zero, q, _material, gameObject.layer, null, 0, _block);
//}
}
public int GetNumVertices()
{
return amDraft.vertices.Count;
}
public Vector3 GetVertex(int i)
{
return amDraft.vertices[i];
}
public void SetVertex(int i, Vector3 v)
{
amDraft.vertices[i] = v;
}
public void MeshDraft_ToMesh()
{
if (amDraft != null)
{
GetComponent<MeshFilter>().mesh = amDraft.ToMesh();
}
}
public void initAddMeshDraft(MeshDraft md)
{
MeshFilter mf = GetComponent<MeshFilter>();
if (mf.mesh == null)
mf.mesh = new Mesh();
mesh = mf.mesh;
amDraft = new ArgosMeshDraft();
if (amDraft != null)
{
amDraft.Add(md);
GetComponent<MeshFilter>().mesh = amDraft.ToMesh();
}
}
public void Paint(Vector3 v0, Vector3 v1, Vector3 v2, Vector3 v3, Color col, float duration, float fade_start)
{
if (amDraft != null)
{
amDraft.Quad_Paint(v0, v1, v2, v3, col, duration, fade_start);
}
}
public void PaintHex(Vector3 pos, Vector3 vOffset, float lineWidth)
{
float pointDist = lineWidth * 7f;
Vector3 vDiff = pos - vLast_Point;
if (vDiff.magnitude > lineWidth)
{
CatmullAdd(pos + vOffset,lineWidth);
vLast_Point = pos;
}
int newIdx = argosSphere.getHexIdx(pos);
dbgTex.text = "CM DIST = " + pointDist.ToString("F3");
if(newIdx != lastHexIdx && newIdx != -1)
{
List<Vector3> vL = argosSphere.HexQuads[newIdx].mdH.vertices;
Paint(vL[0] + vOffset, vL[1] + vOffset, vL[3] + vOffset, vL[2] + vOffset, color, 0.1f, 0.7f);
lastHexIdx = newIdx;
}
}
public void CatmullAdd(Vector3 pos,float lineWidth)
{
Vector3 vUp;
Vector3 vc12;
Vector3 vcIn; //break by 8 for music
CatmullRom_Point_List.Add(pos);
Instantiate(newHex_Loc_Prefab, pos, Quaternion.identity);
if(CatmullRom_Point_List.Count > 3)
{
Vector3[] splinePoints = new Vector3[8];
int cmc = CatmullRom_Point_List.Count - 1;
for (int i = 0; i<8; i++)
{
splinePoints[i] = ReturnCatmullRom((float)i / 8f,
CatmullRom_Point_List[cmc - 3],
CatmullRom_Point_List[cmc - 2],
CatmullRom_Point_List[cmc - 1],
CatmullRom_Point_List[cmc]);
Equi_Distant_Points.Add(splinePoints[i]);//add points for serialized equidistant points
}
if (CatmullRom_Point_List.Count == 4)
{
vc12 = CatmullRom_Point_List[cmc - 1] - CatmullRom_Point_List[cmc - 2];
vcIn = CatmullRom_Point_List[cmc - 2] - argosSphere.transform.position;
vc12.Normalize();
vcIn.Normalize();
vUp_Last = Vector3.Cross(vcIn, vc12);
vUp_Last.Normalize();
}
Vector3[] verts = new Vector3[4];
for (int j = 0; j<7;j++)
{
Vector3 v12 = splinePoints[j + 1] - splinePoints[j];
Vector3 vIn = splinePoints[j + 1] - argosSphere.transform.position;
vIn.Normalize();
v12.Normalize();
vUp = Vector3.Cross(vIn, v12);
vUp.Normalize();
verts[0] = splinePoints[j] - vUp_Last * lineWidth / 2f;
verts[1] = splinePoints[j] + vUp_Last * lineWidth / 2f;
verts[2] = splinePoints[j+1] + vUp * lineWidth / 2f;
verts[3] = splinePoints[j+1] - vUp * lineWidth / 2f;
Paint(verts[0], verts[1], verts[2], verts[3], color, 30f, 0.7f);
vUp_Last = vUp;
}
vc12 = CatmullRom_Point_List[cmc-1] - splinePoints[6];
vcIn = CatmullRom_Point_List[cmc - 1] - argosSphere.transform.position;
vc12.Normalize();
vcIn.Normalize();
vUp = Vector3.Cross(vcIn, vc12);
vUp.Normalize();
verts[0] = splinePoints[6] - vUp_Last * lineWidth / 2f;
verts[1] = splinePoints[6] + vUp_Last * lineWidth / 2f;
verts[2] = CatmullRom_Point_List[cmc - 1] + vUp * lineWidth / 2f;
verts[3] = CatmullRom_Point_List[cmc - 1] - vUp * lineWidth / 2f;
Paint(verts[0], verts[1], verts[2], verts[3], color, 30f, 0.7f);
vUp_Last = vUp;
}
}
//Returns a position between 4 Vector3 with Catmull-Rom Spline algorithm
//http://www.iquilezles.org/www/articles/minispline/minispline.htm
Vector3 ReturnCatmullRom(float t, Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3)
{
Vector3 a = 0.5f * (2f * p1);
Vector3 b = 0.5f * (p2 - p0);
Vector3 c = 0.5f * (2f * p0 - 5f * p1 + 4f * p2 - p3);
Vector3 d = 0.5f * (-p0 + 3f * p1 - 3f * p2 + p3);
Vector3 pos = a + (b * t) + (c * t * t) + (d * t * t * t);
return pos;
}
public void AddMeshDraft(MeshDraft md)
{
if (amDraft != null)
{
amDraft.Add(md);
GetComponent<MeshFilter>().mesh = amDraft.ToMesh();
}
}
public void AddMeshDraft_Only(MeshDraft md)
{
if (amDraft != null)
{
amDraft.Add(md);
}
}
public void AddQuad(Vector3 v0, Vector3 v1, Vector3 v2, Vector3 v3)
{
if (amDraft != null)
{
amDraft.Add(MeshDraft.Quad(v0, v1, v2, v3));
GetComponent<MeshFilter>().mesh = amDraft.ToMesh();
}
}
public void AddQuad(Vector3 v0, Vector3 v1, Vector3 v2, Vector3 v3, Color col)
{
if (amDraft != null)
{
amDraft.Add(MeshDraft.Quad(v0, v1, v2, v3, col));
GetComponent<MeshFilter>().mesh = amDraft.ToMesh();
}
}
}






