Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Symons · Sep 09, 2017 at 02:22 PM · meshverticesprocedural meshtriangles

Procedural Mesh Problems

I am honestly stumped, there is clearly some stupid easy problem with my code that I just can't see because iv'e been trying to fix it for ages now. What my code should do;

  1. Raycast from each corner of the viewport and find the position that ray collides with the terrain (working)

  2. Send an array of those points to another script (working)

  3. Use those points to as the vertices of a mesh that shows what the camera can view (not working)

So the vertices are in the right place, I have a draw gizmos lines that shows a square that is exactly what the player can view but yet the mesh doesn't work, it shows up as a stretched triangle is the camera goes out of bounds and one corner of the camera has nothing to collide with but otherwise it isn't drawn. If I put in some dummy vertices it gets drawn fine. Here are the code snippets, please help me; 1.

 private Vector3 TLray ()
     {
         Ray TLCam = cam.ViewportPointToRay (new Vector3(0, 1, 0));
         RaycastHit TLhit;
         if (Physics.Raycast (TLCam, out TLhit)) 
         {
             //TLHitPos = new Vector3 (TLhit.point.x, 1, TLHitPos.y);
             Vector3 TLHitPos = new Vector3 (TLhit.point.x, TLhit.point.y, TLhit.point.z);
             Debug.Log ("TL" + TLHitPos.ToString ());
             return(TLHitPos);
         }
         else
             return(Vector3.one);
     }

(For each corner) 2.

 Vector3 [] corners = new Vector3[4]{TLray(),TRray(),BLray(),BRray()};
         Minimap.SetRectSize (corners);

3.

 public GameObject ViewGO;
     private MeshFilter ViewMf;
     private Mesh ViewMesh;
     public Material mat;
     private int[] tris = new int[6]{0,2,3,3,1,0};
     private Vector2[] uv = new Vector2[4]{ new Vector2 (0, 0), new Vector2 (0, 1), new Vector2 (1, 1), new Vector2 (1, 0) };
 void Start ()
     {
         ViewMf = ViewGO.AddComponent (typeof(MeshFilter)) as MeshFilter;
         MeshRenderer rend = ViewGO.AddComponent (typeof(MeshRenderer)) as MeshRenderer;
         ViewMesh = new Mesh ();
         ViewMesh.vertices = verts;
         ViewMf.mesh = ViewMesh;
         rend.material = mat;
         ViewMesh.uv = uv;
         ViewMesh.triangles = tris;
         ViewMesh.RecalculateBounds ();
         ViewMesh.RecalculateNormals();
     }

 public void SetRectSize (Vector3 [] verts ) 
     {
         Verts = verts;
 
         ViewMesh = ViewMf.sharedMesh;
         ViewMesh.Clear();
         ViewMesh.vertices = verts;
         ViewMf.mesh = ViewMesh;
         ViewMesh.uv = uv;
         ViewMesh.triangles = tris;
         ViewMesh.RecalculateBounds ();
         ViewMesh.RecalculateNormals();
 
     }
 

These are just the relevant snippets. Please help, is it my tris order or something else that shows my naivety?

Comment
Add comment · Show 3
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 Symons · Sep 09, 2017 at 02:28 PM 0
Share

The gizmo lines being drawn;

alt text

One tri shows up if the camera doesn't have anything to collide with on one corner or edge; alt text And the out of bounds vertices become vector3.one. Does this mean my verts are incorrect or the tri order is incorrect maybe?

gizmoworking.jpg (43.4 kB)
cameraoutofbounds.jpg (33.9 kB)
avatar image Symons · Sep 10, 2017 at 12:41 PM 0
Share

This works fine;

 void OnDrawGizmos ()
     {
         Gizmos.color = Color.red;
         Gizmos.DrawLine (Verts [0], Verts [1]);
         Gizmos.DrawLine (Verts [0], Verts [2]);
         Gizmos.DrawLine (Verts [1], Verts [3]);
         Gizmos.DrawLine (Verts [2], Verts [3]);
     }


avatar image Symons · Sep 10, 2017 at 12:52 PM 0
Share

Are mesh vertices using local Vector3 coordinates?

0 Replies

· Add your reply
  • Sort: 

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

121 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How is a Mesh Built? 1 Answer

How can I identify the location of mesh triangles at run time? (Procedural Generation) 1 Answer

Some triangles are black on a mesh generated by script 1 Answer

Creating triangles around a mesh using an array of vertices 1 Answer

Getting face of mesh when you already have one tri 0 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