Adam's Lair Forum

game development and casual madness
It is currently 2019/12/12, 00:12

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: 2016/09/04, 15:41 
Veteran Member
Veteran Member
User avatar

Joined: 2016/01/10, 10:56
Posts: 151
Location: Budapest
Role: Hobbyist
Hello,
The title basically says it: I'd like to use the world space (or object space, doesn't really matter) position of vertices in my shader, which is rendering a tilemap. The default vertex shader has this line:
Code:
gl_Position = ftransform();

But it returns the screen space position of the vertex. Searched it with google, but didn't succeed to find anything useful.
Thanks in advance!


Top
 Profile  
 
PostPosted: 2016/09/04, 15:46 
Site Admin
Site Admin
User avatar

Joined: 2013/05/11, 22:30
Posts: 2073
Location: Germany
Role: Professional
It's currently not possible to get the world space vertex position directly, mostly because the vertices that are passed to the backend are already transformed by the renderer on CPU side, using drawDevice.PreprocessCoords. This is one of the things that will change in v3.0.

Until then, you can reverse-engineer their world space position using builtin shader variables. The sample vertex shader from the basic shaders package does this step one of this, which is determining the scale. It also floats around the vertex a bit over time, ignore that part ^^

Edit: You'll also need the builtin CameraPosition field.

_________________
Blog | GitHub | Twitter (@Adams_Lair)


Top
 Profile  
 
PostPosted: 2016/09/04, 15:47 
Veteran Member
Veteran Member
User avatar

Joined: 2016/01/10, 10:56
Posts: 151
Location: Budapest
Role: Hobbyist
Okay, thank you for the quick reply ^^


Top
 Profile  
 
PostPosted: 2016/09/04, 16:58 
Veteran Member
Veteran Member
User avatar

Joined: 2016/01/10, 10:56
Posts: 151
Location: Budapest
Role: Hobbyist
Here's the vertex shader code if someone is interested:

Code:
vec3 reverse ()
{
   // Duality uses software pre-transformation of vertices
   // gl_Vertex is already in parallax (scaled) view space when arriving here.
   vec4 vertex = gl_Vertex;
   
   // Reverse-engineer the scale that was previously applied to the vertex
   float scale = 1.0;
   if (CameraParallax)
   {
      scale = CameraFocusDist / vertex.z;
   } else {
   // default focus dist is 500
      scale = CameraFocusDist / 500.0;
   }
   
   return vec3 (vertex.xyz + vec3 (CameraPosition.xy, 0)) / scale;
}


Top
 Profile  
 
PostPosted: 2016/09/04, 17:48 
Site Admin
Site Admin
User avatar

Joined: 2013/05/11, 22:30
Posts: 2073
Location: Germany
Role: Professional
loeller wrote:
Here's the vertex shader code if someone is interested:

:+1:

_________________
Blog | GitHub | Twitter (@Adams_Lair)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group