Example #1
0
        public void UpdateLayersWithContent(BodymovinLayer l)
        {
            content         = l;
            gameObject.name = content.ind + "  " + content.nm;

            positionOffset = content.positionOffset;

            transform.localPosition = content.position + positionOffset;
            transform.localRotation = content.rotation;
            transform.localScale    = content.scale;

            finalRotation = content.rotationEuler;

            MotionSetup(ref positionAnimated, ref mpos, content.positionSets);
            MotionSetup(ref anchorAnimated, ref manchor, content.anchorSets);
            MotionSetup(ref scaleAnimated, ref mscale, content.scaleSets);
            MotionSetup(ref rotationXAnimated, ref mrotx, content.rotationXSets);
            MotionSetup(ref rotationYAnimated, ref mroty, content.rotationYSets);
            MotionSetup(ref rotationZAnimated, ref mrotz, content.rotationZSets);
            MotionSetup(ref opacityAnimated, ref mopacity, content.opacitySets);

            for (int i = 0; i < shapes.Length; i++)
            {
                shapes[i].UpdateLayersWithContent(l.shapes[i]);
            }
        }
Example #2
0
        /* ---- BLENDING ---- */


        public void CreateBlendKeyframe(BodymovinLayer blendLayer, float duration, Vector2[] ease)
        {
            positionAnimated = true;
            CreateKeyframe(ref mpos, 0, duration, ease, transform.localPosition, blendLayer.position + positionOffset);

            anchorAnimated = true;
            CreateKeyframe(ref manchor, 0, duration, ease, currentAnchor, blendLayer.anchorPoint);

            scaleAnimated = true;
            CreateKeyframe(ref mscale, 0, duration, ease, transform.localScale, blendLayer.scale);

            rotationXAnimated = true;
            CreateKeyframe(ref mrotx, 0, duration, ease, new Vector3(finalRotation.x, 0, 0), new Vector3(blendLayer.rotationEuler.x, 0, 0));

            rotationYAnimated = true;
            CreateKeyframe(ref mroty, 0, duration, ease, new Vector3(finalRotation.y, 0, 0), new Vector3(blendLayer.rotationEuler.y, 0, 0));

            rotationZAnimated = true;
            CreateKeyframe(ref mrotz, 0, duration, ease, new Vector3(finalRotation.z, 0, 0), new Vector3(blendLayer.rotationEuler.z, 0, 0));

            opacityAnimated = true;
            CreateKeyframe(ref mopacity, 0, duration, ease, new Vector3(currentOpacity, 0, 0), new Vector3(blendLayer.opacity, 0, 0));

            for (int i = 0; i < shapes.Length; i++)
            {
                shapes[i].CreateBlendKeyframe(blendLayer.shapes[i], duration, ease);
            }
        }
        public static BodymovinLayer ParseLayer(JSONNode d)
        {
            BodymovinLayer i = new BodymovinLayer
            {
                nm        = d["nm"],
                inFrame   = d["ip"],
                outFrame  = d["op"],
                blendMode = d["bm"],
                refId     = d["refId"],
                startTime = d["st"]
            };

            ParseShapes(ref i, d);
            return(i);
        }
Example #4
0
        public MovinLayer(Movin movin, BodymovinLayer layer, int sort = 0)
        {
            this.movin   = movin;
            this.content = layer;
            this.sort    = sort;

            gameObject = new GameObject(content.ind + "  " + content.nm);
            transform.SetParent(movin.container.transform, false);

            positionOffset = content.positionOffset;

            transform.localPosition = content.position + positionOffset;
            transform.localRotation = content.rotation;
            transform.localScale    = content.scale;

            finalRotation = content.rotationEuler;


            /* ANIM SETUP */

            MotionSetup(ref positionAnimated, ref mpos, content.positionSets);
            MotionSetup(ref anchorAnimated, ref manchor, content.anchorSets);
            MotionSetup(ref scaleAnimated, ref mscale, content.scaleSets);
            MotionSetup(ref rotationXAnimated, ref mrotx, content.rotationXSets);
            MotionSetup(ref rotationYAnimated, ref mroty, content.rotationYSets);
            MotionSetup(ref rotationZAnimated, ref mrotz, content.rotationZSets);
            MotionSetup(ref opacityAnimated, ref mopacity, content.opacitySets);

            currentAnchor  = content.anchorPoint;
            currentOpacity = content.opacity;


            /* SHAPES */

            shapes = new MovinShape[content.shapes.Length];

            int j = 0;

            for (int i = content.shapes.Length - 1; i >= 0; i--)
            {
                MovinShape shape = new MovinShape(this, content.shapes[i]);
                shape.UpdateOpacity(content.opacity);
                shapes[i] = shape;

                //shape.transform.localPosition += new Vector3(0, 0, -32 * j);
                j += 1;
            }
        }
        public static void ParseLayers(ref BodymovinContent b, JSONNode n)
        {
            int assetLayers  = 0;
            int highestIndex = 0;

            if (n["assets"].Count > 0)
            {
                for (int q = 0; q < n["layers"].Count; q++)
                {
                    JSONNode d = n["layers"][q];

                    string r = d["refId"];
                    if (r != null)
                    {
                        for (int s = 0; s < n["assets"].Count; s++)
                        {
                            JSONNode a = n["assets"][s];
                            if (r == a["id"])
                            {
                                assetLayers += a["layers"].Count;
                                break;
                            }
                        }
                    }
                }
            }


            int j = 0;

            b.layers = new BodymovinLayer[n["layers"].Count + assetLayers];

            for (int q = 0; q < n["layers"].Count; q++)
            {
                JSONNode d = n["layers"][q];

                BodymovinLayer layer = ParseLayer(d);
                highestIndex = layer.ind > highestIndex ? layer.ind : highestIndex;
                b.layers[j]  = layer;

                if (layer.refId != null)
                {
                    for (int c = 0; c < n["assets"].Count; c++)
                    {
                        JSONNode a = n["assets"][c];

                        if (a["id"] == layer.refId)
                        {
                            for (int z = 0; z < a["layers"].Count; z++)
                            {
                                JSONNode e = a["layers"][z];
                                j++;

                                BodymovinLayer i = ParseLayer(e);
                                i.id         = a["id"];
                                i.ind       += b.layers.Length + j;
                                highestIndex = i.ind > highestIndex ? i.ind : highestIndex;
                                i.startTime  = layer.startTime;

                                if (i.parent > 0)
                                {
                                    i.parent += b.layers.Length + j + 1;
                                }
                                else
                                {
                                    i.parent         = layer.ind;
                                    i.positionOffset = -layer.anchorPoint;
                                }

                                b.layers[j] = i;
                            }
                            break;
                        }
                    }
                }

                j++;
            }

            b.highestLayerIndex = highestIndex;
        }
        public static void ParseShapes(ref BodymovinLayer b, JSONNode n)
        {
            int j = 0;

            b.nm            = n["nm"];
            b.parent        = n["parent"];
            b.ind           = n["ind"];
            b.shapes        = new BodymovinShape[n["shapes"].Count];
            b.anchorPoint   = new Vector3(n["ks"]["a"]["k"][0].AsFloat, -n["ks"]["a"]["k"][1], n["ks"]["a"]["k"][2]);
            b.position      = new Vector3(n["ks"]["p"]["k"][0].AsFloat, -n["ks"]["p"]["k"][1], n["ks"]["p"]["k"][2]);
            b.rotationEuler = new Vector3(-n["ks"]["rx"]["k"].AsFloat, n["ks"]["ry"]["k"].AsFloat, -n["ks"]["rz"]["k"].AsFloat);
            b.rotationXSets = new BodymovinAnimatedProperties[n["ks"]["rx"]["k"].Count];
            b.rotationYSets = new BodymovinAnimatedProperties[n["ks"]["ry"]["k"].Count];
            b.rotationZSets = new BodymovinAnimatedProperties[n["ks"]["rz"]["k"].Count];
            b.scale         = new Vector3(n["ks"]["s"]["k"][0].AsFloat * 0.01f, n["ks"]["s"]["k"][1] * 0.01f, n["ks"]["s"]["k"][2] * 0.01f);
            b.opacity       = n["ks"]["o"]["k"].AsFloat;
            b.opacitySets   = new BodymovinAnimatedProperties[n["ks"]["o"]["k"].Count];

            int positionAnimated = n["ks"]["p"]["a"].AsInt;

            b.positionSets = new BodymovinAnimatedProperties[positionAnimated == 1 ? n["ks"]["p"]["k"].Count : 0];

            int scaleAnimated = n["ks"]["s"]["a"].AsInt;

            b.scaleSets = new BodymovinAnimatedProperties[scaleAnimated == 1 ? n["ks"]["s"]["k"].Count : 0];

            int anchorAnimated = n["ks"]["a"]["a"].AsInt;

            b.anchorSets = new BodymovinAnimatedProperties[anchorAnimated == 1 ? n["ks"]["a"]["k"].Count : 0];


            // 2D Rotation
            if (b.rotationEuler == Vector3.zero)
            {
                b.rotationEuler = new Vector3(0, 0, -n["ks"]["r"]["k"].AsFloat);
            }

            int rotation2DAnimated = n["ks"]["r"]["a"].AsInt;

            if (rotation2DAnimated > 0)
            {
                b.rotationZSets = new BodymovinAnimatedProperties[n["ks"]["r"]["k"].Count];
            }

            // Animated opacity
            if (b.opacitySets.Length > 0)
            {
                for (int i = 0; i < n["ks"]["o"]["k"].Count; i++)
                {
                    JSONNode k = n["ks"]["o"]["k"][i];
                    b.opacitySets[i] = new BodymovinAnimatedProperties
                    {
                        t = k["t"],
                        i = new Vector2(k["i"]["x"][0].AsFloat, k["i"]["y"][0].AsFloat),
                        o = new Vector2(k["o"]["x"][0].AsFloat, k["o"]["y"][0].AsFloat),

                        sf = k["s"][0].AsFloat,
                        ef = k["e"][0].AsFloat
                    };

                    //Debug.Log(i + " - " + b.rotationXSets[i].i + "  " + b.rotationXSets[i].o + "  " + b.rotationXSets[i].sf + "  " + b.rotationXSets[i].ef + "  " + b.rotationXSets[i].t);
                }
            }

            // Rotation X
            if (b.rotationXSets.Length > 0)
            {
                for (int i = 0; i < n["ks"]["rx"]["k"].Count; i++)
                {
                    JSONNode k = n["ks"]["rx"]["k"][i];
                    b.rotationXSets[i] = new BodymovinAnimatedProperties
                    {
                        t = k["t"],
                        i = new Vector2(k["i"]["x"][0].AsFloat, k["i"]["y"][0].AsFloat),
                        o = new Vector2(k["o"]["x"][0].AsFloat, k["o"]["y"][0].AsFloat),

                        sf = -k["s"][0].AsFloat,
                        ef = -k["e"][0].AsFloat
                    };

                    //Debug.Log(i + " - " + b.rotationXSets[i].i + "  " + b.rotationXSets[i].o + "  " + b.rotationXSets[i].sf + "  " + b.rotationXSets[i].ef + "  " + b.rotationXSets[i].t);
                }

                b.rotationEuler.x = b.rotationXSets[0].sf;
            }


            // Rotation Y
            if (b.rotationYSets.Length > 0)
            {
                for (int i = 0; i < n["ks"]["ry"]["k"].Count; i++)
                {
                    JSONNode k = n["ks"]["ry"]["k"][i];
                    b.rotationYSets[i] = new BodymovinAnimatedProperties
                    {
                        t = k["t"],
                        i = new Vector2(k["i"]["x"][0].AsFloat, k["i"]["y"][0].AsFloat),
                        o = new Vector2(k["o"]["x"][0].AsFloat, k["o"]["y"][0].AsFloat),

                        sf = k["s"][0].AsFloat,
                        ef = k["e"][0].AsFloat
                    };

                    //Debug.Log(i + " - " + b.rotationYSets[i].i + "  " + b.rotationYSets[i].o + "  " + b.rotationYSets[i].sf + "  " + b.rotationYSets[i].ef + "  " + b.rotationYSets[i].t);
                }

                b.rotationEuler.y = b.rotationYSets[0].sf;
            }


            // Rotation Z
            if (b.rotationZSets.Length > 0)
            {
                string r = rotation2DAnimated > 0 ? "r" : "rz";

                for (int i = 0; i < n["ks"][r]["k"].Count; i++)
                {
                    JSONNode k = n["ks"][r]["k"][i];
                    b.rotationZSets[i] = new BodymovinAnimatedProperties
                    {
                        t = k["t"],
                        i = new Vector2(k["i"]["x"][0].AsFloat, k["i"]["y"][0].AsFloat),
                        o = new Vector2(k["o"]["x"][0].AsFloat, k["o"]["y"][0].AsFloat),

                        sf = -k["s"][0].AsFloat,
                        ef = -k["e"][0].AsFloat
                    };

                    //Debug.Log(i + " - " + b.rotationZSets[i].i + "  " + b.rotationZSets[i].o + "  " + b.rotationZSets[i].sf + "  " + b.rotationZSets[i].ef + "  " + b.rotationZSets[i].t);
                }

                b.rotationEuler.z = b.rotationZSets[0].sf;
            }

            b.rotation = Quaternion.Euler(b.rotationEuler);


            // Scale
            if (b.scaleSets.Length > 0)
            {
                for (int i = 0; i < n["ks"]["s"]["k"].Count; i++)
                {
                    JSONNode k = n["ks"]["s"]["k"][i];
                    b.scaleSets[i] = new BodymovinAnimatedProperties
                    {
                        t  = k["t"],
                        ix = k["i"]["x"],
                        iy = k["i"]["y"],
                        ox = k["o"]["x"],
                        oy = k["o"]["y"],

                        s = k["s"],
                        e = k["e"]
                    };

                    b.scaleSets[i].s *= 0.01f;
                    b.scaleSets[i].e *= 0.01f;

                    //Debug.Log(i + " scale - " + b.scaleSets[i].ix + "  " + b.scaleSets[i].ox + "  " + b.scaleSets[i].s + "  " + b.scaleSets[i].e + "  " + b.scaleSets[i].t);
                }

                b.scale = b.scaleSets[0].s;
            }


            // Position
            if (b.positionSets.Length > 0)
            {
                for (int i = 0; i < n["ks"]["p"]["k"].Count; i++)
                {
                    JSONNode k = n["ks"]["p"]["k"][i];
                    b.positionSets[i] = new BodymovinAnimatedProperties
                    {
                        t  = k["t"],
                        i  = k["i"],
                        o  = k["o"],
                        to = k["to"],
                        ti = k["ti"],

                        s = k["s"],
                        e = k["e"]
                    };

                    b.positionSets[i].s.y = -b.positionSets[i].s.y;
                    b.positionSets[i].e.y = -b.positionSets[i].e.y;

                    //Debug.Log(i + " - " + b.positionSets[i].i + "  " + b.positionSets[i].o + "  " + b.positionSets[i].s + "  " + b.positionSets[i].e + "  " + b.positionSets[i].t);
                }

                b.position = b.positionSets[0].s;
            }



            // Anchor point
            if (b.anchorSets.Length > 0)
            {
                for (int i = 0; i < n["ks"]["a"]["k"].Count; i++)
                {
                    JSONNode k = n["ks"]["a"]["k"][i];
                    b.anchorSets[i] = new BodymovinAnimatedProperties
                    {
                        t  = k["t"],
                        i  = k["i"],
                        o  = k["o"],
                        to = k["to"],
                        ti = k["ti"],

                        s = k["s"],
                        e = k["e"]
                    };

                    b.anchorSets[i].s.y = -b.anchorSets[i].s.y;
                    b.anchorSets[i].e.y = -b.anchorSets[i].e.y;
                }

                b.anchorPoint = b.anchorSets[0].s;
            }


            // Items
            for (int i = 0; i < n["shapes"].Count; i++)
            {
                JSONNode       d = n["shapes"][i];
                BodymovinShape s = new BodymovinShape {
                    ty = d["ty"]
                };

                ParseItems(ref s, d);
                b.shapes[j] = s;
                j++;
            }
        }