void ImportTRS(string strNodeName, GameObject goNode, GameObject goTopAttachNode) { // TRS IntPtr pFBXGetNodeTRS = FBXImporterGetNodeTRS(strNodeName); if (pFBXGetNodeTRS != IntPtr.Zero) { FBXNodeTRS sFBXNodeTRS = new FBXNodeTRS(); IntPtr pFBXNodeTRS = Marshal.AllocHGlobal(Marshal.SizeOf(sFBXNodeTRS)); try { sFBXNodeTRS = (FBXNodeTRS)Marshal.PtrToStructure(pFBXGetNodeTRS, typeof(FBXNodeTRS)); } finally { Marshal.FreeHGlobal(pFBXNodeTRS); } float[] fLclTranslation = new float[3]; Marshal.Copy(sFBXNodeTRS.fLclTranslation, fLclTranslation, 0, 3); float[] fLclRotation = new float[4]; Marshal.Copy(sFBXNodeTRS.fLclRotation, fLclRotation, 0, 4); float[] fLclScale = new float[3]; Marshal.Copy(sFBXNodeTRS.fLclScale, fLclScale, 0, 3); goNode.transform.localPosition = new Vector3(-fLclTranslation[0] * fGlobalScale, fLclTranslation[1] * fGlobalScale, fLclTranslation[2] * fGlobalScale); goNode.transform.localRotation = new Quaternion(-fLclRotation[0], fLclRotation[1], fLclRotation[2], -fLclRotation[3]); goNode.transform.localScale = new Vector3(fLclScale[0], fLclScale[1], fLclScale[2]); } }
void ImportTRSAnimation(int iAnimationClip, int iFrameRate, int iTimeMode, WrapMode eWrapMode, string strNodeName, GameObject goNode, GameObject goTopAttachNode) { IntPtr pFBXGetNodeTRSAnim = FBXImporterGetNodeTRSAnimation(strNodeName, iAnimationClip, iTimeMode); if (pFBXGetNodeTRSAnim != IntPtr.Zero) { FBXNodeTRS sFBXNodeTRSAnim = new FBXNodeTRS(); IntPtr pFBXNodeTRS = Marshal.AllocHGlobal(Marshal.SizeOf(sFBXNodeTRSAnim)); try { sFBXNodeTRSAnim = (FBXNodeTRS)Marshal.PtrToStructure(pFBXGetNodeTRSAnim, typeof(FBXNodeTRS)); } finally { Marshal.FreeHGlobal(pFBXNodeTRS); } float[] fLclTranslation = new float[3 * sFBXNodeTRSAnim.iCount]; Marshal.Copy(sFBXNodeTRSAnim.fLclTranslation, fLclTranslation, 0, 3 * sFBXNodeTRSAnim.iCount); float[] fLclRotation = new float[4 * sFBXNodeTRSAnim.iCount]; Marshal.Copy(sFBXNodeTRSAnim.fLclRotation, fLclRotation, 0, 4 * sFBXNodeTRSAnim.iCount); float[] fLclScale = new float[3 * sFBXNodeTRSAnim.iCount]; Marshal.Copy(sFBXNodeTRSAnim.fLclScale, fLclScale, 0, 3 * sFBXNodeTRSAnim.iCount); // Keyframeの生成. if (iFrameRate == 0) { iFrameRate = iDefaultFrameRate; } float fTime = (float)sFBXNodeTRSAnim.iFrameStart / iFrameRate; Keyframe[] key_localPositionX = new Keyframe[sFBXNodeTRSAnim.iCount]; Keyframe[] key_localPositionY = new Keyframe[sFBXNodeTRSAnim.iCount]; Keyframe[] key_localPositionZ = new Keyframe[sFBXNodeTRSAnim.iCount]; Keyframe[] key_localRotationX = new Keyframe[sFBXNodeTRSAnim.iCount]; Keyframe[] key_localRotationY = new Keyframe[sFBXNodeTRSAnim.iCount]; Keyframe[] key_localRotationZ = new Keyframe[sFBXNodeTRSAnim.iCount]; Keyframe[] key_localRotationW = new Keyframe[sFBXNodeTRSAnim.iCount]; Keyframe[] key_localScaleX = new Keyframe[sFBXNodeTRSAnim.iCount]; Keyframe[] key_localScaleY = new Keyframe[sFBXNodeTRSAnim.iCount]; Keyframe[] key_localScaleZ = new Keyframe[sFBXNodeTRSAnim.iCount]; for (int i = 0; i < sFBXNodeTRSAnim.iCount; i++) { key_localPositionX[i] = new Keyframe(fTime, -fLclTranslation[(i * 3) + 0] * fGlobalScale); key_localPositionY[i] = new Keyframe(fTime, fLclTranslation[(i * 3) + 1] * fGlobalScale); key_localPositionZ[i] = new Keyframe(fTime, fLclTranslation[(i * 3) + 2] * fGlobalScale); key_localRotationX[i] = new Keyframe(fTime, -fLclRotation[(i * 4) + 0]); key_localRotationY[i] = new Keyframe(fTime, fLclRotation[(i * 4) + 1]); key_localRotationZ[i] = new Keyframe(fTime, fLclRotation[(i * 4) + 2]); key_localRotationW[i] = new Keyframe(fTime, -fLclRotation[(i * 4) + 3]); key_localScaleX[i] = new Keyframe(fTime, fLclScale[(i * 3) + 0]); key_localScaleY[i] = new Keyframe(fTime, fLclScale[(i * 3) + 1]); key_localScaleZ[i] = new Keyframe(fTime, fLclScale[(i * 3) + 2]); fTime += (float)1.0f / iFrameRate; } // AnimationCurveの生成. AnimationCurve curve_localPositionX = new AnimationCurve(key_localPositionX); AnimationCurve curve_localPositionY = new AnimationCurve(key_localPositionY); AnimationCurve curve_localPositionZ = new AnimationCurve(key_localPositionZ); AnimationCurve curve_localRotationX = new AnimationCurve(key_localRotationX); AnimationCurve curve_localRotationY = new AnimationCurve(key_localRotationY); AnimationCurve curve_localRotationZ = new AnimationCurve(key_localRotationZ); AnimationCurve curve_localRotationW = new AnimationCurve(key_localRotationW); AnimationCurve curve_localScaleX = new AnimationCurve(key_localScaleX); AnimationCurve curve_localScaleY = new AnimationCurve(key_localScaleY); AnimationCurve curve_localScaleZ = new AnimationCurve(key_localScaleZ); // AnimationCurveの追加. string strRelativePath = "/" + goNode.name; Transform transParent = goNode.transform.parent; while (transParent != null) { strRelativePath = "/" + transParent.name + strRelativePath; transParent = transParent.parent; } // Animation Clip AnimationClip animationClip = null; if (listSAnimationClips != null) { if (listSAnimationClips.Length > iAnimationClip) { animationClip = listSAnimationClips[iAnimationClip].animationClip; if (animationClip == null) { animationClip = new AnimationClip(); animationClip.name = listSAnimationClips[iAnimationClip].strClipName; animationClip.legacy = true; animationClip.frameRate = iFrameRate; animationClip.wrapMode = eWrapMode; listSAnimationClips[iAnimationClip].animationClip = animationClip; } } } if (animationClip != null) { animationClip.SetCurve(strRelativePath, typeof(Transform), "localPosition.x", curve_localPositionX); animationClip.SetCurve(strRelativePath, typeof(Transform), "localPosition.y", curve_localPositionY); animationClip.SetCurve(strRelativePath, typeof(Transform), "localPosition.z", curve_localPositionZ); animationClip.SetCurve(strRelativePath, typeof(Transform), "localRotation.x", curve_localRotationX); animationClip.SetCurve(strRelativePath, typeof(Transform), "localRotation.y", curve_localRotationY); animationClip.SetCurve(strRelativePath, typeof(Transform), "localRotation.z", curve_localRotationZ); animationClip.SetCurve(strRelativePath, typeof(Transform), "localRotation.w", curve_localRotationW); animationClip.SetCurve(strRelativePath, typeof(Transform), "localScale.x", curve_localScaleX); animationClip.SetCurve(strRelativePath, typeof(Transform), "localScale.y", curve_localScaleY); animationClip.SetCurve(strRelativePath, typeof(Transform), "localScale.z", curve_localScaleZ); } } }