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
2
Question by ivansi · May 30, 2014 at 12:54 AM · proceduralprocedural meshuv mapping

How to calculate uvs for roof procedurally generated?

Hi, after a few attempts I decided to seek help here. I have a roof generated procedurally and I'm using to its coordinates (u, v) the same coordinates of the vertices (x, z), but the result is not as expected and similar to reality.

alt text

Can anyone help with any tips on how to calculate the coordinates (u, v)?

Note: Vertices not shared. Have also tried to use this script: link text

Sorry for my english.

Thanks!

untitled-1.jpg (240.7 kB)
Comment
Add comment · Show 1
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 Daviz · Feb 09, 2015 at 03:14 PM 0
Share

Hey, i know it is offtopic but, I am very curoius what algorithm do you use for generating roof mesh because I am currently working on that too. Thanks :)

3 Replies

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

Answer by DMGregory · May 30, 2014 at 01:19 AM

Try something like this for each vertex...

 // This gives a vector pointing up the roof:
 Vector3 vAxis = Vector3.Scale(normal, new Vector3(-1, 0, -1)).normalized;
 
 // This will make the u axis perpendicular to the v axis (ie. parallel to the roof edge)
 Vector3 uAxis = new Vector3(vAxis.z, 0, -vAxis.x);

 // I originally used vAxis here, but changed to position.y so you get more predticable alignment at edges.
 // Set eaveHeight to the y coordinate of the bottom edge of the roof.
 Vector2 uv = new Vector2(Vector3.Dot(position, uAxis), position.y - eaveHeight);

 // You may need to scale the uv vector's x and y to get the aspect ratio you want.
 // The scale factor will vary with the roof's slope.


Note that this won't behave well where the vertices on a triangle have different normals, like around a curved turret, or level portions where the normal points straight up.

Edges between different slopes may act up, so it may look good to add an extra bit of geometry to cap them: alt text

Comment
Add comment · Show 10 · 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 ivansi · May 30, 2014 at 03:00 AM 1
Share

$$anonymous$$aybe I did something wrong, because wrong generated. I'll trying to adjust your approach to see if I can adjust. Code:

 private void TestUvRoof$$anonymous$$ap($$anonymous$$esh mesh)
         {
             var newUvs = new Vector2[mesh.vertices.Length];
             for (var v = 0; v < mesh.vertices.Length; v++)
             {
                 var vertex = mesh.vertices[v];
                 var normal = mesh.normals[v];
                 // This gives a vector pointing up the roof:
                 var vAxis = Vector3.Scale(normal, new Vector3(-1, 0, -1)).normalized;
                 // This will make the u axis perpendicular to the v axis (ie. parallel to the roof edge)
                 var uAxis = new Vector3(vAxis.y, 0, -vAxis.x);
                 // I originally used vAxis here, but changed to position.y so you get more predticable alignment at edges.
                 // Set eaveHeight to the y coordinate of the bottom edge of the roof.
                 var uv = new Vector2(Vector3.Dot(vertex, uAxis), vertex.y - 0);
 
                 newUvs[v] = uv;
 
                 // You may need to scale the uv vector's x and y to get the aspect ratio you want.
                 // The scale factor will vary with the roof's slope.
             }
             mesh.uv = newUvs;
         }

alt text

roofuvmaptest.jpg (135.2 kB)
avatar image DMGregory · May 30, 2014 at 03:20 AM 2
Share

Sorry! Typo in my code. Should be z, not y:

var uAxis = new Vector3(vAxis.z, 0, -vAxis.x);

avatar image DMGregory · May 30, 2014 at 11:29 PM 2
Share

No bother. Sorry I didn't see it right away - I don't get very good notifications from UnityAnswers.

