【问题标题】:Unity3d render texture based on positionUnity3d基于位置渲染纹理
【发布时间】:2014-07-18 00:27:25
【问题描述】:

我目前正在尝试在 Unity 中创建一个包含多个四边形的容器,并且正在寻找一种方法来阻止渲染容器边界之外的四边形部分?

我只花了 20 分钟试图弄清楚如何正确解释问题(但失败了),所以创建了这张漂亮的图片。红线代表边界,黑色方块代表我的纹理四边形。

【问题讨论】:

  • 确保在 Unity 上发布答案,您更有可能在那里得到回复。
  • 感谢您的提示,会的
  • 您的问题对细节有点轻描淡写。你是在纹理上做这一切,还是在 Unity 中四边形是实际的网格?如果是网格,您要么需要进行数学运算来切割超出范围的四边形,要么使用着色器将超出范围的像素设置为透明。这两个选项都不容易在 SO 答案中表达。

标签: c# unity3d


【解决方案1】:

试试这个:

private Rect ClipRectToScreen (Rect input)
                    {
                            // Special handling for screen reading of render texture so it always stay in visible area.
                            // Will throw error withoud this fix.
                            Rect r;
                            r = new Rect (input.x, (Screen.height - input.y - input.height) , input.width, input.height);
                            if (r.x < 0f) {
                                    r.width = r.width - UnityEngine.Mathf.Abs (r.x);
                                    r.x = 0f;
    //                              Debug.Log ("x < 0");
                            }
                            if (r.y < 0f) {
                                    r.height = r.height - UnityEngine.Mathf.Abs (r.y);
                                    r.y = 0f;
    //                              Debug.Log ("y < 0");
                            }
                            if (r.x > Screen.width) {
                                    r.width = r.width - UnityEngine.Mathf.Abs (r.x);
                                    r.x = 0f;
    //                              Debug.Log ("x > Screen.width");
                            }
                            if (r.y > Screen.height) {
                                    r.height = r.height - UnityEngine.Mathf.Abs (r.y);
                                    r.y = 0f;
    //                              Debug.Log ("y > Screen.height");
                            }
                            if ((r.x + r.width) > Screen.width) {
                                    r.width = Screen.width - r.x;
    //                              Debug.Log ("r.x + r.width > Screen.width");
                            }
                            if ((r.y + r.height) > Screen.height) {
                                    r.height = Screen.height - r.y;
    //                              Debug.Log ("r.y + r.height > Screen.height");
                            }

                            return r;
                    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-31
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    • 2013-12-27
    • 2012-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多