• Godot Help
  • Planar Projection of a Sphere using Shaders

Hello Community,

I am very new to game engines and shaders in general and to Godot in particular. I am currently trying to use it in order to create a virtual environment for training Reinforcement Learning agents on visual navigation tasks. In order to do so, I need to extract a spherical 'all around' view of the world around the agents current position, preferably as a planar projection of the spherical view into a rectangle (e.g. Mercator projection)

So far I have managed to create a Cubemap from 6 ViewportTextures (associated with 6 cameras pointing in all directions), passing this Cubemap to a 3d shader as a samplerCube and use this shader to project the Cubemap onto the sphere surface like so

`shader_type spatial;
uniform samplerCube cubemap;

void vertex() {
}

void fragment() {
// get normalized vertex vectors in world coordinates
vec3 direction = normalize(INV_VIEW_MATRIX * vec4(VERTEX,1.0)).xyz;


// Sample from the cube map using the direction
vec3 color = texture(cubemap, direction).rgb;

// Output the color
ALBEDO = color/10.;

}`

This works fine so far. As far as I understand, the ALBEDO attribute now holds the color information of each fragment, whereas the UV attribute should hold the position of each fragment on the sphere in spherical (unit radius) coordinates.

Now comes the part where I am utterly stuck: Mathematically, it should be trivial to use these UV coordinates in order to draw the planar projection of the spherical surface, but I can't seem to manage to do so with shaders in Godot.
How do I access the UV coordinates of the shader?
Do I have to pass them to a secondary 2d shader? And if so, how do I do that?

In case this is very trivial, I apologise, as I said I am very new to all of this, but any help or hints would be very much appreciated.

  • xyz replied to this.

    lochikus Is it arbitrary plane in space or a screen (camera projection) plane?

      xyz In the end I would like to render it on a sprite2d, so I guess it would be screen plane right?

      • xyz replied to this.

        In the end, I am trying perform a mapping form the surface of the sphere into the plane (like a map of the earth). This map should then be rendered onto a sprite2d.

        • xyz replied to this.

          lochikus That's a strange thing to want to do 🙂. If you map something onto a sphere you already need to have it in a "planar" form in the first place, i.e. as a 2D texture.