using System.Collections; using System.Collections.Generic; using UnityEngine; namespace EZXR.Glass.Inputs { public class InputRayDrawer : MonoBehaviour { InputInfoBase handInfo; /// /// 手指关节点0 /// public Transform joint0; /// /// 手指关节点1 /// public Transform joint1; /// /// 线 /// public LineRenderer lineRenderer; /// /// 实线和虚线的material /// public Material[] materials; ///// ///// 线总长度 ///// //public float length = 5f; /// /// 单线段长度 /// public float eachLength = 0.01f; /// /// 是否是实线 /// bool solidLine; /// /// 贝塞尔曲线的p2点 /// Vector3 p2; Vector3 p0; Vector3 p1; // Start is called before the first frame update void Start() { } /// /// 重置lineRenderer为直线 /// void ResetPositions() { for (int i = 0; i < lineRenderer.positionCount; i++) { lineRenderer.SetPosition(i, new Vector3(0, 0, eachLength * (lineRenderer.positionCount - 1 - i))); } } // Update is called once per frame void Update() { if (joint0 != null) { //Debug.Log(handInfo.curRayContactingPoint.ToString()); if (!handInfo.IsRayGrabbing) { p2 = joint1.position; } p0 = joint0.position; p1 = joint1.position; transform.position = joint0.position; lineRenderer.positionCount = Mathf.Clamp(Mathf.CeilToInt(Vector3.Magnitude(joint1.position - joint0.position) / eachLength), 2, 10000); for (int i = 0; i < lineRenderer.positionCount; i++) { lineRenderer.SetPosition(i, CalculateBezier(1 - ((float)i / (lineRenderer.positionCount - 1)))); } } } Vector3 CalculateBezier(float t) { Vector3 p0p1 = (1 - t) * p0 + t * p1; Vector3 p1p2 = (1 - t) * p1 + t * p2; Vector3 result = (1 - t) * p0p1 + t * p1p2; return result; } public void SetUp(InputInfoBase handInfo, Transform joint0, Transform joint1) { this.handInfo = handInfo; this.joint0 = joint0; this.joint1 = joint1; Gradient gradient = new Gradient(); gradient.mode = GradientMode.Blend; gradient.SetKeys( new GradientColorKey[] { new GradientColorKey(Color.white, 0.0f), new GradientColorKey(Color.white, 1.0f) }, new GradientAlphaKey[] { new GradientAlphaKey(0, 0.0f), new GradientAlphaKey(0.2f, 0.75f), new GradientAlphaKey(1, 1.0f) } ); lineRenderer.colorGradient = gradient; //lineRenderer.positionCount = Mathf.CeilToInt(length / eachLength); Vector3[] positions = new Vector3[lineRenderer.positionCount]; for (int i = 0; i < positions.Length; i++) { positions[i] = new Vector3(0, 0, eachLength * (positions.Length - 1 - i)); } lineRenderer.SetPositions(positions); } /// /// 设置线的状态是实线还是虚线 /// /// public void SetLineState(bool solidLine) { this.solidLine = solidLine; if (solidLine) { lineRenderer.material = materials[0]; lineRenderer.textureMode = LineTextureMode.Stretch; } else { //ResetPositions(); lineRenderer.material = materials[1]; lineRenderer.textureMode = LineTextureMode.RepeatPerSegment; } } /// /// 设置贝塞尔曲线用的p2点(最终显示的曲线的终点,是滤波之后的点),p0是射线的起点,p1是贝塞尔曲线用到的折弯点(射线的终点) /// /// public void SetLineBezierPoint(Vector3 p2) { this.p2 = p2; } } }