コード例 #1
0
    private bool UpdateAnimationLayer(int layerNum)
    {
        var layer = Anim.layers[layerNum];

        var lastFrame  = 0;
        var lastSource = 0;
        var startAnim  = -1;
        var nextAnim   = -1;

        for (var i = 0; i < layer.animations.Length; i++)
        {
            var a = layer.animations[i];
            if (a.frame < frame)
            {
                if (a.type == 0)
                {
                    startAnim = i;
                }
                if (a.type == 1)
                {
                    nextAnim = i;
                }
            }

            lastFrame = Mathf.Max(lastFrame, a.frame);
            if (a.type == 0)
            {
                lastSource = Mathf.Max(lastSource, a.frame);
            }
        }

        if (startAnim < 0 || (nextAnim < 0 && lastFrame < frame))
        {
            return(false);
        }

        var from = layer.animations[startAnim];

        STR.Animation to = null;

        if (nextAnim >= 0)
        {
            to = layer.animations[nextAnim];
        }
        var delta     = frame - from.frame;
        var blendSrc  = (int)from.srcAlpha;
        var blendDest = (int)from.destAlpha;

        var mat  = GetEffectMaterial(layerNum, blendSrc, blendDest);
        var go   = layerObjects[layerNum];
        var mr   = layerRenderers[layerNum];
        var mf   = layerFilters[layerNum];
        var mesh = layerMeshes[layerNum];

        mr.material = mat;

        if (nextAnim != startAnim + 1 || to?.frame != from.frame)
        {
            if (to != null && lastSource <= from.frame)
            {
                return(false);
            }

            var fixedFrame = layer.texturesIds[(int)from.animFrame];
            UpdateMesh(mf, mesh, from.xy, from.uv, from.angle, fixedFrame);
            UpdateLayerData(go, mat, from.position, from.color);
            return(true);
        }

        var prog = Mathf.InverseLerp(from.frame, to.frame, frame);

        for (var i = 0; i < 4; i++)
        {
            tempPositions2[i] = from.xy[i] + to.xy[i] * delta;
            tempUvs2[i]       = from.uv[i] + to.uv[i] * delta;
        }

        //Debug.Log("from: " + from.Position + " to: " + to.Position + " delta:" + delta);
        var pos   = from.position + to.position * delta;
        var angle = from.animFrame + to.angle * delta;
        var color = from.color + to.color * delta;
        //color.a *= 0.5f;
        //Debug.Log(color);

        var frameId = 0;

        switch (to.animType)
        {
        case 1:
            frameId = Mathf.FloorToInt(from.animFrame + to.animFrame * delta);
            break;

        case 2:
            frameId = Mathf.FloorToInt(Mathf.Min(from.animFrame + to.delay * delta, layer.textures.Length - 1));
            break;

        case 3:
            frameId = Mathf.FloorToInt((from.animFrame + to.delay * delta) % layer.textures.Length);
            break;

        case 4:
            frameId = Mathf.FloorToInt((from.animFrame - to.delay * delta) % layer.textures.Length);
            break;
        }

        var texIndex = layer.texturesIds[frameId];

        UpdateMesh(mf, mesh, tempPositions2, tempUvs2, angle, texIndex);
        UpdateLayerData(go, mat, pos, color);

        return(true);
    }
コード例 #2
0
        public static STR Load(MemoryStreamReader data, string path)
        {
            var header = data.ReadBinaryString(4);

            if (!header.Equals(STR.Header))
            {
                throw new Exception("EffectLoader.Load: Header (" + header + ") is not \"STRM\"");
            }

            var version = data.ReadUInt();

            if (version != 0x94)
            {
                throw new Exception("EffectLoader.Load: Unsupported STR version (v" + version + ")");
            }

            STR str = new STR();

            str.version = version;
            str.fps     = data.ReadUInt();
            str.maxKey  = data.ReadUInt();
            var layerCount = data.ReadUInt();

            data.Seek(16, System.IO.SeekOrigin.Current);


            //read layers
            str.layers = new STR.Layer[layerCount];
            for (uint i = 0; i < layerCount; i++)
            {
                STR.Layer layer = str.layers[i] = new STR.Layer();

                //read texture filenames
                var textureCount = data.ReadInt();
                layer.textures    = new Texture2D[textureCount];
                layer.texturesIds = new List <int>();
                for (int j = 0; j < textureCount; j++)
                {
                    var tex     = data.ReadBinaryString(128);
                    var texture = FileManager.Load(path + "/" + tex) as Texture2D;
                    layer.textures[j] = texture;

                    if (!textureNames.Contains(tex))
                    {
                        layer.texturesIds.Add(textureNames.Count);
                        textureIdLookup.Add(tex, textureNames.Count);
                        textureNames.Add(tex);
                        textures.Add(texture);
                    }
                    else
                    {
                        layer.texturesIds.Add(textureIdLookup[tex]);
                    }
                }

                //read animations
                var animCount = data.ReadInt();
                layer.animations = new STR.Animation[animCount];
                for (int j = 0; j < animCount; j++)
                {
                    var entry = new STR.Animation()
                    {
                        frame    = data.ReadInt(),
                        type     = data.ReadUInt(),
                        position = new Vector2(data.ReadFloat(), data.ReadFloat())
                    };

                    var uv = new float[] {
                        data.ReadFloat(), data.ReadFloat(), data.ReadFloat(), data.ReadFloat(),
                        data.ReadFloat(), data.ReadFloat(), data.ReadFloat(), data.ReadFloat()
                    };
                    var xy = new float[] {
                        data.ReadFloat(), data.ReadFloat(), data.ReadFloat(), data.ReadFloat(),
                        data.ReadFloat(), data.ReadFloat(), data.ReadFloat(), data.ReadFloat()
                    };

                    entry.uv    = new Vector2[4];
                    entry.uv[0] = new Vector2(0, 0);
                    entry.uv[1] = new Vector2(1, 0);
                    entry.uv[2] = new Vector2(0, 1);
                    entry.uv[3] = new Vector2(1, 1);

                    entry.xy    = new Vector2[4];
                    entry.xy[0] = new Vector2(xy[0], -xy[4]);
                    entry.xy[1] = new Vector2(xy[1], -xy[5]);
                    entry.xy[2] = new Vector2(xy[3], -xy[7]);
                    entry.xy[3] = new Vector2(xy[2], -xy[6]);

                    entry.animFrame = data.ReadFloat();
                    entry.animType  = data.ReadUInt();
                    entry.delay     = data.ReadFloat();
                    entry.angle     = data.ReadFloat() / (1024f / 360f);
                    entry.color     = new Color(data.ReadFloat() / 255, data.ReadFloat() / 255, data.ReadFloat() / 255, data.ReadFloat() / 255);
                    entry.srcAlpha  = data.ReadUInt();
                    entry.destAlpha = data.ReadUInt();
                    entry.mtPreset  = data.ReadUInt();

                    layer.animations[j] = entry;
                }
            }

            return(MakeAtlas(str, path));
        }