Jump to content

Set object color?


Captain Cody

Recommended Posts

You may have to edit the code if you want it to work with just one object.

Lua code

local texName = "" 
local r, g, b, a = 255, 0, 0, 255 
  
shader = dxCreateShader("shader.fx", 0, 0, false) 
engineApplyShaderToWorldTexture(shader, texName) 
dxSetShaderValue(shader, "gColor", r, g, b, a) 

Shader

float4 gColor = float4(1,1,1,1); 
bool bIsGTADiffuse = true; 
  
//--------------------------------------------------------------------- 
// Include some common stuff 
//--------------------------------------------------------------------- 
#include "mta-helper.fx" 
  
  
//--------------------------------------------------------------------- 
// Sampler for the main texture 
//--------------------------------------------------------------------- 
sampler Sampler0 = sampler_state 
{ 
    Texture = (gTexture0); 
}; 
  
  
//--------------------------------------------------------------------- 
// Structure of data sent to the vertex shader 
//--------------------------------------------------------------------- 
struct VSInput 
{ 
  float3 Position : POSITION0; 
  float4 Diffuse : COLOR0; 
  float2 TexCoord : TEXCOORD0; 
}; 
  
//--------------------------------------------------------------------- 
// Structure of data sent to the pixel shader ( from the vertex shader ) 
//--------------------------------------------------------------------- 
struct PSInput 
{ 
  float4 Position : POSITION0; 
  float4 Diffuse : COLOR0; 
  float2 TexCoord : TEXCOORD0; 
}; 
  
  
//------------------------------------------------------------------------------------------ 
// VertexShaderFunction 
//  1. Read from VS structure 
//  2. Process 
//  3. Write to PS structure 
//------------------------------------------------------------------------------------------ 
PSInput VertexShaderFunction(VSInput VS) 
{ 
    PSInput PS = (PSInput)0; 
  
    // Calculate screen pos of vertex 
    PS.Position = MTACalcScreenPosition ( VS.Position ); 
  
    // Pass through tex coord 
    PS.TexCoord = VS.TexCoord; 
  
    // Calculate GTA lighting for buildings 
    float4 Diffuse = MTACalcGTABuildingDiffuse( VS.Diffuse ); 
    PS.Diffuse = 0; 
    if (bIsGTADiffuse) PS.Diffuse = Diffuse; 
    else PS.Diffuse = float4(1,1,1,Diffuse.a); 
    PS.Diffuse *= gColor; 
    return PS; 
} 
  
//------------------------------------------------------------------------------------------ 
// PixelShaderFunction 
//  1. Read from PS structure 
//  2. Process 
//  3. Return pixel color 
//------------------------------------------------------------------------------------------ 
float4 PixelShaderFunction(PSInput PS) : COLOR0 
{ 
    // Get texture pixel 
    float4 texel = tex2D(Sampler0, PS.TexCoord); 
  
    // Apply diffuse lighting 
    float4 finalColor = texel * PS.Diffuse; 
  
    return finalColor; 
} 
  
  
//------------------------------------------------------------------------------------------ 
// Techniques 
//------------------------------------------------------------------------------------------ 
technique colorize 
{ 
    pass P0 
    { 
        VertexShader = compile vs_2_0 VertexShaderFunction(); 
        PixelShader = compile ps_2_0 PixelShaderFunction(); 
    } 
} 
  
// Fallback 
technique fallback 
{ 
    pass P0 
    { 
        // Just draw normally 
    } 
} 

Link to comment

Make sure you also have mta-helper.fx.

// 
// mta-helper.fx
//
// File version: 0.0.1
// Date updated: 2011-09-26
//
// Big file of doom containg most of the stuff you need to get shaders working with MTA
//
 
 
//
// This file has 4 sections:
//      1. Variables
//      2. Renders states (parital - includes only those that are used the most)
//      3. Helper functions
//      4. Normal generation
//
 
 
 
//####################################################################################################################
//####################################################################################################################
//
// Section #1 : Variables
//
//####################################################################################################################
//####################################################################################################################
 
//---------------------------------------------------------------------
// These parameters are set by MTA whenever a shader is drawn
//---------------------------------------------------------------------
 
