Jump to content

engineRemoveShaderFromWorldTexture problem..


WiBox

Recommended Posts

When I enable the skin, it work 100% but when I try to disable it, it doesn't work, 

the "ShaderElement", "textureName", "element" data at "engineApplyShaderToWorldTexture" has the same data of "engineRemoveShaderFromWorldTexture"

Here's the code:

function applyShader ( element, table, _type)
    for index, value in pairs ( table ) do
        if ( index == 1 ) then
            model = value
        elseif ( index == 2 ) then
            textureName = value
        elseif ( index == 3 ) then
            texturePath = value
        end
    end
    local shaderElement = dxCreateShader ( "shader/shader.fx", 0, 0, false, _type == "skin" and "ped" or "vehicle" or "object" )
    local textureElement = dxCreateTexture ( _type.."/textures/"..texturePath )
    if ( shaderElement and textureElement ) then
        dxSetShaderValue ( shaderElement, "Tex0", textureElement )
        engineApplyShaderToWorldTexture ( shaderElement, textureName, element )
    end
end
addEvent("shaders.applyShader", true)
addEventHandler("shaders.applyShader", root, applyShader)

function removeShader ( element, table, _type)
    for index, value in pairs ( table ) do
        if ( index == 1 ) then
            model = value
        elseif ( index == 2 ) then
            textureName = value
        elseif ( index == 3 ) then
            texturePath = value
        end
    end
    local shaderElement = dxCreateShader ( "shader/shader.fx", 0, 0, false, _type == "skin" and "ped" or "vehicle" )
    local textureElement = dxCreateTexture ( _type.."/textures/"..texturePath )
    if ( shaderElement and textureElement ) then
        dxSetShaderValue ( shaderElement, "Tex0", textureElement )
        engineRemoveShaderFromWorldTexture ( shaderElement, textureName, element )
    end
end
addEvent("shaders.removeShader", true)
addEventHandler("shaders.removeShader", root, removeShader)

 

Edited by R.I.P Logic
Link to comment

The variable "shaderElement" refers to a different shader element than the "shaderElement" you've applied to world texture. Thus, this statement is inaccurate 

1 hour ago, R.I.P Logic said:

"element" data at "engineApplyShaderToWorldTexture" has the same data of "engineRemoveShaderFromWorldTexture"

Specifically, the shaderElement at engineRemoveShaderFromWorldTexture is a shader that has just been created on line 31, rather than the one created and applied in lines 11 and 15. If you keep a reference to the shader in a table (named 'shaders' in example below), and insert every created shader into that table under a key of the element who which you've applied it, you'll be able to refer to it later on in different parts of the code.

local shaders = {}
function applyShader ( element, table, _type)
  if shaders[elem] then outputDebugString("Attempted to call applyShader when a shader is already applied") return end -- this blocks applying two shaders on the same element
  for index, value in pairs ( table ) do
    if ( index == 1 ) then
      model = value
    elseif ( index == 2 ) then
      textureName = value
    elseif ( index == 3 ) then
      texturePath = value
    end
  end
  shaders[element] = dxCreateShader ( "shader/shader.fx", 0, 0, false, _type == "skin" and "ped" or "vehicle" or "object" )
  local textureElement = dxCreateTexture ( _type.."/textures/"..texturePath )
  if ( shaders[element] and textureElement ) then
    dxSetShaderValue ( shaders[element], "Tex0", textureElement )
    engineApplyShaderToWorldTexture ( shaders[element], textureName, element )
  end
end
addEvent("shaders.applyShader", true)
addEventHandler("shaders.applyShader", root, applyShader)

function removeShader ( element, table, _type)
  for index, value in pairs ( table ) do
    if ( index == 1 ) then
      model = value
    elseif ( index == 2 ) then
      textureName = value
    elseif ( index == 3 ) then
      texturePath = value
    end
  end
  if ( shaders[element] and textureElement ) then
    engineRemoveShaderFromWorldTexture ( shaders[element], textureName, element )
    destroyElement(shaders[element]) -- don't forget to destroy the shader
    shaders[element] = nil
  end
end
addEvent("shaders.removeShader", true)
addEventHandler("shaders.removeShader", root, removeShader)

If you need this to handle multiple shaders, you'll probably need to change how they're stored in the table, as this one only allows one shader to be stored at a time, and I'll leave that as an exercise for yourself.

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