Jump to content

One Shader - multiple objects with different textures


launemax

Recommended Posts

Hy there!

I have a question about shaders:

I want to have a lot of flags or signs, where the players can put their own texture/image on. So i have to create for each object a new shader and set the shadervalue of the texture to a different image, but then i have lots of shaders.

Is there a way to create 1 shader and depending on which ElementData an object has, to set a different shadervalue? So i can save a lot of load of my server.

thanks for the answer in advance,

regards,

launemax

Link to comment

This is an exmaple code

local flags = { 
"Flag" 
"Flag2" 
} 
local flagsShaders = {} 
  
addEventHandler("onClientResourceStart",root, 
function() 
    for k,image in ipairs(flags) do 
    local flagTexture = dxCreateTexture(image..".png","dxt3") 
    local flagShader = dxCreateShader("shader.fx") 
    dxSetShaderValue(flagShader,"YourShaderTextureName",flagTexture) 
    flagsShaders[image] = flagShader 
    end 
end 
) 
  
addEventHandler("onClientElementStreamIn",root, 
function() 
    if getElementType(source) == "object" then 
        if getElementModel(source) == 0 then -- Your flag object id 
        local flagIMG = getElementData(source,"flagIMG") 
        engineApplyShaderToWorldTexture(flagsShaders[flagIMG],"YourObjectTextureName",source) 
        end 
    end 
end 
) 

Link to comment

Thanks, but thats the thing i want to avoid. :)

There are a lot of Shaders. I thought, if I just create one shader and apply different textures, it would be more efficient.

Am I right? The more shaders i create, the more rendering power is required?

Link to comment
Thanks, but thats the thing i want to avoid. :)

There are a lot of Shaders. I thought, if I just create one shader and apply different textures, it would be more efficient.

Am I right? The more shaders i create, the more rendering power is required?

This doesn't seem to matter much. What matters is how many textures visible on screen the shader is applied to. vertex and pixel calculations are done when they're needed. (You can always create shader per entity - when it's streamed in) And you can change values per shader, not per texture. Exception are dxDrawImage and dxDrawMateriaLine3D - after drawing one you can just change values before you draw another (in the same frame).

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...