I'm making a model editor for Unity 5 that will allow the developer to draw an image, and then inflate the
image into a 3D object. Of course, it would not inflate transparent points. In order to do this, I've created a script that's connected to a plane with particles/alpha blended enabled in order to ensure the plane shows both sides. I've also created a custom editor script that links to the plane's script. I decided it would be a good idea to disable the mesh renderer when the plane is inflated to ensure people can't see the deflated object. What I want to do is to loop through each Vector3 on the plane's surface and create a new object with new points based on the pixels in the object.
So the question is: How can I loop through every 3D point on a plane's surface in C#?
Bonus points if you can help me find the color of each point, but I think I have that figured out.
UPDATE:
Thanks for all the support! I actually thought of a way to do this that I'm suprised I never thought of.
My idea only works well with planes, but that's all that I need. I also figured out how to filter colors! You can view the code below.
public static IEnumerable GetPoints(GameObject obj, float res)
{
Vector3[] verts = obj.GetComponent().sharedMesh.vertices;
float x1 = float.MaxValue;
float x2 = 0;
float z1 = float.MaxValue;
float z2 = 0;
foreach (Vector3 vert in verts)
{
if (vert.x < x1)
{
x1 = vert.x;
}
if (vert.x > x2)
{
x2 = vert.x;
}
if (vert.z < z1)
{
z1 = vert.z;
}
if (vert.z > z2)
{
z2 = vert.z;
}
}
Vector2 size = new Vector2(Mathf.Abs(x1 - x2), Mathf.Abs(z1 - z2));
Texture2D tex = ((Texture2D)obj.GetComponent().sharedMaterial.mainTexture);
for (float x = -(size.x / 2); x <= size.x / 2; x += res)
{
for (float z = -(size.y / 2); z <= size.y / 2; z += res)
{
Vector3 V3 = obj.transform.TransformPoint(new Vector3(x, 1, z));
if (tex.GetPixel(tex.width - (int)(((x + (size.x / 2)) / size.x) * tex.width),
tex.height - (int)(((z + (size.y / 2)) / size.y) * tex.height)).a > 0.5f)
yield return V3;
}
}
}
If anyone is wondering how to use the code above, here's a snipplet (0.1f is the increment):
foreach (Vector3 V3 in GetPoints(gameObject, 0.1f))
{
GameObject t = Instantiate(gameObject2);
t.transform.position = V3;
}
Here's a couple screenshots of what you can do:
![alt text][1]
![alt text][2]
[1]: /storage/temp/74896-plane.png
[2]: /storage/temp/74901-tree.png
↧