Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 SNS_Case · Aug 22, 2012 at 05:16 AM · meshscaledistancemanipulationinset

Creating a Mesh Inset by a Specified Distance

Alright, this is probably something fairly simple for those well-versed in mesh manipulation within Unity but I'm having trouble wrapping my head around a solution and haven't found anything else close enough to my problem to get me started in the right direction.

I have an existing mesh plane, which could be either a rectangle, or any convex/concave polygon in the end. What I need to do is create an identical mesh, and then effectively scale that mesh down to create a border of 'd' unit distance between the edges of the original and duplicate mesh. Does anyone have a best practice for doing this?

Thus far I have duplicated the original mesh, but I haven't worked with manipulating vertices/etc enough to just dive in without a little further direction. Normally, I would simply scale an object down by hand until it "looked right", but in this instance it needs to be inset dynamically at runtime by an input value, and be technically correct. Thanks for any help in advance!

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

3 Replies

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

Answer by SNS_Case · Apr 01, 2013 at 07:34 PM

For anyone else who comes across this question, I have since revised my method for creating a mesh inset by a specific distance. Unfortunately, I can't reasonably show the whole of the code here but I can give the general idea behind how I solved the problem. The primary issue is that the other answers were inaccurate for measuring from an edge. You could scale a vertex by x distance, but I needed offsets from the edge.

Some of the more common tools used were a "PointInPolygon" check and Line classes with intersection and other methods; for the most part these are pretty simple to find via google and aren't too hard to codify for whatever circumstance you may need. For my checks, only 2D checks were necessary which further simplified things.

The process involved taking the vertices of a mesh and then creating an exterior of Lines using those vertices. Doing a PIP check against the original mesh using the normal of each Line determined direction to offset (specified distance / unit of measurement used) regardless of winding direction, and the Line was moved away from its origin by that offset. This continues until all Lines have been iterated through. The new vertices are found by doing intersection checks against the Lines in order.

That pretty much sums it up! Feel free to post with any questions.

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

Answer by AlucardJay · Aug 22, 2012 at 08:43 AM

Here is a script that will take in a mesh, then scale it depending on the value given. I have not included scaling of the collider though this would be easy to add.

 #pragma strict
 
 public var theObject : Transform;
 private var theMesh : Mesh;
 private var originalVerts : Vector3[];
 private var scaledVerts : Vector3[];
 public var scaleFactor : float = 1.0;
 
 function Start() 
 {
     if (!theObject)
     {
        theObject = this.transform;
     }
 
     theMesh = theObject.GetComponent(MeshFilter).mesh as Mesh;
 
     originalVerts = new Vector3[ theMesh.vertices.length ];
     originalVerts = theMesh.vertices;
 
     scaledVerts = new Vector3[ originalVerts.length ];

     ScaleMesh();
 }
 
 function Update() 
 {
     if (Input.GetMouseButtonUp(0))
     {
        ScaleMesh();
     }
 }
 
 function ScaleMesh() 
 {
     for (var vert : int = 0; vert < originalVerts.length; vert ++)
     {
        scaledVerts[vert] = originalVerts[vert] * scaleFactor;
     }
     
     theMesh.vertices = scaledVerts;
 }

If you wanted to save the meshes to use in setting up rather than having them scaled at runtime, you could export the mesh from a 'helper scene' with something like AssetDatabase.CreateAsset( newObjectFilter.mesh, "Assets/Generated/My" + objectToSave.name + "_mesh.asset" );

I think this method may be slightly better, with my limited knowledge you won't be able to batch drawcalls on anything that has been scaled through the inspector or localScale.

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

Answer by Fattie · Aug 22, 2012 at 06:23 AM

SNS, are you aware you can very simply and easily scale any object in Unity, just using localScale?

You can do this at runtime and it will be technically correct.

http://docs.unity3d.com/Documentation/ScriptReference/Transform-localScale.html

It would be very difficult (ie months of work) to write code to re-make mesh on a smaller scale - and in the end it would just do what localScale does! Hope it helps


If you mean simply the problem of how to calculate what fraction you want, you use division to find a fraction. So you use division, with the new and old lengths.

 desiredLength = your desired length in meters
 var lengthNow:float;
 lengthNow = ourSprite.renderer.bounds.size.x;
 lengthNow will be the length now in meters
 
 transform.localScale.x = ( desiredLength / lengthNow );

OK ?

As I already said, almost certainly you are going to want to make the scale "about 0.9"

You will have to TRY THAT (takes maybe 10 seconds), and you will see if 0.9 is correct.

If it is not correct, try other values. For example, you could try 0.91.

Comment
Add comment · Show 2 · 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 SNS_Case · Aug 22, 2012 at 08:24 AM 1
Share

Thank you, what about scaling it by distance from an edge though? Is there a way I can calculate the difference in scale in a physical manner (i.e. an object with a scale of 1 is not likely to be a single unit in size) while using localScale? That was my initial idea, but doesn't allow me to specify the actual distance between old/new mesh edges, it's much more like a percentage. With variable scales/shapes per mesh, the method I use will have to be able to deter$$anonymous$$e a difference by in-engine units.

$$anonymous$$y most recent thought is to do this by using the bounds.size somehow and figuring by displacement, but I'm still not sure how that would work. Any ideas?

avatar image SNS_Case · Aug 22, 2012 at 06:30 PM 1
Share

Thanks Fattie, that seemed to do the trick for now, I was definitely over-thinking the problem. I just localScale'd it like you said using a difference of the border*2 from its bounds, and all the numbers match up. I appreciate it!

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

8 People are following this question.

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

Related Questions

Scale mesh between two Vectors 1 Answer

Unrealistic size of sun, viewed from earth 2 Answers

Bake mesh scale. 1 Answer

spawned object wont change scale? 1 Answer

Baking the distance from a point along faces 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