How to Enable Lighting on T2DShape3D
by Alex Richards · in Torque X 2D · 08/28/2009 (7:00 am) · 5 replies
if you have source code access you can get lighting, for 3DShapes, by modifying the SetMaterial method in TorqueCoreTStsRender.cs
Change
to this and add
also normal maps can be enabled by adding
but the normal map file name has to be the same as the diffuse map name, but with "normal" added to the end of the file name.
also under TorqueCoreMaterialsLightingMaterial comment out
the for (; i < MaxSupportedLights; i++) loop in the _SetupObjectParameters method.
*EDIT*
also change in the private, protected, internal fields section of LightingMaterial.cs from
to
Otherwise you'll get an array out of bounds error, if the amount of lights is not equal to MaxSupportedLights.
Then all you need to do is add a light through code or in the builder
*EDIT*
added normal map stuff...
Change
SimpleMaterial effect = new SimpleMaterial();
to this and add
LightingMaterial effect = new LightingMaterial(); effect.LightingMode = LightingMode.PerPixel;
also normal maps can be enabled by adding
String normalName = Shape.CurrentFilePath + @"" + material.Name + "normal";
if (File.Exists(normalName + ".xnb"))
{
effect.NormalMapFilename = normalName;
material._renderMaterial = MaterialManager.Add(normalName, effect);
}but the normal map file name has to be the same as the diffuse map name, but with "normal" added to the end of the file name.
also under TorqueCoreMaterialsLightingMaterial comment out
the for (; i < MaxSupportedLights; i++) loop in the _SetupObjectParameters method.
*EDIT*
also change in the private, protected, internal fields section of LightingMaterial.cs from
Vector3[] _positions;
Vector3[] _colors;
Vector3[] _ambients;
Vector2[] _attenuations;to
Vector3[] _positions = new Vector3[_globalMaxLightCount];
Vector3[] _colors = new Vector3[_globalMaxLightCount];
Vector3[] _ambients = new Vector3[_globalMaxLightCount];
Vector2[] _attenuations = new Vector2[_globalMaxLightCount];Otherwise you'll get an array out of bounds error, if the amount of lights is not equal to MaxSupportedLights.
Then all you need to do is add a light through code or in the builder
*EDIT*
added normal map stuff...
#2
0.5 constant attenuation
0.0 linear attenuation
0.0 xyz offset
0.1 for xyz ambient
0.9 for xyz diffuse
and positioned it where i wanted
or to add through code
08/29/2009 (6:21 pm)
I just added a blank sceneObject in TorqueX builder and gave it a T2DLightComponent with settings of0.5 constant attenuation
0.0 linear attenuation
0.0 xyz offset
0.1 for xyz ambient
0.9 for xyz diffuse
and positioned it where i wanted
or to add through code
public void createLight()
{
//create light
T2DSceneObject obj = new T2DSceneObject();
obj.Name = "Sun";
obj.Position = new Vector2(-200f, 0f);
DirectionalLight sun = new DirectionalLight();
sun.ConstantAttenuation = 1.0f;
sun.LinearAttenuation = 0.0f;
sun.AmbientColor = new Vector3(0.7f, 0.4f, 0.4f);
sun.DiffuseColor = new Vector3(0.75f, 0.75f, 0.75f);
sun.Direction = Vector3.Up;
T2DLightComponent light = new T2DLightComponent();
light.IsEnabled = true;
light.LightList.Add(sun);
obj.Components.AddComponent(light);
TorqueObjectDatabase.Instance.Register(obj);
}
#3
John K.
www.envygames.com
08/29/2009 (8:11 pm)
Looks great, Alex. Added to the Torque X 3.1.4 releaseJohn K.
www.envygames.com
#4
_positions[i] = lights[i].Position;
It's in the for loop right above the one that you said to comment out.
EDIT:
I added the following above the for loop
now it seems to work
actually, let me rephrase that...
WOOOHOOOOO!!! IT WORKS!!!
08/29/2009 (8:36 pm)
I'm getting a nullReferenceException on this line:_positions[i] = lights[i].Position;
It's in the for loop right above the one that you said to comment out.
EDIT:
I added the following above the for loop
_positions = new Vector3[8]; _colors = new Vector3[8]; _ambients = new Vector3[8]; _attenuations = new Vector2[8];
now it seems to work
actually, let me rephrase that...
WOOOHOOOOO!!! IT WORKS!!!
#5
08/29/2009 (9:36 pm)
Glad it works, yeah sorry about that nullReferenceException. I forgot to add the change I made to the light variables, edited original post to reflect that.
Torque Owner David Horn
I followed your instructions, however, I'm a little confused about the adding a light part. I added a torque object with a light component, but all of my T2DShape3D objects are completely black.
Is it because I'm using a 2D light? If so, could you possibly show me how to add the light? I have the pro versions of both TX2D and TX3D.
EDIT:
I tried it myself but no luck.
I imported the Torque3D into my solution and wrote the folloring code I got from another post:
//create light TorqueObject obj = new TorqueObject(); obj.Name = "Sun"; T3DSceneComponent scene = new T3DSceneComponent(); scene.SceneGroup = "Sun"; scene.Position = new Vector3(1050, 1050, 400); obj.Components.AddComponent(scene); DirectionalLight sun = new DirectionalLight(); sun.ConstantAttenuation = 1.0f; sun.LinearAttenuation = 0.0f; sun.AmbientColor = new Vector3(0.7f, 0.4f, 0.4f); sun.DiffuseColor = new Vector3(0.75f, 0.75f, 0.75f); sun.Direction = Vector3.Down; T3DLightComponent light = new T3DLightComponent(); light.SceneGroupName = obj.Name; light.IsEnabled = true; light.LightList.Add(sun); obj.Components.AddComponent(light); TorqueObjectDatabase.Instance.Register(obj);But my objects are still black.