BabylonJS / BabylonToolkit

Home of the community maintained Unity & Unreal exporters for Babylon.js
179 stars 47 forks source link

Unity Exporter; Wrong direction of babylon's `Spot light` #6

Closed Troxid closed 5 years ago

Troxid commented 5 years ago

Exporter: Unity - Version 4.0.0 - Alpha 10

When Spot light is used in the parent and its parent get transform - an exported direction is wrong.

Unity scene and right light direction (pink arrows): capture1

Exported scene: capture2

I think problem is in Babylon/Sources/SceneBuilder.Light.cs

var direction = new Vector3(0, 0, 1);
var transformedDirection = light.transform.TransformDirection(direction);
babylonLight.direction = transformedDirection.ToFloat();

I changed it to:

var direction = light.transform.forward;
var transformedDirection = light.transform.InverseTransformDirection(direction);            
babylonLight.direction = transformedDirection.ToFloat();

and it seems to be working fine:

capture3

I can't check it for 100%, because the Inspector doesn't show me the orientation of light.

deltakosh commented 5 years ago

Pinging @MackeyK24

MackeyK24 commented 5 years ago

Can you please send me the project as a .unitypackage soi can look at the parent child relationship and hopefully make code adjustments (apparently when light is a child transform )

MackeyK24 commented 5 years ago

Just to clarify... So you are saying that using the Inverse Transform Direction when light is a child ?

Troxid commented 5 years ago

Hello @MackeyK24

Can you please send me the project as a .unitypackage soi can look at the parent child relationship and hopefully make code adjustments (apparently when light is a child transform )

No sorry, i can't send the original project, but i have created a simple project which reproduce the problem. (red lamps without parents, green lamps with parent). https://drive.google.com/open?id=1lw5jBwvReA-3fIa9CMWz7PtBTUrKQoKv

Just to clarify... So you are saying that using the Inverse Transform Direction when light is a child ?

Yep, i had tried with TransformDirection and it was not working properly. As i undestand from documentation:

Transforms a direction from world space to local space.

What we need.

We can use old behavior for non-child light and fixed (i hope it's true) for childs.

if (light.transform.parent != null)
{
    var direction = light.transform.forward;
    var transformedDirection = light.transform.InverseTransformDirection(direction);            
    babylonLight.direction = transformedDirection.ToFloat();
}
else
{
    // Old behavior for global lights
    var direction = new Vector3(0, 0, 1);
    var transformedDirection = light.transform.TransformDirection(direction);
    babylonLight.direction = transformedDirection.ToFloat();
}
deltakosh commented 5 years ago

Up?

MackeyK24 commented 5 years ago

Well, for now, folks that are still gonna use the old version. This one can be fixed by using @Troxid fixes above.

New toolkit is GLTF 2.0 spec now

Light can camera transforms are exported differently (GLTF 2.0 spec) now and does NOT use light.transform.TransformDirection or the Inverse for exported the light component that is attached to a node. They are exported as component on a node where the node itself has the transform. The light or camera is just a child rig wither point Babylon.Forward for left and systems and BABYLON.Backwards() for right handed system.