Shader issue with uniform (Solved)

Hello and happy holidays to all! ^^

I’m again facing a small problem with a shader that I’m trying to reproduce from a Löve2d project I did a while ago.

Here’s what I’ve rewritten so far (I made quite a few versions of it but I always have the same problem, this one is one of several others):

varying mediump vec4 position;
varying mediump vec2 var_texcoord0;

uniform lowp sampler2D texture_sampler;
uniform lowp vec4 tint;

uniform lowp vec4 img_size;
uniform lowp vec4 phase;

void main() // Sine distortion of sprite
{
	lowp vec4 tint_pm = vec4(tint.xyz * tint.w, tint.w);

	mediump vec2 sample_pos;
	sample_pos.x = var_texcoord0.x - img_size.x * sin(phase.x + img_size.y * var_texcoord0.y);
	sample_pos.y = var_texcoord0.y;

	mediump vec4 _sample = texture2D(texture_sampler, sample_pos.xy);
	gl_FragColor = _sample * tint_pm;
}

Here is an overview of the material file:

So there is the phase value that needs to be set from a script, so I do it like this:

function init(self)
	self.phase = 0
end

function update(self, dt)
	self.phase = self.phase + dt
	go.set("#sprite", "phase", vmath.vector4(self.phase, 0, 0, 0))
end

But I get this error:

ERROR:GRAPHICS: OpenGLSetConstantM4(2044): gl error 1282: invalid operation

dmengine: ../src/opengl/graphics_opengl.cpp:2044: void dmGraphics::OpenGLSetConstantM4(dmGraphics::HContext, const dmVMath::Vector4 *, int, int): Assertion `0' failed.

I just noticed that if I change this line:

mediump vec4 _sample = texture2D(texture_sampler, sample_pos.xy);

By that:

mediump vec4 _sample = texture2D(texture_sampler, var_texcoord0.xy);

It ignores the superfluous and it works, so the error comes from the lines I added but why? I don’t even know if this assertion error comes from my code or not. I’m very far from being a shader expert so excuse me if an obvious detail escapes me…

Sorry, This is totally an inadvertent error on my part, in the material file img_size must be in User and not Texture

As a bonus not to make this post too useless I share a working copy of the shader in question to operate a sinusoidal defromation of an image :innocent:

varying mediump vec4 position;
varying mediump vec2 var_texcoord0;

uniform lowp sampler2D texture_sampler;
uniform lowp vec4 tint;

uniform lowp vec4 img_size;
uniform lowp vec4 phase;

void main() // Sine distortion of sprite
{
	lowp vec4 tint_pm = vec4(tint.xyz * tint.w, tint.w);

	mediump vec2 sample_pos;
	sample_pos.x = var_texcoord0.x - (10.0 / img_size.x) * sin(phase.x + .025 * img_size.y * var_texcoord0.y);
	sample_pos.y = var_texcoord0.y;

	mediump vec4 _sample = texture2D(texture_sampler, sample_pos);
	gl_FragColor = _sample * tint_pm;
}

Well done for solving this yourself! I often find the solution only after re-reading my post. Can I have an example of sinusoidal deformation of an image?

Sure ! For my exercise on Defold I wanted to make an animated fishing net as if it were underwater like this:

But it can be customized to any sauce like this for example:

I had the idea to reproduce it by seeing let’s play on old Amiga games, I can’t remember which of these games had this effect (or even if that’s how they achieve it) but it is where the original idea came from ^^

7 Likes