public override void render(AsRenderSupport support, float parentAlpha)
 {
     if (mFlattenedContents != null || mFlattenRequested)
     {
         if (mFlattenedContents == null)
         {
             mFlattenedContents = new AsVector <AsQuadBatch>();
         }
         if (mFlattenRequested)
         {
             AsQuadBatch.compile(this, mFlattenedContents);
             mFlattenRequested = false;
         }
         float    alpha      = parentAlpha * this.getAlpha();
         int      numBatches = (int)(mFlattenedContents.getLength());
         AsMatrix mvpMatrix  = support.getMvpMatrix();
         support.finishQuadBatch();
         support.raiseDrawCount((uint)(numBatches));
         int i = 0;
         for (; i < numBatches; ++i)
         {
             AsQuadBatch quadBatch = mFlattenedContents[i];
             String      blendMode = quadBatch.getBlendMode() == AsBlendMode.AUTO ? support.getBlendMode() : quadBatch.getBlendMode();
             quadBatch.renderCustom(mvpMatrix, alpha, blendMode);
         }
     }
     else
     {
         base.render(support, parentAlpha);
     }
 }
 public static void uploadBitmapData(bc.flash.display3D.textures.AsTexture nativeTexture, AsBitmapData data, bool generateMipmaps)
 {
     nativeTexture.uploadFromBitmapData(data);
     if (generateMipmaps && data.getWidth() > 1 && data.getHeight() > 1)
     {
         int          currentWidth  = data.getWidth() >> 1;
         int          currentHeight = data.getHeight() >> 1;
         int          level         = 1;
         AsBitmapData canvas        = new AsBitmapData(currentWidth, currentHeight, true, 0);
         AsMatrix     transform     = new AsMatrix(.5f, 0, 0, .5f);
         AsRectangle  bounds        = new AsRectangle();
         while (currentWidth >= 1 || currentHeight >= 1)
         {
             bounds.width  = currentWidth;
             bounds.height = currentHeight;
             canvas.fillRect(bounds, (uint)(0));
             canvas.draw(data, transform, null, null, null, true);
             nativeTexture.uploadFromBitmapData(canvas, (uint)(level++));
             transform.scale(0.5f, 0.5f);
             currentWidth  = currentWidth >> 1;
             currentHeight = currentHeight >> 1;
         }
         canvas.dispose();
     }
 }
        public static void prependRotation(AsMatrix matrix, float angle)
        {
            float sin = AsMath.sin(angle);
            float cos = AsMath.cos(angle);

            matrix.setTo(matrix.a * cos + matrix.c * sin, matrix.b * cos + matrix.d * sin, matrix.c * cos - matrix.a * sin, matrix.d * cos - matrix.b * sin, matrix.tx, matrix.ty);
        }
        public virtual void addQuadBatch(AsQuadBatch quadBatch, float parentAlpha, AsMatrix modelViewMatrix, String blendMode)
        {
            if (modelViewMatrix == null)
            {
                modelViewMatrix = quadBatch.getTransformationMatrix();
            }
            bool  tinted   = quadBatch.mTinted || parentAlpha != 1.0f;
            float alpha    = parentAlpha * quadBatch.getAlpha();
            int   vertexID = mNumQuads * 4;
            int   numQuads = quadBatch.getNumQuads();

            if (mNumQuads + numQuads > getCapacity())
            {
                expand(mNumQuads + numQuads);
            }
            if (mNumQuads == 0)
            {
                this.setBlendMode(blendMode != null ? blendMode : quadBatch.getBlendMode());
                mTexture   = quadBatch.mTexture;
                mTinted    = tinted;
                mSmoothing = quadBatch.mSmoothing;
                mVertexData.setPremultipliedAlpha(quadBatch.mVertexData.getPremultipliedAlpha(), false);
            }
            quadBatch.mVertexData.copyTo(mVertexData, vertexID, 0, numQuads * 4);
            mVertexData.transformVertex(vertexID, modelViewMatrix, numQuads * 4);
            if (alpha != 1.0f)
            {
                mVertexData.scaleAlpha(vertexID, alpha, numQuads * 4);
            }
            mSyncRequired = true;
            mNumQuads     = mNumQuads + numQuads;
        }
