Apply rotation to shader terrain

Hello i want to transform my shader plane terrain to asteroid, actualy i can create half asteroid like this :

i change vertex shader for have this with this code :

// get center of map
vec2 from_center = terrain_uv - vec2(0.5);
float center = dot(from_center, from_center);
// update z position
chunk_position.z += texture(ShaderTerrainMesh.heightfield, terrain_uv).x - (center * 25);

but i must to apply rotation for have complete asteroid, how to do it?
i need like this when I go beyond the middle :

  if (dot(from_center, from_center) > 0.25) {
    // rotate vertex and change z position

The default terrain shader should already be taking care of any rotation that is applied to the node using this line, which contains the effects of any setHpr() on the node:

gl_Position = p3d_ModelViewProjectionMatrix * vec4(chunk_position, 1);

So you could simply have eight terrain meshes and have different rotations applied to each node.

hello rdb, thanks for your help, I didn’t understand what you meant.
what is gl_position ? i try to modify this value but i have no effect
for example :
gl_Position = gl_Position - 90;

Actually you need 6 terrain meshes arranged as a cube and perform the following actions on each of them :

  • Rotate the mesh to the proper face of the cube
  • “Spherify” each faces (usually by normalizing the vertex before applying the transformation)
  • Rotate of flip the surface texture

Note that the heightmap used to displace the terrain must be also transformed to compensate the spherification process (and probably rotated or flipped to be consistent with the face orientation) or you will have distortion the closer you get to the face edges.

your solution seems rather complex
i found formula for unity engine :

but it’s for cube not 6 squares and I don’t know if a terrain mesh can handle such this transformation ?

Well, it describes exactly what I said, but much more verbosely :slight_smile: A cube is also 6 squares put together, so there is no differences, except here I explained how to use the ShaderTerrainMesh as you asked.

Btw, I talked also about distortion, the article explain a more complex procedure to spherify the cube with less (but still visible) distortion than simply normalizing the vertices.