Jump to content

Create one shader but replace multiple textures


Dzsozi (h03)

Recommended Posts

Hello!

I would like to optimize my character system and create only one shader for every player, instead of creating a shader for every player and every clothing type. My current script handles shaders this way:

for k, player in pairs(getElementsByType("player")) do
  cachedData[player] = {}
  cachedData[player].clothingShader = {}
  cachedData[player].bodyShader = {}
end

...

cachedData[player].clothingShader[clothingType] = dxCreateShader("files/shader.fx", 0, SHADER_MAX_VISIBLE_DISTANCE, true, "ped")

Where the clothingType key is the actual clothing type from here https://wiki.multitheftauto.com/wiki/CJ_Clothes of the cloth or body texture that I would like to change.

I tried changing up the things little bit and made this to my shader file (before I used the simple texture replace shader), this is my bodyShader.fx:

texture cj_ped_torso;
texture cj_ped_head;
texture cj_ped_legs;
texture cj_ped_feet;

technique TexReplace
{
    pass P0
    {
		DepthBias = -0.0002;
        Texture[0] = cj_ped_torso;
        Texture[1] = cj_ped_head;
        Texture[2] = cj_ped_legs;
        Texture[3] = cj_ped_feet;
    }
}

I named the textures after the texture to replace, so therefore I can use a table where clothing type is the key and the texture to replace is the value, that way I can easily replace the textures.

My problem now is that the same texture gets applied everywhere, how can I fix this? As I mentioned before, I would like to create only one shader for body textures and only one shader for clothing textures for every player, if that is possible. Here's my function for changing the face texture for an example of how I did things.

clothingTypeToTexture = {
	[0] = "cj_ped_torso",
	[1] = "cj_ped_head",
	[2] = "cj_ped_legs",
	[3] = "cj_ped_feet",
	[13] = "cj_ped_necklace",
	[14] = "cj_ped_watch",
	[15] = "cj_ped_glasses",
	[16] = "cj_ped_hat",
	[17] = "cj_ped_extra1",
}

local function createBodyShader(player)
	if not isElement(cachedData[player].bodyShader) then
		return dxCreateShader("files/bodyShader.fx", 0, SHADER_MAX_VISIBLE_DISTANCE, false, "ped")
	end
	return nil
end

function changePlayerFaceTexture(player, faceID)
	if utility:isPlayer(player) and cachedData[player] then
		if availableFaceTextures[tonumber(faceID)] == "face-" .. tostring(faceID) then
			faceID = tonumber(faceID)
			local skinColor = currentSkinColor[player] or defaultSkinColor
			
			cachedData[player].bodyShader = cachedData[player].bodyShader or createBodyShader(player)
			
			engineApplyShaderToWorldTexture(cachedData[player].bodyShader, clothingTypeToTexture[1], player, false)
			dxSetShaderValue(cachedData[player].bodyShader, clothingTypeToTexture[1], cachedFaceTextures[skinColor][faceID])
			
			currentFace[player] = faceID
		end
	end
end

What is the problem with this script? When I call the changePlayerBodyTexture function, which is almost the same structure but for the other body parts, every part gets the same texture, even the face, but it is not defined there, since it is being handled with a different function (this).

EDIT: I realised that the problem is in the shader file, the texture that is used to replace the other is always the one that is defined as Texture[0] in the shader file at the pass. How can I make it functioning so I can handle all four in a different way?

Edited by Dzsozi (h03)
Link to comment

That has nothing to do with my problem.

Quote

EDIT: I realised that the problem is in the shader file, the texture that is used to replace the other is always the one that is defined as Texture[0] in the shader file at the pass. How can I make it functioning so I can handle all four in a different way?

My problem is that I don't know how to write the shader file properly to handle 4 textures that is being changed in the script.

Link to comment

Try this:

texture cj_ped_torso;
texture cj_ped_head;
texture cj_ped_legs;
texture cj_ped_feet;

technique TexReplace
{
	pass P0
	{
		DepthBias = -0.0002;
		Texture[0] = cj_ped_torso;
	}
	pass P1
	{
		DepthBias = -0.0002;
		Texture[0] = cj_ped_head;
	}
	pass P2
	{
		DepthBias = -0.0002;
		Texture[0] = cj_ped_legs;
	}
	pass P3
	{
		DepthBias = -0.0002;
		Texture[0] = cj_ped_feet;
	}
}

 

Link to comment
  • Discord Moderators
4 hours ago, Jayceon said:

Try this:


texture cj_ped_torso;
texture cj_ped_head;
texture cj_ped_legs;
texture cj_ped_feet;

technique TexReplace
{
	pass P0
	{
		DepthBias = -0.0002;
		Texture[0] = cj_ped_torso;
	}
	pass P1
	{
		DepthBias = -0.0002;
		Texture[0] = cj_ped_head;
	}
	pass P2
	{
		DepthBias = -0.0002;
		Texture[0] = cj_ped_legs;
	}
	pass P3
	{
		DepthBias = -0.0002;
		Texture[0] = cj_ped_feet;
	}
}

 

That will just simply replace the texture to cj_ped_feet, I think.

Link to comment
On 30/10/2018 at 14:29, Jayceon said:

Try this:


texture cj_ped_torso;
texture cj_ped_head;
texture cj_ped_legs;
texture cj_ped_feet;

