unity 插件 color picker htc手柄控制,扣动扳机生成三维点线

1,unity中使用插件simple color picker选择颜色。

2,导入运行的结果

可以用鼠标点击屏幕修改颜色。

默认的六个脚本

3 修改为用htc 手柄发射线改变颜色,生成三维的点线。

a,steamvr包的导入和相关设置。

b, 把colorpicker预制体拖到Hierarchy,并添加两个脚本。

ColorManger.cs

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

public class ColorManager : UnitySingleton<ColorManager>
{
    private Color color;
	// Use this for initialization
	void Start ()
    {
		
	}
	
	// Update is called once per frame
	void Update ()
    {
		
	}
    void OnColorChange(HSBColor color)
    {
        this.color = color.ToColor();
    }
    public Color GetCurColor()
    {
        return this.color;
    }
}

UnitySingleton.cs

using UnityEngine;
using System.Collections;
public class UnitySingleton<T> : MonoBehaviour
    where T : Component
{
    private static T _instance;
    public static T Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = FindObjectOfType(typeof(T)) as T;
                if (_instance == null)
                {
                    GameObject obj = new GameObject();
                    obj.hideFlags = HideFlags.HideInHierarchy;//隐藏实例化的new game object,下同
                    _instance = (T)obj.AddComponent(typeof(T));
                }
            }
            return _instance;
        }
    }

    public virtual void Awake()
    {
        DontDestroyOnLoad(this.gameObject);
        if (_instance == null)
        {
            _instance = this as T;
        }
        else
        {
            Destroy(gameObject);
        }
    }
}

c,在右手柄上添加小 球表示画笔。

适当修改大小和位置

d, 获取手柄并做相关设置。

新建一个空物体,把脚本lineManager挂上去,并给lineManger脚本中申请的变量赋值。

linemanager.cs

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

public class LineManager : MonoBehaviour
{
    public Material lineMat;
    public SteamVR_TrackedObject trackedObj;
    private MeshLineRenderer curLine;
	public GameObject sphere;
	// Use this for initialization
	void Start ()
    {

	}
	// Update is called once per frame
	void Update ()
    {
        if (trackedObj==null)
        {
            return;
        }

		SteamVR_Controller.Device device=SteamVR_Controller.Input((int)trackedObj.index);
		//SteamVR_Controller.Device device=SteamVR_Controller.Input(4);
        if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))
        {
            GameObject go = new GameObject();
            curLine = go.AddComponent<MeshLineRenderer>();
            curLine.setWidth(0.01f);
			curLine.lmat = new Material(lineMat);
        }
        else if (device.GetTouch(SteamVR_Controller.ButtonMask.Trigger))
        {
			curLine.AddPoint(sphere.transform.position);
        }
        else if (device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger))
        {
            curLine = null;
        }
        if (curLine != null)
        {
            curLine.lmat.color = ColorManager.Instance.GetCurColor();
        }
	}
}

MeshLineRenderer.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshFilter))]
public class MeshLineRenderer : MonoBehaviour
{
    public Material lmat;
    private Mesh ml;
    private Vector3 s;
    private float lineSize = 0.1f;
    private bool firstQuad = true;

    // Use this for initialization
    void Start()
    {
        ml = GetComponent<MeshFilter>().mesh;
        GetComponent<MeshRenderer>().material = lmat;
    }

    public void setWidth(float width)
    {
        lineSize = width;
    }

    public void AddPoint(Vector3 point)
    {
        if (s != Vector3.zero)
        {
            AddLine(ml, MakeQuad(s, point, lineSize, firstQuad));
            firstQuad = false;
        }
        s = point;
    }

