Skip to content

Commit 4ee4631

Browse files
authored
Merge pull request #540 from Drigax/drigax/FixTextureTransformCalculation
Fix texture transform calculation, Update Babylon texture export message
2 parents 5512d89 + 09b958b commit 4ee4631

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

3ds Max/Max2Babylon/Exporter/BabylonExporter.Texture.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -755,33 +755,35 @@ private IStdUVGen _exportUV(IStdUVGen uvGen, BabylonTexture babylonTexture)
755755

756756
var offset = new BabylonVector3(babylonTexture.uOffset, -babylonTexture.vOffset, 0);
757757
var scale = new BabylonVector3(babylonTexture.uScale, babylonTexture.vScale, 1);
758-
var rotation = new BabylonQuaternion();
758+
var rotationEuler = new BabylonVector3(uvGen.GetUAng(0), uvGen.GetVAng(0), uvGen.GetWAng(0));
759+
var rotation = BabylonQuaternion.FromEulerAngles(rotationEuler.X, rotationEuler.Y, rotationEuler.Z);
759760
var pivotCenter = new BabylonVector3(-0.5f, -0.5f, 0);
760761
var transformMatrix = Tools.ComputeTextureTransformMatrix(pivotCenter, offset, rotation, scale);
761762

762-
transformMatrix.decompose(scale, rotation, offset);
763+
transformMatrix.decompose(scale, rotation, offset);
764+
var texTransformRotationEuler = rotation.toEulerAngles();
765+
763766
babylonTexture.uOffset = -offset.X;
764767
babylonTexture.vOffset = -offset.Y;
765768
babylonTexture.uScale = scale.X;
766769
babylonTexture.vScale = -scale.Y;
767770
babylonTexture.uRotationCenter = 0.0f;
768771
babylonTexture.vRotationCenter = 0.0f;
769772
babylonTexture.invertY = false;
773+
babylonTexture.uAng = texTransformRotationEuler.X;
774+
babylonTexture.vAng = texTransformRotationEuler.Y;
775+
babylonTexture.wAng = texTransformRotationEuler.Z;
770776

771777
if (Path.GetExtension(babylonTexture.name).ToLower() == ".dds")
772778
{
773779
babylonTexture.vScale *= -1; // Need to invert Y-axis for DDS texture
774780
}
775781

776-
babylonTexture.uAng = uvGen.GetUAng(0);
777-
babylonTexture.vAng = uvGen.GetVAng(0);
778-
babylonTexture.wAng = uvGen.GetWAng(0);
779-
780-
781-
// TODO - rotation and scale
782-
if (babylonTexture.wAng != 0f && (babylonTexture.uScale != 1f || babylonTexture.vScale != 1f))
782+
if (babylonTexture.wAng != 0f
783+
&& (babylonTexture.uScale != 1f || babylonTexture.vScale != 1f)
784+
&& (Math.Abs(babylonTexture.uScale) - Math.Abs(babylonTexture.vScale)) > float.Epsilon)
783785
{
784-
RaiseWarning("Rotation and tiling (scale) on a texture are only supported separatly. You can use the map UV of the mesh for those transformation.", 3);
786+
RaiseWarning("Rotation and non-uniform tiling (scale) on a texture is not supported as it will cause texture shearing. You can use the map UV of the mesh for those transformations.", 3);
785787
}
786788

787789

SharedProjects/BabylonExport.Entities/BabylonQuaternion.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,41 @@ public BabylonVector3 toEulerAngles()
6565
return result;
6666
}
6767

68-
public override string ToString()
68+
public static BabylonQuaternion FromEulerAngles(float x, float y, float z)
69+
{
70+
var q = new BabylonQuaternion();
71+
BabylonQuaternion.RotationYawPitchRollToRef(y, x, z, q);
72+
return q;
73+
}
74+
75+
/**
76+
* Creates a new rotation from the given Euler float angles (y, x, z) and stores it in the target quaternion
77+
* @param yaw defines the rotation around Y axis
78+
* @param pitch defines the rotation around X axis
79+
* @param roll defines the rotation around Z axis
80+
* @param result defines the target quaternion
81+
*/
82+
public static void RotationYawPitchRollToRef(float yaw, float pitch, float roll, BabylonQuaternion result)
83+
{
84+
// Produces a quaternion from Euler angles in the z-y-x orientation (Tait-Bryan angles)
85+
var halfRoll = roll * 0.5;
86+
var halfPitch = pitch * 0.5;
87+
var halfYaw = yaw * 0.5;
88+
89+
var sinRoll = Math.Sin(halfRoll);
90+
var cosRoll = Math.Cos(halfRoll);
91+
var sinPitch = Math.Sin(halfPitch);
92+
var cosPitch = Math.Cos(halfPitch);
93+
var sinYaw = Math.Sin(halfYaw);
94+
var cosYaw = Math.Cos(halfYaw);
95+
96+
result.X = (float)((cosYaw* sinPitch * cosRoll) + (sinYaw* cosPitch * sinRoll));
97+
result.Y = (float)((sinYaw* cosPitch * cosRoll) - (cosYaw* sinPitch * sinRoll));
98+
result.Z = (float)((cosYaw* cosPitch * sinRoll) - (sinYaw* sinPitch * cosRoll));
99+
result.W = (float)((cosYaw* cosPitch * cosRoll) + (sinYaw* sinPitch * sinRoll));
100+
}
101+
102+
public override string ToString()
69103
{
70104
return "{ X=" + X + ", Y=" + Y + ", Z=" + Z + ", W=" + W + " }";
71105
}

0 commit comments

Comments
 (0)