GLSL Shader uniforms

Monkey Forums/Monkey Programming/GLSL Shader uniforms

malublu(Posted 2016) [#1]
Simple question!
I look how to set Shader uniforms.
precision mediump float;
uniform vec4 u_color;
void shader() {
    gl_FragColor = u_color;
}

Local shader:Shader = New Shader(shadercode)
shader.AddUniform????

Is there any stuff like this?
Thanks for the answere.

I also tried with:
Local m:Material = New Material(New Shader(shadercode))
m.SetVector("u_color", [0.0,0.0,0.0,1.0])



ImmutableOctet(SKNG)(Posted 2016) [#2]
Assuming you're talking about Mojo 2, you do this using 'Material' objects, which are used by 'Image' objects. So, you use the material to pass values to the shader by getting it from the 'Image'. Click here for an example. (Uniform declared here)

The full repository may interest you.


malublu(Posted 2016) [#3]
Yes i talking about mojo2, sorry.

So if i lokking to your repo, I see:
Local Barrel:= BarrelShader.Instance()		
Local T:= New Texture(480, 480, 4, Texture.ClampST|Texture.RenderTarget)
Local M:= New Material(Barrel)
M.SetTexture("ColorTexture", T)

Which is like my code i posted before.
via new material with new shader

I draw it like:
Local m:Material = New Material(New Shader(shadercode))
canvas.DrawRect(0, 0, DeviceWidth(), DeviceHeight(), m)


If i use fixed color:
void shader() {
    gl_FragColor = vec4(1.0,0.0, 0.0, 1.0);
}

it works!

so i try it with SetVector:
uniform vec4 u_color;
void shader() {
    gl_FragColor = u_color;
}

Local m:Material = New Material(New Shader(shadercode))
m.SetVector("u_color", [1.0,0.0, 0.0, 1.0])
canvas.DrawRect(0, 0, DeviceWidth(), DeviceHeight(), m)


its only white ...
I don't know why?

Thank you for your answere.


ImmutableOctet(SKNG)(Posted 2016) [#4]
You'll want to use 'b3d_Ambient', 'b3d_Diffuse' and 'b3d_Color', as 'gl_FragColor' gets calculated by the parent code generated around your shader. Along with these, there's also 'b3d_Alpha', which is dedicated to the alpha channel. For reference, try looking at the "mojo2_fastshader.glsl" file.

EDIT: Tried to make this less confusing. Yeah, Mark made this a bit more complicated than it needed to be.


malublu(Posted 2016) [#5]
Sorry ^^.
I don't get running it.
I change my glsl file to:
uniform vec4 u_color;
void shader() {
    b3d_Ambient = u_color;
    b3d_Alpha=b3d_Ambient.a;
}


Now it is black.

I believe I stand on the hose .


k.o.g.(Posted 2016) [#6]
it has to be like this! (for the others: it's a prank bro!)


malublu(Posted 2016) [#7]
Some freaky stuff:
If i use this, it works:
uniform vec4 UColor;
void shader() {
    gl_FragColor = UColor;
}


.SetVector("UColor", [1.0, 0.0, 0.0, 1.0])


Some names are not useable: u_color, ucolor, U_color

Only UColor works.

It should be a bug!