    Vector3[] MakeQuad(Vector3 s, Vector3 e, float w, bool all)
    {
        w = w / 2;
        Vector3[] q;
        if (all)
            q = new Vector3[4];
        else
            q = new Vector3[2];
        Vector3 n = Vector3.Cross(s, e);
        Vector3 l = Vector3.Cross(n, e - s);
        l.Normalize();
        if (all)
        {
            q[0] = transform.InverseTransformPoint(s + l * w);
            q[1] = transform.InverseTransformPoint(s + l * -w);
            q[2] = transform.InverseTransformPoint(e + l * w);
            q[3] = transform.InverseTransformPoint(e + l * -w);
        }
        else
        {
            q[0] = transform.InverseTransformPoint(s + l * w);
            q[1] = transform.InverseTransformPoint(s + l * -w);
        }
        return q;
    }


    void AddLine(Mesh m, Vector3[] quad)
    {
        int vl = m.vertices.Length;
        Vector3[] vs = m.vertices;
        vs = resizeVertices(vs, 2 * quad.Length);
        for (int i = 0; i < 2 * quad.Length; i += 2)
        {
            vs[vl + i] = quad[i / 2];
            vs[vl + i + 1] = quad[i / 2];
        }

        Vector2[] uvs = m.uv;
        uvs = resizeUVs(uvs, 2 * quad.Length);
        if (quad.Length == 4)
        {
            uvs[vl] = Vector2.zero;
            uvs[vl + 1] = Vector2.zero;
            uvs[vl + 2] = Vector2.right;
            uvs[vl + 3] = Vector2.right;
            uvs[vl + 4] = Vector2.up;
            uvs[vl + 5] = Vector2.up;
            uvs[vl + 6] = Vector2.one;
            uvs[vl + 7] = Vector2.one;
        }
        else
        {
            if (vl % 8 == 0)
            {
                uvs[vl] = Vector2.zero;
                uvs[vl + 1] = Vector2.zero;
                uvs[vl + 2] = Vector2.right;
                uvs[vl + 3] = Vector2.right;
            }
            else
            {
                uvs[vl] = Vector2.up;
                uvs[vl + 1] = Vector2.up;
                uvs[vl + 2] = Vector2.one;
                uvs[vl + 3] = Vector2.one;
            }
        }

        int tl = m.triangles.Length;

        int[] ts = m.triangles;
        ts = resizeTriangles(ts, 12);

        if (quad.Length == 2)
            vl -= 4;

        ts[tl] = vl;
        ts[tl + 1] = vl + 2;
        ts[tl + 2] = vl + 4;

        ts[tl + 3] = vl + 2;
        ts[tl + 4] = vl + 6;
        ts[tl + 5] = vl + 4;

        ts[tl + 6] = vl + 5;
        ts[tl + 7] = vl + 3;
        ts[tl + 8] = vl + 1;

        ts[tl + 9] = vl + 5;
        ts[tl + 10] = vl + 7;
        ts[tl + 11] = vl + 3;

        m.vertices = vs;
        m.uv = uvs;
        m.triangles = ts;
        m.RecalculateBounds();
        m.RecalculateNormals();
    }

    Vector3[] resizeVertices(Vector3[] ovs, int ns)
    {
        Vector3[] nvs = new Vector3[ovs.Length + ns];
        for (int i = 0; i < ovs.Length; i++)
            nvs[i] = ovs[i];
        return nvs;
    }

    Vector2[] resizeUVs(Vector2[] uvs, int ns)
    {
        Vector2[] nvs = new Vector2[uvs.Length + ns];
        for (int i = 0; i < uvs.Length; i++)
            nvs[i] = uvs[i];
        return nvs;
    }

    int[] resizeTriangles(int[] ovs, int ns)
    {
        int[] nvs = new int[ovs.Length + ns];
        for (int i = 0; i < ovs.Length; i++)
            nvs[i] = ovs[i];
        return nvs;
    }
}

e, 修改Draggable.cs脚本

using UnityEngine;

public class Draggable : MonoBehaviour
{
	public bool fixX;
	public bool fixY;
	public Transform thumb;	
	bool dragging;

	public SteamVR_TrackedObject trackedObj;

