【问题标题】:How to redraw pixels on a 2DTexture without it flickering如何在不闪烁的情况下重绘 2DTexture 上的像素
【发布时间】:2015-12-02 19:56:15
【问题描述】:

我想在统一的 2DTexture 上画一条线。这在编辑器中运行良好。但是,在编译的版本中,它会闪烁,因为它会用清晰的颜色填充纹理的像素,然后重新绘制线条。

这是我的绘图代码:

public void DrawLineWithMouse (Vector2 _middleVector)
{
    tex.SetPixels(fillPixels); // Fills texture with an array of clear pixels
    Vector3 newMousePos = Input.mousePosition;
    newMousePos.z = 0;
    int x0 = ((int)newMousePos.x)/RenderingManager.pixelSize  / thickness; //Screen.width-
    int y0 = ((int)newMousePos.y)/RenderingManager.pixelSize  / thickness; //Screen.height-
    int x1 =  (int)_middleVector.x - (x0 - (int)_middleVector.x );
    int y1 =  (int)_middleVector.y - (y0 - (int)_middleVector.y );
    DrawLineHelper(tex, x0, y0, x1, y1, Color.yellow); // redraws the line

    tex.Apply();
}

当我创建新纹理然后应用它时,我不会闪烁。但是,这非常、非常昂贵,并且仅用于测试目的。

    public void DrawLineWithMouse (Vector2 _middleVector)
{
    tex = new Texture2D(Screen.width/RenderingManager.pixelSize / thickness, Screen.height/RenderingManager.pixelSize / thickness);
    tex.SetPixels(fillPixels);
    tex.SetPixels(fillPixels);
    tex.filterMode = FilterMode.Point;



    Vector3 newMousePos = Input.mousePosition;
    newMousePos.z = 0;
    int x0 = ((int)newMousePos.x)/RenderingManager.pixelSize  / thickness; //Screen.width-
    int y0 = ((int)newMousePos.y)/RenderingManager.pixelSize  / thickness; //Screen.height-
    int x1 =  (int)_middleVector.x - (x0 - (int)_middleVector.x );
    int y1 =  (int)_middleVector.y - (y0 - (int)_middleVector.y );
    DrawLineHelper(tex, x0, y0, x1, y1, Color.yellow);
    GetComponent<Renderer>().material.mainTexture = tex;

【问题讨论】:

  • 很好奇你是怎么打电话给DrawLineWithMouse()的。您是在Update 还是FixedUpdate 中调用它?
  • if (Input.GetMouseButton(0)){drawLineUtility.DrawLineWithMouse(startMousePointInPixels);} 在更新中

标签: unity3d rendering


【解决方案1】:

它闪烁是因为 apply 是一项昂贵的操作。 The documentation 状态:

这是一个潜在的昂贵操作,因此您需要在 Apply 调用之间更改尽可能多的像素。

你必须想办法减少对DrawLineWithMouse 的调用。什么是最好的解决方案,这取决于您并且取决于您在做什么。

另一种方法是累积鼠标移动的增量并在 x 毫秒后绘制。另一种选择是在纹理顶部绘制网格而不是更改纹理,然后仅在必要时更改纹理(如果有)(请注意,您可以在此处使用 RenderTexture 轻松绘制网格在纹理中)。

最后,降低纹理的分辨率可以让Apply 运行得更快。

【讨论】:

  • 感谢您的回答,但这是不正确的。这是因为我是tex.SetPixels(fillPixels); 填充像素是一个清晰像素的数组。然后它重新绘制线。我会更新我的问题,使其更清晰
  • 我还是不明白为什么你必须调用SetPixels并清除所有内容而不是调用SetPixel逐像素绘制,只改变你想要的。等等... ...这是正确的答案吗?
  • @Jim 你让它工作了吗?你看到我的消息了吗?
  • 感谢您关注罗伯托。可惜没有。
猜你喜欢
  • 2012-06-07
  • 2013-01-02
  • 1970-01-01
  • 2013-08-08
  • 2013-07-20
  • 1970-01-01
  • 2022-12-31
  • 2011-05-18
  • 1970-01-01
相关资源
最近更新 更多