From Multi Theft Auto: Wiki
Revision as of 12:20, 30 October 2018 by Samr46 (talk | contribs) (→‎Example)
Jump to navigation Jump to search

This function creates a shader element that can be used in the dxDraw functions. Successful shader creation is not guaranteed unless the Effect File contains a fallback technique which will work on every PC in the universe.

It is highly recommended that dxSetTestMode is used when writing and testing scripts using dxCreateShader.


element, string dxCreateShader ( string filepath / string raw_data [, float priority = 0, float maxDistance = 0, bool layered = false, string elementTypes = "world,vehicle,object,other" ] )

OOP Syntax Help! I don't understand this!

Method: DxShader(...)

Required Arguments

  • filepath / raw_data: The filepath of the shader Effect File (.fx) file or whole data buffer of the shader file

Optional Arguments

All the following optional arguments are only relevant when the shader is used with engineApplyShaderToWorldTexture

  • priority: If more than one shader is matched to a world texture, the shader with the highest priority will be used. If there is more than one shader with the same highest priority, the most recently created shader is used.
  • maxDistance: If non-zero, the shader will be applied to textures nearer than maxDistance only. This can speed up rendering, but (to look good) may require the shader to fade out it's own effect as the texture reaches maxDistance.
  • layered: When set to true, the shader will be drawn in a separate render pass. Several layered shaders can be drawn on the same world texture. (To avoid Z fighting artifacts, you may have to add DepthBias=-0.0002; to the technique pass, but this might cause visual artifacts when applied on vehicles)
  • elementTypes: A comma seperated list of element types to restrict this shader to. Valid element types are:
    • world - Textures in the GTA world
    • ped - Player and ped textures
    • vehicle - Vehicles textures
    • object - Objects textures
    • other - Element textures which are not peds, vehicles or objects
    • all - Everything


  • element: A shader element if successful, false if invalid arguments were passed to the function. You should always check to see if this function has returned false.
  • string: The name of the technique that will be used.


addEventHandler( "onClientRender", root,
        if myShader then
            dxDrawImage( 100, 350, 300, 350, myShader )

-- Use 'toggle' command to switch shader on and off
addCommandHandler( "toggle",
        if not myShader then
            myShader = dxCreateShader( "fancything.fx" )  -- Create shader
            destroyElement( myShader )                    -- Destroy shader
            myShader = nil


This example creates basic shader from raw data (without i/o) on resource start:

local myShader_raw_data = [[
	texture tex;
	technique replace {
		pass P0 {
			Texture[0] = tex;

addEventHandler("onClientResourceStart", resourceRoot, function()
	local myShader = dxCreateShader(myShader_raw_data) -- create shader from raw data
	if isElement(myShader) then
		local myTexture = dxCreateTexture("some_image.png") -- create texture from image file
		if isElement(myTexture) then
			-- apply image to world texture via shader
			dxSetShaderValue(myShader, "tex", myTexture)
			engineApplyShaderToWorldTexture(myShader, "shad_ped")
			outputDebugString("Unable to load texture", 1)
		outputDebugString("Unable to create shader", 1)

You can pass raw data (shader code) directly into the function (example uses variable myShader_raw_data).


Version Description
1.3.0-9.04435 Added layered and elementTypes arguments
1.5.6-9.14403 Added option to use raw data instead of a file name

See Also