Hi, everyone. I’m trying to create a particular shader but I have only a little bit of experience actually writing shaders so I wanted to ask for advice first. What I’m trying to do is texture a sphere with a cubemap instead of the standard texture map. There are a few reasons why I want to do this:
- I want to prevent the waste of texture detail and ugly texture pinching that occurs near the poles of a traditionally textured sphere.
- I want to give the user the ability to alter the texture while it’s mapped to the sphere. Any markings the user puts on a texture mapped in the traditional way will be distorted near the poles.
I think the only way to use a cubemap like this is to write a shader for it. Is that true and if so, is it possible? Where should I start?
I don’t think a shader is the right way to go here (but, I’m terrible at shaders, so I’m rather biased.)
I would put the cubemap on a script. The script would take those 6 textures and generate one tiled texture (you’d probably have to have it be 4x2 with 2 wasted spots due to power of two requirements). Then, I’d either generate a sphere mesh or modify the existing one so that the UVs tile the way you want the map to be rendered. You’d probably have to generate one.
The problem I think with using a shader is that you probably won’t be able to get it into one draw call with 6 separate textures. And you’d need to change the UVs anyway.
Is this really true? My understanding of rendering speed is hazy at best but I can’t imagine anyone wanting to use cubemaps if they require six draw calls.
No actually cubemaps are a special type of texture that is sampled by giving a directional vector instead of a 2 component position.
You should download the builtin shaders and look for the cubemapped ones. If you can get past the UsePass BS and other things that make it hard to figure out what is actually going on, you will see a part in the fragment program where they sample the cubemap using the view direction bounced off the normal. You want to instead just use the model space normal to sample the cubemap.
I decided to go with a mesh-based solution (like StarManta suggested) because I wanted to learn how to create a mesh like this. The idea for this particular approach was actually my co-worker’s, so I can’t take credit for it, but I will explain it here for the public good.
The mesh was created as a cube with an arbitrary number of vertices. I made each face a submesh textured with the corresponding cubemap face. This got me around the problem suggested by StarManta of tiling all the faces in the same texture and therefore limiting the resolution of each face.
After the vertices of the cube were assigned, I normalized them to create a sphere. With the textures set to clamp the seams become invisible and the effect looks perfect.