Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
LUA Shader Question - How to read and write pixel info from texture — Gideros Forum

LUA Shader Question - How to read and write pixel info from texture

Hi everyone,
Currently, I am learning LUA Shader and I am trying to pass a texture and show it to the screen:

1. Load texture from a png
2. Pass it to shader by setConstant function
3. Read and show pixel color to a Sprite

(I need to read information from some textures like noise, height map ..., not the texture that the shader currently applies)
When I run the code, nothing shows, only black color on the 1024* 1024 region.

Please help me how to fix this. Thank you so much!

(You can change "maps/color.png" to any image)
--!NEEDS:luashader/luashader.lua
local tex_color=Texture.new("maps/color.png",false)
 
local function makeEffect(name,vshader,fshader)
	local s=Shader.lua(vshader,fshader,0,
	{
	{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="fTextureColor",type=Shader.CTEXTURE,vertex=false},
 
	{name="fTextureInfo",type=Shader.CFLOAT4,sys=Shader.SYS_TEXTUREINFO,vertex=false},
	{name="fTime",type=Shader.CFLOAT,sys=Shader.SYS_TIMER,vertex=false},
	},
	{
	{name="vVertex",type=Shader.DFLOAT,mult=2,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},
	},
	{
	{name="fTexCoord",type=Shader.CFLOAT2},
	}
	)
	s.name=name
	return s
end
 
 
local effect =makeEffect("Custom",
	function (vVertex,vColor,vTexCoord) : Shader
		local vertex = hF4(vVertex,0.0,1.0)
		fTexCoord=vTexCoord
		return vMatrix*vertex
	end,
 
	function () : Shader
 
		-- How to read and copy pixel color from input texture (input by setContant)
	 local frag = texture2D(fTextureColor, fTexCoord)
	 return frag
	end
 
 
)
 
 
effect:setConstant("fTextureColor", Shader.CTEXTURE,1,tex_color)
 
local px = Pixel.new(1024,1024)
px:setShader(effect)
stage:addChild(px)
Coming soon

Comments

  • keszeghkeszegh Member
    for assigning multiple textures see the 'slot' parameter here:
    https://wiki.giderosmobile.com/index.php/Mesh:setTexture
    see also
    https://forum.giderosmobile.com/discussion/8571/multiple-texture-slots-for-bitmap/p1
    perhaps a texture can be directly assigned to the shader too, i'm not sure.
    +1 -1 (+1 / -0 )Share on Facebook
  • Hi @keszegh
    Yeah, reading the Pixel setTexture function leads me to the question of how to get texture from slots in shader code. That may be a way to bind all needed textures to a Pixel instance.
    https://wiki.gideros.rocks/index.php/Pixel:setTexture

    Coming soon
  • MoKaLuxMoKaLux Member
    edited May 2023
    https://forum.gideros.rocks/discussion/comment/67635/#Comment_67635

    https://wiki.gideros.rocks/index.php/Shader:setConstant may be what your are looking for?

    Example:
    local LightingShaderConstants={ -- you can add your own variables here
    {name="g_Color",type=Shader.CFLOAT4,mult=1,sys=Shader.SYS_COLOR},
    {name="lightPos",type=Shader.CFLOAT4,mult=1,vertex=false},
    {name="ambient",type=Shader.CFLOAT,mult=1,vertex=false},
    {name="xshadow",type=Shader.CFLOAT,mult=1,vertex=false},
    {name="g_Texture",type=Shader.CTEXTURE,mult=1,vertex=false},
    {name="g_NormalMap",type=Shader.CTEXTURE,mult=1,vertex=false,code="n"},
    {name="g_ShadowMap",type=Shader.CTEXTURE,subtype="shadow",mult=1,vertex=false,code="s"},
    -- ...
    }
    Sorry I cannot help you put it in your custom effect, I am still a noob with shaders :'(

    For an extensive use of shaders you can have a look at the gideros sample 3D Horse or 3D Animation?
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • hgy29hgy29 Maintainer
    edited May 2023 Accepted Answer
    Hi @vitalitymobile, slots number correspond to the order un which textures where declared in the constant list, see @MoKaLux answet above: g_Texture is the first slot, g_NormalMap is the second slot, and so on. Note that texture slots begin at slot 0

    In your example, replace this:
    --effect:setConstant("fTextureColor", Shader.CTEXTURE,1,tex_color)
    px:setTexture(tex_color,1)
    +1 -1 (+2 / -0 )Share on Facebook
  • Yeah It works like a charm
    local px = Pixel.new(nil,1024,1024)
    px:setTexture(tex_color,1)

    Likes: keszegh, MoKaLux

    Coming soon
    +1 -1 (+2 / -0 )Share on Facebook
Sign In or Register to comment.