Unity 3D 画笔

其实挺简单的,拿个物体移动,把物体移动的坐标添加到  Line  的坐标里  ,  如果再 把每次创建的 Line  存储起来, 可以再做个删除键  ,  VR 更不用说了  同理

下面是代码

using UnityEngine;
using System.Collections;
using GoWorldUnity3D;
using Valve.VR.Extras;
using Valve.VR;

/// <summary>
/// 画笔画线
/// </summary>
public class Line : MonoBehaviour
{
    public LineRenderer line;
    //LineRenderer
    private LineRenderer lineRenderer;
    //定义一个Vector3,用来存储鼠标点击的位置
    private Vector3 position;
    //用来索引端点
    private int index = 0;
    //端点数
    private int LengthOfLineRenderer = 0;
    //LineRenderer个数标志
    private int LineIndex = 1;

    //画笔颜色
    public Color color;
    //画笔大小
    public float Size;
    private ListAttr list;
    //private ListAttr pos;
    private ListAttr rgb;

    private SteamVR_Behaviour_Pose pose;
    public SteamVR_Action_Boolean teleport = SteamVR_Input.GetBooleanAction("InteractUI");

    /// <summary>
    /// 是否在白板上画
    /// </summary>
    public bool lineState = false;
    public Vector3 startPos;
    public Vector3 endPos;

    public float y;
    void Start()
    {      
        list = new ListAttr();
        rgb = new ListAttr();
        CommonData.materialLine = line;

        if (CommonData.isVR)
        {
            pose = GameObject.Find("RightHand").GetComponent<SteamVR_Behaviour_Pose>();
          //  Debug.LogError("AAAAAAAAAAAAAAAAA" + CommonData.materialLine.name);
        }
        CommonData.materialLine = line;
        if (gameObject.name== "TheBrush")
            CommonData.materialLine = line;

     
    }

    void LineParameterSetting()
    {

        

        //设置材质
        // lineRenderer.material = new Material(Shader.Find("Particles/Additive"));
        //设置颜色
        lineRenderer.startColor = color;
        lineRenderer.endColor = color;

        float width = Size /100-0.007f;
        //设置宽度
        lineRenderer.startWidth = width;
        lineRenderer.endWidth = width;

        Debug.Log("---------------------"+color);
    }

    private void OnEnable()
    {
        CommonData.materialLine = line;
        if (CommonData.isVR)
        GetComponent<MeshRenderer>().materials[0].color = color;
       
    }

    void Update()
    {
      //  Debug.Log(gameObject.name);

        if (!CommonData.isVR)
        {
            
            #region PCLine
            //鼠标左击
            if (Input.GetMouseButton(0))
            {
                
                if (Input.GetMouseButtonDown(0))
                {
                  
                    // lineRenderer = new GameObject("Line" + LineIndex).AddComponent<LineRenderer>();
                    lineRenderer = GameObject.Instantiate(line);

                  

                    lineRenderer.gameObject.SetActive(true);

                    lineRenderer.name = ("Line" + LineIndex);

                    LineParameterSetting();
                    LineIndex++;

                    color = lineRenderer.startColor;


                   

                    int r = (int)(color.r*255);
                    int b = (int)(color.b*255);
                    int g = (int)(color.g*255);

                  //  Debug.Log(r + "------" + g + "------" + b);
                    rgb.append(r);
                    rgb.append(g);
                    rgb.append(b);
                    rgb.append((int)Size);


                }

                //将鼠标点击的屏幕坐标转换为世界坐标,然后存储到position中
                position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1f));

                if(lineState)
                {
                    //lineRenderer.transform.SetParent( GameObject.Find("Board").transform);
                    //lineRenderer.transform.localPosition = new Vector3(-1.8f, -2f, -20f);

                    lineRenderer.transform.localEulerAngles = GameObject.Find("WriteBoard").transform.localEulerAngles;
                    position = new Vector3(position.x, position.y, startPos.z);

                    position.x = position.x < startPos.x ? startPos.x : position.x;
                    position.x = position.x > endPos.x ? endPos.x : position.x;

                    position.y = position.y < startPos.y ? startPos.y : position.y;
                    position.y = position.y > endPos.y ? endPos.y : position.y;

                  
                }
                

                //端点数+1
                LengthOfLineRenderer++;
                //设置线段的端点数
                lineRenderer.positionCount = LengthOfLineRenderer;

            }
            //连续绘制线段
            while (index < LengthOfLineRenderer)
            {

                //两点确定一条直线,所以我们依次绘制点就可以形成线段了
                lineRenderer.SetPosition(index, position);
                index++;


                ListAttr pos = new ListAttr();

                pos.append(position.x);
                pos.append(position.y);
                pos.append(position.z);

               // pos.append(0);

                list.append(pos);


            }

            if (Input.GetMouseButtonUp(0) && index != 0)
            {
                
                GoWorldUnity3D.GoWorld.ClientOwner.CallServer("SyncBrushData", list, rgb);
                list.RemoveAll();
                rgb.RemoveAll();

                index = 0;
                LengthOfLineRenderer = 0;

            }

            if (Input.GetKeyDown(KeyCode.Delete))
            {
                if (LineIndex > 0)
                {
                    Destroy(GameObject.Find("Line" + --LineIndex));
                }

            }
            #endregion
        }
        else
        {
            #region VRLine
            if (pose == null)
                pose = GameObject.Find("RightHand").GetComponent<SteamVR_Behaviour_Pose>();

            if (teleport.GetStateDown(pose.inputSource))
            {
                Debug.Log("按下右扳机");
                lineRenderer = GameObject.Instantiate(line);
                lineRenderer.gameObject.SetActive(true);
                lineRenderer.name = ("Line" + LineIndex);

                LineParameterSetting();
                LineIndex++;

                Debug.Log(color.r + "------" + color.b + "------" + color.g);
                int r = (int)(color.r * 255);
                int b = (int)(color.b * 255);
                int g = (int)(color.g * 255);
                rgb.append(r);
                rgb.append(g);
                rgb.append(b);
            }

            if (teleport.GetState(pose.inputSource))
            {
                LengthOfLineRenderer++;
                lineRenderer.positionCount = LengthOfLineRenderer;
                position = transform.position;

                if (lineState)
                {
                    position = new Vector3(position.x, position.y, startPos.z);

                    position.x = position.x < startPos.x ? startPos.x : position.x;
                    position.x = position.x > endPos.x ? endPos.x : position.x;

                    position.y = position.y < startPos.y ? startPos.y : position.y;
                    position.y = position.y > endPos.y ? endPos.y : position.y;
                }
               

                while (index < LengthOfLineRenderer)
                {
                    lineRenderer.SetPosition(index, position);
                    index++;

                    ListAttr pos = new ListAttr();
                    pos.append(position.x);
                    pos.append(position.y);
                    pos.append(position.z);
                    list.append(pos);
                }
            }

            if (teleport.GetStateUp(pose.inputSource))
            {
                GoWorldUnity3D.GoWorld.ClientOwner.CallServer("SyncBrushData", list, rgb);
                list.RemoveAll();
                rgb.RemoveAll();

                index = 0;
                LengthOfLineRenderer = 0;
            }

            if (Input.GetKeyDown(KeyCode.Delete))
            {
                if (LineIndex > 0)
                {
                    Destroy(GameObject.Find("Line" + --LineIndex));
                }

            } 
            #endregion

        }
    }
}

 

posted @ 2020-07-07 19:48  D个人笔记  阅读(471)  评论(0编辑  收藏  举报