	void FixedUpdate()
	{

		if (trackedObj == null)
		{
			return;
		}
		SteamVR_Controller.Device device = SteamVR_Controller.Input((int)trackedObj.index);
		// SteamVR_Controller.Device device = SteamVR_Controller.Input(3);
		if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))
		{
			dragging = false;
			Ray ray = new Ray(trackedObj.transform.position,trackedObj.transform.forward);
			//var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			RaycastHit hit;
			if (GetComponent<Collider>().Raycast(ray, out hit, 100))
			{
				dragging = true;
			}
		}

		if (device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger)) dragging = false;
		if (dragging && device.GetTouch(SteamVR_Controller.ButtonMask.Trigger))
		{
			Ray ray = new Ray(trackedObj.transform.position, trackedObj.transform.forward);
			//var point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
			RaycastHit hit;
			if (GetComponent<Collider>().Raycast(ray, out hit, 100))
			{
				var point = hit.point;
				point = GetComponent<Collider>().ClosestPointOnBounds(point);
				SetThumbPosition(point);
				SendMessage("OnDrag", Vector3.one - (thumb.position - GetComponent<Collider>().bounds.min) / GetComponent<Collider>().bounds.size.x);
			}    
		}
//		if (Input.GetMouseButtonDown(0)) {
//			dragging = false;
//			var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//			RaycastHit hit;
//			if (GetComponent<Collider>().Raycast(ray, out hit, 100)) {
//				dragging = true;
//			}
//		}
//		if (Input.GetMouseButtonUp(0)) dragging = false;
//		if (dragging && Input.GetMouseButton(0)) {
//			var point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
//			point = GetComponent<Collider>().ClosestPointOnBounds(point);
//			SetThumbPosition(point);
//			SendMessage("OnDrag", Vector3.one - (thumb.position - GetComponent<Collider>().bounds.min) / GetComponent<Collider>().bounds.size.x);
//		}
	}

	void SetDragPoint(Vector3 point)
	{
		point = (Vector3.one - point) * GetComponent<Collider>().bounds.size.x + GetComponent<Collider>().bounds.min;
		SetThumbPosition(point);
	}

	void SetThumbPosition(Vector3 point)
	{
		thumb.position = new Vector3(fixX ? thumb.position.x : point.x, fixY ? thumb.position.y : point.y, thumb.position.z);
	}
}

这样就好了。

 

可以用htc手柄的射线碰到虚拟物体是,在虚拟物体上生成三维的连续曲线。如果效果不好,可以用小球(动态生成或调用预制体)来表示交点。

 

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

public class LineManager : MonoBehaviour
{
    public Material lMat;
    public SteamVR_TrackedObject trackedObj;
    public GameObject las;
    private MeshLineRenderer curLine;
    //public GameObject sphere;
	// Use this for initialization
	void Start ()
    {

	}
	// Update is called once per frame
	void Update ()
    {
        if (trackedObj==null)
        {
            return;
        }

		SteamVR_Controller.Device device=SteamVR_Controller.Input((int)trackedObj.index);
		//SteamVR_Controller.Device device=SteamVR_Controller.Input(4);
        if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))
        {
            GameObject go = new GameObject();
            curLine = go.AddComponent<MeshLineRenderer>();
            curLine.setWidth(0.01f);
            curLine.lmat = new Material(lMat);
        }
        else if (device.GetTouch(SteamVR_Controller.ButtonMask.Trigger))
        {
			
            Ray ray = new Ray(las.transform.position, las.transform.forward);
            //var point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            RaycastHit hit;
            if (GetComponent<Collider>().Raycast(ray, out hit, 100))
            {
                var point = hit.point;
                curLine.AddPoint(point);
            }    
        }
        else if (device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger))
        {
            curLine = null;
        }
        //if (curLine != null)
        //{
        //    curLine.lmat.color = ColorManager.Instance.GetCurColor();
        //}
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值