// Warning: Some assembly references could not be resolved automatically. This might lead to incorrect decompilation of some parts,
// for ex. property getter/setter access. To get optimal decompilation results, please manually add the missing references to the list of loaded assemblies.
// EdenAutoMorpherScript, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// Eden.AutoMorpher.SkinningUtil
using UnityEngine;

public class SkinningUtil
{
	public Vector3 WorldPosToBindPos(SkinnedMeshRenderer smr, Mesh bindMesh, int vertexIndex, Vector3 targetWorld)
	{
		if (smr == null || bindMesh == null)
		{
			return Vector3.zero;
		}
		BoneWeight[] boneWeights = bindMesh.boneWeights;
		Matrix4x4[] bindposes = bindMesh.bindposes;
		Transform[] bones = smr.bones;
		if (boneWeights == null || bindposes == null || bones == null)
		{
			return smr.transform.InverseTransformPoint(targetWorld);
		}
		if (vertexIndex < 0 || vertexIndex >= boneWeights.Length)
		{
			return smr.transform.InverseTransformPoint(targetWorld);
		}
		if (bindposes.Length != bones.Length)
		{
			return smr.transform.InverseTransformPoint(targetWorld);
		}
		BoneWeight boneWeight = boneWeights[vertexIndex];
		bool flag = boneWeight.boneIndex0 >= 0 && boneWeight.boneIndex0 < bones.Length && bones[boneWeight.boneIndex0] != null && boneWeight.weight0 > 0f;
		bool flag2 = boneWeight.boneIndex1 >= 0 && boneWeight.boneIndex1 < bones.Length && bones[boneWeight.boneIndex1] != null && boneWeight.weight1 > 0f;
		bool flag3 = boneWeight.boneIndex2 >= 0 && boneWeight.boneIndex2 < bones.Length && bones[boneWeight.boneIndex2] != null && boneWeight.weight2 > 0f;
		bool num = boneWeight.boneIndex3 >= 0 && boneWeight.boneIndex3 < bones.Length && bones[boneWeight.boneIndex3] != null && boneWeight.weight3 > 0f;
		float num2 = (flag ? boneWeight.weight0 : 0f);
		float num3 = (flag2 ? boneWeight.weight1 : 0f);
		float num4 = (flag3 ? boneWeight.weight2 : 0f);
		float num5 = (num ? boneWeight.weight3 : 0f);
		float num6 = num2 + num3 + num4 + num5;
		if (num6 <= 1E-08f)
		{
			return smr.transform.InverseTransformPoint(targetWorld);
		}
		num2 /= num6;
		num3 /= num6;
		num4 /= num6;
		num5 /= num6;
		Vector3 vector = smr.transform.InverseTransformPoint(targetWorld);
		Matrix4x4 objWorldInv = smr.transform.worldToLocalMatrix;
		Matrix4x4 acc = Matrix4x4.zero;
		if (num2 > 0f)
		{
			Acc(ref acc, boneWeight.boneIndex0, num2);
		}
		if (num3 > 0f)
		{
			Acc(ref acc, boneWeight.boneIndex1, num3);
		}
		if (num4 > 0f)
		{
			Acc(ref acc, boneWeight.boneIndex2, num4);
		}
		if (num5 > 0f)
		{
			Acc(ref acc, boneWeight.boneIndex3, num5);
		}
		if (Mathf.Abs(acc.m00 * (acc.m11 * acc.m22 - acc.m12 * acc.m21) - acc.m01 * (acc.m10 * acc.m22 - acc.m12 * acc.m20) + acc.m02 * (acc.m10 * acc.m21 - acc.m11 * acc.m20)) < 1E-12f)
		{
			return vector;
		}
		acc.m33 = 1f;
		Vector3 vector2 = Matrix4x4.Inverse(acc).MultiplyPoint3x4(vector);
		Debug.Log($"Diff{smr.transform.TransformPoint(vector2) - targetWorld}");
		return vector2;
		void Acc(ref Matrix4x4 reference, int boneIndex, float w)
		{
			if (!(w <= 0f))
			{
				Matrix4x4 matrix4x = objWorldInv * bones[boneIndex].localToWorldMatrix * bindposes[boneIndex];
				reference.m00 += matrix4x.m00 * w;
				reference.m01 += matrix4x.m01 * w;
				reference.m02 += matrix4x.m02 * w;
				reference.m10 += matrix4x.m10 * w;
				reference.m11 += matrix4x.m11 * w;
				reference.m12 += matrix4x.m12 * w;
				reference.m20 += matrix4x.m20 * w;
				reference.m21 += matrix4x.m21 * w;
				reference.m22 += matrix4x.m22 * w;
			}
		}
	}

