[Tutorial] Working with object materials and material text
#1

Introduction

There are two new object functions that added in SA-MP 0.3e, called SetObjectMaterial and SetObjectMaterialText.
Both of them has the ability to change existing object color.
I've decided to write this tutorial for anyone who want to learn how to work with these new functions.

Object Material

Material of object is the texture / color of the object. Every object has one or more materials, which can be used with the new functions on other objects.

For example - you can take the sand material from the desert and use it on a road, so you'll have road object coloured with the texture of sand.
This is the material we gonna use:

On this object (9120):

And the object after setting the material:


However, setting the material it's a bit more complicated then what you can see in the images.
I have to explain more about it.

As I said above, every object has one or more materials. Most objects has more then one. Also, every single material of object has its index (ID). Also, for each material, there are more than one parameters:
modelid - The original object model ID where the material is used.
txdname - The TXD file to take the texture from. You can consider it as a part of the material name.
texturename - The name of the texture to use.
materialcolor - We can set the object color to a specific color and not to material. Because this tutorial is about object materials, we'll use 0. When using 0 here the color of the object will not change.

Now, lets go back to the sand object we used earlier:

This object contains more than just sand. We used the sand before, but we can use more materials the object is using. It has sand, path, and more... at all, it uses 6 different materials. There are indexses #0 to #5. Each one of them is another material:

#1 - Yellow line. I guess we couldn't see it from here
#2 - Sand
#3 - Grey floor
#4 - Sand path
#0 and #5 aren't visible from here. However, they're part of the object too.

Finding Materials

To set a object material, all you need to do is an object to set and material information.
There are some ways to find material names:

1) Taking a look into the TXD files. I don't really remember the program to use it, and that's why i'll not explain more about this way. I also prefer to use the other ways.

2) Sharing information; cool materials can be found in scripts that using it, or in posts used to share the materials.

3) Using JernejL map editor you can find texture names only. All you need to do is to double-click on object and take a look at "Model textures", you'll find every texture name of any index. So, with this you can know what are the indexes of object and what is the texture name of every index. However, this is not getting the txdname, which is required to set object material.

Using materials is just on it's start so there are no a lot of resources to find them, but with the time passing, there will be more and more places to find them/

Setting Material

SetObjectMaterial(objectid, materialindex, modelid, txdname[], texturename[], materialcolor)

Setting material requires two things:
1) Creating a new object
2) Knowing the material you want (and its properties)

I'll use the road I used in the start, with the material of sand, which its details are:
modelid = 4242
txdname = seabed
texturename = des_dirt1

So, the objectid will be the ID of the object we'll create, the road. The materialindex will be #3, which is the road itself. The modelid, txtname and texturename will be the same as above. materialcolor will be set as 0, because when you're using material and not color, this parameter will set as 0.

pawn Code:
new oid = CreateObject(9120,0.0,0.0,0.0,0.0,0.0,0.0);
SetObjectMaterial(oid,3,4242,"seabed","des_dirt1");
That's how you can set object's material.

Object Material Text

Another new feature is to set an object material text. That feature allows you to write your own text on any object, but it requires you to change the object to a specific color.

It means, that you can't write a text on a road when staying it's material, you'll have to change its color.

This is a road which I set its text to "Test" on the #3 index.

SetObjectMaterialText(objectid, text[], materialindex = 0, materialsize = OBJECT_MATERIAL_SIZE_256x128, fontface[] = "Arial", fontsize = 24, bold = 1, fontcolor = 0xFFFFFFFF, backcolor = 0, textalignment = 0)
objectid - The ID of object to use
text - The string to write on the object
materialindex - The index of object to use
materialsize - Size of the object material. Used to decide the 'resolution' of the text on the object
fontface - The font name
fontsize - The font size
bold - 1 for bold, 0 for normal text
fontcolor - Color of the text in ARGB format (will be explained later)
backcolor - Color for the background, the object itself (in ARGB format too)
textalignment - The alignment of the text. 0 = left, 1 = center, 2 = right

So, to create a road and write a "test" on it just as I did above, you'll use the function like this code:
pawn Code:
new oid = CreateObject(9120,0.0,0.0,0.0,0.0,0.0,0.0);
SetObjectMaterialText(oid,"Test",3,OBJECT_MATERIAL_SIZE_256x128,"Arial",24",1,0xFFFFFFFF,0xFF000000,1);
ARGB Format

Colors we use in most of functions are defined as RGBA format colors. Its because when reading them as hexadecimal colors, we'll read them as 0xRRGGBBAA.
It means, that 0xFF0000FF will be red; R = red, G = green, B = blue and A = alpha, their values can be between 0 and 255. 00 is 0, and FF is 255. The meaning of 0xFF0000FF is 255 red, 0 green, 0 blue and 255 alpha (so the color will be visible).

Both the new functions requires ARGB format color, it means AARRGGBB. So a red color, that was 0xFF0000FF, will be now 0xFFFF0000. That's how you can turn the colors defined in your script into ARGB format colors for materials.

Reset Object Material

At all, there is no direct way to reset object material and make it return to its original one. The way to do this is to destroy the object and recreate it.

If i'm not wrong, Incognito's streamer contains GetDynamicObjectMaterial; if you're using it it will be probably easy to store the first material data into variables and use them to reset the material to the original one.

Related Links

A Thread to share materials
Texture Viewer
Texture Workshop V1.6 - Not offensive, but this one is a little bugged, but still can be used to test materials


That's all, I hope some users will find this tutorial helpful.
Reply
#2

nice tutorial!
Reply
#3

Awesome tutorial! 99/10!

EDITED post: Nice tutorial, 10/10.

But seriously, do not create a thread if you are just waiting to edit it.

If you made a tutorial before this, and decided to change it up, do not just remove the whole tutorial. Keep it until you edit it.
Reply
#4

It took some time to complete this. Anyway, now the replies can be real.
Reply
#5

GREAT TUTORIAL!!!
GJ Amit
Reply
#6

Awesome Man, thanks for the tutorial xD
Reply
#7

Can you reply with a complete code that i should only paste and work, a random object with random text, i only want to see how the complete functional code works.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)