Using the y coordinate ensures that rows of shingles match up, and that the bottom edge of the roof always shows one full row, but the height/aspect ratio of each shingle varies with the slope of the roof segment. (That's what I meant about the scale factor above)

Try this ins$$anonymous$$d:

 var vertex = mesh.vertices[v];
 var normal = mesh.normals[v];
 
 // This will make the u axis run along the roof edge
 // (Same as before, I just collapsed the old "vAxis" into this line)
 var uAxis = (new Vector3(-normal.z, 0, normal.x)).normalized;
 
 // This will make the v axis run up the roof
 // (Rising off the xz plane, unlike the old one)
 var vAxis = Vector3.Cross(uAxis, normal);
 // I'm assu$$anonymous$$g the normal is already unit length, but you can normalize for safety.

 // Translates the UVs so the eaves sit at v = 0
 var eaveCorrection = Dot(vAxis, vertex + vAxis * (eaveHeight - vertex.y)/vAxis.y);
 
 var uv = new Vector2(Vector3.Dot(vertex, uAxis), Dot(vAxis, vertex) - eaveCorrection);
     
 newUvs[v] = uv;

This is just back-of-napkin, and I've got a bit of scotch in me, so I can't 100% guarantee it'll work... ^_^;

avatar image ivansi · May 31, 2014 at 12:12 AM 2
Share

Yeah! its working! Even with a bit of scotch! :D $$anonymous$$any thanks again! I'm still newbie with mathematics of vectors.

Not looking at the skeleton of the roof, the mapping was very good! Now I will study your code to learn more.

sample01, sample02

sample01.jpg (134.1 kB)
sample02.jpg (60.9 kB)
avatar image DMGregory · May 31, 2014 at 12:25 AM 3
Share

I'll run you through it:

The normal of the roof is a vector pointing out from it perpendicularly. By zeroing its y coordinate, we get a vector in the xz plane pointing in the downslope direction - think of it as the shadow cast by the normal when the sun is straight overhead.

The cross-slope direction (what we want as our u direction) also sits in the xz plane, at 90 degrees to this one. We can make such a vector by exchanging the x and z coordinates of the downslope vector and negating one of them. (This ensures that the dot product of the two vectors is zero: (x, 0, z) dot (-z, 0 x) = -xz + xz = 0, which is a mathematical way of saying "perpendicular")

We want our v axis to be perpendicular to the u axis (so our texture isn't skewed), and in the plane of the roof (perpendicular to the normal). The cross product gives us a vector perpendicular to its two inputs, so we get that almost for free from its definition.

Together, these two axes define a coordinate system for the plane of the roof. Dotting the position with each axis gives us the vertex position in this 2-dimensional roof coordinate system.

$$anonymous$$aking each vector unit length ensures that we're applying the same real-world scale to each texture axis, so we don't get the uneven aspect ratio of the earlier version.

The last bit is ensuring that the bottom of the texture lines up with the bottom of the roof. Here, we calculate what the v axis dot product would be for a vertex at the eaves, and subtract that from every vertex. That way vertices at the eaves get the correct v=0, and all other vertices are adjusted accordingly.

Show more comments
avatar image
1

Answer by ivansi · Feb 09, 2015 at 03:30 PM

Hey, i know it is offtopic but, I am very curoius what algorithm do you use for generating roof mesh because I am currently working on that too. Thanks :)

Hello Daviz, I use Straight Skeleton Algorithm to make the roof Skeleton, and after I use triangulation to make the Mesh.

Comment
Add comment · Show 1 · 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 dawnmichal · Aug 12, 2017 at 09:07 PM 0
Share

Would you be so kind and share your straight skeleton algorithm? I was searching in the internet for some time and can't found useful code or more detailed example. I'm working on my in game editor, whitch allow players to create their own maps. I have floors, walls, doors (windows) and now I need roofs ;) .... and I stuck here :D I appreciate any help in this regard. Thank you.

avatar image
-2

Answer by doodo · Sep 19, 2015 at 08:54 PM

easier to use this calculator http://myrooff.com/roof-pitch-calculator/

Comment
Add comment · 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

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

26 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

Related Questions

How do I assign a mesh's UVs so that part of a part of a mesh is one part of the texture and another part of the mesh is another part of the texture 0 Answers

Procedural Mesh UV Problem (UVs 'mirrored' instead of 'repeating') 0 Answers

Creating 3D representation of procedurally generated 2D tile map 1 Answer

Shared Meshes, do they auto-update? 0 Answers

UV problem on procedural mesh generation 1 Answer


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