DxCreateShader: Difference between revisions
Jump to navigation
Jump to search
m (Used <h5> tag so no edit section link appears when it shouldn't) |
mNo edit summary |
||
(16 intermediate revisions by 7 users not shown) | |||
Line 1: | Line 1: | ||
{{Client function}} | {{Client function}} | ||
__NOTOC__ | __NOTOC__ | ||
This function creates a [[shader]] element that can be used in the dxDraw functions. Successful shader creation is not guaranteed unless the [[shader|Effect File]] contains a fallback technique which will work on every PC | This function creates a [[shader]] element that can be used in the dxDraw functions. Successful shader creation is not guaranteed unless the [[shader|Effect File]] contains a fallback technique which will work on every existing PC. | ||
{{Note|It is highly recommended that [[dxSetTestMode]] is used when writing and testing scripts using dxCreateShader.}} | |||
< | {{Deprecated items|3.0159|1.5.8| | ||
==Syntax== | |||
<syntaxhighlight lang="lua"> | |||
element, string dxCreateShader ( string filepath / string raw_data [, float priority = 0, float maxDistance = 0, bool layered = false, string elementTypes = "world,ped,vehicle,object,other,all" ] ) | |||
</syntaxhighlight> | |||
{{OOP||[[Shader|DxShader]]}} | |||
===Required Arguments=== | |||
*'''filepath / raw_data:''' The filepath of the [[shader|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 [http://en.wikipedia.org/wiki/Z-fighting Z fighting] artifacts, you may have to add '''DepthBias<nowiki>=</nowiki>-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 | |||
===Returns=== | |||
*'''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. | |||
|20688}} | |||
{{Updated feature/item|1.5.9|1.5.8|20688| | |||
==Syntax== | ==Syntax== | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
element, string dxCreateShader ( string filepath [, float priority = 0, float maxDistance = 0, bool layered = false, string elementTypes = "world,vehicle,object,other" ] ) | element, string dxCreateShader ( string filepath / string raw_data [ [, table macros = {} ], float priority = 0, float maxDistance = 0, bool layered = false, string elementTypes = "world,ped,vehicle,object,other,all" ] ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
{{OOP||[[Shader|DxShader]]}} | {{OOP||[[Shader|DxShader]]}} | ||
===Required Arguments=== | ===Required Arguments=== | ||
*'''filepath:''' The filepath of the [[shader|shader Effect File]](.fx) file | *'''filepath / raw_data:''' The filepath of the [[shader|shader Effect File]] (.fx) file or whole data buffer of the shader file | ||
===Optional Arguments=== | ===Optional Arguments=== | ||
''All the following optional arguments are only relevant when the shader is used with [[engineApplyShaderToWorldTexture]]'' | ''All the following optional arguments are only relevant when the shader is used with [[engineApplyShaderToWorldTexture]]'' | ||
*'''macros:''' A table contains macros in an ordered and/or unordered way. See example below. | |||
*'''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. | *'''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. | *'''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 [http://en.wikipedia.org/wiki/Z-fighting Z fighting] artifacts, you may have to add '''DepthBias<nowiki>=</nowiki>-0.0002;''' to the technique pass) | *'''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 [http://en.wikipedia.org/wiki/Z-fighting Z fighting] artifacts, you may have to add '''DepthBias<nowiki>=</nowiki>-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: | *'''elementTypes:''' A comma seperated list of element types to restrict this shader to. Valid element types are: | ||
** world - Textures in the GTA world | ** world - Textures in the GTA world | ||
Line 29: | Line 57: | ||
*'''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.''' | *'''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. | *'''string:''' The name of the technique that will be used. | ||
}} | |||
==Example== | ==Example== | ||
Line 52: | Line 81: | ||
) | ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<br/> | |||
{{New items|3.0157|1.5.6-9.14403| | |||
This example creates basic shader from raw data (without i/o) on resource start: | |||
<syntaxhighlight lang="lua"> | |||
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") | |||
else | |||
outputDebugString("Unable to load texture", 1) | |||
end | |||
else | |||
outputDebugString("Unable to create shader", 1) | |||
end | |||
end) | |||
</syntaxhighlight> | |||
You can pass raw data (shader code) directly into the function (example uses variable ''myShader_raw_data''). | |||
}} | |||
{{Updated feature/item|1.5.9|1.5.8|20688| | |||
This example creates a basic shader using macros to change shader's behaviour: | |||
<syntaxhighlight lang="lua"> | |||
local shaderRawStr = [[ | |||
texture MACRO_TEX_NAME; | |||
technique simple | |||
{ | |||
pass P0 | |||
{ | |||
//-- Set up texture stage 0 | |||
Texture[0] = MACRO_TEX_NAME; | |||
ColorOp[0] = SelectArg1; | |||
#ifdef MACRO_FIRST_ARG | |||
ColorArg1[0] = Texture; | |||
#else | |||
ColorArg1[0] = Diffuse; | |||
#endif | |||
AlphaOp[0] = SelectArg1; | |||
AlphaArg1[0] = Texture; | |||
//-- Disable texture stage 1 | |||
ColorOp[1] = Disable; | |||
AlphaOp[1] = Disable; | |||
} | |||
} | |||
]] | |||
addEventHandler( "onClientResourceStart", resourceRoot, | |||
function ( ) | |||
local shader, tech = dxCreateShader( shaderRawStr, { MACRO_TEX_NAME = "Tex0", MACRO_FIRST_ARG = true } ) | |||
if not shader or tech ~= "simple" then | |||
outputDebugString( "An error was occured" ) | |||
return | |||
end | |||
local texture = dxCreateTexture( "test.png" ) | |||
if not texture then | |||
outputDebugString( "An error was occured" ) | |||
return | |||
end | |||
dxSetShaderValue( shader, "Tex0", texture ) | |||
addEventHandler( "onClientRender", root, | |||
function() | |||
dxDrawImage( 0, 0, 500, 500, shader ) | |||
end | |||
, false ) | |||
end | |||
, false ) | |||
</syntaxhighlight> | |||
You can also pass macros in key-value pairs when the order is important. | |||
}} | |||
==Changelog== | ==Changelog== | ||
{{ChangelogHeader}} | {{ChangelogHeader}} | ||
{{ChangelogItem|1.3.0-9.04435|Added layered and elementTypes arguments}} | {{ChangelogItem|1.3.0-9.04435|Added layered and elementTypes arguments}} | ||
{{ChangelogItem|1.5.6-9.14403|Added option to use raw data instead of a file name}} | |||
{{ChangelogItem|1.5.8-9.20688|Added option to use macros}} | |||
==See Also== | ==See Also== | ||
{{Drawing_functions}} | {{Drawing_functions}} | ||
[[hu:dxCreateShader]] |
Latest revision as of 23:18, 8 February 2023
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 existing PC.
Syntax
element, string dxCreateShader ( string filepath / string raw_data [ [, table macros = {} ], float priority = 0, float maxDistance = 0, bool layered = false, string elementTypes = "world,ped,vehicle,object,other,all" ] )
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
- macros: A table contains macros in an ordered and/or unordered way. See example below.
- 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
Returns
- 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.
Example
addEventHandler( "onClientRender", root, function() if myShader then dxDrawImage( 100, 350, 300, 350, myShader ) end end ) -- Use 'toggle' command to switch shader on and off addCommandHandler( "toggle", function() if not myShader then myShader = dxCreateShader( "fancything.fx" ) -- Create shader else destroyElement( myShader ) -- Destroy shader myShader = nil end end )
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") else outputDebugString("Unable to load texture", 1) end else outputDebugString("Unable to create shader", 1) end end)
You can pass raw data (shader code) directly into the function (example uses variable myShader_raw_data).
This example creates a basic shader using macros to change shader's behaviour:
local shaderRawStr = [[ texture MACRO_TEX_NAME; technique simple { pass P0 { //-- Set up texture stage 0 Texture[0] = MACRO_TEX_NAME; ColorOp[0] = SelectArg1; #ifdef MACRO_FIRST_ARG ColorArg1[0] = Texture; #else ColorArg1[0] = Diffuse; #endif AlphaOp[0] = SelectArg1; AlphaArg1[0] = Texture; //-- Disable texture stage 1 ColorOp[1] = Disable; AlphaOp[1] = Disable; } } ]] addEventHandler( "onClientResourceStart", resourceRoot, function ( ) local shader, tech = dxCreateShader( shaderRawStr, { MACRO_TEX_NAME = "Tex0", MACRO_FIRST_ARG = true } ) if not shader or tech ~= "simple" then outputDebugString( "An error was occured" ) return end local texture = dxCreateTexture( "test.png" ) if not texture then outputDebugString( "An error was occured" ) return end dxSetShaderValue( shader, "Tex0", texture ) addEventHandler( "onClientRender", root, function() dxDrawImage( 0, 0, 500, 500, shader ) end , false ) end , false )
You can also pass macros in key-value pairs when the order is important.
Changelog
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 |
1.5.8-9.20688 | Added option to use macros |
See Also
- dxConvertPixels
- dxCreateFont
- dxCreateRenderTarget
- dxCreateScreenSource
- dxCreateShader
- dxCreateTexture
- dxDrawCircle
- dxDrawImage
- dxDrawImageSection
- dxDrawLine
- dxDrawLine3D
- dxDrawMaterialLine3D
- dxDrawMaterialPrimitive
- dxDrawMaterialPrimitive3D
- dxDrawMaterialSectionLine3D
- dxDrawPrimitive
- dxDrawPrimitive3D
- dxDrawRectangle
- dxDrawText
- dxDrawWiredSphere
- dxGetBlendMode
- dxGetFontHeight
- dxGetMaterialSize
- dxGetPixelColor
- dxGetPixelsSize
- dxGetPixelsFormat
- dxGetStatus
- dxGetTextSize
- dxGetTextWidth
- dxGetTexturePixels
- dxIsAspectRatioAdjustmentEnabled
- dxSetAspectRatioAdjustmentEnabled
- dxSetBlendMode
- dxSetPixelColor
- dxSetRenderTarget
- dxSetShaderValue
- dxSetShaderTessellation
- dxSetShaderTransform
- dxSetTestMode
- dxSetTextureEdge
- dxSetTexturePixels
- dxUpdateScreenSource