//
// Matrices
//
float4x4 gWorld : WORLD;
float4x4 gView : VIEW;
float4x4 gProjection : PROJECTION;
float4x4 gWorldView : WORLDVIEW;
float4x4 gWorldViewProjection : WORLDVIEWPROJECTION;
float4x4 gViewProjection : VIEWPROJECTION;
float4x4 gViewInverse : VIEWINVERSE;
float4x4 gWorldInverseTranspose : WORLDINVERSETRANSPOSE;
float4x4 gViewInverseTranspose : VIEWINVERSETRANSPOSE;
 
//
// Camera
//
float3 gCameraPosition : CAMERAPOSITION;
float3 gCameraDirection : CAMERADIRECTION;
 
//
// Seconds counter
//
float gTime : TIME;
 
//
// Strongest light influence
//
float4 gLightAmbient : LIGHTAMBIENT;
float4 gLightDiffuse : LIGHTDIFFUSE;
float4 gLightSpecular : LIGHTSPECULAR;
float3 gLightDirection : LIGHTDIRECTION;
 
 
 
//####################################################################################################################
//####################################################################################################################
//
// Section #2 : Renders states
//
//####################################################################################################################
//####################################################################################################################
 
//---------------------------------------------------------------------
// The parameters below mirror the contents of the D3D registers.
// They are only relevant when using engineApplyShaderToWorldTexture.
//---------------------------------------------------------------------
 
//------------------------------------------------------------------------------------------
// renderState (partial) - String value should be one of D3DRENDERSTATETYPE without the D3DRS_  http://msdn.microsoft.com/en-us/library/bb172599%28v=vs.85%29.aspx
//------------------------------------------------------------------------------------------
 
int gLighting                      < string renderState="LIGHTING"; >;                        //  = 137,
 
float4 gGlobalAmbient              < string renderState="AMBIENT"; >;                    //  = 139,
 
int gDiffuseMaterialSource         < string renderState="DIFFUSEMATERIALSOURCE"; >;           //  = 145,
int gSpecularMaterialSource        < string renderState="SPECULARMATERIALSOURCE"; >;          //  = 146,
int gAmbientMaterialSource         < string renderState="AMBIENTMATERIALSOURCE"; >;           //  = 147,
int gEmissiveMaterialSource        < string renderState="EMISSIVEMATERIALSOURCE"; >;          //  = 148,
 
 
//------------------------------------------------------------------------------------------
// materialState - String value should be one of the members from D3DMATERIAL9  http://msdn.microsoft.com/en-us/library/bb172571%28v=VS.85%29.aspx
//------------------------------------------------------------------------------------------
 
float4 gMaterialAmbient     < string materialState="Ambient"; >;
float4 gMaterialDiffuse     < string materialState="Diffuse"; >;
float4 gMaterialSpecular    < string materialState="Specular"; >;
float4 gMaterialEmissive    < string materialState="Emissive"; >;
float gMaterialSpecPower    < string materialState="Power"; >;
 
 
//------------------------------------------------------------------------------------------
// textureState (partial) - String value should be a texture number followed by 'Texture'
//------------------------------------------------------------------------------------------
 
texture gTexture0           < string textureState="0,Texture"; >;
texture gTexture1           < string textureState="1,Texture"; >;
texture gTexture2           < string textureState="2,Texture"; >;
texture gTexture3           < string textureState="3,Texture"; >;
 
 
//------------------------------------------------------------------------------------------
// vertexDeclState  (partial)
//------------------------------------------------------------------------------------------
 
int gDeclNormal             < string vertexDeclState="Normal"; >;       // Set to 1 if vertex stream includes normals
 
 
 
//####################################################################################################################
//####################################################################################################################
//
// Section #3 : Helper functions
//
//####################################################################################################################
//####################################################################################################################
 
//------------------------------------------------------------------------------------------
// MTAUnlerp
// - Find a the relative position between 2 values
//------------------------------------------------------------------------------------------
float MTAUnlerp( float from, float to, float pos )
{
    if ( from == to )
        return 1.0;
    else
        return ( pos - from ) / ( to - from );
}
 
 
//------------------------------------------------------------------------------------------
// MTACalcScreenPosition
// - Transform vertex position for the camera
//------------------------------------------------------------------------------------------
float4 MTACalcScreenPosition( float3 InPosition )
{
    float4 posWorld = mul(float4(InPosition,1), gWorld);
    float4 posWorldView = mul(posWorld, gView);
    return mul(posWorldView, gProjection);
}
 
//------------------------------------------------------------------------------------------
// MTACalcWorldPosition
// - Transform position by current world matix
//------------------------------------------------------------------------------------------
float3 MTACalcWorldPosition( float3 InPosition )
{
    return mul(float4(InPosition,1), gWorld).xyz;
}
 
//------------------------------------------------------------------------------------------
// MTACalcWorldNormal
// - Rotate normal by current world matix
//------------------------------------------------------------------------------------------
float3 MTACalcWorldNormal( float3 InNormal )
{
    return mul(InNormal, (float3x3)gWorld);
}
 
//------------------------------------------------------------------------------------------
// MTACalcGTABuildingDiffuse
// - Calculate GTA lighting for buildings
//------------------------------------------------------------------------------------------
float4 MTACalcGTABuildingDiffuse( float4 InDiffuse )
{
    float4 OutDiffuse;
 
    if ( !gLighting )
    {
        // If lighting render state is off, pass through the vertex color
        OutDiffuse = InDiffuse;
    }
    else
    {
        // If lighting render state is on, calculate diffuse color by doing what D3D usually does
        float4 ambient  = gAmbientMaterialSource  == 0 ? gMaterialAmbient  : InDiffuse;
        float4 diffuse  = gDiffuseMaterialSource  == 0 ? gMaterialDiffuse  : InDiffuse;
        float4 emissive = gEmissiveMaterialSource == 0 ? gMaterialEmissive : InDiffuse;
        OutDiffuse = gGlobalAmbient * saturate( ambient + emissive );
        OutDiffuse.a *= diffuse.a;
    }
    return OutDiffuse;
}
 
//------------------------------------------------------------------------------------------
// MTACalcGTAVehicleDiffuse
// - Calculate GTA lighting for vehicles
//------------------------------------------------------------------------------------------
float4 MTACalcGTAVehicleDiffuse( float3 WorldNormal, float4 InDiffuse)
{
    // Calculate diffuse color by doing what D3D usually does
    float4 ambient  = gAmbientMaterialSource  == 0 ? gMaterialAmbient  : InDiffuse;
    float4 diffuse  = gDiffuseMaterialSource  == 0 ? gMaterialDiffuse  : InDiffuse;
    float4 emissive = gEmissiveMaterialSource == 0 ? gMaterialEmissive : InDiffuse;
 
    float4 TotalAmbient = ambient * ( gGlobalAmbient + gLightAmbient );
 
    // Add the strongest light
    float DirectionFactor = max(0.2, dot(WorldNormal, -gLightDirection ));
    float4 TotalDiffuse = (diffuse * DirectionFactor );
 
    float4 OutDiffuse = saturate(TotalDiffuse + TotalAmbient + emissive);
    OutDiffuse.a *= diffuse.a;
 
    return OutDiffuse;
}
 
float4 MTACalcGTAVehicleDiffuse2( float3 WorldNormal, float4 InDiffuse, float3 LightDirection )
{
    // Calculate diffuse color by doing what D3D usually does
    float4 ambient  = gAmbientMaterialSource  == 0 ? gMaterialAmbient  : InDiffuse;
    float4 diffuse  = gDiffuseMaterialSource  == 0 ? gMaterialDiffuse  : InDiffuse;
    float4 emissive = gEmissiveMaterialSource == 0 ? gMaterialEmissive : InDiffuse;
 
    float4 TotalAmbient = ambient * ( gGlobalAmbient + gLightAmbient );
 
    // Add the strongest light
    float3 lightDirection = LightDirection;
    float DirectionFactor = max(0.2, dot(WorldNormal, -lightDirection ));
    float4 TotalDiffuse = (diffuse * DirectionFactor );
 
    float4 OutDiffuse = saturate(TotalDiffuse + TotalAmbient + emissive);
    OutDiffuse.a *= diffuse.a;
 
    return OutDiffuse;
}
 
