Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Freaking-Pingo · Jul 13, 2013 at 12:04 AM · camerameshverticesfrustrumvertice manipulation

Modifying plane to fit view frustrum issue

Hi there, I am currently trying to fit the Unity's default plane (The one with 121 vertices) to fit the frustrum of a camera (Not the main camera). I know I am nearly there, but there is something that bugs me. Whenever I manipulate my mesh through its vertices, at the point, the mesh simply disappears.

The code I am using is this:

 using UnityEngine;
 using System.Collections;
 
 public class TestMapScript : MonoBehaviour {
 
     public Camera mapCamera;
     public GameObject texture;
 
     void Start()
     {
         MeshFilter textureMesh = texture.GetComponent<MeshFilter>();
         Vector3[] arrayOfvertices = textureMesh.mesh.vertices;
 
         for (int i = 0; i < arrayOfvertices.Length; i++)
         {
             arrayOfvertices[i].x += 5;
             arrayOfvertices[i].z += 5;
             arrayOfvertices[i].y = arrayOfvertices[i].z;
             arrayOfvertices[i].z = 0;
             arrayOfvertices[i] = (arrayOfvertices[i] / 10);
             arrayOfvertices[i] = mapCamera.ViewportToWorldPoint(arrayOfvertices[i]);
             
             Debug.DrawRay(arrayOfvertices[i], Vector3.up*100, Color.red, 1000f);
         }
 
         textureMesh.mesh.Clear();
         textureMesh.mesh.vertices = arrayOfvertices;
         /*textureMesh.mesh.uv = textureMesh.sharedMesh.uv;
         textureMesh.mesh.triangles = textureMesh.sharedMesh.triangles;*/
         textureMesh.mesh.RecalculateNormals();
         textureMesh.mesh.RecalculateBounds();
         
     }
 }


What the script basically do is it takes texture (I know silly name) and fit it's vertices into the view frustrum of the mapCamera. Through some testing I have figured out that the mesh seems to be properly manipulated during these vertice manipulations:

                 arrayOfvertices[i].x += 5;
                 arrayOfvertices[i].z += 5;
                 arrayOfvertices[i].y = arrayOfvertices[i].z;
                 arrayOfvertices[i].z = 0;
                 arrayOfvertices[i] = (arrayOfvertices[i] / 10);

But as soon as I utilize a matrix:

 arrayOfvertices[i] = mapCamera.ViewportToWorldPoint(arrayOfvertices[i]);

It disappears.

By using the Debug.DrawRay(); I see the vertices are placed correctly on the view frustrum as expected, but its not visible.

An illustration that shows how the vertices are placed.

Any idea what causes this? What have I forgotten when utilizing the matrix? Any help would be appreciated.

PS. My own personal idea is that after the matrix have been applied to all the vertices, the UV/Normal/Triangles or something have been reordered, but I can't figure out whether this is the actual problem. If this was the case, I would at least expect some sort of distorted plane.

vertices.png (373.0 kB)
Comment
Add comment · Show 4
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 Benproductions1 · Jul 13, 2013 at 01:22 PM 0
Share

Can you view the plane from the other side? Also, whats the problem with just scaling it?

avatar image Freaking-Pingo · Jul 13, 2013 at 02:25 PM 0
Share

The plane is not visible on the other side. There is properly the possibility of scaling, but I can't figure out the scaling factors. The Camera frustrum resolution is dynamic and may change depending on user setup, and so I can't figure out the scaling factors for the plane to match the orthogonal size of the camera.

avatar image robertbu · Jul 13, 2013 at 02:40 PM 0
Share

If you are dealing with an Orthographic camera, Camera.orthographicSize is 1/2 of the vertical size the camera is viewing. So you want to size your mesh to have a vertical size twice this value. You get the horizontal size by multiplying vertical size by Screen.width/Screen.height. Do you need the calculations for a perspective camera as well?

avatar image Benproductions1 · Jul 14, 2013 at 01:27 AM 0
Share

(Just FYI) if you can already calculate the vertex positions according to your view, then with a simple bit of maths it's easy to calculate the scaling :)

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

16 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

Related Questions

Mesh's corners are not where it's supposed to be 0 Answers

Flattening a mesh by deforming it 0 Answers

Adding vertices to procedural mesh generator 2 Answers

Mesh is deleting itself when the camera approaches it 2 Answers

Camera frustums + custom field of view 2 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