Game Development Community

Changing a T2DStaticSprite Material

by Tony Pitman · in Torque X 2D · 07/16/2009 (10:15 pm) · 3 replies

How can I change a T2DStaticSprite material in code at run time? I tried changing the Material.Name to the new material name and nothing happens.

#1
07/17/2009 (2:18 am)
I can change the material of a T2DStaticSprite by assigning the new material like this:

BlendMaterial _bm = new BlendMaterial();
SimpleMaterial m = null;
m = (Owner as T2DStaticSprite).Material as SimpleMaterial;

_bm.TextureFilename = m.TextureFilename;
_bm.TextureDivider = m.TextureDivider;

// custom technique/.fx file
_bm.Technique = _Technique;

(Owner as T2DStaticSprite).Material = _bm;

I do this in the _OnRegister method of a component.
#2
07/17/2009 (5:03 am)
If you just want to apply a named, standard material (i.e one that the scene editor creates), then you can grab it from the object database and assign it to the Material property of the sprite object.

mySpriteObject.Material = TorqueObjectDatabase.Instance.FindObject<RenderMaterial>("AnotherMaterial");
#3
07/17/2009 (6:13 am)
Thank you both for your answers. The second one is what I needed....