Texture Atlas Shader Node

I just finished a Texture atlas shader node and was wondering if @nehon wanted to include it in 3.2

I have no idea how to use git but I will drop the code here for you.

TextureAtlas.j3sn

ShaderNodeDefinitions{ 
    ShaderNodeDefinition TextureAtlas {      
        Type: Fragment

        Shader GLSL100: MatDefs/TextureAtlas100.frag
        
        Documentation{
            Outputs the Color by slicing the texture into tiles and using one tile based on tilePosition.

Tile side is defined by tilesWidth and tilesHeight            
            @input texture2D atlas The Atlas image
            @input vec2 tilePosition A 2D vector describing the tile position to use
            @input float tileWidth The number of tiles wide
            @input float tileHeight The number of tiles heigh
            @input vec2 atlasOffset The atlas offset in pixles
            @input vec2 inTextCord The texture Cordinates
            @output vec4 outColor The output color
        }
        Input {
            sampler2D atlas
            vec2 tilePosition
            float tileWidth
            float tileHeight
            vec2 inTextCord
            vec2 atlasOffset
        }
        Output {
             vec4 outColor
        }
    }
}

TextureAtlas100.frag

void main(){
        //@input texture2D atlas The Atlas image
    //@input vec2 tilePosition A 2D vector describing the tile position to use
    //@input float tileWidth The width of the tile in pixles
    //@input float tileHeight The height of the tile in pixles
    //@input vec2 inTextCord The texture Cordinates
    //@input vec2 atlasOffset The atlas offset in pixles
    //@output vec4 outColor The output color

    //insert glsl code here

    float scaleX = 1.0 / tileWidth;
    float scaleY = 1.0 / tileHeight;
    vec2 realTexCord = inTextCord;
    realTexCord.x = realTexCord.x + (atlasOffset.x  * scaleX);
    realTexCord.y = realTexCord.y + (atlasOffset.y * scaleY);
    outColor = texture2D(atlas, vec2((realTexCord.x  * scaleX) + tilePosition.x * scaleX , (realTexCord.y * scaleY) + tilePosition.y * scaleY)); 
}
1 Like

Not in 3.2 for sure, maybe in 3.3 :wink:
Thanks!

1 Like

Generally, I’d expect a texture atlas shader to not use a material parameter for the tile but to include it into vertex attributes. Else, I’m not sure what the point is. (no offense, I just really don’t get the use-case… maybe you can explain.)

those are not a material parameter they are shader node inputs.
the source of the information is independent to the node itself.
here you will just wire in a vec2 for tilePosition that can come from inTexcoord2 for example or inTexCoord.yz.

2 Likes

Ahah…

Pardon my ignorance. :slight_smile:

That’s ok for this time… :wink:

1 Like

What??? I know something about JME that @pspeed does not? Hell has frozen over :slight_smile: Has the student surpassed the master? … nah, I just love using shader nodes instead of normal material defs because it gives me maximum reuse of my glsl code

3 Likes

The sad thing is that shader nodes are inspired by an idea I had a long time ago and talked about with nehon in the JME chat… then he ran with it. So it’s a little sad that I don’t know more about it.

Edit: and I will say it evolved into something pretty different than my original idea of composable shaderlets. I didn’t mean to diminish the work put into it or take credit for it or whatever.

2 Likes

By the way … because this code is a shader node you can easily turn it into a sprite sheet shader with a few drag and drops :wink: I am currently using it to be able to manage all my in game icons with only one texture in memory and wired it into lemure’s style sheet as well :wink:

2 Likes