Example #1
0
        private Dictionary <string, float> calculatePropertyErrors(RecordedData recordingData)
        {
            var       propertyToMaxError = new Dictionary <string, float>();
            Transform currTransform      = recordingData.transform;

            while (currTransform != null)
            {
                var compressionSettings = currTransform.GetComponent <PropertyCompression>();
                if (compressionSettings != null)
                {
                    foreach (var setting in compressionSettings.compressionOverrides)
                    {
                        if (!propertyToMaxError.ContainsKey(setting.propertyName))
                        {
                            propertyToMaxError.Add(setting.propertyName, setting.maxError);
                        }
                    }
                }
                currTransform = currTransform.parent;
            }

            return(propertyToMaxError);
        }
Example #2
0
        private void doCompression(ProgressBar progress,
                                   RecordedData recordingData,
                                   Dictionary <EditorCurveBinding, AnimationCurve> toCompress,
                                   Dictionary <EditorCurveBinding, AnimationCurve> bindingMap)
        {
            var propertyToMaxError = calculatePropertyErrors(recordingData);

            List <EditorCurveBinding> bindings;

            progress.Begin(6, "", "", () => {
                //First do rotations
                bindings = toCompress.Keys.Query().ToList();
                progress.Begin(bindings.Count, "", "", () => {
                    foreach (var wBinding in bindings)
                    {
                        progress.Step();

                        if (!wBinding.propertyName.EndsWith(".w"))
                        {
                            continue;
                        }

                        string property = wBinding.propertyName.Substring(0, wBinding.propertyName.Length - 2);
                        string xProp    = property + ".x";
                        string yProp    = property + ".y";
                        string zProp    = property + ".z";

                        var xMaybe = toCompress.Keys.Query().FirstOrNone(t => t.propertyName == xProp);
                        var yMaybe = toCompress.Keys.Query().FirstOrNone(t => t.propertyName == yProp);
                        var zMaybe = toCompress.Keys.Query().FirstOrNone(t => t.propertyName == zProp);

                        Maybe.MatchAll(xMaybe, yMaybe, zMaybe, (xBinding, yBinding, zBinding) => {
                            float maxAngleError;
                            if (!propertyToMaxError.TryGetValue(property, out maxAngleError))
                            {
                                maxAngleError = rotationMaxError;
                            }

                            AnimationCurve compressedX, compressedY, compressedZ, compressedW;
                            AnimationCurveUtil.CompressRotations(toCompress[xBinding],
                                                                 toCompress[yBinding],
                                                                 toCompress[zBinding],
                                                                 toCompress[wBinding],
                                                                 out compressedX,
                                                                 out compressedY,
                                                                 out compressedZ,
                                                                 out compressedW,
                                                                 maxAngleError);

                            bindingMap[xBinding] = compressedX;
                            bindingMap[yBinding] = compressedY;
                            bindingMap[zBinding] = compressedZ;
                            bindingMap[wBinding] = compressedW;

                            toCompress.Remove(xBinding);
                            toCompress.Remove(yBinding);
                            toCompress.Remove(zBinding);
                            toCompress.Remove(wBinding);
                        });
                    }
                });

                //Next do scales
                bindings = toCompress.Keys.Query().ToList();
                progress.Begin(bindings.Count, "", "", () => {
                    foreach (var binding in bindings)
                    {
                        progress.Step();

                        if (!binding.propertyName.EndsWith(".x") &&
                            !binding.propertyName.EndsWith(".y") &&
                            !binding.propertyName.EndsWith(".z"))
                        {
                            continue;
                        }

                        if (!binding.propertyName.Contains("LocalScale"))
                        {
                            continue;
                        }

                        bindingMap[binding] = AnimationCurveUtil.CompressScale(toCompress[binding], scaleMaxError);
                        toCompress.Remove(binding);
                    }
                });

                //Next do positions
                bindings = toCompress.Keys.Query().ToList();
                progress.Begin(bindings.Count, "", "", () => {
                    foreach (var xBinding in bindings)
                    {
                        progress.Step();

                        if (!xBinding.propertyName.EndsWith(".x"))
                        {
                            continue;
                        }

                        string property = xBinding.propertyName.Substring(0, xBinding.propertyName.Length - 2);
                        string yProp    = property + ".y";
                        string zProp    = property + ".z";

                        var yMaybe = toCompress.Keys.Query().FirstOrNone(t => t.propertyName == yProp);
                        var zMaybe = toCompress.Keys.Query().FirstOrNone(t => t.propertyName == zProp);

                        Maybe.MatchAll(yMaybe, zMaybe, (yBinding, zBinding) => {
                            float maxDistanceError;
                            if (!propertyToMaxError.TryGetValue(property, out maxDistanceError))
                            {
                                maxDistanceError = positionMaxError;
                            }

                            AnimationCurve compressedX, compressedY, compressedZ;
                            AnimationCurveUtil.CompressPositions(toCompress[xBinding],
                                                                 toCompress[yBinding],
                                                                 toCompress[zBinding],
                                                                 out compressedX,
                                                                 out compressedY,
                                                                 out compressedZ,
                                                                 maxDistanceError);

                            bindingMap[xBinding] = compressedX;
                            bindingMap[yBinding] = compressedY;
                            bindingMap[zBinding] = compressedZ;

                            toCompress.Remove(xBinding);
                            toCompress.Remove(yBinding);
                            toCompress.Remove(zBinding);
                        });
                    }
                });

                //Next do colors
                bindings = toCompress.Keys.Query().ToList();
                progress.Begin(bindings.Count, "", "", () => {
                    foreach (var rBinding in bindings)
                    {
                        progress.Step();

                        if (!rBinding.propertyName.EndsWith(".r"))
                        {
                            continue;
                        }

                        string property = rBinding.propertyName.Substring(0, rBinding.propertyName.Length - 2);
                        string gProp    = property + ".g";
                        string bProp    = property + ".b";

                        var gMaybe = toCompress.Keys.Query().FirstOrNone(t => t.propertyName == gProp);
                        var bMaybe = toCompress.Keys.Query().FirstOrNone(t => t.propertyName == bProp);

                        Maybe.MatchAll(gMaybe, bMaybe, (gBinding, bBinding) => {
                            AnimationCurve compressedR, compressedG, compressedB;
                            AnimationCurveUtil.CompressColorsHSV(toCompress[rBinding],
                                                                 toCompress[gBinding],
                                                                 toCompress[bBinding],
                                                                 out compressedR,
                                                                 out compressedG,
                                                                 out compressedB,
                                                                 colorHueMaxError,
                                                                 colorSaturationMaxError,
                                                                 colorValueMaxError);

                            bindingMap[rBinding] = compressedR;
                            bindingMap[gBinding] = compressedG;
                            bindingMap[bBinding] = compressedB;
                        });
                    }
                });

                //Then do color alpha
                bindings = toCompress.Keys.Query().ToList();
                progress.Begin(bindings.Count, "", "", () => {
                    foreach (var aBinding in bindings)
                    {
                        progress.Step();

                        if (!aBinding.propertyName.EndsWith(".a"))
                        {
                            continue;
                        }

                        var compressedA = AnimationCurveUtil.Compress(toCompress[aBinding], colorAlphaMaxError);

                        toCompress.Remove(aBinding);
                        bindingMap[aBinding] = compressedA;
                    }
                });

                //Then everything else
                bindings = toCompress.Keys.Query().ToList();
                progress.Begin(bindings.Count, "", "", () => {
                    foreach (var binding in bindings)
                    {
                        progress.Step();

                        float maxError;
                        if (!propertyToMaxError.TryGetValue(binding.propertyName, out maxError))
                        {
                            maxError = genericMaxError;
                        }

                        var compressedCurve = AnimationCurveUtil.Compress(toCompress[binding], maxError);

                        toCompress.Remove(binding);
                        bindingMap[binding] = compressedCurve;
                    }
                });
            });
        }