美文网首页
优化动画精度

优化动画精度

作者: 此年此景 | 来源:发表于2017-04-20 11:52 被阅读0次

通过SerializedObject优化动画数据精度:

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

class MyUtility
{
    [MenuItem("Assets/Optimize Anim")]
    static public void OptimizePrecision()
    {
        AnimationClip clip = Selection.activeObject as AnimationClip;
        string path = AssetDatabase.GUIDToAssetPath(Selection.assetGUIDs[0]);
        if (clip != null)
        {
            // 优化anim数据精度
            SerializedObject serializedObject = new SerializedObject(clip);
            SerializedProperty curvesProperty = serializedObject.FindProperty("m_FloatCurves");
            OptCurves(curvesProperty);
            SerializedProperty rotationProperty = serializedObject.FindProperty("m_RotationCurves");
            OptCurves(rotationProperty);
            SerializedProperty positionProperty = serializedObject.FindProperty("m_PositionCurves");
            OptCurves(positionProperty);
            SerializedProperty scaleProperty = serializedObject.FindProperty("m_ScaleCurves");
            OptCurves(scaleProperty);

            serializedObject.ApplyModifiedProperties();
            AssetDatabase.SaveAssets();
        }
    }

    private static void OptCurves(SerializedProperty property)
    {
        if (property != null && property.isArray)
        {
            for (int i = 0; i < property.arraySize; ++i)
            {
                SerializedProperty curveProperty = property.GetArrayElementAtIndex(i).FindPropertyRelative("curve");
                SerializedProperty keyframeProperty = curveProperty.FindPropertyRelative("m_Curve");
                if (keyframeProperty != null && keyframeProperty.isArray)
                {
                    for (int j = 0; j < keyframeProperty.arraySize; ++j)
                    {
                        SerializedProperty kf = keyframeProperty.GetArrayElementAtIndex(j);

                        SerializedProperty time = kf.FindPropertyRelative("time");
                        OptValue(time);

                        SerializedProperty value = kf.FindPropertyRelative("value");
                        OptValue(value);

                        SerializedProperty inSlope = kf.FindPropertyRelative("inSlope");
                        OptValue(inSlope);

                        SerializedProperty outSlope = kf.FindPropertyRelative("outSlope");
                        OptValue(outSlope);
                    }
                }
            }
        }
    }

    private static void OptValue(SerializedProperty target)
    {
        if (target.type == "float")
            target.floatValue = OptFloat(target.floatValue);
        else if (target.type == "Vector3")
            target.vector3Value = OptVector3(target.vector3Value);
        else if (target.type == "Vector4")
            target.vector4Value = OptVector4(target.vector4Value);
        else if (target.type == "Quaternion")
            target.quaternionValue = OptQuaternion(target.quaternionValue);
    }

    private static float OptFloat(float src)
    {
        return Mathf.Floor(src * 1000 + 0.5f) / 1000;
    }

    private static Quaternion OptQuaternion(Quaternion src)
    {
        return new Quaternion(OptFloat(src.x), OptFloat(src.y), OptFloat(src.z), OptFloat(src.w));
    }

    private static Vector4 OptVector4(Vector4 src)
    {
        return new Vector4(OptFloat(src.x), OptFloat(src.y), OptFloat(src.z), OptFloat(src.w));
    }

    private static Vector3 OptVector3(Vector3 src)
    {
        return new Vector3(OptFloat(src.x), OptFloat(src.y), OptFloat(src.z));
    }

}

相关文章

  • 优化动画精度

    通过SerializedObject优化动画数据精度:

  • 动画优化

    压缩动画精度,去除动画大小缩放曲线

  • 面试准备--项目介绍CUDA

    CUDA程序优化 CUDA程序优化应该考虑的点: 精度:只在关键步骤使用双精度,其他部分仍然使用单精度浮点以获得指...

  • Unity中动画资源优化

    这个是转自侑虎的文章 下图中的动画文件不包含Scale曲线,所以这次优化中只压缩了浮点数精度。 我分别对比了动画文...

  • 多精度优化

    Wang, Handing, Yaochu Jin, and John Doherty. "A Generic T...

  • 前端知识归纳(3)-- HTML/CSS(part2 CSS动画

    目录概览: transition animation 常见CSS动画库 动画性能优化 一、transition 概...

  • 轻量级CNN及网络优化

    TX2上yolov3精度和速度优化方向 - U_C - 博客园 CNN网络优化学习总结——从MobileNet到S...

  • 05 性能优化-动画优化-帧动画

    Android 提供了AnimationDrawable用于实现帧动画。在动画开始之前,所有帧的图片都被解析并占用...

  • h5移动端性能优化

    渲染:1.动画优化a.使用css3动画b.使用requestAninmatation+Frame动画代替setTi...

  • css动画优化

    减少动画元素 减少动画元素,是动画性能优化中首先需要完成的。通过审查页面动画 DOM 元素结构,去除不必要的动画元...

网友评论

      本文标题:优化动画精度

      本文链接:https://www.haomeiwen.com/subject/gstlzttx.html