Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Cube mapped sphere (aka quad sphere) asset?

Discussion in 'General Graphics' started by JoeStrout, Jan 22, 2015.

  1. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I'm working with a team of students, and we're going to need some textured spheres that have nice detailed textures even at the poles. The obvious answer is a quad sphere, or as many folks seem to call it, a cube-mapped sphere. These provide artifact-free textures on all sides.

    But while I can find lots of people talking about it, I can't find such a cube-mapped sphere asset available anywhere. I've searched the googles as well as the asset store. I can see how to go about making it myself... But it's such an insanely useful alternative to the default sphere, it's hard to believe it's not already out there.

    Anybody know of something I'm missing?

    Thanks,
    - Joe
     
  2. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Perhaps, that you're thinking you want one thing, when you want three?
    1. Cubemap
    2. Sphere mesh
    3. Environment mapped material
    These should be easy to come by; Unity comes with at least 2/3.
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    No, I'm pretty sure I want one thing: a sphere whose UVs are set up according to the quad sphere mapping (as opposed to the Unity default sphere, which uses something like a cylindrical mapping, and so can't represent the poles very well).

    It's also known as the Quadrilateralized Spherical Cube. If you wanted to represent an Earth globe this way, the textures would look like either of these:

    This image is from this explanation.

    Basically, the idea is that this is a very simple way to break a sphere into six identical curved surfaces, each of which is approximately a square, and so can be mapped with six textures in a very straightforward way with very little distortion. It's commonly used in space games for rendering planets... though for the project my students are working on, we're going to use it for something like the KSP navball.

    I just can't believe there's not some off-the-shelf solution for this.
     
    domkia likes this.
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Hah! Found one. In the Procedural Examples asset from Unity, the Sources/Artwork/sphere.fbx model is exactly this sort of sphere, and texture mapped in a reasonably sensible way.

    Saves me some work for sure.

    Thought I'd share in case somebody else looking for this someday might stumble across this.

    Incidentally, the artist called it a "subdivision sphere" — I guess that's another term to search for.

    Cheers,
    - Joe
     
    PrimalCoder and EastOfEden like this.
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    OK, after more study, I decided I didn't like the UV mapping in that model, so ended up making my own after all.

    Mine uses a more standard arrangement of the faces, with the four faces around the equator all in a line, and the two pole faces hanging off the top and bottom, like this:


    Here's the result within Unity... note that we're looking in at the north pole here, and everything is awesome!


    Finally, here's what it looks like with just the blank UV template as a texture, so you can see the shape of the mesh:

    I should probably write this up on my blog, or find some other place to post it. But with the Global Game Jam starting tomorrow, it's probably not going to happen before next week! If anybody wants this before then, just drop me a note.

    Cheers,
    - Joe
     
  7. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    I think you disregarded my answer because you didn't know what environment mapping is.

    I like that you've got drive, but you need to abandon these 2D mapmaking concepts. You're dealing with a sphere. Why would you want to give it 2D texture coordinates? UVs are not actually the solution anyone wants for any mesh; they've just been practical because computers haven't been powerful enough to handle anything more human-usable. A cubemapped sphere is fortunately a case where you don't need to bother with them. You'll still have uneven texel density, but you seem to be fine with that.
    1. Get a cubemap.
    2. Get a sphere. Its topology and UVs don't matter.
    3. Use vertex positions as the lookups into the cube map, in your shader. You don't even need to bother normalizing them. Anything you do with texture coordinates will be wasted time, cognitive resources, and data. And it will look worse.
    Anything you do with texture coordinates will be wasted time, cognitive resources, and data. And it will look worse.
    As it is now, you'd have to manually UV any object that you want to use your texture, and you're wasting a ton of texel data. Cubemapping solves this. Break your idea up into reusable chunks.
     
    Last edited: Jan 23, 2015
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Wow, hostility! That seems undeserved. In fact I do know what environment mapping is. I abandoned your answer because it didn't seem pertinent to my question. (To a different question, it might have been a great answer.) I figured my question was just unclear, so I attempted to clarify it.

    This is an interesting view of things. I see it differently. UVs are integral to texture mapping, and texture mapping is integral to computer graphics. They also very nicely fit people's mental model. In the real world, they have a ball with some pattern painted on it. They want to have the same thing in the computer. How do they do that? They wrap a texture around it.

    The only problem with the built-in sphere is that it uses a cylindrical mapping, so (1) you can't have different textures on the two sides (since it's set up to repeat the texture twice horizontally), and (2) there is extreme distortion near the poles.

    It's a clever idea, but way more complicated than simply importing a model and customizing the texture. I'm working with middle-school students here. I'm not going to teach them ShaderLab — not this year, anyway.
    No, it will look exactly the same, assuming your custom shader does the same thing (apart from how it looks up the texture color) as whatever shader we pick with the quadsphere approach. But the quadsphere approach has the advantage that we can pick any of the many built-in shaders that take a texture (with or without a bump map, etc.), and change this at any time or in different situations, without having to muck about in custom shader land.

    There's only one object, and I finished the UV mapping last night. The same object can be used for the navball, and for any planets we might want to make — including lumpy distorted ones (which we can easily get by applying a noise function to the vertex positions). You're right about the texel data; I did consider a custom shader... but then I decided I didn't care. In the grand scheme of things, it's not that much (we're using 512x512 textures here).

    And hey, let's remember, we're all on the same team here... Unity rocks, in large part because of its great community. We can differ in our opinions and what solutions we consider best, without being jerks about it.

    Best,
    - Joe
     
    EastOfEden likes this.
  9. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325

    Attached Files:

    Last edited: Jan 23, 2015
    iSinner likes this.
  10. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    Hey Joe what's wrong with unwrapping a regular sphere in any 3d package and applying a simple 2D texture?
    I believe NASA offers free to use high resolution planet textures to map just fine onto unwrapped spheres.
    The process you detailed seems overly complicated for - as you said - middle school students, where unwrapping and mapping a 2D texture seems like a simple process for the up and coming generation to follow.

    Though your approach is interesting I don't see the need to complicate matters beyond the simple (optimized) solution of the unwrap that solves acceptably (for teaching kids anyway) the minor issue (distortion at the poles).

    Just asking NICELY :) for my own knowledge. Thanks
     
  11. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Well, that depends on what you mean by "a regular sphere" (and where you intend to get it). If the geometry is like Unity's built-in sphere, then these have singularities at the poles that mean you can't get good detail at the poles. Maybe fine if you're making something where you don't care — e.g. a planet with fairly featureless icecaps on the poles — but a big problem if you're making something that requires detail on all sides, like the navball my students need to make.

    (And if it's UV mapped like the Unity one, you can't even put a different texture on both sides.)

    And if you mean a sphere that doesn't have that sort of geometry... well, that's exactly what I've done.

    I'm not sure what you mean by that; there are many different ways to unwrap (map) a sphere. The quad sphere is a fairly commonly used one, because it doesn't suffer from the problems of other mappings, and I posted a NASA-derived planet texture in that format above. It works great.

    I'm not sure what you mean. Mapping a 2D texture onto a sphere is all we're doing. It's just that, unlike the built-in sphere, I've made one that unwraps in a way that works well from all directions. It's also much easier for the students to work with this texture, since the texel sizes are approximately equal — unlike the Unity mapping, where texels around the equator are huge compared to ones near the poles.

    Well, one of us doesn't understand the other. :) There's nothing complicated about what I've done, and to me, the distortion at the poles is a major issue that would be very complicated (darn near impossible) to work around. I've got instead a very simple solution, where you just paint your texture in the standard box-map format, and it wraps neatly around the sphere with almost no distortion at all.

    Cheers,
    - Joe
     
    CD-WR and EastOfEden like this.
  12. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    You're outnumbered 2 to 1 on that opinion so far.
    It's solved for any convex mesh in that package I gave you.
     

    Attached Files:

  13. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    This is what I mean by a regular polygonal sphere. Most 3D packages have this basic standard primitive.
    upload_2015-1-27_18-25-10.png


    No - I understand the Unity sphere doesn't have proper UV's.

    But I recall several years ago creating an animated logo with the earth in it and unwrapping a regular sphere like the one above without having to cube a sphere or sphere a cube - no pun intended. Though with my brain being filled up several times over since then - at that time I created that logo I may have resorted to slight (only visible to the meticulous artist [as most of us are] ) seams at the equator.

    Hope your instruction is well received. I'm sure if we were in person I'd understand the concept better.

    It's probably me. :) I'll save this post for later reference.
     
  14. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    OK, @theANMATOR2b, thanks for the nifty picture! That does clear things up nicely.

    So, yes, you're right, that's the standard sphere geometry in most modeling packages. It doesn't work for our purposes because of the issue with the poles, no matter how it's UV mapped. It does work for some purposes, when the poles don't have any important detail.

    But I dig the quad sphere because it's really no more complex, but has nice property that it's easy to map without distortion, no matter which way you look at it.

    And note that while I had to poke around in Cheetah3D a bit to make it, my students won't have to — I'm just going to say, "This is a quad sphere, and here's an example texture map. Now go make it into what you need."

    That'll happen at our next meeting, which is tomorrow... I'll let you know how it goes!

    Cheers,
    - Joe
     
    theANMATOR2b likes this.
  15. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Joe cant stand to be wrong, and I can't stand that he believes that his students are stupid, so I'm putting him on block. Hopefully my package helps someone else reading this.
     
  16. PixelMind

    PixelMind

    Joined:
    Aug 16, 2013
    Posts:
    101
    Got to agree with Jessy here. Using cubemap for a case like this is the standard approach. It also works on any mesh automatically.

    If you manually map a sphere with custom uv's like that then you are quite likely to see mipmap errors at the uv edges. Or you have to do extra step of manually expanding the edges on your texture to hide it. For teaching purposes this is probably fine. It does have a benefit of being able to paint one texture map instead separating to 6 textures.

    But if someone is reading this and want to use it for a game then it's definitely better to use Jessy's shader instead of doing custom mapping manually.
     
  17. iSinner

    iSinner

    Joined:
    Dec 5, 2013
    Posts:
    201
    I was struggling with this problem (mapping a sphere w/o distortion) so much, and the solution was so simple!
    Thank you, it did help a lot after all!
     
    JoeStrout likes this.
  18. srabutdotcom

    srabutdotcom

    Joined:
    Apr 27, 2015
    Posts:
    1
    Hi Joe,

    This is what I am looking for. Could you share how to do this, like creating cube sphere and creating earth map. I would like to apply it for moon surface and other planet.

    We can communicate thru my email handoko75@gmail.com

    Regards
    Handoko
     
  19. ristophonics

    ristophonics

    Joined:
    May 23, 2014
    Posts:
    32
    I am doing this manually in photoshop and Rhino+Grasshopper.. taking ages.. esp with LOD cubemapprocess1.jpg
     
  20. superdudeman

    superdudeman

    Joined:
    Sep 16, 2013
    Posts:
    3
    ina likes this.
  21. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Wrote it from scratch, and completely untested, but should work exactly the same.

    Code (csharp):
    1. Shader "CubeMap Texture" {
    2.     Properties {
    3.         _CubeMap ("Cube Map", Cube) = "white" {}
    4.     }
    5.     SubShader {
    6.         Pass {
    7.             Tags { "DisableBatching"="True" }
    8.             CGPROGRAM
    9.             #pragma vertex vert
    10.             #pragma fragment frag
    11.             #include "UnityCG.cginc"
    12.  
    13.             samplerCUBE _CubeMap;
    14.  
    15.             struct v2f {
    16.                 float4 pos : SV_Position;
    17.                 half3 uv : TEXCOORD0;
    18.             };
    19.  
    20.             v2f vert (appdata_img v) {
    21.                 v2f o;
    22.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    23.                 o.uv = v.vertex.xyz * half3(-1,1,1); // mirror so cubemap projects as expected
    24.                 return o;
    25.             }
    26.  
    27.             fixed4 frag (v2f i) : SV_Target {
    28.                 return texCUBE(_CubeMap, i.uv);
    29.             }
    30.             ENDCG
    31.         }
    32.     }
    33. }
    Super simple shader as you can see. Pretty much the same as a normal 2d textured unlit shader just with "2D" replaced with "CUBE" and the uv is a half3 instead of a float2 and is a copy of the unmodified model vertex positions instead of the model's UVs.

    edit: Cleaned up the shader a little and made it actually work (was missing SV_Target originally).
    cubemaptexture.png
    Using the same cubemap as @Jessy referred to from this link:
    https://mycodingwrongs.wordpress.com/2010/07/24/reprojecting-blue-marble/
    https://mycodingwrongs.files.wordpress.com/2010/07/world_cube_net.png
     
    Last edited: Jul 14, 2016
  22. superdudeman

    superdudeman

    Joined:
    Sep 16, 2013
    Posts:
    3
    Thanks! That was a lot simpler than expected.
     
  23. scottunity

    scottunity

    Joined:
    Feb 1, 2014
    Posts:
    72