关闭

unity3D 从摄像机发射射线,检测画线

标签: unity3d摄像头脚本射线c#
2556人阅读 评论(0) 收藏 举报
分类:

                最近做项目的时候,有一个需要从摄像机发出一条射线,当射线检测到有碰撞物体的时候,点击鼠标左键开始画线。我是创立了一个平板drawing,当然Cube什么的也是可以的。

                c#脚本代码,将这个脚本拖到摄像机里,然后拖拽材质到画线材质,拖拽碰撞体到脚本那里即可以。

                代码脚本:

using UnityEngine;
using System.Collections;

public class test : MonoBehaviour {

    //-----------划线所能够用到材质
     public Material lineMaterial  ;
	//
      int maxPoints = 5000;
      //全局变量的定义方法
     public static float lineWidth = 4.0f;
	 int minPixelMove = 2;	// Must move at least this many pixels per sample for a new segment to be recorded
     public Vector2[] linePoints;
     private VectorLine  line ;
     private int  lineIndex = 0;
      private   Vector2   previousPosition;
      private int  sqrMinPixelMove ;
      private bool canDraw = false;
    //记录当下画板的位置,判断平板是否平移等
      public Vector3 boardPosition;
      public GameObject board;
	//记录当下画板的大小,判断画板是否缩放等	
	 public Vector3 boardScale;	
	//初始化平板的大小与位置
	void IntializeScaleAndPosition()
	{
		boardPosition=Camera.mainCamera.WorldToScreenPoint(board.transform.position);
	    boardScale=board.transform.localScale;
		
	}
	// 初始化
	void Start () {
        linePoints = new Vector2[maxPoints];
        line = new VectorLine("DrawnLine", linePoints,lineMaterial, lineWidth, LineType.Continuous);
        sqrMinPixelMove = minPixelMove * minPixelMove;
		IntializeScaleAndPosition();
	}
	// Update is called once per frame
	void Update () {
        Vector2 mousePos = Input.mousePosition;
		//当画板移动时,将所有的点按照统一方向移动相同的距离
		//当前画板的位置
	   Vector3 currentBoardPositon=Camera.mainCamera.WorldToScreenPoint(board.transform.position);
		
		//画板的移动距离
		Vector3  moveDistance=currentBoardPositon-boardPosition;
		
	    if(moveDistance.x!=0||moveDistance.y!=0)
		{
			//将所有的画下的点都平移
			for(int i=0;i<linePoints.Length;i++)
			{
			Vector2 dis=new Vector2(moveDistance.x,moveDistance.y);
			linePoints[i]+=dis;
			}
			//销毁当前画下的线
		    Vector.DestroyLine(ref line);
			
			//距离平移后,所有的点的位置已经重新获得,重新画下新的线
			
			 line = new VectorLine("DrawnLine", linePoints, lineMaterial, lineWidth, LineType.Continuous);
			 line.minDrawIndex = 1;
	         line.maxDrawIndex = lineIndex;
		    Vector.DrawLine(line);
			
		}	
		boardPosition=Camera.mainCamera.WorldToScreenPoint(board.transform.position);
        //平板的缩放操作,对平板进行缩放,同时画出的线也要能够实现缩放。
		//检测平板是否已经开始缩放
		float currentScale=board.transform.localScale.x;
		float scaleFactor=currentScale/(boardScale.x);
		//Debug.Log("scaleFactor "+scaleFactor+" currentScale "+currentScale+" boardScale "+boardScale.x);
		boardScale=board.transform.localScale;
	    if(scaleFactor!=1||scaleFactor!=1)
		{	
			// 求出画板中心的位置
		    Vector3 boardPostionToSceern=Camera.mainCamera.WorldToScreenPoint(board.transform.position);
			
			  //当平板缩放时,所有的点都要同比例缩放。
			for(int i=0;i<linePoints.Length;i++)
			{
			Vector3 pointPositon=new Vector3(linePoints[i].x,linePoints[i].y,0);
			Vector3 PointToCenterDistance=(pointPositon-boardPostionToSceern)*scaleFactor;
			linePoints[i]=new Vector2((boardPostionToSceern+PointToCenterDistance).x,(boardPostionToSceern+PointToCenterDistance).y);
			}		
			//销毁当前画下的线
		    Vector.DestroyLine(ref line);
			//画板缩放后,所有的点的位置已经重新获得,重新画下新的线
			 line = new VectorLine("DrawnLine", linePoints, lineMaterial, lineWidth, LineType.Continuous);
			 line.minDrawIndex = 1;
	         line.maxDrawIndex = lineIndex;
		    Vector.DrawLine(line);	
		}
		//从摄像头发出一条射线
		Ray ray=Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
	    RaycastHit hit;
		if(Physics.Raycast(ray,out hit,1000))
		{	
		//检测到物体时才开始画
		if(hit.transform.gameObject.name=="drawing")
		{		
		    if (Input.GetMouseButtonDown(0))
	        {
	          //  Vector.ZeroPointsInLine(line);
	           // line.minDrawIndex = lineIndex;
	          //  Vector.DrawLine(line);
	           // linePoints[lineIndex] = mousePos;
	            previousPosition  = mousePos;
	            lineIndex++;
	            linePoints[lineIndex] = mousePos;
	            canDraw = true;
	        }
	        else if (Input.GetMouseButton(0) && (mousePos - previousPosition).sqrMagnitude > sqrMinPixelMove && canDraw)
	        {
	            previousPosition = linePoints[++lineIndex] = mousePos;
	            line.minDrawIndex = lineIndex - 1;
	            line.maxDrawIndex = lineIndex;
	            if (lineIndex >= maxPoints - 1) canDraw = false;
	            Vector.DrawLine(line);
	        }				
		}	
	}

                 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:37322次
    • 积分:703
    • 等级:
    • 排名:千里之外
    • 原创:33篇
    • 转载:0篇
    • 译文:0篇
    • 评论:16条
    文章存档
    最新评论