我尝试使用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;
答案 0 :(得分:2)
只需为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;
}
}
答案 1 :(得分:1)
@ChrisH的更多优化解决方案
(当我尝试在1000x1000的纹理上绘制300个圆时,原本的PC速度降低了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;
}