输入鼠标位置未准确捕获?

时间:2017-07-09 18:06:10

标签: c# unity3d

我正在使用鼠标输入在纹理上从点A到点B绘制一条线。我已经意识到我没有击中每个像素,因此将鼠标从屏幕的左侧移动到最右侧,它会跳过一些值,导致间隙。当我慢慢移动鼠标时,它变得更好。

我使用以下方式获取鼠标位置:

var viewPortPosition = Camera.main.ScreenToViewportPoint (Input.mousePosition);

enter image description here

2 个答案:

答案 0 :(得分:1)

我最终做的是通过Bresenham的线条绘制功能运行到目前为止收集的鼠标位置来栅格化线条。

来源:

    public void line(Vector2 pointA, Vector2 pointB, Texture2D texture) {
    int x = (int)pointA.x;
    int y = (int)pointA.y;

    int x2 = (int)pointB.x;
    int y2 = (int)pointB.y;

    int w = x2- x ;
    int h = y2 - y;
    int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0 ;
    if (w<0) dx1 = -1 ; else if (w>0) dx1 = 1 ;
    if (h<0) dy1 = -1 ; else if (h>0) dy1 = 1 ;
    if (w<0) dx2 = -1 ; else if (w>0) dx2 = 1 ;
    int longest = Math.Abs(w) ;
    int shortest = Math.Abs(h) ;
    if (!(longest>shortest)) {
        longest = Math.Abs(h) ;
        shortest = Math.Abs(w) ;
        if (h<0) dy2 = -1 ; else if (h>0) dy2 = 1 ;
        dx2 = 0 ;            
    }
    int numerator = longest >> 1 ;
    for (int i=0;i<=longest;i++) {
        texture.SetPixel (x, y, Color.red);

        numerator += shortest ;
        if (!(numerator<longest)) {
            numerator -= longest ;
            x += dx1 ;
            y += dy1 ;
        } else {
            x += dx2 ;
            y += dy2 ;
        }
    }
}

enter image description here

  
    
      

原始代码来源,但针对Unity C进行了修改#:All cases covered Bresenham's line-algorithm

    
  

答案 1 :(得分:0)

如果你想画一条线,我建议你使用line renderer。假设您在Update()函数中实现了代码 - 每次渲染时都会调用它。因此,即使你的动作非常缓慢,显然已经足够在画线上留下空隙了。

以下是此link的一些代码,它们可以正常使用:

using System.Collections.Generic;
 using UnityEngine;

 [RequireComponent(typeof(LineRenderer))]
 public class LineRendererTest : MonoBehaviour
 {
     List<Vector3> linePoints = new List<Vector3>();
     LineRenderer lineRenderer;
     public float startWidth = 1.0f;
     public float endWidth = 1.0f;
     public float threshold = 0.001f;
     Camera thisCamera;
     int lineCount = 0;

     Vector3 lastPos = Vector3.one * float.MaxValue;


     void Awake()
     {
         thisCamera = Camera.main;
         lineRenderer = GetComponent<LineRenderer>();
     }

     void Update()
     {
         Vector3 mousePos = Input.mousePosition;
         mousePos.z = thisCamera.nearClipPlane;
         Vector3 mouseWorld = thisCamera.ScreenToWorldPoint(mousePos);

         float dist = Vector3.Distance(lastPos, mouseWorld);
         if(dist <= threshold)
             return;

         lastPos = mouseWorld;
         if(linePoints == null)
             linePoints = new List<Vector3>();
         linePoints.Add(mouseWorld);

         UpdateLine();
     }


     void UpdateLine()
     {
         lineRenderer.SetWidth(startWidth, endWidth);
         lineRenderer.SetVertexCount(linePoints.Count);

         for(int i = lineCount; i < linePoints.Count; i++)
         {
             lineRenderer.SetPosition(i, linePoints[i]);
         }
         lineCount = linePoints.Count;
     }