bug fix
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
using System.Collections.Generic;
|
||||
using Unity.Profiling;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UnityGLTF.Extensions
|
||||
{
|
||||
public class KHR_animation_pointer_Resolver
|
||||
{
|
||||
private readonly List<KHR_animation_pointer> registered = new List<KHR_animation_pointer>();
|
||||
private static readonly ProfilerMarker animationPointerResolverMarker = new ProfilerMarker("Resolve Animation Pointer");
|
||||
|
||||
public void Add(KHR_animation_pointer anim)
|
||||
{
|
||||
registered.Add(anim);
|
||||
}
|
||||
|
||||
// private struct MaterialMapping
|
||||
// {
|
||||
// public string propertyName;
|
||||
// public string exportName;
|
||||
// }
|
||||
//
|
||||
// private readonly Dictionary<Material, List<MaterialMapping>> mappings = new Dictionary<Material, List<MaterialMapping>>();
|
||||
//
|
||||
// // TODO: should we use a static switch instead?
|
||||
// public void RegisterMapping(Material mat, string propertyName, string exportedPropertyName)
|
||||
// {
|
||||
// if (!mappings.ContainsKey(mat))
|
||||
// {
|
||||
// mappings.Add(mat, new List<MaterialMapping>());
|
||||
// }
|
||||
// mappings[mat].Add(new MaterialMapping() { propertyName = propertyName, exportName = exportedPropertyName });
|
||||
// }
|
||||
|
||||
public void Resolve(GLTFSceneExporter exporter)
|
||||
{
|
||||
foreach (var reg in registered)
|
||||
{
|
||||
animationPointerResolverMarker.Begin();
|
||||
int id = exporter.GetIndex(reg.animatedObject);
|
||||
switch (reg.animatedObject)
|
||||
{
|
||||
case Light light:
|
||||
reg.path = "/extensions/KHR_lights_punctual/lights/" + id + "/" + reg.propertyBinding;
|
||||
break;
|
||||
case Camera camera:
|
||||
reg.path = "/cameras/" + id + "/" + reg.propertyBinding;
|
||||
break;
|
||||
case SkinnedMeshRenderer smr:
|
||||
if (smr && reg.propertyBinding == "weights")
|
||||
{
|
||||
reg.path = "/nodes/" + id + "/" + reg.propertyBinding;
|
||||
break;
|
||||
}
|
||||
goto ResolveComponent;
|
||||
case Component comp:
|
||||
case GameObject g:
|
||||
ResolveComponent:
|
||||
reg.path = "/nodes/" + id + "/" + reg.propertyBinding;
|
||||
var componentPath = reg.path;
|
||||
|
||||
var anyOtherResolverWasAbleToResolve = false;
|
||||
foreach (var res in exporter.pointerResolvers)
|
||||
{
|
||||
if (res.TryResolve(reg.animatedObject, ref componentPath))
|
||||
{
|
||||
reg.path = componentPath;
|
||||
anyOtherResolverWasAbleToResolve = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (exporter.pointerResolvers.Count > 0 && !anyOtherResolverWasAbleToResolve)
|
||||
{
|
||||
// we don't need to warn for regular transforms that are not RectTransforms,
|
||||
// but we want to warn for everything else that may be animated.
|
||||
if (!(reg.animatedObject is Transform && !(reg.animatedObject is RectTransform)))
|
||||
Debug.LogWarning("Wasn't able to resolve animation pointer for " + reg.animatedObject + " at " + componentPath + ". You can attach custom resolvers to animate properties in extensions.", reg.animatedObject as Object);
|
||||
}
|
||||
else if (!anyOtherResolverWasAbleToResolve)
|
||||
{
|
||||
// we don't need to warn for regular transforms that are not RectTransforms,
|
||||
// but we want to warn for everything else that may be animated.
|
||||
if (!(reg.animatedObject is Transform && !(reg.animatedObject is RectTransform)))
|
||||
Debug.LogWarning("Wasn't able to resolve animation pointer for " + reg.animatedObject + " at " + componentPath + ". You can attach custom resolvers to animate properties in extensions.", reg.animatedObject as Object);
|
||||
}
|
||||
break;
|
||||
case Material mat:
|
||||
reg.path = "/materials/" + id + "/" + reg.propertyBinding;
|
||||
break;
|
||||
}
|
||||
animationPointerResolverMarker.End();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 23086a9473b049b3ab8e64001d13a28b
|
||||
timeCreated: 1648544951
|
||||
@@ -0,0 +1,201 @@
|
||||
using System.Collections.Generic;
|
||||
using GLTF.Schema;
|
||||
using UnityEngine;
|
||||
using UnityGLTF.Extensions;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
||||
namespace UnityGLTF
|
||||
{
|
||||
public static class MaterialExtensions
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
[InitializeOnLoadMethod]
|
||||
#endif
|
||||
[RuntimeInitializeOnLoadMethod]
|
||||
static void InitExt()
|
||||
{
|
||||
GLTFSceneExporter.AfterMaterialExport += GLTFSceneExporterOnAfterMaterialExport;
|
||||
}
|
||||
|
||||
private static readonly int thicknessTexture = Shader.PropertyToID("thicknessTexture");
|
||||
private static readonly int thicknessFactor = Shader.PropertyToID("thicknessFactor");
|
||||
private static readonly int attenuationDistance = Shader.PropertyToID("attenuationDistance");
|
||||
private static readonly int attenuationColor = Shader.PropertyToID("attenuationColor");
|
||||
private static readonly int ior = Shader.PropertyToID("ior");
|
||||
private static readonly int transmissionFactor = Shader.PropertyToID("transmissionFactor");
|
||||
private static readonly int transmissionTexture = Shader.PropertyToID("transmissionTexture");
|
||||
|
||||
private static readonly int iridescenceFactor = Shader.PropertyToID("iridescenceFactor");
|
||||
private static readonly int iridescenceIor = Shader.PropertyToID("iridescenceIor");
|
||||
private static readonly int iridescenceThicknessMinimum = Shader.PropertyToID("iridescenceThicknessMinimum");
|
||||
private static readonly int iridescenceThicknessMaximum = Shader.PropertyToID("iridescenceThicknessMaximum");
|
||||
private static readonly int iridescenceTexture = Shader.PropertyToID("iridescenceTexture");
|
||||
private static readonly int iridescenceThicknessTexture = Shader.PropertyToID("iridescenceThicknessTexture");
|
||||
|
||||
private static readonly int specularFactor = Shader.PropertyToID("specularFactor");
|
||||
private static readonly int specularColorFactor = Shader.PropertyToID("specularColorFactor");
|
||||
private static readonly int specularTexture = Shader.PropertyToID("specularTexture");
|
||||
private static readonly int specularColorTexture = Shader.PropertyToID("specularColorTexture");
|
||||
|
||||
private static readonly int clearcoatFactor = Shader.PropertyToID("clearcoatFactor");
|
||||
private static readonly int clearcoatTexture = Shader.PropertyToID("clearcoatTexture");
|
||||
private static readonly int clearcoatRoughnessFactor = Shader.PropertyToID("clearcoatRoughnessFactor");
|
||||
private static readonly int clearcoatRoughnessTexture = Shader.PropertyToID("clearcoatRoughnessTexture");
|
||||
private static readonly int clearcoatNormalTexture = Shader.PropertyToID("clearcoatNormalTexture");
|
||||
|
||||
|
||||
private static void GLTFSceneExporterOnAfterMaterialExport(GLTFSceneExporter exporter, GLTFRoot gltfroot, Material material, GLTFMaterial materialnode)
|
||||
{
|
||||
if (!material) return;
|
||||
|
||||
var usesTransmission = material.IsKeywordEnabled("_VOLUME_TRANSMISSION_ON");
|
||||
var usesVolume = material.HasProperty("_VOLUME_ON") && material.GetFloat("_VOLUME_ON") > 0.5f;
|
||||
var hasIor = material.HasProperty(ior) && !Mathf.Approximately(material.GetFloat(ior), KHR_materials_ior.DefaultIor);
|
||||
|
||||
if (hasIor)
|
||||
{
|
||||
if (materialnode.Extensions == null)
|
||||
materialnode.Extensions = new Dictionary<string, IExtension>();
|
||||
|
||||
var vi = new KHR_materials_ior();
|
||||
if (materialnode.Extensions.TryGetValue(KHR_materials_ior_Factory.EXTENSION_NAME, out var vv1))
|
||||
vi = (KHR_materials_ior) vv1;
|
||||
else
|
||||
materialnode.Extensions.Add(KHR_materials_ior_Factory.EXTENSION_NAME, vi);
|
||||
|
||||
exporter.DeclareExtensionUsage(KHR_materials_ior_Factory.EXTENSION_NAME, false);
|
||||
|
||||
if (material.HasProperty(ior))
|
||||
vi.ior = material.GetFloat(ior);
|
||||
}
|
||||
|
||||
if (usesTransmission)
|
||||
{
|
||||
if (materialnode.Extensions == null)
|
||||
materialnode.Extensions = new Dictionary<string, IExtension>();
|
||||
|
||||
exporter.DeclareExtensionUsage(KHR_materials_transmission_Factory.EXTENSION_NAME, false);
|
||||
|
||||
// if the material already has an extension, we should get and modify that
|
||||
var vt = new KHR_materials_transmission();
|
||||
|
||||
if (materialnode.Extensions.TryGetValue(KHR_materials_transmission_Factory.EXTENSION_NAME, out var vv2))
|
||||
vt = (KHR_materials_transmission) vv2;
|
||||
else
|
||||
materialnode.Extensions.Add(KHR_materials_transmission_Factory.EXTENSION_NAME, vt);
|
||||
|
||||
if (material.HasProperty(transmissionFactor))
|
||||
vt.transmissionFactor = material.GetFloat(transmissionFactor);
|
||||
if (material.HasProperty(transmissionTexture) && material.GetTexture(transmissionTexture))
|
||||
vt.transmissionTexture = exporter.ExportTextureInfo(material.GetTexture(transmissionTexture), nameof(transmissionTexture));
|
||||
}
|
||||
|
||||
if (usesVolume)
|
||||
{
|
||||
if (materialnode.Extensions == null)
|
||||
materialnode.Extensions = new Dictionary<string, IExtension>();
|
||||
|
||||
exporter.DeclareExtensionUsage(KHR_materials_volume_Factory.EXTENSION_NAME, false);
|
||||
|
||||
// if the material already has an extension, we should get and modify that
|
||||
var ve = new KHR_materials_volume();
|
||||
|
||||
if (materialnode.Extensions.TryGetValue(KHR_materials_volume_Factory.EXTENSION_NAME, out var vv0))
|
||||
ve = (KHR_materials_volume)vv0;
|
||||
else
|
||||
materialnode.Extensions.Add(KHR_materials_volume_Factory.EXTENSION_NAME, ve);
|
||||
|
||||
if (material.HasProperty(thicknessFactor))
|
||||
ve.thicknessFactor = material.GetFloat(thicknessFactor);
|
||||
if (material.HasProperty(thicknessTexture) && material.GetTexture(thicknessTexture))
|
||||
ve.thicknessTexture = exporter.ExportTextureInfo(material.GetTexture(thicknessTexture), nameof(thicknessTexture));
|
||||
if (material.HasProperty(attenuationDistance))
|
||||
ve.attenuationDistance = material.GetFloat(attenuationDistance);
|
||||
if (material.HasProperty(attenuationColor))
|
||||
ve.attenuationColor = material.GetColor(attenuationColor).ToNumericsColorRaw();
|
||||
}
|
||||
|
||||
if (material.IsKeywordEnabled("_IRIDESCENCE_ON"))
|
||||
{
|
||||
exporter.DeclareExtensionUsage(KHR_materials_iridescence_Factory.EXTENSION_NAME, false);
|
||||
|
||||
if (materialnode.Extensions == null)
|
||||
materialnode.Extensions = new Dictionary<string, IExtension>();
|
||||
|
||||
var vir = new KHR_materials_iridescence();
|
||||
|
||||
if (materialnode.Extensions.TryGetValue(KHR_materials_iridescence_Factory.EXTENSION_NAME, out var vv0))
|
||||
vir = (KHR_materials_iridescence) vv0;
|
||||
else
|
||||
materialnode.Extensions.Add(KHR_materials_iridescence_Factory.EXTENSION_NAME, vir);
|
||||
|
||||
if (material.HasProperty(iridescenceFactor))
|
||||
vir.iridescenceFactor = material.GetFloat(iridescenceFactor);
|
||||
if (material.HasProperty(iridescenceIor))
|
||||
vir.iridescenceIor = material.GetFloat(iridescenceIor);
|
||||
if (material.HasProperty(iridescenceThicknessMinimum))
|
||||
vir.iridescenceThicknessMinimum = material.GetFloat(iridescenceThicknessMinimum);
|
||||
if (material.HasProperty(iridescenceThicknessMaximum))
|
||||
vir.iridescenceThicknessMaximum = material.GetFloat(iridescenceThicknessMaximum);
|
||||
if (material.HasProperty(iridescenceTexture) && material.GetTexture(iridescenceTexture))
|
||||
vir.iridescenceTexture = exporter.ExportTextureInfo(material.GetTexture(iridescenceTexture), nameof(iridescenceTexture));
|
||||
if (material.HasProperty(iridescenceThicknessTexture) && material.GetTexture(iridescenceThicknessTexture))
|
||||
vir.iridescenceThicknessTexture = exporter.ExportTextureInfo(material.GetTexture(iridescenceThicknessTexture), nameof(iridescenceThicknessTexture));
|
||||
}
|
||||
|
||||
if (material.IsKeywordEnabled("_SPECULAR_ON"))
|
||||
{
|
||||
exporter.DeclareExtensionUsage(KHR_materials_specular_Factory.EXTENSION_NAME, false);
|
||||
|
||||
if (materialnode.Extensions == null)
|
||||
materialnode.Extensions = new Dictionary<string, IExtension>();
|
||||
|
||||
var vir = new KHR_materials_specular();
|
||||
|
||||
if (materialnode.Extensions.TryGetValue(KHR_materials_specular_Factory.EXTENSION_NAME, out var vv0))
|
||||
vir = (KHR_materials_specular) vv0;
|
||||
else
|
||||
materialnode.Extensions.Add(KHR_materials_specular_Factory.EXTENSION_NAME, vir);
|
||||
|
||||
if (material.HasProperty(specularFactor))
|
||||
vir.specularFactor = material.GetFloat(specularFactor);
|
||||
if (material.HasProperty(specularColorFactor))
|
||||
vir.specularColorFactor = material.GetColor(specularColorFactor).ToNumericsColorRaw();
|
||||
if (material.HasProperty(specularTexture) && material.GetTexture(specularTexture))
|
||||
vir.specularTexture = exporter.ExportTextureInfo(material.GetTexture(specularTexture), nameof(specularTexture));
|
||||
if (material.HasProperty(specularColorTexture) && material.GetTexture(specularColorTexture))
|
||||
vir.specularColorTexture = exporter.ExportTextureInfo(material.GetTexture(specularColorTexture), nameof(specularColorTexture));
|
||||
}
|
||||
|
||||
if (material.IsKeywordEnabled("_CLEARCOAT_ON"))
|
||||
{
|
||||
exporter.DeclareExtensionUsage(KHR_materials_clearcoat_Factory.EXTENSION_NAME, false);
|
||||
|
||||
if (materialnode.Extensions == null)
|
||||
materialnode.Extensions = new Dictionary<string, IExtension>();
|
||||
|
||||
var cc = new KHR_materials_clearcoat();
|
||||
|
||||
if (materialnode.Extensions.TryGetValue(KHR_materials_clearcoat_Factory.EXTENSION_NAME, out var vv0))
|
||||
cc = (KHR_materials_clearcoat) vv0;
|
||||
else
|
||||
materialnode.Extensions.Add(KHR_materials_clearcoat_Factory.EXTENSION_NAME, cc);
|
||||
|
||||
if (material.HasProperty(clearcoatFactor))
|
||||
cc.clearcoatFactor = material.GetFloat(clearcoatFactor);
|
||||
if (material.HasProperty(clearcoatTexture))
|
||||
cc.clearcoatTexture = exporter.ExportTextureInfo(material.GetTexture(clearcoatTexture), nameof(clearcoatTexture));
|
||||
if (material.HasProperty(clearcoatRoughnessFactor))
|
||||
cc.clearcoatRoughnessFactor = material.GetFloat(clearcoatRoughnessFactor);
|
||||
if (material.HasProperty(clearcoatRoughnessTexture))
|
||||
cc.clearcoatRoughnessTexture = exporter.ExportTextureInfo(material.GetTexture(clearcoatRoughnessTexture), nameof(clearcoatRoughnessTexture));
|
||||
if (material.HasProperty(clearcoatNormalTexture))
|
||||
cc.clearcoatNormalTexture = exporter.ExportTextureInfo(material.GetTexture(clearcoatNormalTexture), nameof(clearcoatNormalTexture));
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5152940c6ada2f3449baca5def38ea64
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,606 @@
|
||||
using GLTF;
|
||||
using GLTF.Schema;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UnityGLTF.Extensions
|
||||
{
|
||||
public static class SchemaExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Define the transformation between Unity coordinate space and glTF.
|
||||
/// glTF is a right-handed coordinate system, where the 'right' direction is -X relative to
|
||||
/// Unity's coordinate system.
|
||||
/// glTF matrix: column vectors, column-major storage, +Y up, +Z forward, -X right, right-handed
|
||||
/// unity matrix: column vectors, column-major storage, +Y up, +Z forward, +X right, left-handed
|
||||
/// multiply by a negative X scale to convert handedness
|
||||
/// </summary>
|
||||
public static readonly GLTF.Math.Vector3 CoordinateSpaceConversionScale = new GLTF.Math.Vector3(-1, 1, 1);
|
||||
|
||||
/// <summary>
|
||||
/// Define whether the coordinate space scale conversion above means we have a change in handedness.
|
||||
/// This is used when determining the conventional direction of rotation - the right-hand rule states
|
||||
/// that rotations are clockwise in left-handed systems and counter-clockwise in right-handed systems.
|
||||
/// Reversing the direction of one or three axes of reverses the handedness.
|
||||
/// </summary>
|
||||
public static bool CoordinateSpaceConversionRequiresHandednessFlip
|
||||
{
|
||||
get
|
||||
{
|
||||
return CoordinateSpaceConversionScale.X * CoordinateSpaceConversionScale.Y * CoordinateSpaceConversionScale.Z < 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
public static readonly GLTF.Math.Vector4 TangentSpaceConversionScale = new GLTF.Math.Vector4(-1, 1, 1, -1);
|
||||
|
||||
/// <summary>
|
||||
/// Get the converted unity translation, rotation, and scale from a gltf node
|
||||
/// </summary>
|
||||
/// <param name="node">gltf node</param>
|
||||
/// <param name="position">unity translation vector</param>
|
||||
/// <param name="rotation">unity rotation quaternion</param>
|
||||
/// <param name="scale">unity scale vector</param>
|
||||
public static void GetUnityTRSProperties(this Node node, out Vector3 position, out Quaternion rotation,
|
||||
out Vector3 scale)
|
||||
{
|
||||
if (!node.UseTRS)
|
||||
{
|
||||
Matrix4x4 unityMat = node.Matrix.ToUnityMatrix4x4Convert();
|
||||
unityMat.GetTRSProperties(out position, out rotation, out scale);
|
||||
}
|
||||
else
|
||||
{
|
||||
position = node.Translation.ToUnityVector3Convert();
|
||||
rotation = node.Rotation.ToUnityQuaternionConvert();
|
||||
scale = node.Scale.ToUnityVector3Raw();
|
||||
}
|
||||
}
|
||||
|
||||
internal static readonly Quaternion InvertDirection = new Quaternion(0, -1, 0, 0);
|
||||
|
||||
/// <summary>
|
||||
/// Set a gltf node's converted translation, rotation, and scale from a unity transform
|
||||
/// </summary>
|
||||
/// <param name="node">gltf node to modify</param>
|
||||
/// <param name="transform">unity transform to convert</param>
|
||||
/// <param name="invertLookDirection">invert look direction (e.g. for lights and cameras)</param>
|
||||
public static void SetUnityTransform(this Node node, Transform transform, bool invertLookDirection)
|
||||
{
|
||||
node.Translation = transform.localPosition.ToGltfVector3Convert();
|
||||
node.Rotation = (transform.localRotation * (invertLookDirection ? InvertDirection : Quaternion.identity)).ToGltfQuaternionConvert();
|
||||
node.Scale = transform.localScale.ToGltfVector3Raw();
|
||||
}
|
||||
|
||||
// todo: move to utility class
|
||||
/// <summary>
|
||||
/// Get unity translation, rotation, and scale from a unity matrix
|
||||
/// </summary>
|
||||
/// <param name="mat">unity matrix to get properties from</param>
|
||||
/// <param name="position">unity translation vector</param>
|
||||
/// <param name="rotation">unity rotation quaternion</param>
|
||||
/// <param name="scale">unity scale vector</param>
|
||||
public static void GetTRSProperties(this Matrix4x4 mat, out Vector3 position, out Quaternion rotation,
|
||||
out Vector3 scale)
|
||||
{
|
||||
position = mat.GetColumn(3);
|
||||
|
||||
Vector3 x = mat.GetColumn(0);
|
||||
Vector3 y = mat.GetColumn(1);
|
||||
Vector3 z = mat.GetColumn(2);
|
||||
Vector3 calculatedZ = Vector3.Cross(x, y);
|
||||
bool mirrored = Vector3.Dot(calculatedZ, z) < 0.0f;
|
||||
|
||||
scale = new Vector3(x.magnitude * (mirrored ? -1.0f : 1.0f), y.magnitude, z.magnitude);
|
||||
|
||||
rotation = Quaternion.LookRotation(mat.GetColumn(2), mat.GetColumn(1));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get converted unity translation, rotation, and scale from a gltf matrix
|
||||
/// </summary>
|
||||
/// <param name="gltfMat">gltf matrix to get and convert properties from</param>
|
||||
/// <param name="position">unity translation vector</param>
|
||||
/// <param name="rotation">unity rotation quaternion</param>
|
||||
/// <param name="scale">unity scale vector</param>
|
||||
public static void GetTRSProperties(this GLTF.Math.Matrix4x4 gltfMat, out Vector3 position, out Quaternion rotation,
|
||||
out Vector3 scale)
|
||||
{
|
||||
gltfMat.ToUnityMatrix4x4Convert().GetTRSProperties(out position, out rotation, out scale);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a gltf column vector from a gltf matrix
|
||||
/// </summary>
|
||||
/// <param name="mat">gltf matrix</param>
|
||||
/// <param name="columnNum">the specified column vector from the matrix</param>
|
||||
/// <returns></returns>
|
||||
public static GLTF.Math.Vector4 GetColumn(this GLTF.Math.Matrix4x4 mat, uint columnNum)
|
||||
{
|
||||
switch (columnNum)
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
return new GLTF.Math.Vector4(mat.M11, mat.M21, mat.M31, mat.M41);
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
return new GLTF.Math.Vector4(mat.M12, mat.M22, mat.M32, mat.M42);
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
return new GLTF.Math.Vector4(mat.M13, mat.M23, mat.M33, mat.M43);
|
||||
}
|
||||
case 3:
|
||||
{
|
||||
return new GLTF.Math.Vector4(mat.M14, mat.M24, mat.M34, mat.M44);
|
||||
}
|
||||
default:
|
||||
throw new System.Exception("column num is out of bounds");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert gltf quaternion to a unity quaternion
|
||||
/// </summary>
|
||||
/// <param name="gltfQuat">gltf quaternion</param>
|
||||
/// <returns>unity quaternion</returns>
|
||||
public static Quaternion ToUnityQuaternionConvert(this GLTF.Math.Quaternion gltfQuat)
|
||||
{
|
||||
Vector3 fromAxisOfRotation = new Vector3(gltfQuat.X, gltfQuat.Y, gltfQuat.Z);
|
||||
float axisFlipScale = CoordinateSpaceConversionRequiresHandednessFlip ? -1.0f : 1.0f;
|
||||
Vector3 toAxisOfRotation = axisFlipScale * Vector3.Scale(fromAxisOfRotation, CoordinateSpaceConversionScale.ToUnityVector3Raw());
|
||||
|
||||
return new Quaternion(toAxisOfRotation.x, toAxisOfRotation.y, toAxisOfRotation.z, gltfQuat.W);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert unity quaternion to a gltf quaternion
|
||||
/// </summary>
|
||||
/// <param name="unityQuat">unity quaternion</param>
|
||||
/// <returns>gltf quaternion</returns>
|
||||
public static GLTF.Math.Quaternion ToGltfQuaternionConvert(this Quaternion unityQuat)
|
||||
{
|
||||
Vector3 fromAxisOfRotation = new Vector3(unityQuat.x, unityQuat.y, unityQuat.z);
|
||||
float axisFlipScale = CoordinateSpaceConversionRequiresHandednessFlip ? -1.0f : 1.0f;
|
||||
Vector3 toAxisOfRotation = axisFlipScale * Vector3.Scale(fromAxisOfRotation, CoordinateSpaceConversionScale.ToUnityVector3Raw());
|
||||
|
||||
return new GLTF.Math.Quaternion(toAxisOfRotation.x, toAxisOfRotation.y, toAxisOfRotation.z, unityQuat.w);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert gltf matrix to a unity matrix
|
||||
/// </summary>
|
||||
/// <param name="gltfMat">gltf matrix</param>
|
||||
/// <returns>unity matrix</returns>
|
||||
public static Matrix4x4 ToUnityMatrix4x4Convert(this GLTF.Math.Matrix4x4 gltfMat)
|
||||
{
|
||||
Matrix4x4 rawUnityMat = gltfMat.ToUnityMatrix4x4Raw();
|
||||
Vector3 coordinateSpaceConversionScale = CoordinateSpaceConversionScale.ToUnityVector3Raw();
|
||||
Matrix4x4 convert = Matrix4x4.Scale(coordinateSpaceConversionScale);
|
||||
Matrix4x4 unityMat = convert * rawUnityMat * convert;
|
||||
return unityMat;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert gltf matrix to a unity matrix
|
||||
/// </summary>
|
||||
/// <param name="unityMat">unity matrix</param>
|
||||
/// <returns>gltf matrix</returns>
|
||||
public static GLTF.Math.Matrix4x4 ToGltfMatrix4x4Convert(this Matrix4x4 unityMat)
|
||||
{
|
||||
Vector3 coordinateSpaceConversionScale = CoordinateSpaceConversionScale.ToUnityVector3Raw();
|
||||
Matrix4x4 convert = Matrix4x4.Scale(coordinateSpaceConversionScale);
|
||||
GLTF.Math.Matrix4x4 gltfMat = (convert * unityMat * convert).ToGltfMatrix4x4Raw();
|
||||
return gltfMat;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert gltf Vector3 to unity Vector3
|
||||
/// </summary>
|
||||
/// <param name="gltfVec3">gltf vector3</param>
|
||||
/// <returns>unity vector3</returns>
|
||||
public static Vector3 ToUnityVector3Convert(this GLTF.Math.Vector3 gltfVec3)
|
||||
{
|
||||
Vector3 coordinateSpaceConversionScale = CoordinateSpaceConversionScale.ToUnityVector3Raw();
|
||||
Vector3 unityVec3 = Vector3.Scale(gltfVec3.ToUnityVector3Raw(), coordinateSpaceConversionScale);
|
||||
return unityVec3;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert unity Vector3 to gltf Vector3
|
||||
/// </summary>
|
||||
/// <param name="unityVec3">unity Vector3</param>
|
||||
/// <returns>gltf Vector3</returns>
|
||||
public static GLTF.Math.Vector3 ToGltfVector3Convert(this Vector3 unityVec3)
|
||||
{
|
||||
Vector3 coordinateSpaceConversionScale = CoordinateSpaceConversionScale.ToUnityVector3Raw();
|
||||
GLTF.Math.Vector3 gltfVec3 = Vector3.Scale(unityVec3, coordinateSpaceConversionScale).ToGltfVector3Raw();
|
||||
return gltfVec3;
|
||||
}
|
||||
|
||||
public static GLTF.Math.Vector3 ToGltfVector3Raw(this Vector3 unityVec3)
|
||||
{
|
||||
GLTF.Math.Vector3 gltfVec3 = new GLTF.Math.Vector3(unityVec3.x, unityVec3.y, unityVec3.z);
|
||||
return gltfVec3;
|
||||
}
|
||||
|
||||
public static GLTF.Math.Vector4 ToGltfVector4Raw(this Vector4 unityVec4)
|
||||
{
|
||||
GLTF.Math.Vector4 gltfVec4 = new GLTF.Math.Vector4(unityVec4.x, unityVec4.y, unityVec4.z, unityVec4.w);
|
||||
return gltfVec4;
|
||||
}
|
||||
|
||||
public static Matrix4x4 ToUnityMatrix4x4Raw(this GLTF.Math.Matrix4x4 gltfMat)
|
||||
{
|
||||
Vector4 rawUnityCol0 = gltfMat.GetColumn(0).ToUnityVector4Raw();
|
||||
Vector4 rawUnityCol1 = gltfMat.GetColumn(1).ToUnityVector4Raw();
|
||||
Vector4 rawUnityCol2 = gltfMat.GetColumn(2).ToUnityVector4Raw();
|
||||
Vector4 rawUnityCol3 = gltfMat.GetColumn(3).ToUnityVector4Raw();
|
||||
Matrix4x4 rawUnityMat = new UnityEngine.Matrix4x4();
|
||||
rawUnityMat.SetColumn(0, rawUnityCol0);
|
||||
rawUnityMat.SetColumn(1, rawUnityCol1);
|
||||
rawUnityMat.SetColumn(2, rawUnityCol2);
|
||||
rawUnityMat.SetColumn(3, rawUnityCol3);
|
||||
|
||||
return rawUnityMat;
|
||||
}
|
||||
|
||||
public static GLTF.Math.Matrix4x4 ToGltfMatrix4x4Raw(this Matrix4x4 unityMat)
|
||||
{
|
||||
GLTF.Math.Vector4 c0 = unityMat.GetColumn(0).ToGltfVector4Raw();
|
||||
GLTF.Math.Vector4 c1 = unityMat.GetColumn(1).ToGltfVector4Raw();
|
||||
GLTF.Math.Vector4 c2 = unityMat.GetColumn(2).ToGltfVector4Raw();
|
||||
GLTF.Math.Vector4 c3 = unityMat.GetColumn(3).ToGltfVector4Raw();
|
||||
GLTF.Math.Matrix4x4 rawGltfMat = new GLTF.Math.Matrix4x4(c0.X, c0.Y, c0.Z, c0.W, c1.X, c1.Y, c1.Z, c1.W, c2.X, c2.Y, c2.Z, c2.W, c3.X, c3.Y, c3.Z, c3.W);
|
||||
return rawGltfMat;
|
||||
}
|
||||
|
||||
public static Vector2 ToUnityVector2Raw(this GLTF.Math.Vector2 vec2)
|
||||
{
|
||||
return new Vector2(vec2.X, vec2.Y);
|
||||
}
|
||||
|
||||
public static Vector2[] ToUnityVector2Raw(this GLTF.Math.Vector2[] inVecArr)
|
||||
{
|
||||
Vector2[] outVecArr = new Vector2[inVecArr.Length];
|
||||
for (int i = 0; i < inVecArr.Length; ++i)
|
||||
{
|
||||
outVecArr[i] = inVecArr[i].ToUnityVector2Raw();
|
||||
}
|
||||
return outVecArr;
|
||||
}
|
||||
|
||||
public static void ToUnityVector2Raw(this GLTF.Math.Vector2[] inArr, Vector2[] outArr, int offset = 0)
|
||||
{
|
||||
for (int i = 0; i < inArr.Length; i++)
|
||||
{
|
||||
outArr[offset + i] = inArr[i].ToUnityVector2Raw();
|
||||
}
|
||||
}
|
||||
|
||||
public static Vector3 ToUnityVector3Raw(this GLTF.Math.Vector3 vec3)
|
||||
{
|
||||
return new Vector3(vec3.X, vec3.Y, vec3.Z);
|
||||
}
|
||||
|
||||
public static Vector3[] ToUnityVector3Raw(this GLTF.Math.Vector3[] inVecArr)
|
||||
{
|
||||
Vector3[] outVecArr = new Vector3[inVecArr.Length];
|
||||
for (int i = 0; i < inVecArr.Length; ++i)
|
||||
{
|
||||
outVecArr[i] = inVecArr[i].ToUnityVector3Raw();
|
||||
}
|
||||
return outVecArr;
|
||||
}
|
||||
|
||||
public static void ToUnityVector3Raw(this GLTF.Math.Vector3[] inArr, Vector3[] outArr, int offset = 0)
|
||||
{
|
||||
for (int i = 0; i < inArr.Length; i++)
|
||||
{
|
||||
outArr[offset + i] = inArr[i].ToUnityVector3Raw();
|
||||
}
|
||||
}
|
||||
|
||||
public static Vector4 ToUnityVector4Raw(this GLTF.Math.Vector4 vec4)
|
||||
{
|
||||
return new Vector4(vec4.X, vec4.Y, vec4.Z, vec4.W);
|
||||
}
|
||||
|
||||
public static Vector4[] ToUnityVector4Raw(this GLTF.Math.Vector4[] inVecArr)
|
||||
{
|
||||
Vector4[] outVecArr = new Vector4[inVecArr.Length];
|
||||
for (int i = 0; i < inVecArr.Length; ++i)
|
||||
{
|
||||
outVecArr[i] = inVecArr[i].ToUnityVector4Raw();
|
||||
}
|
||||
return outVecArr;
|
||||
}
|
||||
|
||||
public static void ToUnityVector4Raw(this GLTF.Math.Vector4[] inArr, Vector4[] outArr, int offset = 0)
|
||||
{
|
||||
for (int i = 0; i < inArr.Length; i++)
|
||||
{
|
||||
outArr[offset + i] = inArr[i].ToUnityVector4Raw();
|
||||
}
|
||||
}
|
||||
|
||||
public static UnityEngine.Color ToUnityColorRaw(this GLTF.Math.Color color)
|
||||
{
|
||||
var c = new UnityEngine.Color(color.R, color.G, color.B, color.A).gamma;
|
||||
return c;
|
||||
}
|
||||
|
||||
public static UnityEngine.Color ToUnityColorLinear(this GLTF.Math.Color color)
|
||||
{
|
||||
var c = new UnityEngine.Color(color.R, color.G, color.B, color.A);
|
||||
return c;
|
||||
}
|
||||
|
||||
public static GLTF.Math.Color ToNumericsColorRaw(this UnityEngine.Color color)
|
||||
{
|
||||
var c = color;
|
||||
return new GLTF.Math.Color(c.r, c.g, c.b, c.a);
|
||||
}
|
||||
|
||||
public static GLTF.Math.Color ToNumericsColorLinear(this UnityEngine.Color color)
|
||||
{
|
||||
var lc = color.linear;
|
||||
return new GLTF.Math.Color(lc.r, lc.g, lc.b, lc.a);
|
||||
}
|
||||
|
||||
public static GLTF.Math.Color[] ToNumericsColorLinear(this UnityEngine.Color[] inColorArr)
|
||||
{
|
||||
GLTF.Math.Color[] outColorArr = new GLTF.Math.Color[inColorArr.Length];
|
||||
for (int i = 0; i < inColorArr.Length; ++i)
|
||||
{
|
||||
outColorArr[i] = inColorArr[i].ToNumericsColorLinear();
|
||||
}
|
||||
return outColorArr;
|
||||
}
|
||||
|
||||
public static UnityEngine.Color[] ToUnityColorRaw(this GLTF.Math.Color[] inColorArr)
|
||||
{
|
||||
UnityEngine.Color[] outColorArr = new UnityEngine.Color[inColorArr.Length];
|
||||
for (int i = 0; i < inColorArr.Length; ++i)
|
||||
{
|
||||
outColorArr[i] = inColorArr[i].ToUnityColorRaw();
|
||||
}
|
||||
return outColorArr;
|
||||
}
|
||||
|
||||
public static UnityEngine.Color[] ToLinear(this UnityEngine.Color[] inColorArr)
|
||||
{
|
||||
UnityEngine.Color[] outColorArr = new UnityEngine.Color[inColorArr.Length];
|
||||
for (int i = 0; i < inColorArr.Length; ++i)
|
||||
{
|
||||
outColorArr[i] = inColorArr[i].linear;
|
||||
}
|
||||
return outColorArr;
|
||||
}
|
||||
|
||||
public static void ToUnityColorRaw(this GLTF.Math.Color[] inArr, Color[] outArr, int offset = 0)
|
||||
{
|
||||
for (int i = 0; i < inArr.Length; i++)
|
||||
{
|
||||
outArr[offset + i] = inArr[i].ToUnityColorRaw();
|
||||
}
|
||||
}
|
||||
|
||||
public static void ToUnityColorLinear(this GLTF.Math.Color[] inArr, Color[] outArr, int offset = 0)
|
||||
{
|
||||
for (int i = 0; i < inArr.Length; i++)
|
||||
{
|
||||
outArr[offset + i] = inArr[i].ToUnityColorLinear();
|
||||
}
|
||||
}
|
||||
|
||||
public static int[] ToIntArrayRaw(this uint[] uintArr)
|
||||
{
|
||||
int[] intArr = new int[uintArr.Length];
|
||||
for (int i = 0; i < uintArr.Length; ++i)
|
||||
{
|
||||
uint uintVal = uintArr[i];
|
||||
Debug.Assert(uintVal <= int.MaxValue);
|
||||
intArr[i] = (int)uintVal;
|
||||
}
|
||||
|
||||
return intArr;
|
||||
}
|
||||
|
||||
public static GLTF.Math.Quaternion ToGltfQuaternionRaw(this Quaternion unityQuat)
|
||||
{
|
||||
return new GLTF.Math.Quaternion(unityQuat.x, unityQuat.y, unityQuat.z, unityQuat.w);
|
||||
}
|
||||
|
||||
public static Quaternion ToUnityQuaternionRaw(this GLTF.Math.Quaternion quaternion)
|
||||
{
|
||||
return new Quaternion(quaternion.X, quaternion.Y, quaternion.Z, quaternion.W);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Flips the V component of the UV (1-V) to put from glTF into Unity space
|
||||
/// </summary>
|
||||
/// <param name="attributeAccessor">The attribute accessor to modify</param>
|
||||
public static void FlipTexCoordArrayV(ref AttributeAccessor attributeAccessor)
|
||||
{
|
||||
for (var i = 0; i < attributeAccessor.AccessorContent.AsVec2s.Length; i++)
|
||||
{
|
||||
attributeAccessor.AccessorContent.AsVec2s[i].Y = 1.0f - attributeAccessor.AccessorContent.AsVec2s[i].Y;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Flip the V component of the UV (1-V)
|
||||
/// </summary>
|
||||
/// <param name="array">The array to copy from and modify</param>
|
||||
/// <returns>Copied Vector2 with coordinates in glTF space</returns>
|
||||
public static UnityEngine.Vector2[] FlipTexCoordArrayVAndCopy(UnityEngine.Vector2[] array)
|
||||
{
|
||||
var returnArray = new UnityEngine.Vector2[array.Length];
|
||||
|
||||
for (var i = 0; i < array.Length; i++)
|
||||
{
|
||||
returnArray[i].x = array[i].x;
|
||||
returnArray[i].y = 1.0f - array[i].y;
|
||||
}
|
||||
|
||||
return returnArray;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts vector3 to specified coordinate space
|
||||
/// </summary>
|
||||
/// <param name="attributeAccessor">The attribute accessor to modify</param>
|
||||
/// <param name="coordinateSpaceCoordinateScale">The coordinate space to move into</param>
|
||||
public static void ConvertVector3CoordinateSpace(ref AttributeAccessor attributeAccessor, GLTF.Math.Vector3 coordinateSpaceCoordinateScale)
|
||||
{
|
||||
for (int i = 0; i < attributeAccessor.AccessorContent.AsVertices.Length; i++)
|
||||
{
|
||||
attributeAccessor.AccessorContent.AsVertices[i].X *= coordinateSpaceCoordinateScale.X;
|
||||
attributeAccessor.AccessorContent.AsVertices[i].Y *= coordinateSpaceCoordinateScale.Y;
|
||||
attributeAccessor.AccessorContent.AsVertices[i].Z *= coordinateSpaceCoordinateScale.Z;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts and copies based on the specified coordinate scale
|
||||
/// </summary>
|
||||
/// <param name="array">The array to convert and copy</param>
|
||||
/// <param name="coordinateSpaceCoordinateScale">The specified coordinate space</param>
|
||||
/// <returns>The copied and converted coordinate space</returns>
|
||||
public static UnityEngine.Vector3[] ConvertVector3CoordinateSpaceAndCopy(Vector3[] array, GLTF.Math.Vector3 coordinateSpaceCoordinateScale)
|
||||
{
|
||||
var returnArray = new UnityEngine.Vector3[array.Length];
|
||||
var coordinateScale = coordinateSpaceCoordinateScale.ToUnityVector3Raw();
|
||||
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
returnArray[i] = array[i];
|
||||
returnArray[i].Scale(coordinateScale);
|
||||
}
|
||||
|
||||
return returnArray;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts vector4 to specified coordinate space
|
||||
/// </summary>
|
||||
/// <param name="attributeAccessor">The attribute accessor to modify</param>
|
||||
/// <param name="coordinateSpaceCoordinateScale">The coordinate space to move into</param>
|
||||
public static void ConvertVector4CoordinateSpace(ref AttributeAccessor attributeAccessor, GLTF.Math.Vector4 coordinateSpaceCoordinateScale)
|
||||
{
|
||||
for (int i = 0; i < attributeAccessor.AccessorContent.AsVec4s.Length; i++)
|
||||
{
|
||||
attributeAccessor.AccessorContent.AsVec4s[i].X *= coordinateSpaceCoordinateScale.X;
|
||||
attributeAccessor.AccessorContent.AsVec4s[i].Y *= coordinateSpaceCoordinateScale.Y;
|
||||
attributeAccessor.AccessorContent.AsVec4s[i].Z *= coordinateSpaceCoordinateScale.Z;
|
||||
attributeAccessor.AccessorContent.AsVec4s[i].W *= coordinateSpaceCoordinateScale.W;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts and copies based on the specified coordinate scale
|
||||
/// </summary>
|
||||
/// <param name="array">The array to convert and copy</param>
|
||||
/// <param name="coordinateSpaceCoordinateScale">The specified coordinate space</param>
|
||||
/// <returns>The copied and converted coordinate space</returns>
|
||||
public static Vector4[] ConvertVector4CoordinateSpaceAndCopy(Vector4[] array, GLTF.Math.Vector4 coordinateSpaceCoordinateScale)
|
||||
{
|
||||
var returnArray = new Vector4[array.Length];
|
||||
var coordinateScale = coordinateSpaceCoordinateScale.ToUnityVector4Raw();
|
||||
|
||||
for (var i = 0; i < array.Length; i++)
|
||||
{
|
||||
returnArray[i] = array[i];
|
||||
returnArray[i].Scale(coordinateScale);
|
||||
}
|
||||
|
||||
return returnArray;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rewinds the indicies into Unity coordinate space from glTF space
|
||||
/// </summary>
|
||||
/// <param name="attributeAccessor">The attribute accessor to modify</param>
|
||||
public static void FlipTriangleFaces(int[] indices)
|
||||
{
|
||||
for (int i = 0; i < indices.Length; i += 3)
|
||||
{
|
||||
int temp = indices[i];
|
||||
indices[i] = indices[i + 2];
|
||||
indices[i + 2] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
public static Matrix4x4 ToUnityMatrix4x4(this GLTF.Math.Matrix4x4 matrix)
|
||||
{
|
||||
return new Matrix4x4()
|
||||
{
|
||||
m00 = matrix.M11,
|
||||
m01 = matrix.M12,
|
||||
m02 = matrix.M13,
|
||||
m03 = matrix.M14,
|
||||
m10 = matrix.M21,
|
||||
m11 = matrix.M22,
|
||||
m12 = matrix.M23,
|
||||
m13 = matrix.M24,
|
||||
m20 = matrix.M31,
|
||||
m21 = matrix.M32,
|
||||
m22 = matrix.M33,
|
||||
m23 = matrix.M34,
|
||||
m30 = matrix.M41,
|
||||
m31 = matrix.M42,
|
||||
m32 = matrix.M43,
|
||||
m33 = matrix.M44
|
||||
};
|
||||
}
|
||||
|
||||
public static Matrix4x4[] ToUnityMatrix4x4(this GLTF.Math.Matrix4x4[] inMatrixArr)
|
||||
{
|
||||
Matrix4x4[] outMatrixArr = new Matrix4x4[inMatrixArr.Length];
|
||||
for (int i = 0; i < inMatrixArr.Length; ++i)
|
||||
{
|
||||
outMatrixArr[i] = inMatrixArr[i].ToUnityMatrix4x4();
|
||||
}
|
||||
return outMatrixArr;
|
||||
}
|
||||
|
||||
public static Quaternion SwitchHandedness(this Quaternion input)
|
||||
{
|
||||
return new Quaternion(-input.x, input.y, input.z, -input.w);
|
||||
}
|
||||
|
||||
/*
|
||||
public static Vector4 SwitchHandedness(this Vector4 input)
|
||||
{
|
||||
return new Vector4(-input.x, input.y, input.z, -input.w);
|
||||
}
|
||||
|
||||
public static Matrix4x4 SwitchHandedness(this Matrix4x4 matrix)
|
||||
{
|
||||
Vector3 position = matrix.GetColumn(3).SwitchHandedness();
|
||||
Quaternion rotation = Quaternion.LookRotation(matrix.GetColumn(2), matrix.GetColumn(1)).SwitchHandedness();
|
||||
Vector3 scale = new Vector3(matrix.GetColumn(0).magnitude, matrix.GetColumn(1).magnitude, matrix.GetColumn(2).magnitude);
|
||||
|
||||
float epsilon = 0.00001f;
|
||||
|
||||
// Some issues can occurs with non uniform scales
|
||||
if (Mathf.Abs(scale.x - scale.y) > epsilon || Mathf.Abs(scale.y - scale.z) > epsilon || Mathf.Abs(scale.x - scale.z) > epsilon)
|
||||
{
|
||||
Debug.LogWarning("A matrix with non uniform scale is being converted from left to right handed system. This code is not working correctly in this case");
|
||||
}
|
||||
|
||||
// Handle negative scale component in matrix decomposition
|
||||
if (Matrix4x4.Determinant(matrix) < 0)
|
||||
{
|
||||
Quaternion rot = Quaternion.LookRotation(matrix.GetColumn(2), matrix.GetColumn(1));
|
||||
Matrix4x4 corr = Matrix4x4.TRS(matrix.GetColumn(3), rot, Vector3.one).inverse;
|
||||
Matrix4x4 extractedScale = corr * matrix;
|
||||
scale = new Vector3(extractedScale.m00, extractedScale.m11, extractedScale.m22);
|
||||
}
|
||||
|
||||
// convert transform values from left handed to right handed
|
||||
return Matrix4x4.TRS(position, rotation, scale);
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e3a3c8e7f1cdae14fa835ee3580e292f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,85 @@
|
||||
using System.Collections.Generic;
|
||||
using GLTF.Schema;
|
||||
using UnityEngine;
|
||||
using UnityGLTF.Extensions;
|
||||
|
||||
namespace UnityGLTF
|
||||
{
|
||||
public static class TextMeshExport
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
[UnityEditor.InitializeOnLoadMethod]
|
||||
#else
|
||||
[RuntimeInitializeOnLoadMethod]
|
||||
#endif
|
||||
static void Init()
|
||||
{
|
||||
GLTFSceneExporter.BeforeMaterialExport += BeforeMaterialExport;
|
||||
GLTFSceneExporter.AfterSceneExport += CleanUpRenderTextureCache;
|
||||
}
|
||||
|
||||
private static void CleanUpRenderTextureCache(GLTFSceneExporter _, GLTFRoot __)
|
||||
{
|
||||
if (rtCache == null) return;
|
||||
foreach (var kvp in rtCache)
|
||||
kvp.Value.Release();
|
||||
rtCache.Clear();
|
||||
}
|
||||
|
||||
private static Material tempMat;
|
||||
private static Dictionary<Texture, RenderTexture> rtCache;
|
||||
|
||||
private static bool BeforeMaterialExport(GLTFSceneExporter exporter, GLTFRoot gltfRoot, Material material, GLTFMaterial materialNode)
|
||||
{
|
||||
if (material.shader.name.Contains("TextMeshPro")) // seems to only work for TextMeshPro/Mobile/ right now (SDFAA_HINTED?)
|
||||
{
|
||||
var s = material.shader;
|
||||
// TODO figure out how to properly use the non-mobile shaders
|
||||
var newS = Shader.Find("TestMeshPro/Mobile/Distance Field");
|
||||
#if UNITY_EDITOR
|
||||
if (!newS)
|
||||
{
|
||||
newS = UnityEditor.AssetDatabase.LoadAssetAtPath<Shader>(UnityEditor.AssetDatabase.GUIDToAssetPath("fe393ace9b354375a9cb14cdbbc28be4")); // same as above
|
||||
}
|
||||
#endif
|
||||
material.shader = newS;
|
||||
|
||||
if (!tempMat) tempMat = new Material(Shader.Find("Unlit/Transparent Cutout"));
|
||||
|
||||
var existingTex = material.mainTexture;
|
||||
if (rtCache == null) rtCache = new Dictionary<Texture, RenderTexture>();
|
||||
if (!rtCache.ContainsKey(existingTex))
|
||||
{
|
||||
var rt = new RenderTexture(existingTex.width, existingTex.height, 0, RenderTextureFormat.ARGB32);
|
||||
if (material.HasProperty("_OutlineSoftness"))
|
||||
material.SetFloat("_OutlineSoftness", 0);
|
||||
// TODO figure out how to get this more smooth
|
||||
Graphics.Blit(existingTex, rt, material);
|
||||
rtCache[existingTex] = rt;
|
||||
rt.anisoLevel = 9;
|
||||
rt.filterMode = FilterMode.Bilinear;
|
||||
}
|
||||
|
||||
tempMat.mainTexture = rtCache[existingTex];
|
||||
|
||||
|
||||
exporter.ExportUnlit(materialNode, tempMat);
|
||||
|
||||
// export material
|
||||
// alternative: double sided, alpha clipping, white RGB + TMPro mainTex R channel as alpha
|
||||
materialNode.DoubleSided = false;
|
||||
materialNode.PbrMetallicRoughness.BaseColorFactor = Color.white.ToNumericsColorLinear();
|
||||
materialNode.AlphaMode = AlphaMode.BLEND;
|
||||
|
||||
material.shader = s;
|
||||
#if UNITY_EDITOR && UNITY_2019_3_OR_NEWER
|
||||
UnityEditor.EditorUtility.ClearDirty(material);
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5dd4c08c7d6d71f4ca7d83fb653d4f9b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user