Unity Community


Results 1 to 7 of 7

  1. Posts
    3

    Spherical Heightmap

    Greetings everyone,
    I've been looking for spherical terrains, spherical heightmap and procedural planets.
    All what i've got are just suggests on how to, without many examples or code easy to understand
    (hell yes, i'm new in Unity). I combined two scripts i got on this website, my goal would be:

    1) Generate a simple heightmap on a plane.
    2) Morph it into a sphere.

    The crappy code is here, if some guru can help would be very appreciated. Thanks.

    Code:  
    1. // This script is placed in public domain. The author takes no responsibility for any possible harm.
    2.  
    3. var heightMap : Texture2D;
    4. var material : Material;
    5. var size = Vector3(200, 30, 200);
    6. var collision : boolean;
    7.  
    8. var radius: float;
    9. var numSegments: int;
    10. var numStrips: int;
    11.  
    12. function Start ()
    13. {   
    14.     GenerateHeightmap(radius, numSegments, numStrips);
    15. }
    16.  
    17. function GenerateHeightmap (radius: float, numSegments: int, numStrips: int)
    18. {
    19.     // Create the game object containing the renderer
    20.     gameObject.AddComponent(MeshFilter);
    21.     gameObject.AddComponent("MeshRenderer");
    22.     if (material)
    23.         renderer.material = material;
    24.     else
    25.         renderer.material.color = Color.white;
    26.  
    27.     // Retrieve a mesh instance
    28.     var mesh : Mesh = GetComponent(MeshFilter).mesh;
    29.    
    30.     //  The number of rings of vertices is one greater then the number of
    31.     //  horizontal strips and each ring has two vertices at the seam.
    32.     var numYLevels = numStrips + 1;
    33.     var numRingSteps = numSegments + 1;
    34.  
    35.     var width : int = Mathf.Min(heightMap.width, 255);
    36.     var height : int = Mathf.Min(heightMap.height, 255);
    37.     var y = 0;
    38.     var x = 0;
    39.  
    40.     // Build vertices and UVs
    41.     var vertices = new Vector3[height * width];
    42.     var uv = new Vector2[height * width];
    43.     var tangents = new Vector4[height * width];
    44.    
    45.     var uvScale = Vector2 (1.0 / (width - 1), 1.0 / (height - 1));
    46.     var sizeScale = Vector3 (size.x / (width - 1), size.y, size.z / (height - 1));
    47.    
    48.     for (y=0;y<height;y++)
    49.     {
    50.         for (x=0;x<width;x++)
    51.         {
    52.             var pixelHeight = heightMap.GetPixel(x, y).grayscale;
    53.             var vertex = Vector3 (x, pixelHeight, y);
    54.             vertices[y*width + x] = Vector3.Scale(sizeScale, vertex);
    55.             uv[y*width + x] = Vector2.Scale(Vector2 (x, y), uvScale);
    56.  
    57.             // Calculate tangent vector: a vector that goes from previous vertex
    58.             // to next along X direction. We need tangents if we intend to
    59.             // use bumpmap shaders on the mesh.
    60.             var vertexL = Vector3( x-1, heightMap.GetPixel(x-1, y).grayscale, y );
    61.             var vertexR = Vector3( x+1, heightMap.GetPixel(x+1, y).grayscale, y );
    62.             var tan = Vector3.Scale( sizeScale, vertexR - vertexL ).normalized;
    63.             tangents[y*width + x] = Vector4( tan.x, tan.y, tan.z, -1.0 );
    64.         }
    65.     }
    66.    
    67.     // Assign them to the mesh
    68.     mesh.vertices = vertices;
    69.     mesh.uv = uv;
    70.  
    71. // --------------------------------------------------------------------------------------------------------------   
    72. //  Store the normalised X and Z values of a ring for repeated usage (this
    73. //  saves a few Cos and Sin calls overall).
    74.     var ring = new Vector3[numRingSteps];
    75.    
    76.     var fracCircle: float;
    77.    
    78.     for (i = 0; i <= numSegments; i++) {
    79.         fracCircle = (i * 1.0) / (numSegments * 1.0);
    80.         var angle = fracCircle * 2.0 * Mathf.PI;
    81.         ring[i].x = Mathf.Sin(angle);
    82.         ring[i].z = Mathf.Cos(angle);
    83.     }
    84.  
    85.  
    86. //  Calculate the vertex positions from the parameter values. The vertex position
    87. //  is the same as the normal vector multiplied by the radius and the UV is easily
    88. //  obtained from the parameter values.
    89.  
    90.     for (l = 0; l <= numStrips; l++) {
    91.         var fracArc = (l * 1.0) / (numStrips * 1.0);
    92.         var yCoord = Mathf.Cos(fracArc * Mathf.PI);
    93.         var radFrac = Mathf.Sin(fracArc * Mathf.PI);
    94.        
    95.         for (s = 0; s <= numSegments; s++) {
    96.             fracCircle = (s * 1.0) / (numSegments * 1.0);
    97.             var normVert = ring[s] * radFrac;
    98.             normVert.y = yCoord;
    99.             vertices[l * numRingSteps + s] = normVert * radius;
    100.         }
    101.     }
    102.     // --------------------------------------------------------------------------------------------------------------
    103.  
    104.     // Build triangle indices: 3 indices into vertex array for each triangle
    105.     var triangles = new int[(height - 1) * (width - 1) * 6];
    106.     var index = 0;
    107.     for (y=0;y<height-1;y++)
    108.     {
    109.         for (x=0;x<width-1;x++)
    110.         {
    111.             // For each grid cell output two triangles
    112.             triangles[index++] = (y     * width) + x;
    113.             triangles[index++] = ((y+1) * width) + x;
    114.             triangles[index++] = (y     * width) + x + 1;
    115.  
    116.             triangles[index++] = ((y+1) * width) + x;
    117.             triangles[index++] = ((y+1) * width) + x + 1;
    118.             triangles[index++] = (y     * width) + x + 1;
    119.         }
    120.     }
    121.    
    122.     // And assign them to the mesh
    123.     mesh.triangles = triangles;
    124.        
    125.     // Auto-calculate vertex normals from the mesh
    126.     mesh.RecalculateNormals();
    127.    
    128.     // Assign tangents after recalculating normals
    129.     mesh.tangents = tangents;
    130.    
    131.     mesh.vertices = vertices;
    132.    
    133.     // Assign collision
    134.     if (collision == true) {
    135.     }
    136. }

  2. Volunteer Moderator
    Posts
    23,693
    I think a better thing to do would be to make 6 heightmaps, and make a cubesphere out of them. I doubt you can get a heightmap to map usefully to a sphere (without distortion).

    --Eric
    SpriteTile: new tile system that works seamlessly with Unity 4.3 sprites
    FlyingText3D: dynamic 3D text with TTF fonts | Vectrosity: fast & easy line drawing
    Nifty utilities! Stitch terrains together - runtime model importing - file browser - fractal landscapes


  3. Posts
    3
    Quote Originally Posted by Eric5h5 View Post
    I think a better thing to do would be to make 6 heightmaps, and make a cubesphere out of them. I doubt you can get a heightmap to map usefully to a sphere (without distortion).

    --Eric
    Thank you for the answer.
    Do you know where i can find some examples about this method? I mean a package or a tutorial. As i said I'm really new in Unity.
    Last edited by Sidus; 06-15-2011 at 05:38 PM.


  4. Location
    Germany
    Posts
    906
    the distortions/anomalies at the poles of a lat/long sphere are not that disturbing.

    search the forums. andeeee has posted a script for a parametrical sphere as the unity built in sphere has some serious uv issues at the poles.

    then you must create some noise for surface details and put it in a texture which is mapped on the sphere.

    perlin noise is widely used but i suggest simplex noise. always head for 3d noise because your sphere is a 3d body and with 2d noise you get into problems with tiling. with 3d noise simply take the positions of the pixels on the sphere to calculate the noise there and when you always pick the octaves of noise from a sphere with different radius for different frequenzies/wavelength you have no problem with tiling.

    i'm currently working at a procedural planet generator which i will sell at the assetstore in some months when things go well.


  5. Posts
    3
    Quote Originally Posted by exiguous View Post
    the distortions/anomalies at the poles of a lat/long sphere are not that disturbing.

    search the forums. andeeee has posted a script for a parametrical sphere as the unity built in sphere has some serious uv issues at the poles.

    then you must create some noise for surface details and put it in a texture which is mapped on the sphere.

    perlin noise is widely used but i suggest simplex noise. always head for 3d noise because your sphere is a 3d body and with 2d noise you get into problems with tiling. with 3d noise simply take the positions of the pixels on the sphere to calculate the noise there and when you always pick the octaves of noise from a sphere with different radius for different frequenzies/wavelength you have no problem with tiling.

    i'm currently working at a procedural planet generator which i will sell at the assetstore in some months when things go well.
    If you saw my script, Paramsphere.js by Andeeee was already "implemented". Paramsphere.js and HeightmapGenerator.js from Procedural Examples was the two examples i used. Now, as i said two times, i'm a newbie regard procedurals so any method NOT documented by some examples or anything related to it isn't an help for me at all. Maybe do you have a starting example (code or package) on how would be possible spherify a simple heightmap keeping its relief around the sphere?

    Note: Till now i used 3ds Max in order to load my heightmaps directly via displace modifier, so to automatically generate an heightfield around a sphere. I think it is a good method but it loose details because vertices optimizations, so i thought could be better generate it procedurally.

    PS. If you didn't understand what i said, is my fault, English isn't my main language, sorry.


  6. Location
    Germany
    Posts
    906
    Quote Originally Posted by sidus
    If you saw my script, Paramsphere.js by Andeeee was already "implemented".
    i have noticed it afterwards.

    Quote Originally Posted by sidus
    Now, as i said two times, i'm a newbie regard procedurals so any method NOT documented by some examples or anything related to it isn't an help for me at all.
    some weeks ago i had also been a newbie in this field. only if you work with the stuff you may get above newbie level. if you just pick a ready implementation you probably wont. there are also enough examples and tutorials outside which are not unity specific but the principle is the same. a bit thinking is still required though.

    Quote Originally Posted by sidus
    Maybe do you have a starting example (code or package) on how would be possible spherify a simple heightmap keeping its relief around the sphere?
    i just have code which depends on many stuff and as i said i want to sell it. publishing it for free would be something between unwise and stupid.

    Quote Originally Posted by sidus
    English isn't my main language
    same applies for me.

    sorry when i can't be of more help but this is a complicated and wide field. so you probably wont find a ready made solution which fits all your needs. i also started from scratch and in 1 or 2 weeks you could have something ready.
    if you have detailed questions there are many people to help you. but its always a bit probelamtic to request other people doing the work for you (which includes internet search for sources/tutorials etc.).

    for your two goals/questions from the first post i had give you some hints and advises in my previous post. what you make out of it is up to you.


  7. Location
    Brazil, SP
    Posts
    126
    You could also start with a geodesic sphere (mesh or built in run time) and then move each vertex along its original normal by some noise (perlin works great here).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •