Jump to content

Shader - Replace Clothes


iAxel

Recommended Posts

Hello everyone!

I wanted to make a system of clothing with shaders.

code:

CLIENT

  
local Shader = dxCreateShader('files/shaders/replace.fx', 0, 0, true, 'ped') --- Create shader for player 
---Clothes Table 
local Clothes = { 
    ['cj_ped_torso'] = { 
        ['tshirtwhite'] = { 
            model = nil 
        }, 
        ['tshirtred'] = { 
            model = nil 
        }, 
        ['tshirtblack'] = { 
            model = nil 
        } 
    } 
} 
  
---Create texture for player 
for model, clothe in pairs(cClothes['cj_ped_torso']) do 
    clothe.model = dxCreateTexture('files/clothes/'..model..'.png') 
end 
  
---Appy Texture for player 
addEvent('addClothes', true) 
function addClothes(player, clothesTexture, clothesModel) 
    local cType = cClothes[clothesModel] 
    local clothe = cType[clothesTexture] 
    if (Shader) then 
        dxSetShaderValue(Shader, 'gTexture', clothe.model) 
        engineApplyShaderToWorldTexture(Shader, clothesModel, player) 
    end 
end 
addEventHandler('addClothes', root, addClothes) 
  

SERVER

  
triggerClientEvent(source, 'addClothes', source, source, 'tshirtblack', 'cj_ped_torso') 
  

replace.fx

  
texture gTexture; 
technique TexReplace 
{ 
    pass P0 
    { 
        Texture[0] = gTexture; 
    } 
} 
  

The function works fine but the texture is visible only localPlayer other players see the standard clothing

Are there any ideas that it was visible all players?

Link to comment
just use this
triggerClientEvent(root, 'addClothes',root, source, 'tshirtblack', 'cj_ped_torso') 

Don't work

If use root then the localPlayer and another Player the same texture

You must add something like this in your client side :

if player == localPlayer then  
  
end  

Link to comment

You must add something like this in your client side :

if player == localPlayer then  
  
end  

In this way?

addEvent('addClothes', true) 
function addClothes(player, clothesTexture, clothesModel) 
    local cType = cClothes[clothesModel] 
    local clothe = cType[clothesTexture] 
    if (player == localPlayer) then 
        if (Shader) then 
            dxSetShaderValue(Shader, 'gTexture', clothe.model) 
            engineApplyShaderToWorldTexture(Shader, clothesModel, player) 
        end 
    end 
end 
addEventHandler('addClothes', root, addClothes) 

Link to comment

You must add something like this in your client side :

if player == localPlayer then  
  
end  

In this way?

addEvent('addClothes', true) 
function addClothes(player, clothesTexture, clothesModel) 
    local cType = cClothes[clothesModel] 
    local clothe = cType[clothesTexture] 
    if (player == localPlayer) then 
        if (Shader) then 
            dxSetShaderValue(Shader, 'gTexture', clothe.model) 
            engineApplyShaderToWorldTexture(Shader, clothesModel, player) 
        end 
    end 
end 
addEventHandler('addClothes', root, addClothes) 

yes

Link to comment
local Shader = dxCreateShader('files/shaders/replace.fx', 0, 0, true, 'ped') --- Create shader for player 
---Clothes Table 
local cClothes = { 
    ['cj_ped_torso'] = { 
        ['tshirtwhite'] = { 
            model = nil 
        }, 
        ['tshirtred'] = { 
            model = nil 
        }, 
        ['tshirtblack'] = { 
            model = nil 
        } 
    } 
} 
  
---Create texture for player 
for model, clothe in pairs(cClothes['cj_ped_torso']) do 
    clothe.model = dxCreateTexture('files/clothes/'..model..'.png') 
end 
  
---Appy Texture for player 
addEvent('addClothes', true) 
function addClothes(player, clothesTexture, clothesModel) 
    local cType = cClothes[clothesModel] 
    local clothe = cType[clothesTexture] 
    local clothes = getElementData(player,"clothes") 
    if type(clothes) == "table" and table.getn(clothes) > 0 then 
        table.insert(clothes,{clothesModel,clothe}) 
        setElementData(player,"clothes",clothes) 
    else 
        local cTable = {} 
        table.insert(cTable,{clothesModel,clothe}) 
        setElementData(player,"clothes",cTable) 
    end 
end 
addEventHandler('addClothes', root, addClothes) 
  
addEventHandler("onClientElementStreamIn",root, 
function() 
    if getElementType(source) == "player" then 
        if (Shader) then 
        local clothes = getElementData(source,"clothes") 
            if type(clothes) == "table" and table.getn(clothes) > 0 then 
                for k,v in pairs(clothes) do 
                dxSetShaderValue(Shader, 'gTexture', v[2].model) 
                engineApplyShaderToWorldTexture(Shader, v[1], source) 
                end 
            end 
        end 
    end 
end 
) 

Link to comment
local Shader = dxCreateShader('files/shaders/replace.fx', 0, 0, true, 'ped') --- Create shader for player 
---Clothes Table 
local cClothes = { 
    ['cj_ped_torso'] = { 
        ['tshirtwhite'] = { 
            model = nil 
        }, 
        ['tshirtred'] = { 
            model = nil 
        }, 
        ['tshirtblack'] = { 
            model = nil 
        } 
    } 
} 
  
---Create texture for player 
for model, clothe in pairs(cClothes['cj_ped_torso']) do 
    clothe.model = dxCreateTexture('files/clothes/'..model..'.png') 
end 
  
---Appy Texture for player 
addEvent('addClothes', true) 
function addClothes(player, clothesTexture, clothesModel) 
    local cType = cClothes[clothesModel] 
    local clothe = cType[clothesTexture] 
    local clothes = getElementData(player,"clothes") 
    if type(clothes) == "table" and table.getn(clothes) > 0 then 
        table.insert(clothes,{clothesModel,clothe}) 
        setElementData(player,"clothes",clothes) 
    else 
        local cTable = {} 
        table.insert(cTable,{clothesModel,clothe}) 
        setElementData(player,"clothes",cTable) 
    end 
end 
addEventHandler('addClothes', root, addClothes) 
  
addEventHandler("onClientElementStreamIn",root, 
function() 
    if getElementType(source) == "player" then 
        if (Shader) then 
        local clothes = getElementData(source,"clothes") 
            if type(clothes) == "table" and table.getn(clothes) > 0 then 
                for k,v in pairs(clothes) do 
                dxSetShaderValue(Shader, 'gTexture', v[2].model) 
                engineApplyShaderToWorldTexture(Shader, v[1], source) 
                end 
            end 
        end 
    end 
end 
) 

Don't Work

Link to comment
local Shader = dxCreateShader('files/shaders/replace.fx', 0, 0, true, 'ped') --- Create shader for player 
---Clothes Table 
local cClothes = { 
    ['cj_ped_torso'] = { 
        ['tshirtwhite'] = { 
            model = nil 
        }, 
        ['tshirtred'] = { 
            model = nil 
        }, 
        ['tshirtblack'] = { 
            model = nil 
        } 
    } 
} 
  
---Create texture for player 
for model, clothe in pairs(cClothes['cj_ped_torso']) do 
    clothe.model = dxCreateTexture('files/clothes/'..model..'.png') 
end 
  
---Appy Texture for player 
addEvent('addClothes', true) 
function addClothes(player, clothesTexture, clothesModel) 
    local cType = cClothes[clothesModel] 
    local clothe = cType[clothesTexture] 
    local clothes = getElementData(player,"clothes") 
    if type(clothes) == "table" and table.getn(clothes) > 0 then 
        table.insert(clothes,{clothesModel,clothe}) 
        setElementData(player,"clothes",clothes) 
    else 
        local cTable = {} 
        table.insert(cTable,{clothesModel,clothe}) 
        setElementData(player,"clothes",cTable) 
    end 
end 
addEventHandler('addClothes', root, addClothes) 
  
addEventHandler("onClientElementStreamIn",root, 
function() 
    if getElementType(source) == "player" then 
        if (Shader) then 
        local clothes = getElementData(source,"clothes") 
            if type(clothes) == "table" and table.getn(clothes) > 0 then 
                for k,v in pairs(clothes) do 
                dxSetShaderValue(Shader, 'gTexture', v[2].model) 
                engineApplyShaderToWorldTexture(Shader, v[1], source) 
                end 
            end 
        end 
    end 
end 
) 

Don't Work

What's doesn't works explain more so we can help you

Link to comment

What's doesn't works explain more so we can help you

In debug no errors or warnings

Use a function but nothing happens

  
triggerClientEvent(root, 'addClothes', source, source, 'tshirtblack', 'cj_ped_torso') 
  

Perhaps from for it?

Server

  
Clothes = { 
    ['cj_ped_torso'] = { 
        {'tshirtwhite'}, 
        {'tshirtgrey'}, 
        {'tshirtbeige'} 
    }, 
    ['cj_ped_legs'] = { 
        {'legsblack'}, 
        {'legsblue'}, 
        {'legsbrown'} 
    } 
} 
local s = Clothes['cj_ped_torso'][math.random(1, 3)] 
local t = Clothes['cj_ped_legs'][math.random(1, 3)] 
triggerClientEvent(root, 'addClothes', source, source, s[1], 'cj_ped_torso') 
triggerClientEvent(root, 'addClothes', source, source, t[1], 'cj_ped_legs') 
  

Client

  
---Shaders 
local Shader = {} 
Shader[1] = dxCreateShader('files/shaders/replace.fx', 0, 0, true, 'ped')---Shirt 
Shader[2] = dxCreateShader('files/shaders/replace.fx', 0, 0, true, 'ped')---Leg 
--Table 
Clothes = { 
    ['cj_ped_torso'] = { 
        ['tshirtwhite'] = { 
            index = 1, 
            model = nil 
        }, 
        ['tshirtgrey'] = { 
            index = 1, 
            model = nil 
        }, 
        ['tshirtbeige'] = { 
            index = 1, 
            model = nil 
        } 
    }, 
    ['cj_ped_legs'] = { 
        ['legsblack'] = { 
            index = 2, 
            model = nil 
        }, 
        ['legsdblue1'] = { 
            index = 2, 
            model = nil 
        }, 
        ['legsblue'] = { 
            index = 2, 
            model = nil 
        } 
    } 
} 
--Loop 
for model, clothe in pairs(cClothes['cj_ped_torso']) do 
    clothe.model = dxCreateTexture('files/clothes/'..model..'.png') 
end 
for model, clothe in pairs(cClothes['cj_ped_legs']) do 
    clothe.model = dxCreateTexture('files/clothes/'..model..'.png') 
end 
  
addEvent('addClothes', true) 
function addClothes(player, clothesTexture, clothesModel) 
    local cType = cClothes[clothesModel] 
    local clothe = cType[clothesTexture] 
    local clothes = getElementData(player, 'clothes') 
    if type(clothes) == 'table' and table.getn(clothes) > 0 then 
        table.insert(clothes, {clothesModel, clothe}) 
        setElementData(player, 'clothes', clothes) 
    else 
        local cTable = {} 
        table.insert(cTable, {clothesModel, clothe}) 
        setElementData(player, 'clothes', cTable) 
    end 
    --[[if (Shader[clothe.index]) then 
                resetClothes(player, clothesModel, clothe.index) 
                dxSetShaderValue(Shader[clothe.index], 'gTexture', clothe.model) 
                engineApplyShaderToWorldTexture(Shader[clothe.index], clothesModel, player) 
            end]] 
end 
addEventHandler('addClothes', root, addClothes) 
  
addEventHandler('onClientElementStreamIn', root, 
    function () 
        if getElementType(source) == 'player' then 
            local clothes = getElementData(source, 'clothes') 
            if type(clothes) == 'table' and table.getn(clothes) > 0 then 
                for k, v in pairs(clothes) do 
                    dxSetShaderValue(Shader[v[2].index], 'gTexture', v[2].model) 
                    engineApplyShaderToWorldTexture(Shader[v[2].index], v[1], source) 
                end 
            end 
        end 
    end 
) 
  

Link to comment

Because there's no erros

use outputDebugString to see what's doesn't works

---Shaders 
local Shader = {} 
Shader[1] = dxCreateShader('files/shaders/replace.fx', 0, 0, true, 'ped')---Shirt 
Shader[2] = dxCreateShader('files/shaders/replace.fx', 0, 0, true, 'ped')---Leg 
--Table 
Clothes = { 
    ['cj_ped_torso'] = { 
        ['tshirtwhite'] = { 
            index = 1, 
            model = nil 
        }, 
        ['tshirtgrey'] = { 
            index = 1, 
            model = nil 
        }, 
        ['tshirtbeige'] = { 
            index = 1, 
            model = nil 
        } 
    }, 
    ['cj_ped_legs'] = { 
        ['legsblack'] = { 
            index = 2, 
            model = nil 
        }, 
        ['legsdblue1'] = { 
            index = 2, 
            model = nil 
        }, 
        ['legsblue'] = { 
            index = 2, 
            model = nil 
        } 
    } 
} 
--Loop 
for model, clothe in pairs(cClothes['cj_ped_torso']) do 
    clothe.model = dxCreateTexture('files/clothes/'..model..'.png') 
end 
for model, clothe in pairs(cClothes['cj_ped_legs']) do 
    clothe.model = dxCreateTexture('files/clothes/'..model..'.png') 
end 
  
addEvent('addClothes', true) 
function addClothes(player, clothesTexture, clothesModel) 
    local cType = cClothes[clothesModel] 
    local clothe = cType[clothesTexture] 
    local clothes = getElementData(player, 'clothes') 
    if type(clothes) == 'table' and table.getn(clothes) > 0 then 
        table.insert(clothes, {clothesModel, clothe}) 
        setElementData(player, 'clothes', clothes) 
        outputDebugString("Cloth element data has been added to"..getPlayerName(player)) 
    else 
        local cTable = {} 
        table.insert(cTable, {clothesModel, clothe}) 
        setElementData(player, 'clothes', cTable) 
        outputDebugString("Cloth element data has been added to"..getPlayerName(player)) 
    end 
    --[[if (Shader[clothe.index]) then 
                resetClothes(player, clothesModel, clothe.index) 
                dxSetShaderValue(Shader[clothe.index], 'gTexture', clothe.model) 
                engineApplyShaderToWorldTexture(Shader[clothe.index], clothesModel, player) 
            end]] 
end 
addEventHandler('addClothes', root, addClothes) 
  
addEventHandler('onClientElementStreamIn', root, 
    function () 
        if getElementType(source) == 'player' then 
            local clothes = getElementData(source, 'clothes') 
            if type(clothes) == 'table' and table.getn(clothes) > 0 then 
            outputDebugString("Adding cloth to "..getPlayerName(source)) 
                for k, v in pairs(clothes) do 
                    dxSetShaderValue(Shader[v[2].index], 'gTexture', v[2].model) 
                    engineApplyShaderToWorldTexture(Shader[v[2].index], v[1], source) 
                    outputDebugString(v[1].." Has been added to"..getPlayerName(source)) 
                end 
            end 
        end 
    end 
) 

Link to comment

I cant help you before i know what's the problem

so work on the code little bit

try to find why onClientElementStreamIn is not working

for example output the element data

because maybe the problem is from the element data

addEventHandler('onClientElementStreamIn', root, 
    function () 
        if getElementType(source) == 'player' then 
            local clothes = getElementData(source, 'clothes') 
            outputDebugString("Clothes data is: "..type(clothes).." - and it's: "..tostring(clothes)) 
            if type(clothes) == 'table' and table.getn(clothes) > 0 then 
            outputDebugString("Adding cloth to "..getPlayerName(source)) 
                for k, v in pairs(clothes) do 
                    dxSetShaderValue(Shader[v[2].index], 'gTexture', v[2].model) 
                    engineApplyShaderToWorldTexture(Shader[v[2].index], v[1], source) 
                    outputDebugString(v[1].." Has been added to"..getPlayerName(source)) 
                end 
            end 
        end 
    end 
) 

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