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 Caiuse · Dec 04, 2012 at 01:40 AM · meshheightmap

Heightmap on primitive plane

I've written a small function that attempts to assign a heightmap data from a procedurally generated image to the plane. The problem I'm having is that the heightmap results are not related to the procedurally generated image.

I've attached an image to demonstrate this:

![alt text][1]

I think I may be missing something vital to do with assigning height map data to displace the verts.

I've posted my function below.

any ideas? C

 public void GenerateHeightmap() {
     
     Texture2D heightMap = renderer.material.mainTexture as Texture2D;
     Mesh mesh = GetComponent<MeshFilter>().mesh;
     Vector3[] baseVertices = mesh.vertices;
     
     print((float)baseVertices.Length * 512f);
     
     //return;
         
     Vector3[] vertices = new Vector3[baseVertices.Length];
     print(baseVertices.Length);
     print(baseVertices[baseVertices.Length - 1]);
     int i = baseVertices.Length - 1;
     
     int y = 0;
     int x = 0;
     int numvertici = (int)Mathf.Sqrt(baseVertices.Length);
     //float risoluzionehmp = heightMap.height;
     int counter = 0;
     
     for (y=numvertici;y>0;y--)
     {
         for (x=0;x<numvertici;x++)
         {
             float pixelHeight = heightMap.GetPixel(x, y).grayscale;
             counter++;
                    print(pixelHeight);
             
             Vector3 vertex = baseVertices[i];
             vertex = vertex + new Vector3(vertex.x,vertex.y,vertex.z - pixelHeight);
             vertices[i] = vertex;
             i--;    
         }
     }
     mesh.vertices = vertices;
     mesh.RecalculateNormals();
     mesh.RecalculateBounds();
 }
screen shot 2012-12-04 at 01.37.43.png (240.3 kB)
Comment
Add comment · Show 6
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 whydoidoit · Dec 04, 2012 at 01:32 PM 0
Share

So the plane shares all of its vertices between triangles? In your line:

  vertex = vertex + new Vector3...

you are effectively doubling the vertex (albeit the z is only set to 2z - pixelHeight)

avatar image Caiuse · Dec 04, 2012 at 01:51 PM 0
Share

Please could you expanded on that, I think I'm not sure I grasp the concept of heightmapping very well. You have any links for further reading?

avatar image whydoidoit · Dec 04, 2012 at 01:56 PM 0
Share

Well so

1: If you plane is made of triangles then it is possible that individual triangles do not share vertices even though they share vertex locations (so there are 2 vertices in the same physical place). It depends on how the plane was created. Secondly - it is very likely that the vertices in the plane are not laid out in neat y/x order!

So to circumvent this the best idea would be to iterate through all of the vertices - convert their model space coordinates to height map coordinates and then read in the height map data - this allows the vertices to be in any order.

2: At the moment you are adding the vertex to itself - that's can't be right. Presumably you just want to add the offset data so your line should read:

    vertex = vertex - Vector3.up * pixelHeight;

or if you prefer

    vertex = vertex - new Vector3(0,0,pixelHeight);
avatar image whydoidoit · Dec 04, 2012 at 01:57 PM 0
Share

Also are you really modelling height in the Z dimension? Normally I have height as Y.

avatar image whydoidoit · Dec 04, 2012 at 02:05 PM 0
Share
 //Work out the model space size of the plane
 var $$anonymous$$X = float.$$anonymous$$axValue;
 var $$anonymous$$Y = float.$$anonymous$$axValue;
 var maxX = float.$$anonymous$$inValue;
 var maxY = float.$$anonymous$$inValue;
 var vertices = mesh.vertices;
 for(var v = 0; v < vertices.Length; v++)
 {
      var vertex = vertices[v];
      $$anonymous$$X = $$anonymous$$athf.$$anonymous$$in(vertex.x, $$anonymous$$X);
      $$anonymous$$Y = $$anonymous$$athf.$$anonymous$$in(vertex.y, $$anonymous$$Y);
      maxX = $$anonymous$$athf.$$anonymous$$ax(vertex.x, maxX);
      maxY = $$anonymous$$athf.$$anonymous$$ax(vertex.y, maxY);
 }

 for(var v = 0; v < vertices.Length; v++)
 {
      var vertex = vertices[v];
      vertex -=  Vector3.up *  height$$anonymous$$ap.GetPixel($$anonymous$$athf.FloorToInt(height$$anonymous$$ap.width * (vertex.x - $$anonymous$$X)/(maxX - $$anonymous$$X))),  $$anonymous$$athf.FloorToInt(height$$anonymous$$ap.height * (vertex.y - $$anonymous$$Y)/(maxY - $$anonymous$$Y)))).grayscale * someScalingFactor;
      vertices[v] = vertex;
 }
Show more comments

2 Replies

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

Answer by Bunny83 · Dec 04, 2012 at 03:04 PM

When the positive y axis is your planes normal vector (the buildin plane in Unity uses y), just do this:

 baseVertices[i] += Vector3.up * pixelHeight * desiredMaxExtrude;

Vector3.up is just this vector: new Vector(0,1,0) so here's the long form:

 baseVertices[i] = baseVertices[i] + new Vector3(0, pixelHeight * desiredMaxExtrude, 0);

Another alternative would be:

 baseVertices[i].y = baseVertices[i].y + pixelHeight * desiredMaxExtrude;
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 alessandro_88 · Apr 19, 2014 at 05:49 PM

dear caiuse, could you please post the final working code? thanks a lot! :)

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

12 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

Related Questions

3D Tilemap 1 Answer

using grayscale texture as heightmap, vertices and pixel don't match 0 Answers

Why would a mesh, by default, be rotated 180 on the X / Z axis?,Why would a texture / mesh be rotated on the z / x and invert all height values by defaul? 0 Answers

Import a 513x513 Texture2D (heightmap) 0 Answers

Terrain Clone 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