Example #5
0
 public static AsPoint transformCoords(AsMatrix matrix, float x, float y, AsPoint resultPoint)
 {
     if (resultPoint == null)
     {
         resultPoint = new AsPoint();
     }
     matrix.transformPointCords(x, y, resultPoint);
     return(resultPoint);
 }
 public static AsPoint transformCoords(AsMatrix matrix, float x, float y, AsPoint resultPoint)
 {
     if (resultPoint == null)
     {
         resultPoint = new AsPoint();
     }
     resultPoint.x = matrix.a * x + matrix.c * y + matrix.tx;
     resultPoint.y = matrix.d * y + matrix.b * x + matrix.ty;
     return(resultPoint);
 }
        public override AsRectangle getBounds(AsDisplayObject targetSpace, AsRectangle resultRect)
        {
            if (resultRect == null)
            {
                resultRect = new AsRectangle();
            }
            AsMatrix transformationMatrix = targetSpace == this ? null : getTransformationMatrix(targetSpace, sHelperMatrix);

            return(mVertexData.getBounds(transformationMatrix, 0, mNumQuads * 4, resultRect));
        }
 public AsDisplayObject()
 {
     if (AsCapabilities.getIsDebugger() && AsGlobal.getQualifiedClassName(this) == "starling.display::DisplayObject")
     {
         throw new AsAbstractClassError();
     }
     mX                    = mY = mPivotX = mPivotY = mRotation = mSkewX = mSkewY = 0.0f;
     mScaleX               = mScaleY = mAlpha = 1.0f;
     mVisible              = mTouchable = true;
     mBlendMode            = AsBlendMode.AUTO;
     mTransformationMatrix = new AsMatrix();
     mOrientationChanged   = mUseHandCursor = false;
 }
        public virtual void transformVertex(int vertexID, AsMatrix matrix, int numVertices)
        {
            int offset = getOffset(vertexID) + POSITION_OFFSET;
            int i      = 0;

            for (; i < numVertices; ++i)
            {
                float x = mRawData[offset];
                float y = mRawData[(offset + 1)];
                mRawData[offset]       = matrix.a * x + matrix.c * y + matrix.tx;
                mRawData[(offset + 1)] = matrix.d * y + matrix.b * x + matrix.ty;
                offset = offset + ELEMENTS_PER_VERTEX;
            }
        }
        public virtual AsRectangle getBounds(AsMatrix transformationMatrix, int vertexID, int numVertices, AsRectangle resultRect)
        {
            if (resultRect == null)
            {
                resultRect = new AsRectangle();
            }
            if (numVertices < 0 || vertexID + numVertices > mNumVertices)
            {
                numVertices = mNumVertices - vertexID;
            }
            float minX   = AsNumber.MAX_VALUE;
            float maxX   = -AsNumber.MAX_VALUE;
            float minY   = AsNumber.MAX_VALUE;
            float maxY   = -AsNumber.MAX_VALUE;
            int   offset = getOffset(vertexID) + POSITION_OFFSET;
            float x      = 0;
            float y      = 0;
            int   i      = 0;

            if (transformationMatrix == null)
            {
                for (i = vertexID; i < numVertices; ++i)
                {
                    x      = mRawData[offset];
                    y      = mRawData[(offset + 1)];
                    offset = offset + ELEMENTS_PER_VERTEX;
                    minX   = minX < x ? minX : x;
                    maxX   = maxX > x ? maxX : x;
                    minY   = minY < y ? minY : y;
                    maxY   = maxY > y ? maxY : y;
                }
            }
            else
            {
                for (i = vertexID; i < numVertices; ++i)
                {
                    x      = mRawData[offset];
                    y      = mRawData[(offset + 1)];
                    offset = offset + ELEMENTS_PER_VERTEX;
                    AsMatrixUtil.transformCoords(transformationMatrix, x, y, sHelperPoint);
                    minX = minX < sHelperPoint.x ? minX : sHelperPoint.x;
                    maxX = maxX > sHelperPoint.x ? maxX : sHelperPoint.x;
                    minY = minY < sHelperPoint.y ? minY : sHelperPoint.y;
                    maxY = maxY > sHelperPoint.y ? maxY : sHelperPoint.y;
                }
            }
            resultRect.setTo(minX, minY, maxX - minX, maxY - minY);
            return(resultRect);
        }
 public static AsMatrix3D convertTo3D(AsMatrix matrix, AsMatrix3D resultMatrix)
 {
     if (resultMatrix == null)
     {
         resultMatrix = new AsMatrix3D();
     }
     sRawData[0]  = matrix.a;
     sRawData[1]  = matrix.b;
     sRawData[4]  = matrix.c;
     sRawData[5]  = matrix.d;
     sRawData[12] = matrix.tx;
     sRawData[13] = matrix.ty;
     resultMatrix.copyRawDataFrom(sRawData);
     return(resultMatrix);
 }
 public AsRenderSupport()
 {
     mProjectionMatrix   = new AsMatrix();
     mModelViewMatrix    = new AsMatrix();
     mMvpMatrix          = new AsMatrix();
     mMvpMatrix3D        = new AsMatrix3D();
     mMatrixStack        = new AsVector <AsMatrix>();
     mMatrixStackSize    = 0;
     mDrawCount          = 0;
     mRenderTarget       = null;
     mBlendMode          = AsBlendMode.NORMAL;
     mCurrentQuadBatchID = 0;
     mQuadBatches        = new AsVector <AsQuadBatch>();
     loadIdentity();
     setOrthographicProjection(0, 0, 400, 300);
 }
        public static void skew(AsMatrix matrix, float skewX, float skewY)
        {
            float a    = matrix.a;
            float b    = matrix.b;
            float c    = matrix.c;
            float d    = matrix.d;
            float tx   = matrix.tx;
            float ty   = matrix.ty;
            float sinX = AsMath.sin(skewX);
            float cosX = AsMath.cos(skewX);
            float sinY = AsMath.sin(skewY);
            float cosY = AsMath.cos(skewY);

            matrix.a = a * cosY + c * sinY;
            matrix.b = b * cosY + d * sinY;
            matrix.c = c * cosX - a * sinX;
            matrix.d = d * cosX - b * sinX;
        }
        public virtual void renderCustom(AsMatrix mvpMatrix, float parentAlpha, String blendMode)
        {
            if (mNumQuads == 0)
            {
                return;
            }
            if (mSyncRequired)
            {
                syncBuffers();
            }
            bool        pma         = mVertexData.getPremultipliedAlpha();
            AsContext3D context     = AsStarling.getContext();
            bool        tinted      = mTinted || (parentAlpha != 1.0f);
            String      programName = mTexture != null?getImageProgramName(tinted, mTexture.getMipMapping(), mTexture.getRepeat(), mTexture.getFormat(), mSmoothing) : QUAD_PROGRAM_NAME;

            sRenderAlpha[0] = sRenderAlpha[1] = sRenderAlpha[2] = pma ? parentAlpha : 1.0f;
            sRenderAlpha[3] = parentAlpha;
            AsMatrixUtil.convertTo3D(mvpMatrix, sRenderMatrix);
            AsRenderSupport.setBlendFactors(pma, blendMode != null ? blendMode : this.getBlendMode());
            context.setProgram(AsStarling.getCurrent().getProgram(programName));
            context.setProgramConstantsFromVector(AsContext3DProgramType.VERTEX, 0, sRenderAlpha, 1);
            context.setProgramConstantsFromMatrix(AsContext3DProgramType.VERTEX, 1, sRenderMatrix, true);
            context.setVertexBufferAt(0, mVertexBuffer, AsVertexData.POSITION_OFFSET, AsContext3DVertexBufferFormat.FLOAT_2);
            if (mTexture == null || tinted)
            {
                context.setVertexBufferAt(1, mVertexBuffer, AsVertexData.COLOR_OFFSET, AsContext3DVertexBufferFormat.FLOAT_4);
            }
            if (mTexture != null)
            {
                context.setTextureAt(0, mTexture.get_base());
                context.setVertexBufferAt(2, mVertexBuffer, AsVertexData.TEXCOORD_OFFSET, AsContext3DVertexBufferFormat.FLOAT_2);
            }
            context.drawTriangles(mIndexBuffer, 0, mNumQuads * 2);
            if (mTexture != null)
            {
                context.setTextureAt(0, null);
                context.setVertexBufferAt(2, null);
            }
            context.setVertexBufferAt(1, null);
            context.setVertexBufferAt(0, null);
        }
        public virtual void setTransformationMatrix(AsMatrix matrix)
        {
            mOrientationChanged = false;
            mTransformationMatrix.copyFrom(matrix);
            mX = matrix.tx;
            mY = matrix.ty;
            float a = matrix.a;
            float b = matrix.b;
            float c = matrix.c;
            float d = matrix.d;

            mScaleX = AsMath.sqrt(a * a + b * b);
            if (mScaleX != 0)
            {
                mRotation = AsMath.atan2(b, a);
            }
            else
            {
                mRotation = 0;
            }
            float cosTheta = AsMath.cos(mRotation);
            float sinTheta = AsMath.sin(mRotation);

            mScaleY = d * cosTheta - c * sinTheta;
            if (mScaleY != 0)
            {
                mSkewX = AsMath.atan2(d * sinTheta + c * cosTheta, mScaleY);
            }
            else
            {
                mSkewX = 0;
            }
            mSkewY  = 0;
            mPivotX = 0;
            mPivotY = 0;
        }
 public virtual AsRectangle getBounds(AsMatrix transformationMatrix)
 {
     return(getBounds(transformationMatrix, 0, -1, null));
 }
 public virtual AsRectangle getBounds(AsMatrix transformationMatrix, int vertexID)
 {
     return(getBounds(transformationMatrix, vertexID, -1, null));
 }
 public virtual void transformVertex(int vertexID, AsMatrix matrix)
 {
     transformVertex(vertexID, matrix, 1);
 }
        public virtual void addQuad(AsQuad quad, float parentAlpha, AsTexture texture, String smoothing, AsMatrix modelViewMatrix, String blendMode)
        {
            if (modelViewMatrix == null)
            {
                modelViewMatrix = quad.getTransformationMatrix();
            }
            bool tinted = texture != null?quad.getTinted() || parentAlpha != 1.0f : false;

            float alpha    = parentAlpha * quad.getAlpha();
            int   vertexID = mNumQuads * 4;

            if (mNumQuads + 1 > mVertexData.getNumVertices() / 4)
            {
                expand();
            }
            if (mNumQuads == 0)
            {
                this.setBlendMode(blendMode != null ? blendMode : quad.getBlendMode());
                mTexture   = texture;
                mTinted    = tinted;
                mSmoothing = smoothing;
                mVertexData.setPremultipliedAlpha(texture != null ? texture.getPremultipliedAlpha() : true, false);
            }
            quad.copyVertexDataTo(mVertexData, vertexID);
            mVertexData.transformVertex(vertexID, modelViewMatrix, 4);
            if (alpha != 1.0f)
            {
                mVertexData.scaleAlpha(vertexID, alpha, 4);
            }
            mSyncRequired = true;
            mNumQuads++;
        }
 public virtual void transform(AsMatrix matrix)
 {
     mCurrentMatrix.concat(matrix);
 }
 private static int compileObject(AsDisplayObject _object, AsVector <AsQuadBatch> quadBatches, int quadBatchID, AsMatrix transformationMatrix, float alpha, String blendMode)
 {
     return(compileObject(_object, quadBatches, quadBatchID, transformationMatrix, alpha, blendMode, false));
 }
        private static int compileObject(AsDisplayObject _object, AsVector <AsQuadBatch> quadBatches, int quadBatchID, AsMatrix transformationMatrix, float alpha, String blendMode, bool ignoreCurrentFilter)
        {
            int         i                      = 0;
            AsQuadBatch quadBatch              = null;
            bool        isRootObject           = false;
            float       objectAlpha            = _object.getAlpha();
            AsDisplayObjectContainer container = _object as AsDisplayObjectContainer;
            AsQuad           quad              = _object as AsQuad;
            AsQuadBatch      batch             = _object as AsQuadBatch;
            AsFragmentFilter filter            = _object.getFilter();

            if (quadBatchID == -1)
            {
                isRootObject = true;
                quadBatchID  = 0;
                objectAlpha  = 1.0f;
                blendMode    = _object.getBlendMode();
                if (quadBatches.getLength() == 0)
                {
                    quadBatches.push(new AsQuadBatch());
                }
                else
                {
                    quadBatches[0].reset();
                }
            }
            if (filter != null && !ignoreCurrentFilter)
            {
                if (filter.getMode() == AsFragmentFilterMode.ABOVE)
                {
                    quadBatchID = compileObject(_object, quadBatches, quadBatchID, transformationMatrix, alpha, blendMode, true);
                }
                quadBatchID = compileObject(filter.compile(_object), quadBatches, quadBatchID, transformationMatrix, alpha, blendMode);
                if (filter.getMode() == AsFragmentFilterMode.BELOW)
                {
                    quadBatchID = compileObject(_object, quadBatches, quadBatchID, transformationMatrix, alpha, blendMode, true);
                }
            }
            else
            {
                if (container != null)
                {
                    int      numChildren = container.getNumChildren();
                    AsMatrix childMatrix = new AsMatrix();
                    for (i = 0; i < numChildren; ++i)
                    {
                        AsDisplayObject child        = container.getChildAt(i);
                        bool            childVisible = child.getAlpha() != 0.0f && child.getVisible() && child.getScaleX() != 0.0f && child.getScaleY() != 0.0f;
                        if (childVisible)
                        {
                            String childBlendMode = child.getBlendMode() == AsBlendMode.AUTO ? blendMode : child.getBlendMode();
                            childMatrix.copyFrom(transformationMatrix);
                            AsRenderSupport.transformMatrixForObject(childMatrix, child);
                            quadBatchID = compileObject(child, quadBatches, quadBatchID, childMatrix, alpha * objectAlpha, childBlendMode);
                        }
                    }
                }
                else
                {
                    if (quad != null || batch != null)
                    {
                        AsTexture texture   = null;
                        String    smoothing = null;
                        bool      tinted    = false;
                        int       numQuads  = 0;
                        if (quad != null)
                        {
                            AsImage image = quad as AsImage;
                            texture = image != null?image.getTexture() : null;

                            smoothing = image != null?image.getSmoothing() : null;

                            tinted   = quad.getTinted();
                            numQuads = 1;
                        }
                        else
                        {
                            texture   = batch.mTexture;
                            smoothing = batch.mSmoothing;
                            tinted    = batch.mTinted;
                            numQuads  = batch.mNumQuads;
                        }
                        quadBatch = quadBatches[quadBatchID];
                        if (quadBatch.isStateChange(tinted, alpha * objectAlpha, texture, smoothing, blendMode, numQuads))
                        {
                            quadBatchID++;
                            if (quadBatches.getLength() <= quadBatchID)
                            {
                                quadBatches.push(new AsQuadBatch());
                            }
                            quadBatch = quadBatches[quadBatchID];
                            quadBatch.reset();
                        }
                        if (quad != null)
                        {
                            quadBatch.addQuad(quad, alpha, texture, smoothing, transformationMatrix, blendMode);
                        }
                        else
                        {
                            quadBatch.addQuadBatch(batch, alpha, transformationMatrix, blendMode);
                        }
                    }
                    else
                    {
                        throw new AsError("Unsupported display object: " + AsGlobal.getQualifiedClassName(_object));
                    }
                }
            }
            if (isRootObject)
            {
                for (i = (int)(quadBatches.getLength() - 1); i > quadBatchID; --i)
                {
                    quadBatches.pop().dispose();
                }
            }
            return(quadBatchID);
        }
 public virtual void renderCustom(AsMatrix mvpMatrix)
 {
     renderCustom(mvpMatrix, 1.0f, null);
 }
 public virtual void addImage(AsImage image, float parentAlpha, AsMatrix modelViewMatrix, String blendMode)
 {
     addQuad(image, parentAlpha, image.getTexture(), image.getSmoothing(), modelViewMatrix, blendMode);
 }
 public virtual void addQuad(AsQuad quad, float parentAlpha, AsTexture texture, String smoothing, AsMatrix modelViewMatrix)
 {
     addQuad(quad, parentAlpha, texture, smoothing, modelViewMatrix, null);
 }
        public virtual AsMatrix getTransformationMatrix(AsDisplayObject targetSpace, AsMatrix resultMatrix)
        {
            if (resultMatrix != null)
            {
                resultMatrix.identity();
            }
            else
            {
                resultMatrix = new AsMatrix();
            }
            AsDisplayObject currentObject = this;

            if (targetSpace == this)
            {
                return(resultMatrix);
            }
            else
            {
                if (targetSpace == mParent || (targetSpace == null && mParent == null))
                {
                    if (mPivotX != 0.0f || mPivotY != 0.0f)
                    {
                        resultMatrix.translate(-mPivotX, -mPivotY);
                    }
                    if (mScaleX != 1.0f || mScaleY != 1.0f)
                    {
                        resultMatrix.scale(mScaleX, mScaleY);
                    }
                    if (mRotation != 0.0f)
                    {
                        resultMatrix.rotate(AsMathHelper.toRadians(mRotation));
                    }
                    if (mX != 0.0f || mY != 0.0f)
                    {
                        resultMatrix.translate(mX, mY);
                    }
                    return(resultMatrix);
                }
                else
                {
                    if (targetSpace == null)
                    {
                        currentObject = this;
                        while (currentObject != null)
                        {
                            currentObject.getTransformationMatrix(currentObject.mParent, sHelperMatrix);
                            resultMatrix.concat(sHelperMatrix);
                            currentObject = currentObject.getParent();
                        }
                        return(resultMatrix);
                    }
                    else
                    {
                        if (targetSpace.mParent == this)
                        {
                            targetSpace.getTransformationMatrix(this, resultMatrix);
                            resultMatrix.invert();
                            return(resultMatrix);
                        }
                    }
                }
            }
            sAncestors.setLength(0);
            AsDisplayObject commonParent = null;

            while (currentObject != null)
            {
                sAncestors.push(currentObject);
                currentObject = currentObject.getParent();
            }
            currentObject = targetSpace;
            while (currentObject != null && sAncestors.indexOf(currentObject) == -1)
            {
                currentObject = currentObject.getParent();
            }
            if (currentObject == null)
            {
                throw new AsArgumentError("Object not connected to target");
            }
            else
            {
                commonParent = currentObject;
            }
            currentObject = this;
            while (currentObject != commonParent)
            {
                currentObject.getTransformationMatrix(currentObject.mParent, sHelperMatrix);
                resultMatrix.concat(sHelperMatrix);
                currentObject = currentObject.getParent();
            }
            sTargetMatrix.identity();
            currentObject = targetSpace;
            while (currentObject != commonParent)
            {
                currentObject.getTransformationMatrix(currentObject.mParent, sHelperMatrix);
                sTargetMatrix.concat(sHelperMatrix);
                currentObject = currentObject.getParent();
            }
            sTargetMatrix.invert();
            resultMatrix.concat(sTargetMatrix);
            return(resultMatrix);
        }
 public AsRenderSupport()
 {
     mMatrixStack     = new AsVector <AsMatrix>();
     mMatrixStackSize = 0;
     mCurrentMatrix   = new AsMatrix();
 }
 private static int compileObject(AsDisplayObject _object, AsVector <AsQuadBatch> quadBatches, int quadBatchID, AsMatrix transformationMatrix)
 {
     return(compileObject(_object, quadBatches, quadBatchID, transformationMatrix, 1.0f, null, false));
 }
 public virtual void addQuadBatch(AsQuadBatch quadBatch, float parentAlpha, AsMatrix modelViewMatrix)
 {
     addQuadBatch(quadBatch, parentAlpha, modelViewMatrix, null);
 }
 public virtual void addImage(AsImage image, float parentAlpha, AsMatrix modelViewMatrix)
 {
     addImage(image, parentAlpha, modelViewMatrix, null);
 }