Captain Cody Posted February 20, 2016 Share Posted February 20, 2016 Is there any way to set an objects color, currently working on a fully custom mod/tune shop for my server, but I cannot figure out how to change the color of the part 'If at all possible'. Link to comment
Noki Posted February 20, 2016 Share Posted February 20, 2016 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
Captain Cody Posted February 20, 2016 Author Share Posted February 20, 2016 How would I get the texture name? Never mind about that. How would I go about making it load for every person, instead of the just the one who made it? Link to comment
SpecT Posted February 20, 2016 Share Posted February 20, 2016 How would I get the texture name? Never mind about that.How would I go about making it load for every person, instead of the just the one who made it? The 3rd argument of engineApplyShaderToWorldTexture is targetElement. So you could loop thru all the players and apply it to everyone. Link to comment
Captain Cody Posted February 20, 2016 Author Share Posted February 20, 2016 But what about players who joined in after? Almost every every individual part needs to be colored to match the base vehicle. Looping through this would cause server to implode with enough people online. Link to comment
Captain Cody Posted February 20, 2016 Author Share Posted February 20, 2016 Would I have to store the info of what has shaders in a table or something? Link to comment
Captain Cody Posted February 20, 2016 Author Share Posted February 20, 2016 -- I tried the code, but it's not working, nothing is happening Link to comment
Noki Posted February 21, 2016 Share Posted February 21, 2016 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
Captain Cody Posted February 21, 2016 Author Share Posted February 21, 2016 I do have it. Link to comment
Captain Cody Posted February 21, 2016 Author Share Posted February 21, 2016 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
Captain Cody Posted February 21, 2016 Author Share Posted February 21, 2016 Well it appears the shader is being applied, but there's no color it's just pitch black. Link to comment
Captain Cody Posted February 21, 2016 Author Share Posted February 21, 2016 Anything I'm doing wrong here? Link to comment
Captain Cody Posted February 21, 2016 Author Share Posted February 21, 2016 Any body?? Link to comment
ozulus Posted February 22, 2016 Share Posted February 22, 2016 You are appliying shader to object but you wrote vehicle texture name(vehiclegrunge256) in engineApplyShaderToTexture. Write the texture name of attached element instead of vehiclegrunge256 Link to comment
Captain Cody Posted February 22, 2016 Author Share Posted February 22, 2016 vehiclegrunge256 is the texture name I checked the textures on the objects, and they either have vehiclegrunge or the jester Link to comment
Gaimo Posted January 30, 2021 Share Posted January 30, 2021 (edited) Spoiler 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: I am starting the script and this warning already appears. shader line 13,9 -> sampler Sampler0 = sampler_state Edited January 30, 2021 by Gaimo Link to comment
Administrators Tut Posted February 1, 2021 Administrators Share Posted February 1, 2021 As this is being gravebumped, I'll apply a lock - please consider creating a new thread as needed Link to comment
Recommended Posts