Set Color of Sub Material

I have downloaded a present model from the workshop that has 3 sub materials. What’s the best way to set the colour of these submaterials?

I want to set the colour server-side so everyone sees.

The $color2 material variable should work, it takes a normalized vector

That will change it on every model the material is used it though, if you are going to use Material():SetVector() or whatever

I basically have a Sub Material called ‘griim/christmas/present_box_colourable’ and want to be able to create a red, green, blue and yellow version of it. At the moment it’s plain white.

Can anyone provide an example? I’ve tried playing with CreateMaterial but unsure how that works, and apart from that there doesn’t seem to be a quick an easy way to do this.

Any ideas?

If its colourable, couldnt you use the color tool on that? Some materials are set to work with that already

I already told you Daniel.

CreateMaterial with different $color and apply it as a submat

CreateMaterial can only be used client-side? Is there no way to do it server-side? Surely otherwise you’d have to send every entity that needs re-colouring to the client and change it for each player?

The entirety of rendering is done clientside. Whatever you do, you WILL have to network it (Your color choices) to every client and render it in whatever way you choose on the client. That is how it works.

Ok thanks - I wasn’t sure if like Entity:SetColor there was a way to do this server-side for sub-materials.

Would you mind providing an example of how I can create a new material based on griim/christmas/present_box_colourable and give it a new color attribute? Would I just use $color as a parameter, if so, what would this look like in the params array?

Look at how DarkRP Arrest stick does it. https://github.com/FPtje/DarkRP/blob/master/entities/weapons/stick_base/shared.lua#L43

This is how I’m trying to do it using the example on https://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index3aee.html however i have no idea if this is on the right line.

This is in the cl_init.lua file of my entity.



function ENT:Initialize()

    CreateMaterial("MyMaterial", "UnlitGeneric", {
		["$basetexture"] = "models/debug/debugwhite",
		["$nodecal"] = 1,
		["$model"] = 1,
		["$additive"] = 1,
		["$nocull"] = 1,
                ["$color"] = "{ 255 0 0 }"
    })
    self:SetSubMaterial(1,"MyMaterial")
    
end


Umm thats why I linked you to something that is already basicly doing what you want. Read over that and try adjusting your code accordingly. Make sure you are also reading up on the wiki https://wiki.garrysmod.com/page/Entity/SetSubMaterial because it clearly states you’ll need to add an ! Before your mat name in order to use materials created in Lua.

Ok, I’ve given this a try and can’t work it out. I did look at the DarkRP setup, but then tried changing it as it didn’t work - however I had forgot about the ! before material name - so please don’t think I’m just ignoring advice and wanting it handed too me.

This is what I’ve tried now:




    CreateMaterial("MyMaterial", "UnlitGeneric", {
        ["$basetexture"] = "models/debug/debugwhite",
        ["$surfaceprop"] = "metal",
        ["$envmap"] = "env_cubemap",
        ["$envmaptint"] = "[ .5 .5 .5 ]",
        ["$selfillum"] = 0,
        ["$model"] = 1
    }):SetVector("$color", Vector(255,0,0))

    self:SetSubMaterial( 1, "!MyMaterial" )



I have tried a few variations of the above and still don’t seem to have any affect. Right now I’m not 100% sure what everything I’ve written does, as it’s copy and paste, however right now I’d be happy for any change in the appearance of the entity so I can then adapt and play when I’ve got working code.

So a few questions - what is the first parameter of SetVector, the wiki say’s ‘Sets the MaterialVector’ but what does this mean? Is it meant to be $color? DarkRP uses $color2, where is the reference to this?

[editline]25th December 2016[/editline]

I have tried $color2 as well as the MaterialVector’s name

[editline]25th December 2016[/editline]

Eg



    CreateMaterial("MyNewMaterial", "VertexLitGeneric", {
        ["$basetexture"] = "models/debug/debugwhite",
        ["$surfaceprop"] = "metal",
        ["$envmap"] = "env_cubemap",
        ["$envmaptint"] = "[ .5 .5 .5 ]",
        ["$selfillum"] = 0,
        ["$model"] = 1
    }):SetVector("$color2", Vector(255,0,0))

    self:SetSubMaterial(1,"!MyNewMaterial")


[editline]25th December 2016[/editline]

I’ve also looked at this page https://developer.valvesoftware.com/wiki/$color which is why I thought maybe ["$color"] = “{255,255,255}” might work.

-SNIP-

**This is giving me the best result - I can change the Vector numbers, however they don’t seem to accurately represent RGB. I can get bright yellow, bright green, purple, white etc. But if I did say Vector(50,25,0) it would be bright yellow?
**



function ENT:Draw()
    CreateMaterial("SGNNewMaterial55", "VertexLitGeneric", {
        ["$basetexture"] = "models/debug/debugwhite",
        ["$surfaceprop"] = "metal"
    }):SetVector("$color2", Vector(50,25,255))

    self:SetSubMaterial(0,"!SGNNewMaterial55")
	self:DrawModel()
end



[editline]25th December 2016[/editline]

**AAANNNDDD that would be because it’s not 0-255 but a float 0-1 :slight_smile:

Flipping hell that was a challenge. Should have read more closely.**



function ENT:Draw()
    CreateMaterial("SGNNewMaterial55", "VertexLitGeneric", {
        ["$basetexture"] = "models/debug/debugwhite",
        ["$surfaceprop"] = "metal"
    }):SetVector("$color2", Vector(1.0,0.5,0.19))

    self:SetSubMaterial(0,"!SGNNewMaterial55")
	self:DrawModel()
end


I did say normalized vector

Setting submaterial in Draw is dumbbbbbbbbbbbbbbbbbbb.

Really? I did just message you that :stuck_out_tongue:

I was only doing that as part of testing. However this:




function ENT:Initialize()
	CreateMaterial("BasicColour", "VertexLitGeneric", 
		{ ["$basetexture"] = "models/debug/debugwhite" }
	):SetVector("$color2", Vector(1.0,0.5,0.19))

	self:SetModelScale(0.75)
	
end

function ENT:Draw()
	self:SetSubMaterial(0,"!BasicColour")
	self:DrawModel()
end



Seems to make it come out white, whereas if I set the submaterial in DRAW it works fine. How do you do this properly?