Jump to content

Shader doesnt get restricted to 1 element as it should


Recommended Posts

Hello community. I have a little question about shaders, because this seems to be ridiculous:

function addToTable() 
if getElementType(source) == "vehicle" then 
    local model = getElementModel(source) 
    if tank[model] and not table.contains(tanks, source) then 
        table.insert(tanks, source) 
        local leftShader = dxCreateShader("replacer.fx", 0, 100, true) 
        local rightShader = dxCreateShader("replacer.fx", 0, 100, true) 
        setElementData(source, "leftShader", leftShader, false) 
        setElementData(source, "rightShader", rightShader, false) 
        engineApplyShaderToWorldTexture(leftShader, "tex1", source) 
        engineApplyShaderToWorldTexture(rightShader, "tex2", source) 
    end 
end 

Why does this code NOT restrict the shader to the "source" element? The shader gets applied to ALL elements, but engineApplyShaderToWorldTexture clearly restricts it to "source". Am i stupid?

end

addEventHandler( "onClientElementStreamIn", root, addToTable)

Link to comment

Thanks Walid, but i found the issue!

dxSetShaderValue(leftShader, "gUVMove", 0, 0)

dxSetShaderValue(rightShader, "gUVMove", 0, 0)

These two values have to be set directly after creation of the shader; otherwise (for some reason) if you manipulate that value elsewhere, it gets applied to all vehicles. I dont know why, i dont know how. But it works.

I saw that the shader doesnt manipulate any vehicle that has already been entered before so i thought it might be possible that a value has to be set or is missing, i just tried all values after line 11 and gUVMove was the one that caused it.

gUVMove gets modified in a onClientRender loop for each vehicle, i thought it should be fine since the shader is limited anyway, but i was wrong

Link to comment

i don't know what are yo trying to do. Try something like this

local tanks = {} -- tanks table 
  
-- Add vehicle to table and apply shader 
function addToTable(vehicle) 
    if vehicle then  
        local model = getElementModel(vehicle) 
        if tank[model] and not tanks[vehicle] then 
            tanks[vehicle] = true 
            local leftShader = dxCreateShader("replacer.fx", 0, 100, true,"vehicle") 
            local rightShader = dxCreateShader("replacer.fx", 0, 100, true,"vehicle") 
            setElementData(vehicle, "leftShader", leftShader, false) 
            setElementData(vehicle, "rightShader", rightShader, false) 
            engineApplyShaderToWorldTexture(leftShader, "tex1", vehicle) 
            engineApplyShaderToWorldTexture(rightShader, "tex2", vehicle) 
        end  
    end 
end  
  
  
function checkAndApply() 
    local vehicles = getElementsByType ( "vehicle" ) 
    for i , v in pairs (vehicles) do 
        addToTable(v) 
    end  
end  

Link to comment

No, no, the rest of the code consists of 3539 lines :D

onClientElementStreamIn is indeed the correct event. I was just wondering why the shader got applied to all vehicles. It seems, shaders with no set values are using values from other shaders until they get a value. Not sure if this is a bug or a feature of MTA.

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...