	public Vector3 WorldPosToBindPos_Full(SkinnedMeshRenderer smr, Mesh bindMesh, int vertexIndex, Vector3 targetWorld)
	{
		if (smr == null || bindMesh == null)
		{
			return Vector3.zero;
		}
		BoneWeight[] boneWeights = bindMesh.boneWeights;
		Matrix4x4[] bindposes = bindMesh.bindposes;
		Transform[] bones = smr.bones;
		if (boneWeights == null || bindposes == null || bones == null)
		{
			return smr.transform.InverseTransformPoint(targetWorld);
		}
		if (vertexIndex < 0 || vertexIndex >= boneWeights.Length)
		{
			return smr.transform.InverseTransformPoint(targetWorld);
		}
		if (bindposes.Length != bones.Length)
		{
			return smr.transform.InverseTransformPoint(targetWorld);
		}
		BoneWeight boneWeight = boneWeights[vertexIndex];
		float[] array = new float[4];
		int[] array2 = new int[4];
		array[0] = boneWeight.weight0;
		array2[0] = boneWeight.boneIndex0;
		array[1] = boneWeight.weight1;
		array2[1] = boneWeight.boneIndex1;
		array[2] = boneWeight.weight2;
		array2[2] = boneWeight.boneIndex2;
		array[3] = boneWeight.weight3;
		array2[3] = boneWeight.boneIndex3;
		float num = 0f;
		for (int i = 0; i < 4; i++)
		{
			if (array2[i] >= 0 && array2[i] < bones.Length && bones[array2[i]] != null)
			{
				num += array[i];
			}
		}
		if (num < 1E-08f)
		{
			return smr.transform.InverseTransformPoint(targetWorld);
		}
		for (int j = 0; j < 4; j++)
		{
			array[j] /= num;
		}
		Vector3 result = smr.transform.InverseTransformPoint(targetWorld);
		Vector4 vector = new Vector4(result.x, result.y, result.z, 1f);
		Matrix4x4 objWorldInv = smr.transform.worldToLocalMatrix;
		Matrix4x4 acc = Matrix4x4.zero;
		for (int k = 0; k < 4; k++)
		{
			if (array[k] > 0f && array2[k] >= 0 && array2[k] < bones.Length)
			{
				Accumulate(ref acc, array2[k], array[k]);
			}
		}
		if (Mathf.Abs(acc.m00 * (acc.m11 * acc.m22 - acc.m12 * acc.m21) - acc.m01 * (acc.m10 * acc.m22 - acc.m12 * acc.m20) + acc.m02 * (acc.m10 * acc.m21 - acc.m11 * acc.m20)) < 1E-12f)
		{
			return result;
		}
		return Matrix4x4.Inverse(acc).MultiplyPoint3x4(vector);
		void Accumulate(ref Matrix4x4 reference, int bi, float w)
		{
			if (!(w <= 0f))
			{
				Matrix4x4 matrix4x = objWorldInv * bones[bi].localToWorldMatrix * bindposes[bi];
				reference.m00 += matrix4x.m00 * w;
				reference.m01 += matrix4x.m01 * w;
				reference.m02 += matrix4x.m02 * w;
				reference.m03 += matrix4x.m03 * w;
				reference.m10 += matrix4x.m10 * w;
				reference.m11 += matrix4x.m11 * w;
				reference.m12 += matrix4x.m12 * w;
				reference.m13 += matrix4x.m13 * w;
				reference.m20 += matrix4x.m20 * w;
				reference.m21 += matrix4x.m21 * w;
				reference.m22 += matrix4x.m22 * w;
				reference.m23 += matrix4x.m23 * w;
				reference.m30 += matrix4x.m30 * w;
				reference.m31 += matrix4x.m31 * w;
				reference.m32 += matrix4x.m32 * w;
				reference.m33 += matrix4x.m33 * w;
			}
		}
	}

