Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How to calc distance from fragmentPixel to Woorld coordinate? — Gideros Forum

How to calc distance from fragmentPixel to Woorld coordinate?

YanYan Member
edited December 2021 in Code snippets
How to calc distance from fragmentPizel to Woorld coordinate?
spriteFog = {}
 
spriteFog.VS_GL = [[
 
	uniform highp mat4 vMatrix;
 
	attribute highp vec3 vVertex;
	attribute mediump vec2 vTexCoord;
 
	varying mediump vec2 fTexCoord;
 
	void main() {
		vec4 vertex = vec4(vVertex, 1.0);
		gl_Position = vMatrix*vertex;
		fTexCoord = vTexCoord;
	}
]]
 
spriteFog.FS_GL = [[
 
	#ifdef GL_ES
		precision highp float;
	#endif
 
	const float offseta = 0.010;
 
	uniform lowp float pX;
	uniform lowp float pY;
 
	uniform lowp vec4 fColor;
	uniform mediump vec4 fTexSize;
	uniform lowp sampler2D fTexture;
 
	varying mediump vec2 fTexCoord;
 
	void main()
	{
		gl_FragColor = vec4(0, 0, 0, 1); 
	}
]]
 
spriteFog.Shader=Shader.new(spriteFog.VS_GL,spriteFog.FS_GL,Shader.FLAG_FROM_CODE, {
	{name="vMatrix",type=Shader.CMATRIX,sys=Shader.SYS_WORLD,vertex=true},
	{name="fColor",type=Shader.CFLOAT4,sys=Shader.SYS_COLOR,vertex=false},
	{name="fTexture",type=Shader.CTEXTURE,vertex=false},
	{name="pX",type=Shader.CFLOAT,vertex=false},
	{name="pY",type=Shader.CFLOAT,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},
});
Im trying to use SYS_WORLD, but shader aint work at all
vk.com/yan_alex

Comments

  • rrraptorrrraptor Member
    edited December 2021 Accepted Answer
    Gradient is drawn by the shader on a RenderTarget:

    Or you need something else?)
    spriteFog = {}
     
    spriteFog.VS_GL = [[
     
    	uniform highp mat4 vMatrix;
     
    	attribute highp vec3 vVertex;
    	attribute mediump vec2 vTexCoord;
     
    	varying mediump vec2 fTexCoord;
     
    	void main() {
    		vec4 vertex = vec4(vVertex, 1.0);
    		gl_Position = vMatrix*vertex;
    		fTexCoord = vTexCoord;
    	}
    ]]
     
    spriteFog.FS_GL = [[
     
    	#ifdef GL_ES
    		precision highp float;
    	#endif
     
    	uniform lowp vec2 fWorldPoint;
     
    	uniform lowp vec4 fColor;
    	uniform mediump vec4 fTexSize;
    	uniform lowp sampler2D fTexture;
     
    	varying mediump vec2 fTexCoord;
     
    	void main()
    	{
    		vec3 color = vec3(distance(fTexCoord, fWorldPoint) * 2.0);
    		gl_FragColor = vec4(color, 1.0);
    	}
    ]]
     
    spriteFog.Shader=Shader.new(spriteFog.VS_GL,spriteFog.FS_GL,Shader.FLAG_FROM_CODE, {
    	{name="vMatrix",type=Shader.CMATRIX,sys=Shader.SYS_WVP,vertex=true},
    	{name="fColor",type=Shader.CFLOAT4,sys=Shader.SYS_COLOR,vertex=false},
    	{name="fTexture",type=Shader.CTEXTURE,vertex=false},
    	{name="fWorldPoint",type=Shader.CFLOAT2,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 W = 128
    local H = 128
    local texelSize = {1/W, 1/H} 
     
    local RT = RenderTarget.new(W, H)
    RT:clear(0, 1)
    local img = Bitmap.new(RT)
    img:setAnchorPoint(0.5, 0.5)
    img:setShader(spriteFog.Shader)
    stage:addChild(img)
     
    local pt =Pixel.new(0xff0000, 1, 8, 8)
    pt:setAnchorPoint(0.5, 0.5)
    stage:addChild(pt)
     
    function updateShader(x, y)
    	local ax, ay = img:getAnchorPoint()
    	local ptx = texelSize[1] * (x - img:getX() + W * ax)
    	local pty = texelSize[2] * (y - img:getY() + H * ay)
    	spriteFog.Shader:setConstant("fWorldPoint", Shader.CFLOAT2, 1, {ptx, pty})
    end
     
    stage:addEventListener(Event.MOUSE_MOVE, function(e)
    	if (e.button == 1) then 
    		img:setPosition(e.x,e.y)
    		updateShader(pt:getPosition())
    	else
    		pt:setPosition(e.x,e.y)
    		updateShader(e.x,e.y)
    	end
    end)

  • Love you, thanks )
    vk.com/yan_alex
Sign In or Register to comment.