float4 MTACalcGTADynamicDiffuse( float3 WorldNormal, float4 InDiffuse, float3 LightDirection )
{
    // Calculate diffuse color by doing what D3D usually does
    float4 ambient  = gAmbientMaterialSource  == 0 ? gMaterialAmbient  : InDiffuse;
    float4 diffuse  = gDiffuseMaterialSource  == 0 ? gMaterialDiffuse  : InDiffuse;
    float4 emissive = gEmissiveMaterialSource == 0 ? gMaterialEmissive : InDiffuse;
 
    float4 TotalAmbient = ambient * ( gGlobalAmbient + gLightAmbient );
 
    // Add the strongest light
    float DirectionFactor = max(0,dot(WorldNormal, -LightDirection ));
    float4 TotalDiffuse =
Link to comment

Here's code I have

  
shader = dxCreateShader("shader.fx", 1000, 0, false) 
  
function shaders () 
local vehicles = getElementsByType ( "vehicle" ) 
for i,vehiclez in ipairs(vehicles) do  
outputDebugString("Vehicles Found") 
local modded = getElementData (vehiclez,"ModdedinTune") 
if modded == true then 
for i, aObjects in ipairs(getAttachedElements ( vehiclez )) do 
outputDebugString("Shaders added") 
local r, g, b = getVehicleColor ( vehiclez, true ) 
engineApplyShaderToWorldTexture(shader, "#emapjesterbody256",aObjects) 
engineApplyShaderToWorldTexture(shader, "vehiclegrunge256",aObjects) 
dxSetShaderValue(shader, "gColor", r, g, b, 255) 
   end 
end 
end 
end 

Link to comment
  • 4 years later...
Spoiler

spacer.png

spacer.png

Code:
 

local obj
local shader = dxCreateShader("shader.fx", 0, 0, false) 

function positioning()
    local x,y,z = getPositionFromElementOffset(localPlayer, 0, 1.2, 0)
    local rx,ry,rz = getElementRotation(localPlayer)

    setElementPosition(obj, x,y,z)
    setElementRotation(obj, rx,ry,rz)
        

end

function setItemPosition(button, press)
    if button == "mouse1" and press then

        setElementAlpha(obj, 255)
        setElementCollisionsEnabled(obj, true)
        engineApplyShaderToWorldTexture(shader, "crate128") 
        dxSetShaderValue(shader, "gColor", 255, 255, 255, 255) 

        removeEventHandler("onClientRender",root, positioning)
        removeEventHandler("onClientKey",root, setItemPosition)
        
    end

end

function getPositionFromElementOffset(element,offX,offY,offZ)
    local m = getElementMatrix ( element )  -- Get the matrix
    local x = offX * m[1][1] + offY * m[2][1] + offZ * m[3][1] + m[4][1]  -- Apply transform
    local y = offX * m[1][2] + offY * m[2][2] + offZ * m[3][2] + m[4][2]
    local z = offX * m[1][3] + offY * m[2][3] + offZ * m[3][3] + m[4][3]
    return x, y, z                               -- Return the transformed point
end


addCommandHandler("criar", function()

    local x,y,z = getPositionFromElementOffset(localPlayer, 0, 1.2, 0)
    local rx,ry,rz = getElementRotation(localPlayer)
    obj = createObject(1224, x,y,z,rx,ry,rz)

    setElementAlpha(obj, 141)
    setElementCollisionsEnabled(obj, false)

    engineApplyShaderToWorldTexture(shader, "crate128") 
    dxSetShaderValue(shader, "gColor", 0, 255, 0, 200) 



    addEventHandler("onClientRender",root, positioning)
    addEventHandler("onClientKey",root, setItemPosition)

end)

Error:

spacer.png

I am starting the script and this warning already appears.

shader line 13,9 -> sampler Sampler0 = sampler_state 

Edited by Gaimo
Link to comment
  • Tut locked this topic
Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...