Changin material ingame without changing mesh!

My situation is so I need to change object’s material, but not mesh! The reason is so the change mesh is used for another task, but replacing material can be assigned to multiple object’s meshes(because all of them uses the same materials from which one should be replaced). So, as I told- the object has got multiple materials on it and one of them should be changed according to property of object! If that makes it more complicated(but it shouldn’t) the material uses nodes. It is inside of first slot for all meshes. So… How do I change the material in-game?(as it happens when switching between scenes, the script shouldn’t update at all, just one initial run of it)!

No! I meant it like so:
I have an object that has multiple materials, but one of them is node material and uses object color(if that’s metter:D). There are more objects like that in scene, but in other layers. The first object in main scene can replace mesh to mesh that belonds an object in another layers. The object has a property showing it’s material. And, whatever mesh it is wearing, it’s material in Slot 1(where is the node material) should be applied according to the property! There are totally 3 ways of material’s that it can have. It could also switch between node outputs using one material, but it sounds more complicated. I hope you got it.

For easier to understand:
A character in game(this is for example, but not my situation):
-He can switch his jacket to another brand with different shape(change mesh)
-He can set jacket’s material from leather to canvas etc.(the material switch)
-He can set the materials color(object color, don’t mind this because it is not releated to the required help, but it is just a fact)!

Create up to four materials, canvas, leather etc.
Create a fifh node material. In that node material insert all the other materials, as well as a geometry node (to get vertex color) and a separate rgb node.
Create a chain mixing each material based on a vertex color. Eg:

Vertex color -> separate rgb -> red channel -------> mix factor (fac)
Material 1----------> mix col1
Material 2 ---------> mix col2

Now you can have it either material 1 or material 2 by changing the amount of red in the vertex color. You can then chain them, using the green channel, the mixed material, and a third material. The blue channel, the mix of the previous 3, and a fourth material.

Now in-game you want to pick the material. Write a script to set all the vertex colors. It’s about 5 lines.
I’ll see if i can make a demo blend tomorrow.

Sounds a bit too complicated! Any easier way? Isn’t there a method for just setting the material of object using python in exact slot like it can be used to update texture slots?

Have you noticed that when the material is not applied to any vertex, then it won’t even load to in game?

You can’t change the material without changing the mesh; the material owns the vertices.

I gave you the resources to the same approach @sdfgeoff suggest.
It is complicated, but then again you don’t want to create duplicate meshes either.

Aint there anym more simple methods? And I am really weak when getting something new without video tutorial:(

There is nothing more simple. That is just the limitation of the BGE at the moment.
Here’s a similar method using nodes. It’s set up to allow you to change a python variable (0 -> number of materials - 1) which switches the material the node material outputs.

To explain how it works:

It uses a 1x1 texture to store RGB values (so we can store 3 boolean values). We could also use the alpha, but there are already 8 possible combinations, which I’m sure is fine (the alpha channel would give you 16), but you can also just create another texture.

You could have done this using a single colour value (and checking if it is between different values), except that colour correction messes up the colour ranges, so I’ve done this with booleans (0 or 1 values).

A simple node group “Properties” takes the RGB channels and splits them into “a”, “b”, “c”. You can see this by selecting the Properties node and pressing TAB (SHIFT TAB or the UP arrow icon in the node editor settings bar will exit the group). I just made a node group for naming purposes :slight_smile:

I made some simple binary comparison nodes, which basically check if each “bit” (in our case, a, b and c) equal the corresponding y bits.

A lesson in binary:
Base 2 is a representation scheme of numbers. Base 10 is something you’re already familiar with, and each number is expressed as follows, the TOP row is the powers. The BOTTOM row is the number we’re representing in base 10.

[TABLE=“class: grid, width: 500, align: center”]




1748 in base 10 expressed as different powers of ten.

Base 2 is very similar, except it uses powers of two. In a table form:

[TABLE=“class: grid, width: 500, align: center”]




21 in base 2 expressed as different powers of 2 (reads as 10101)

The difference here, besides the power ascension, is that each value can only be 1 or 0. Base Ten can be 0 - 9. Base five would be 0 - 4 etc…

So, we can express single numbers as a collection of 1s and 0s:

1 = 1
2 = 10
3 = 11
4 = 100
5 = 101
6 = 110

So, that’s how we’re converting from the “material_index” in the script to the different factor values in the mix nodes. We set the r, g, b values of the texture from Python by converting a base 10 number to binary. This gives us three “bits” to work with, which is 2 ** 3 different possible values (8).

We then check the values of these bits to see if they are 1, 2, 3 etc in base 2 (using the EQUALS node groups). Only one of these will be “true” (output 1.0).

The mix nodes will output the image from Color1 if Factor is 0.0 otherwise Color2. We cascade them so that each mix node defaults to outputting the previous mix node’s image (and that does the same, and so on). However, if any of the mix nodes are “enabled” (i.e the factor equals 1) they output their image (Color2). Because only one image is enabled at any one time, the other nodes will correctly forward their input image to the next node, and eventually the output colour.

settings (1).blend (775 KB)

OK! The sdfgeoff 's method sounds easier to do, but I still ned to know the script and set up for vertex colors(because I haven’t had to use vertex colors much, almost non at all)!

Well, feel free to check out my blend. It works :slight_smile:
I have a feeling the vertex colours will also be skewed by colour correction.

It sets to green material, yes, but I don’t know how to manipulate it! Aswell I need in-game manipulation with properties!

As I said, in the script window it sets the material index attribute - material_index which you could read from a property if you edited the script.

As things become more complex it’s advisable that you become more familiar with programming in order to solve potential issues.

Sorry, didn’t look at the script:D

OK! now I got so far: Could you, please, make an example .blend which just takes integer intput between 1, 2 and 3 and than sets material according to the number?

In the blend file, the attribute texture_settings.material_index can be changed from to any value in the range {0, 1, 2, 3, 4} to switch between five materials

Oh, I think I figured out the system. I will have to reduce it to 3 anyway. If I’ll need help, I’ll ask you! Anyway- thanks for this useful .blend;)

agoose, a problem:
Implementing your material and script into my game didn’t suceed, I need to know more details about it.
The material reduces framerate drasticly(with single realtime reflection material I have stable 30FPS in fulscreen 1080p and stable 60 FPS in 720p, but with this material framerate jumps between 10-15 FPS in fulscreen: a lot of lag. Any ideas why and how to optimize?

OK! I think I will have to make a large chain of change mesh things…:frowning: