예제 #1
0
    public virtual bool PrepareQuad(RectF cullRect, out QuadSpec quad)
    {
        float entX = Core.SnapToPixel(entity.Position.X);
        float entY = Core.SnapToPixel(entity.Position.Y);
        float entZ = 0; //entY+entX*0.0001f; //TODO

        float S = 4;    // snap positions
        float M = S / (float)Math.PI;
        float visualRotation = (float)Math.Floor((entity.Rotation + Math.PI / S / 2) * M) / M;

        //visualRotation = entity.Rotation;

        bool flipX = quadData.flipX;

        float scaleX = quadData.atlasTile.width * Core.ATLAS_TO_WORLD * quadData.scale.X;
        float scaleY = quadData.atlasTile.height * Core.ATLAS_TO_WORLD * quadData.scale.Y;

        float originX = quadData.origin.X;
        float originY = quadData.origin.Y;

        float topShear   = quadData.topShear;
        float rightShear = quadData.rightShear;

        Vector3 corner0;
        Vector3 corner1;
        Vector3 corner2;
        Vector3 corner3;

        if (Math.Abs(visualRotation) > 0.001f)
        {
            float rotSin = (float)Math.Sin(visualRotation);
            float rotCos = (float)Math.Cos(visualRotation);
            float ooX    = rotCos * originX - rotSin * originY;
            float ooY    = rotSin * originX + rotCos * originY;
            corner0 = new Vector3(entX - ooX, entY - ooY, 0);
            corner1 = new Vector3(entX - rotSin * scaleY - ooX + topShear, entY + rotCos * scaleY - ooY, 0);
            corner2 = new Vector3(entX + (rotCos * scaleX - rotSin * scaleY - ooX) + topShear, entY + (rotCos * scaleY + rotSin * scaleX) - ooY + rightShear, 0);
            corner3 = new Vector3(entX + rotCos * scaleX - ooX, entY + rotSin * scaleX - ooY + rightShear, 0);
        }
        else
        {
            corner0 = new Vector3(entX - originX, entY - originY, entZ);
            corner1 = new Vector3(entX - originX + topShear, entY + scaleY - originY, entZ);
            corner2 = new Vector3(entX + scaleX - originX + topShear, entY + scaleY - originY + rightShear, entZ);
            corner3 = new Vector3(entX + scaleX - originX, entY - originY + rightShear, entZ);
        }

        //narrowphase culling
        RectF aabb = MathUtils.AABBFromCorners(corner0, corner1, corner2, corner3);

        if (!cullRect.Intersects(aabb))
        {
            quad = new QuadSpec();
            return(false);
        }

        Dbg.AddDebugRect(aabb, Color.Yellow, 0.5f);

        //TODO: get verts positions and uv from quaddata method?

        Vector2 corner0uv = new Vector2(quadData.atlasTile.X, quadData.atlasTile.Bottom);
        Vector2 corner1uv = new Vector2(quadData.atlasTile.X, quadData.atlasTile.Y);
        Vector2 corner2uv = new Vector2(quadData.atlasTile.Right, quadData.atlasTile.Y);
        Vector2 corner3uv = new Vector2(quadData.atlasTile.Right, quadData.atlasTile.Bottom);

        if (flipX)
        {
            GeneralUtils.Swap(ref corner0uv, ref corner3uv);
            GeneralUtils.Swap(ref corner1uv, ref corner2uv);
        }

        quad = new QuadSpec(
            corner0, corner1, corner2, corner3,
            corner0uv, corner1uv, corner2uv, corner3uv,
            Color.White, Color.White, Color.White, Color.White
            );
        return(true);
    }