Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by hkarrson · Jul 26, 2016 at 07:39 AM · vector3pointloopspixelsplanes

How can I loop through every 3D point on a plane's surface in C#?

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<Vector3> GetPoints(GameObject obj, float res)
     {
         Vector3[] verts = obj.GetComponent<MeshFilter>().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<Renderer>().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

alt text

plane.png (113.1 kB)
tree.png (81.5 kB)
Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hkarrson · Jul 26, 2016 at 03:02 PM 0
Share

So now I know how to loop through each vertice. All I had to do is get the $$anonymous$$eshFilter component to get the mesh, and from that I got the vertices. The problem is that the vertices aren't on a pixel level, and they don't match up in the game. The distance between the vertices is 10 x 10, and it ranges from -5 to 5. I need to know how to get all the vertices in between based on the actual scale on the plane.

avatar image mrpmorris · Jul 26, 2016 at 03:17 PM 1
Share

Will raycasting over the entire X/Y size of the object do it? If you know the world position of the plane then you could deter$$anonymous$$e height by measuring the distance from the ray cast hit to that. I expect it would be slow though.

avatar image hkarrson mrpmorris · Jul 26, 2016 at 03:37 PM 0
Share

That's a great idea. I'll try it!

avatar image mrpmorris hkarrson · Jul 26, 2016 at 03:53 PM 1
Share

If the texture will be a 2D material then try

 var material = theTarget.renderer.material;
 var texture2D = (Texture2D)material.mainTexture;
 var color = texture2D.GetPixel(..................)

Show more comments
avatar image hkarrson mrpmorris · Jul 26, 2016 at 04:34 PM 0
Share

I like your idea, but I wonder if there's a way to do it with vertices. This idea sounds great, but I'm wondering if the complexity is called for. $$anonymous$$aybe it could be a bit more optimized.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Glurth · Jul 26, 2016 at 04:55 PM

You mentioned that you now know how to get each vertex of the MeshFilter's mesh. These verticies are defined in model-space. In order to convert these to world-space coordinates use the MeshFilter's transform. e.g.

 worldVertexPoint=meshfilter.gameobject.transform.TransformPoint(modelVertexPoint);


You could now put an object at that particular worldVertexPoint like so:

 someObject.transform.position= worldVertexPoint;

https://docs.unity3d.com/ScriptReference/Transform.TransformPoint.html

Regarding getting the color of the texture, at that vertex: Lets go back a step- lets say you got your vertex using

 modelVertexPoint= mesh.vertices[vertexNumber];

We can also get the Texture coordinate (UV), of that vertexNumber

 Vector2 vertexUV = mesh.uv[vertexNumber];

You can then feed this UV coordinate, into the GetPixel function that MrPMorris mentioned.
The advantage to this method is, it eliminates the need to do any raycasting. It does NOT account for any shadows or lighting though.

note: Colored verticies are optional in a Mesh, and they require a vertex-color-shader to work. Fairly rare nowadays. If you import the mesh it might not have vertex colors defined. If you ARE using colored verticies , rather than a texture, it's even simpler:

 Color vertexColors= mesh.color[vertexNumber];

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hkarrson · Jul 26, 2016 at 05:01 PM 0
Share

Thank you so much! I'll give it a shot!

avatar image hkarrson · Jul 26, 2016 at 05:12 PM 0
Share

Thanks a lot! That helped a ton! 10 points goes to you!

avatar image Glurth hkarrson · Jul 26, 2016 at 05:20 PM 0
Share

No additional points needed, thanks tho! Accepting it as the answer will grant me points anyway :) What about the color stuff? Get that figured out? Are you using vertex colors(in which case, you now have the answer) or textures?

avatar image hkarrson Glurth · Jul 26, 2016 at 05:22 PM 0
Share

No problem! Unfortunately, mesh.color32 is returning an empty list. It would be great if you could help me figure that out :)

Show more comments
Show more comments
avatar image hkarrson · Jul 26, 2016 at 05:58 PM 0
Share

Thanks to you, I was able to retrieve some of the pixels, but is it possible to get the same number of vertices as the number of pixels in the image?

Thanks!

avatar image Glurth hkarrson · Jul 26, 2016 at 06:20 PM 0
Share

Not with any of the methods I've described, no. It's starting to sound like what you want is a surface shader. A surface-shader performs processing on each pixel of a drawn object. If you are just looking for a random pixel, here and there, then $$anonymous$$rP$$anonymous$$orris's raycast suggestion might be suitable.

avatar image hkarrson Glurth · Jul 26, 2016 at 06:30 PM 0
Share

Thanks for the reply. Could a surface shader send information back to c# so that it may be processed later?

EDIT: I just found out that what I just asked is totally possible. Thank you SO $$anonymous$$UCH! I now know what to do. :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

57 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Kind of Patrol AI with Point A and B Help 1 Answer

Find Most Distant Geometric Points in the Scene 1 Answer

Find segment point perpendicular to plane 1 Answer

Angle between 2 vectors/points on a local axis 1 Answer

Find center between 4 points 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges