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)
local tex_color=Texture.new("maps/color.png",false)
local function makeEffect(name,vshader,fshader)
	local s=Shader.lua(vshader,fshader,0,
	return s
local effect =makeEffect("Custom",
	function (vVertex,vColor,vTexCoord) : Shader
		local vertex = hF4(vVertex,0.0,1.0)
		return vMatrix*vertex
	function () : Shader
		-- How to read and copy pixel color from input texture (input by setContant)
	 local frag = texture2D(fTextureColor, fTexCoord)
	 return frag
effect:setConstant("fTextureColor", Shader.CTEXTURE,1,tex_color)
local px = Pixel.new(1024,1024)
Coming soon


  • keszeghkeszegh Member
    for assigning multiple textures see the 'slot' parameter here:
    see also
    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.

    Coming soon
  • MoKaLuxMoKaLux Member
    edited May 2023

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

    local LightingShaderConstants={ -- you can add your own variables here
    -- ...
    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)
    +1 -1 (+2 / -0 )Share on Facebook
  • Yeah It works like a charm
    local px = Pixel.new(nil,1024,1024)

    Likes: keszegh, MoKaLux

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