	public Vector3 WorldDirToBindDir_Full(SkinnedMeshRenderer smr, Mesh bindMesh, int vertexIndex, Vector3 targetWorldDir)
	{
		if (smr == null || bindMesh == null)
		{
			return Vector3.zero;
		}
		BoneWeight[] boneWeights = bindMesh.boneWeights;
		Matrix4x4[] bindposes = bindMesh.bindposes;
		Transform[] bones = smr.bones;
		if (boneWeights == null || bindposes == null || bones == null)
		{
			return smr.transform.InverseTransformDirection(targetWorldDir).normalized;
		}
		if (vertexIndex < 0 || vertexIndex >= boneWeights.Length)
		{
			return smr.transform.InverseTransformDirection(targetWorldDir).normalized;
		}
		if (bindposes.Length != bones.Length)
		{
			return smr.transform.InverseTransformDirection(targetWorldDir).normalized;
		}
		BoneWeight boneWeight = boneWeights[vertexIndex];
		float[] array = new float[4];
		int[] array2 = new int[4];
		array[0] = boneWeight.weight0;
		array2[0] = boneWeight.boneIndex0;
		array[1] = boneWeight.weight1;
		array2[1] = boneWeight.boneIndex1;
		array[2] = boneWeight.weight2;
		array2[2] = boneWeight.boneIndex2;
		array[3] = boneWeight.weight3;
		array2[3] = boneWeight.boneIndex3;
		float num = 0f;
		for (int i = 0; i < 4; i++)
		{
			if (array2[i] >= 0 && array2[i] < bones.Length && bones[array2[i]] != null)
			{
				num += array[i];
			}
		}
		if (num < 1E-08f)
		{
			return smr.transform.InverseTransformDirection(targetWorldDir).normalized;
		}
		for (int j = 0; j < 4; j++)
		{
			array[j] /= num;
		}
		Vector3 vector = smr.transform.InverseTransformDirection(targetWorldDir);
		if (vector.sqrMagnitude < 1E-12f)
		{
			return Vector3.up;
		}
		Matrix4x4 objWorldInv = smr.transform.worldToLocalMatrix;
		Matrix4x4 acc = Matrix4x4.zero;
		for (int k = 0; k < 4; k++)
		{
			if (array[k] > 0f && array2[k] >= 0 && array2[k] < bones.Length)
			{
				Accumulate(ref acc, array2[k], array[k]);
			}
		}
		Vector3 vector2 = acc.transpose.MultiplyVector(vector);
		if (vector2.sqrMagnitude < 1E-12f)
		{
			return vector.normalized;
		}
		return vector2.normalized;
		void Accumulate(ref Matrix4x4 reference, int bi, float w)
		{
			if (!(w <= 0f))
			{
				Matrix4x4 matrix4x = objWorldInv * bones[bi].localToWorldMatrix * bindposes[bi];
				reference.m00 += matrix4x.m00 * w;
				reference.m01 += matrix4x.m01 * w;
				reference.m02 += matrix4x.m02 * w;
				reference.m03 += matrix4x.m03 * w;
				reference.m10 += matrix4x.m10 * w;
				reference.m11 += matrix4x.m11 * w;
				reference.m12 += matrix4x.m12 * w;
				reference.m13 += matrix4x.m13 * w;
				reference.m20 += matrix4x.m20 * w;
				reference.m21 += matrix4x.m21 * w;
				reference.m22 += matrix4x.m22 * w;
				reference.m23 += matrix4x.m23 * w;
				reference.m30 += matrix4x.m30 * w;
				reference.m31 += matrix4x.m31 * w;
				reference.m32 += matrix4x.m32 * w;
				reference.m33 += matrix4x.m33 * w;
			}
		}
	}
}
