zak*_*jma 6 c# drawing unity-game-engine
我尝试使用 opencv 和 unity3d 查找和显示角落。我用统一相机捕捉。我将 texture2d 发送到使用 opencv 的 C++ 代码。我使用 opencv(哈里斯角检测器)检测角。和 C++ 代码发送到统一代码角点(图像上的 x,y 位置)。
最后,我想说明这些要点。我尝试在 texture2d 上统一绘制圆圈。我使用下面的代码。但是统一说Type UnityEngine.Texture2D does not contain a definition for DrawCircle and no extension method DrawCircle of type UnityEngine.Texture2D could be found
如何在 unity3d 上绘制简单的形状?
Texture2D texture = new Texture2D(w, h,TextureFormat.RGB24 , false);
texture.DrawCircle(100, 100, 20, Color.green);
// Apply all SetPixel calls
texture.Apply();
mesh_renderer.material.mainTexture = texture;
Run Code Online (Sandbox Code Playgroud)
来自@ChrisH 的更优化的解决方案
(当我尝试在 1000x1000 纹理上绘制 300 个圆圈时,原始解决方案使我的电脑慢了 2 分钟,而新的解决方案由于避免了额外的迭代而立即执行)
public static Texture2D DrawCircle(this Texture2D tex, Color color, int x, int y, int radius = 3)
{
float rSquared = radius * radius;
for (int u = x - radius; u < x + radius + 1; u++)
for (int v = y - radius; v < y + radius + 1; v++)
if ((x - u) * (x - u) + (y - v) * (y - v) < rSquared)
tex.SetPixel(u, v, color);
return tex;
}
Run Code Online (Sandbox Code Playgroud)
只需为Texture2d制作一个扩展方法即可。
public static class Tex2DExtension
{
public static Texture2D Circle(this Texture2D tex, int x, int y, int r, Color color)
{
float rSquared = r * r;
for (int u=0; u<tex.width; u++) {
for (int v=0; v<tex.height; v++) {
if ((x-u)*(x-u) + (y-v)*(y-v) < rSquared) tex.SetPixel(u,v,color);
}
}
return tex;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12603 次 |
| 最近记录: |