Is it possible to access the draw() position in a shader?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Lugen
Prole
Posts: 24
Joined: Mon Nov 10, 2014 8:36 am
Location: Sweden

Is it possible to access the draw() position in a shader?

Post by Lugen »

Greetings everyone. Been a lurker for a good while but couldn't find any hints to this specific question so I thought it was about time I registered. Been working with Love2D for about half a year I think and I'm getting pretty deep into it as well.

I'll post my current project somewhere at a later time. My question is quite general so no need for any .love-file I think.

So to my question...

In love.graphics.draw() you can assign a position offset for the object being drawn. Can you access this position in a shader? Is it included in any of the accessible built-in matrices in Löve perhaps? From what I've tried it doesn't seem like it.

What I'm trying to accomplish is a vertex shader in which all camera translation and scaling are performed. All my values in the Lua-code are meant to be in meters and I want to push the actual pixel-position calculation to the very last in the pipe-line in order to have a more flexible and manageable system.

The scaling is easy. I just have a variable that tells how many pixels a meter is on the screen and multiply that value with the vertex position.
The issue is that the vertex position is local in relation to the object containing the mesh. The object has its own position which is used in love.graphics.draw(). I want to access this position in the vertex shader and scale it up as well.

Here's how the vertex shader looks right now. Objects are drawn in correct size but not with the correct position in relation to the screen. As the positions are in meters they are basically drawn at the top left area of the screen.

Code: Select all

// Camera position in screen space, updated every frame
	extern vec3 positionCamera;

// How many pixels a meter is on the screen, could be used for zooming
	extern float unit;
	
// Vertex shader
	vec4 position(mat4 transform , vec4 positionVertex)
	{
	// Offset the vertex with camera position in screen space
		positionVertex.xy -= positionCamera.xy;

	// Scale the vertex position in order to draw the object at correct size in relation to the screen
		positionVertex.xy *= unit;

		return transform * positionVertex;
	}
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Is it possible to access the draw() position in a shader

Post by s-ol »

It is part of the transform matrix, but I doubt you will be able to extract it if it's mixed with the projection matrix already.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
Lugen
Prole
Posts: 24
Joined: Mon Nov 10, 2014 8:36 am
Location: Sweden

Re: Is it possible to access the draw() position in a shader

Post by Lugen »

S0lll0s wrote:It is part of the transform matrix, but I doubt you will be able to extract it if it's mixed with the projection matrix already.
I see. Tried multiplying it with a single value but only the scale was affected, but maybe I'm missing something concerning matrix math.

In the meantime I'll just send the objects position to the vertex shader.

Code: Select all

// From scene
	extern vec3 positionCamera;
	extern vec3 positionObject;
	extern float unit;
	
// Vertex shader
	vec4 position(mat4 transform , vec4 positionVertex)
	{
		positionVertex.xy += positionObject.xy - positionCamera.xy;
		positionVertex.xy *= unit;

		return transform * positionVertex;
	}
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Is it possible to access the draw() position in a shader

Post by s-ol »

Lugen wrote:
S0lll0s wrote:It is part of the transform matrix, but I doubt you will be able to extract it if it's mixed with the projection matrix already.
I see. Tried multiplying it with a single value but only the scale was affected, but maybe I'm missing something concerning matrix math.

In the meantime I'll just send the objects position to the vertex shader.

Code: Select all

// From scene
	extern vec3 positionCamera;
	extern vec3 positionObject;
	extern float unit;
	
// Vertex shader
	vec4 position(mat4 transform , vec4 positionVertex)
	{
		positionVertex.xy += positionObject.xy - positionCamera.xy;
		positionVertex.xy *= unit;

		return transform * positionVertex;
	}
The transform matrix unifies position, scale and rotation.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Is it possible to access the draw() position in a shader

Post by Positive07 »

Since you multiplied, the thing expanded (modified the scale) if you add a value to it you can move things around, rotation is trickier though
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
ghurk
Prole
Posts: 15
Joined: Tue Apr 25, 2017 11:05 pm

Re: Is it possible to access the draw() position in a shader?

Post by ghurk »

TransformMatrix[0][0] contains vec2 scale data of both draw() scale and love.graphics.scale() already multiplied
vec2( TransformMatrix[3][0], TransformMatrix[3][1] ) contains x and y of both draw() x, y and love.graphics.translate() already multiplied, gonna figure out rest now.
TransformMatrix[3] seems to be: { [0]=x, [1]=y, [2]=z, [3]=w } in pixel shader.
any1 can try it on Vertex matrix if they match?
Post Reply

Who is online

Users browsing this forum: secretsue92 and 80 guests