You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
309 lines
8.5 KiB
309 lines
8.5 KiB
3 years ago
|
using UnityEngine;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using Vectrosity;
|
||
|
using UnityEngine.EventSystems;
|
||
|
using UnityEngine.UI;
|
||
|
|
||
|
public class DrawLinePlate : MonoBehaviour
|
||
|
{
|
||
|
|
||
|
public int segments = 250;
|
||
|
public bool loop = false;
|
||
|
public List<Vector2> linePoints;
|
||
|
|
||
|
public VectorLine Penline;
|
||
|
public VectorLine Rubber;
|
||
|
|
||
|
public Material RubberMaterial;
|
||
|
|
||
|
public Material PenlineMaterial;
|
||
|
|
||
|
public Material RedMaterial;
|
||
|
|
||
|
public Material GreenMaterial;
|
||
|
|
||
|
public Material DlepBlueMaterial;
|
||
|
|
||
|
public Material BlackMaterial;
|
||
|
|
||
|
public Material BlueMaterial;
|
||
|
|
||
|
public Material YellowMaterial;
|
||
|
|
||
|
public int PenlineWith = 1;
|
||
|
|
||
|
public int RubberWith = 10;
|
||
|
|
||
|
public List<GameObject> Record_;
|
||
|
|
||
|
private int TranWidth;
|
||
|
private int TranHeight;
|
||
|
|
||
|
public bool CanDrawing = false;
|
||
|
|
||
|
// Use this for initialization
|
||
|
void Start()
|
||
|
{
|
||
|
Father = GameObject.Find("Canvas").transform.Find("DrawFather").gameObject;
|
||
|
CreatLine(); CreatRubber();
|
||
|
TranWidth = Screen.width;
|
||
|
TranHeight = Screen.height;
|
||
|
}
|
||
|
void Awake()
|
||
|
{
|
||
|
if (instance == null)
|
||
|
{
|
||
|
instance = this;
|
||
|
}
|
||
|
this.gameObject.SetActive(false);
|
||
|
}
|
||
|
public static DrawLinePlate instance;
|
||
|
|
||
|
public void HiStart()
|
||
|
{
|
||
|
Father = GameObject.Find("Canvas").transform.Find("DrawFather").gameObject;
|
||
|
CreatLine(); CreatRubber();
|
||
|
}
|
||
|
public int i = 0;
|
||
|
public static bool Tool;
|
||
|
public Texture MouseTexture_huabi;
|
||
|
|
||
|
public Texture MouseTexture_xiangpi;
|
||
|
|
||
|
Texture MouseTxture;
|
||
|
|
||
|
bool has = false;
|
||
|
|
||
|
void OnGUI()
|
||
|
{
|
||
|
if (Mathf.Abs(Screen.width - TranWidth) > 1 || Mathf.Abs(Screen.height - TranHeight) > 1)
|
||
|
{
|
||
|
TranWidth = Screen.width;
|
||
|
TranHeight = Screen.height;
|
||
|
has = true;
|
||
|
if (GameObject.Find("VectorCam"))
|
||
|
{
|
||
|
GameObject ob = GameObject.Find("VectorCam").gameObject;
|
||
|
Destroy(ob);
|
||
|
has = false;
|
||
|
List<GameObject> list = new List<GameObject>();
|
||
|
foreach (Transform child in Father.transform)
|
||
|
{
|
||
|
Destroy(child.gameObject);
|
||
|
Record_.Remove(child.gameObject);
|
||
|
}
|
||
|
//for (int i = 1; i < list.Count; i++)
|
||
|
//{
|
||
|
// GameObject game = list[i] as GameObject;
|
||
|
// Destroy(game);
|
||
|
// Record_.Remove(game);
|
||
|
//}
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
if (Tool)
|
||
|
{
|
||
|
Vector3 mousePos = Input.mousePosition;
|
||
|
|
||
|
if (EventSystem.current.IsPointerOverGameObject())
|
||
|
{
|
||
|
Cursor.visible = true;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
Cursor.visible = false;
|
||
|
GUI.DrawTexture(new Rect(mousePos.x, Screen.height - mousePos.y, MouseTxture.width, MouseTxture.height), MouseTxture);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
public void GetMaterialColor(int num)
|
||
|
{
|
||
|
switch (num)
|
||
|
{
|
||
|
case 1: PenlineMaterial = BlackMaterial; break;
|
||
|
case 2: PenlineMaterial = BlueMaterial; break;
|
||
|
case 3: PenlineMaterial = DlepBlueMaterial; break;
|
||
|
case 4: PenlineMaterial = RedMaterial; break;
|
||
|
case 5: PenlineMaterial = GreenMaterial; break;
|
||
|
case 6: PenlineMaterial = YellowMaterial; break;
|
||
|
case 7: MouseTxture = MouseTexture_huabi; Tool = true; break;
|
||
|
case 8: MouseTxture = MouseTexture_xiangpi; Tool = true; break;
|
||
|
case 9: MouseTxture=null; Tool = false; break;
|
||
|
}
|
||
|
}
|
||
|
public static bool thekong = true;
|
||
|
Vector3 mousepoint;
|
||
|
void Update()
|
||
|
{
|
||
|
if (EventSystem.current.IsPointerOverGameObject())
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
if (Input.GetMouseButton(0))
|
||
|
{
|
||
|
if (MouseTxture!=null && MouseTxture.name == "eraser")
|
||
|
{
|
||
|
if (mousepoint == Input.mousePosition) return;
|
||
|
mousepoint = Input.mousePosition;
|
||
|
linePoints.Add(Input.mousePosition);
|
||
|
RubberDraw();
|
||
|
}
|
||
|
}
|
||
|
if (Input.GetMouseButton(0))
|
||
|
{
|
||
|
if (MouseTxture != null && MouseTxture.name == "brus")
|
||
|
{
|
||
|
|
||
|
if (mousepoint == Input.mousePosition) return;
|
||
|
|
||
|
mousepoint = Input.mousePosition;
|
||
|
linePoints.Add(Input.mousePosition);
|
||
|
LineDraw();
|
||
|
|
||
|
}
|
||
|
}
|
||
|
if (Input.GetMouseButtonUp(0))
|
||
|
{
|
||
|
if (MouseTxture != null && MouseTxture.name == "brus")
|
||
|
{
|
||
|
CreatLine();
|
||
|
}
|
||
|
}
|
||
|
if (Input.GetMouseButtonUp(0))
|
||
|
{
|
||
|
if (MouseTxture != null && MouseTxture.name == "eraser")
|
||
|
{
|
||
|
CreatRubber();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
void CreatRubber()
|
||
|
{
|
||
|
VectorLine.vectorLayer = 7;
|
||
|
i++;
|
||
|
Rubber = new VectorLine("Rubber", new Vector2[segments + 1], RubberMaterial, RubberWith, LineType.Continuous, Joins.Weld);
|
||
|
Rubber.vectorObject.transform.parent = Father.transform;
|
||
|
linePoints.Clear();
|
||
|
Record_.Add(Rubber.vectorObject);
|
||
|
}
|
||
|
void RubberDraw()
|
||
|
{
|
||
|
|
||
|
Rubber.depth = i;
|
||
|
if (linePoints.Count < 3) return;
|
||
|
Rubber.MakeSpline(linePoints.ToArray(), segments, loop);
|
||
|
Rubber.Draw();
|
||
|
|
||
|
|
||
|
}
|
||
|
//public Mesh Mesh_;
|
||
|
|
||
|
private GameObject Father;
|
||
|
|
||
|
public Slider slidervalue;
|
||
|
void CreatLine()
|
||
|
{
|
||
|
VectorLine.vectorLayer = 7;
|
||
|
i++;
|
||
|
Penline = new VectorLine("Spline", new Vector2[segments + 1], PenlineMaterial, slidervalue.value*10f, LineType.Continuous, Joins.Weld);
|
||
|
Record_.Add(Penline.vectorObject);
|
||
|
Penline.vectorObject.transform.parent = Father.transform;
|
||
|
linePoints.Clear();
|
||
|
|
||
|
|
||
|
}
|
||
|
public void Clear_()
|
||
|
{
|
||
|
linePoints.Clear();
|
||
|
}
|
||
|
void LineDraw()
|
||
|
{
|
||
|
|
||
|
Penline.depth = i;
|
||
|
|
||
|
|
||
|
if (linePoints.Count < 3) return;
|
||
|
Penline.MakeSpline(linePoints.ToArray(), segments, loop);
|
||
|
Penline.material = PenlineMaterial;
|
||
|
|
||
|
Penline.lineWidth = slidervalue.value*10;
|
||
|
Penline.Draw();
|
||
|
|
||
|
}
|
||
|
private void TangentSolver(Mesh theMesh)
|
||
|
{
|
||
|
return;
|
||
|
int vertexCount = theMesh.vertexCount;
|
||
|
Vector3[] vertices = theMesh.vertices;
|
||
|
Vector3[] normals = theMesh.normals;
|
||
|
Vector2[] texcoords = theMesh.uv;
|
||
|
int[] triangles = theMesh.triangles;
|
||
|
int triangleCount = triangles.Length / 3;
|
||
|
Vector4[] tangents = new Vector4[vertexCount];
|
||
|
Vector3[] tan1 = new Vector3[vertexCount];
|
||
|
Vector3[] tan2 = new Vector3[vertexCount];
|
||
|
int tri = 0;
|
||
|
for (int i = 0; i < (triangleCount); i++)
|
||
|
{
|
||
|
int i1 = triangles[tri];
|
||
|
int i2 = triangles[tri + 1];
|
||
|
int i3 = triangles[tri + 2];
|
||
|
|
||
|
Vector3 v1 = vertices[i1];
|
||
|
Vector3 v2 = vertices[i2];
|
||
|
Vector3 v3 = vertices[i3];
|
||
|
|
||
|
Vector2 w1 = texcoords[i1];
|
||
|
Vector2 w2 = texcoords[i2];
|
||
|
Vector2 w3 = texcoords[i3];
|
||
|
|
||
|
float x1 = v2.x - v1.x;
|
||
|
float x2 = v3.x - v1.x;
|
||
|
float y1 = v2.y - v1.y;
|
||
|
float y2 = v3.y - v1.y;
|
||
|
float z1 = v2.z - v1.z;
|
||
|
float z2 = v3.z - v1.z;
|
||
|
|
||
|
float s1 = w2.x - w1.x;
|
||
|
float s2 = w3.x - w1.x;
|
||
|
float t1 = w2.y - w1.y;
|
||
|
float t2 = w3.y - w1.y;
|
||
|
|
||
|
float r = 1.0f / (s1 * t2 - s2 * t1);
|
||
|
Vector3 sdir = new Vector3((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r);
|
||
|
Vector3 tdir = new Vector3((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r);
|
||
|
|
||
|
tan1[i1] += sdir;
|
||
|
tan1[i2] += sdir;
|
||
|
tan1[i3] += sdir;
|
||
|
|
||
|
tan2[i1] += tdir;
|
||
|
tan2[i2] += tdir;
|
||
|
tan2[i3] += tdir;
|
||
|
|
||
|
tri += 3;
|
||
|
}
|
||
|
|
||
|
for (int i = 0; i < (vertexCount); i++)
|
||
|
{
|
||
|
Vector3 n = normals[i];
|
||
|
Vector3 t = tan1[i];
|
||
|
|
||
|
// Gram-Schmidt orthogonalize
|
||
|
Vector3.OrthoNormalize(ref n, ref t);
|
||
|
|
||
|
tangents[i].x = t.x;
|
||
|
tangents[i].y = t.y;
|
||
|
tangents[i].z = t.z;
|
||
|
|
||
|
// Calculate handedness
|
||
|
tangents[i].w = (Vector3.Dot(Vector3.Cross(n, t), tan2[i]) < 0.0) ? -1.0f : 1.0f;
|
||
|
}
|
||
|
theMesh.tangents = tangents;
|
||
|
}
|
||
|
|
||
|
}
|