technique TexReplace
{
	pass P0
	{
		DepthBias = -0.0002;
		Texture[0] = cj_ped_torso;
	}
	pass P1
	{
		DepthBias = -0.0002;
		Texture[0] = cj_ped_head;
	}
	pass P2
	{
		DepthBias = -0.0002;
		Texture[0] = cj_ped_legs;
	}
	pass P3
	{
		DepthBias = -0.0002;
		Texture[0] = cj_ped_feet;
	}
}

 

@Pirulax is right, it doesn't work, the feet texture is being put on every part of the character.

Link to comment

Yes, maybe it is not a big performance question at all, but it would be also easier to change the textures, I mean less lines in the script itself. For example not having to destroy all the shaders and such, just change the shader value.

@Ren_712 Can you give me a helping hand regarding the question of this topic?

Edited by Dzsozi (h03)
Link to comment
4 hours ago, JustinMTA said:

Just learn to do it in a modeling program like zModeler

Well, thanks for your help, that I way I could "just learn" HLSL shader programing and fix the problem on my own. Could you already please tell me what does the modeling has to do with my problem? Learn to do what in a modeling program? I don't even understand... I already made my system and it is working, I just want to improve it by minimizing the amount of shaders I use in my script and that's what I need help with, writing a new shader file that can handle multiple texture replacing.

Quote

Create one shader but replace multiple textures

 

Link to comment

*Sighs*
Okay, ask yourself what shaders actually are bro, (you're applying a texture to an object ped or vehicle)
What are objects peds or vehicles created in? modeling programs.
And what you're wanting here (to be done right) will always use UV mapping because the shape of peds require a lot of bending of the texture.
Long story short I took a few minutes to try and find the default CJ.dff and couldn't, but I found an HD version, the HD version I found has the face, torso, and pants separated, I'm not sure if the default unmodified CJ has the them also separated, but in my honest opinion, you're probably not using the right texture names..
And it also looks like you've got a lot of unnecessary code going on also..
With some quick semi enthusiastic research you could find the answer easy bro xD
There's countless CJ modifications on the internet you could combine all into 1 mod because they'll probably use the same UV map.
You could even find a ped with skin, shirt, shoes, accessories all as separate textures... There's many out there..
There's countless shader examples you can find too.
(Listen man go on Youtube and the MTA forum, use the search function/kitty script from the MTA resources and learn some things)
https://community.multitheftauto.com/index.php?p=resources
sdasdasdasdasdasdasdasd.png

Link to comment

****Sighs****

Okay so you are trying to tell me to look up the wiki and community and basically the whole internet to create a new shader file, that - as I can see I must write it down again - can handle the replace of 4 textures at the same time. What kind of modeller are you if you couldn't find the default CJ textures in the game files bro? xD

48 minutes ago, JustinMTA said:

What are objects peds or vehicles created in? modeling programs.
And what you're wanting here (to be done right) will always use UV mapping because the shape of peds require a lot of bending of the texture.

You are 100% right! But what does that have to do with my problem, nothing? It's a fact that peds vehicles and objects are being made in a modeling program bro, no sh#t!

Have you ever checked this function on the wiki https://wiki.multitheftauto.com/wiki/EngineApplyShaderToWorldTexture ? I quote:

Quote
  • CJ body parts textures can be replaced by using: "cj_ped_head", "cj_ped_hat", "cj_ped_torso", "cj_ped_legs", "cj_ped_feet", "cj_ped_glasses", "cj_ped_necklace", "cj_ped_watch" and "cj_ped_extra1". Latest version of shader_tex_names will show what is being used.

so by that you probably already figured out that I am using the correct texture names, just because your HD downloaded CJ mod doesn't have its files named the same (let me guess they are called player_torso, player_legs, etc), it doesn't mean I am using the wrong textures, since MTA handles those texture names in a different way as we can see, with some quick semi enthusiastic research you could find the answer easy bro. xD

Why are you linking me the MTA community site, I don't even know, so let's not talk about that.

Well, about countless shader examples, I wouldn't say they are countless. But they are not examples for me in this case.

Like you said, 

Quote

Okay, ask yourself what shaders actually are bro, (you're applying a texture to an object ped or vehicle)

yes, those are shaders, and this is what I use them for in this case as well. REPLACE TEXTURES, which means I edit the default texture, for example the torso and REPLACE it with a shader. So what does modeling has to do with it, I still don't get it. I don't have to do any UV mapping nor any modeling, why would I? You yourself said that shaders are for applying a texture to an object ped or vehicle. Looks like you are not completely understanding how texture replacing works if you only edit the default texture(?)

You can keep telling me to go on YouTube or MTA forum (well, in this case this is what I just did, and that's the reason for this topic being existent, so again, I don't understand you), but there's a reason I made this topic... Probably because I didn't find the proper help I need and I am hoping for someone to help me out, don't you think? I didn't create this topic because I was bored.

So, from now on, please avoid telling me the same thing "over and over in different ways" bro. xD Because you are not helping me, you are just trying to prove me wrong, that I must go and learn from wiki and search up the whole internet for a solution to my problem. I just asked a simple question which is: HOW CAN I MAKE A SHADER THAT CAN HANDLE MULTIPLE TEXTURE REPLACES AT ONCE??? No modeling, no UV mapping, no Facebook or YouTube or Twitter search for help, just a simple question on MTA forum in a topic about shaders. You don't have to tell me that I am doing it wrong and must find a different way via modeling, (even if I already have my system ready, just willing to improve it) when you are the one who doesn't even know where to find the default game textures.

And now, back to the main question again:

HOW CAN I MAKE A SHADER THAT CAN HANDLE MULTIPLE TEXTURE REPLACES AT ONCE???

xD

doYGhPZ.png

???

  • Like 1
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...