It looks like you're new here. If you want to get involved, click one of these buttons!
BlurEffect={} BlurEffect.VS_GL=[[ uniform highp mat4 vMatrix; uniform highp mat4 vWMatrix; attribute highp vec3 vVertex; attribute mediump vec2 vTexCoord; varying mediump vec2 fTexCoord; varying mediump vec4 fWorldCoord; void main() { vec4 vertex = vec4(vVertex,1.0); gl_Position = vMatrix*vertex; fTexCoord = vTexCoord; fWorldCoord = vWMatrix*vertex; } ]] BlurEffect.FS_GL=[[ #ifdef GL_ES precision mediump float; #endif const float pi = 3.14159; uniform lowp vec4 fColor; uniform mediump vec4 fTexSize; uniform lowp sampler2D fTexture; uniform lowp int myColor; varying mediump vec2 fTexCoord; varying mediump vec4 fWorldCoord; void main() { float red = 0; float blue = 0; if (myColor == 1) { red = 0.5; } if (myColor == 2) { blue = 0.5; } vec4 solidRed = vec4(red,0,blue,1); gl_FragColor = solidRed; } ]] BlurEffect.Shader=Shader.new(BlurEffect.VS_GL,BlurEffect.FS_GL,Shader.FLAG_FROM_CODE, { {name="vMatrix",type=Shader.CMATRIX,sys=Shader.SYS_WVP,vertex=true}, {name="vWMatrix",type=Shader.CMATRIX,sys=Shader.SYS_WORLD,vertex=true}, {name="fTexture",type=Shader.CTEXTURE,vertex=false}, {name="fTexSize",type=Shader.CFLOAT4,sys=Shader.SYS_TEXTUREINFO,vertex=false}, {name="fColor",type=Shader.CFLOAT4,sys=Shader.SYS_COLOR,vertex=false}, {name="myColor",type=Shader.CINT,vertex=false}, }, { {name="vVertex",type=Shader.DFLOAT,mult=3,slot=0,offset=0}, {name="vColor",type=Shader.DUBYTE,mult=4,slot=1,offset=0}, {name="vTexCoord",type=Shader.DFLOAT,mult=2,slot=2,offset=0}, }); local texture = Texture.new("pixel.jpg", true) --2x2 green pixel local shape1 = Shape.new() shape1:setFillStyle(Shape.TEXTURE, texture) shape1:beginPath() shape1:lineTo(50, 100) shape1:lineTo(450, 100) shape1:lineTo(450, 500) shape1:lineTo(50, 500) shape1:lineTo(50, 100) shape1:closePath() shape1:endPath() BlurEffect.Shader:setConstant("myColor", Shader.CINT, 1, 1) shape1:setShader(BlurEffect.Shader) stage:addChild(shape1) -- If you comment from here, first shape will have "true" color, but if not, the last shape shader will redraw also on first shape local shape2 = Shape.new() shape2:setFillStyle(Shape.TEXTURE, texture) shape2:beginPath() shape2:lineTo(600, 100) shape2:lineTo(900, 100) shape2:lineTo(900, 500) shape2:lineTo(500, 500) shape2:lineTo(500, 100) shape2:closePath() shape2:endPath() BlurEffect.Shader:setConstant("myColor", Shader.CINT, 1, 2) shape2:setShader(BlurEffect.Shader) stage:addChild(shape2) |
Comments
change
About shader:setConstant() vs sprite:setShaderConstant(), the former sets a uniform value directly in the shader program, while the latter instruct gideros to set the uniform right before actually using the shader. The second form is more suitable to customize shader behavior sprite by sprite, while the first can be used to set global behavior for all sprites using the shader.
And yes, you should setShader() before setShaderConstant(), so that gideros can check the constant you gave vs what the shader expects. Removing or changing the shader for the sprite also clears any setShaderConstant() issued beforehand.
Does this make sense ?
P.S. I didnt found setShaderConstant in Gideros reference - think should be fixed, who should be noticed about this ?
If shader is not set it would been taking value from other sprite !!! Even if you use setShaderConstant !!!
Attched test case project
So if shaderConstant set to one of sprites this constant used for other shaders that not yet has setted constant.
Ive solved this by forcing constant value to each sprite after I do setShader to this sprite.