Newbie question - having difficulty applying shader to an image

Hi guys,

I am totally new to shaders and have been banging my head for several hours now. I followed the documentation from Corona, found some examples and even managed to create a simple image with shader.

My nightmare, however, started when I decided to use this specific shader ( which I found in another topic ) : https://goo.gl/kpaqiK

When I follow the tutorials from Corona to implement this shader I get a black rectangle and nothing happens in the simulator.

Here is my code so far:

-- generator.custom.portal local kernel = {} kernel.category = "generator" kernel.isTimeDependent = true kernel.name = "portal" kernel.fragment = [[P\_COLOR vec4 FragmentKernel( P\_UV vec2 texCoord ) { P\_UV vec2 center = vec2(0.5,0.5); P\_UV float speed = 0.035; P\_UV float invAr = CoronaVertexUserData.y / CoronaVertexUserData.x; P\_UV vec2 uv = texCoord.xy / CoronaVertexUserData.xy; P\_COLOR vec3 col = vec4(uv,0.5+0.5\*sin(CoronaTotalTime),1.0).xyz; P\_COLOR vec3 texcol; P\_UV float x = (center.x-uv.x); P\_UV float y = (center.y-uv.y) \*invAr; P\_UV float r = -(x\*x + y\*y); P\_UV float z = 1.0 + 0.5\*sin((r+CoronaTotalTime\*speed)/0.013); texcol.x = z; texcol.y = z; texcol.z = z; return CoronaColorScale(vec4(texture2D(CoronaSampler0, texCoord).rgb \* col\*texcol,1.0)); }]] return kernel

And then in another place:

local k1 = require("generator\_portal") graphics.defineEffect( k1 ) local object = display.newImageRect("images/rainbow.png",160, 160) object.x = \_W/2 object.y = \_H/2 object.fill.effect = "generator.custom.portal"

What I get in the simulator is just a black rectangle. Am I doing something wrong? Any help will be greatly appreciated.

Hi.

I suspect you just need to supply the CoronaVertexUserData fields. See here for info on that. Right now those will just be getting some default (or garbage, possibly), so you’ll wind up with something like 0 / 0 for the invAr and uv fields (maybe not even that, depending on the graphics driver).

In the shader playground, you can see up top where these are set. Just use those values (1 and 1) as your defaults. You don’t even need to change them from the calling code if you like the results.

Hi, StarCrunch.

Thank you for the answer, it really worked! I had problems with other shaders from the forum, as well, but most probably the issue was the same - I was missing the CoronaVertexUserData fields. I hope I’ll get better with these and be able to contribute with my own custom shaders soon. :slight_smile:

Hi.

I suspect you just need to supply the CoronaVertexUserData fields. See here for info on that. Right now those will just be getting some default (or garbage, possibly), so you’ll wind up with something like 0 / 0 for the invAr and uv fields (maybe not even that, depending on the graphics driver).

In the shader playground, you can see up top where these are set. Just use those values (1 and 1) as your defaults. You don’t even need to change them from the calling code if you like the results.

Hi, StarCrunch.

Thank you for the answer, it really worked! I had problems with other shaders from the forum, as well, but most probably the issue was the same - I was missing the CoronaVertexUserData fields. I hope I’ll get better with these and be able to contribute with my own custom shaders soon. :slight_smile: