简体   繁体   English

如何实现纹理的运行时压缩?

[英]how to implement the runtime compression of texture?

I've read the doc here: 我在这里阅读了文档:

http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7d5b.html http://help.adobe.com/zh_CN/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7d5b.html

Flash Player 11.4 and AIR 3.4 support runtime texture compression, which is useful in certain situations, such as when rendering dynami textures from vector art. Flash Player 11.4和AIR 3.4支持运行时纹理压缩,这在某些情况下很有用,例如从矢量艺术渲染动态纹理时。 To use runtime texture compression, perform the following steps: 要使用运行时纹理压缩,请执行以下步骤:

Create the texture object by calling the Context3D.createTexture() method, passing either flash.display3D.Context3DTextureFormat.COMPRESSED orflash.display3D.Context3DTextureFormat.COMPRESSED_ALPHA in the third parameter. 通过调用Context3D.createTexture()方法并在第三个参数中传递flash.display3D.Context3DTextureFormat.COMPRESSED或flash.display3D.Context3DTextureFormat.COMPRESSED_ALPHA来创建纹理对象。 Using the flash.display3D.textures.Texture instance returned by createTexture(), call either flash.display3D.textures.Texture.uploadFromBitmapData() orflash.display3D.textures.Texture.uploadFromByteArray(). 使用createTexture()返回的flash.display3D.textures.Texture实例,调用flash.display3D.textures.Texture.uploadFromBitmapData()或flash.display3D.textures.Texture.uploadFromByteArray()。 These methods upload and compress the texture in one step. 这些方法只需一步即可上传和压缩纹理。

I tried to follow the steps but get an error: 我尝试按照以下步骤操作,但出现错误:

Error: Error #3763: Sampler 0 binds a texture that that does not match the read mode specified in AGAL. 错误:错误#3763:采样器0绑定的纹理与AGAL中指定的读取模式不匹配。 Reading compressed or single/dual channel textures must be explicitly declared. 必须明确声明读取压缩或单/双通道纹理。 at flash.display3D::Context3D/drawTriangles() 在flash.display3D :: Context3D / drawTriangles()

should I put some instructions on agal side also? 我也应该在藻类方面添加一些说明吗?

here is the full code: 这是完整的代码:

NOTE: I didn't use the embedded png for texture after I tried it and failed, just a empty bitmapdata created runtime not gonna work on my macos flash player 11.8 注意:尝试并失败后,我没有使用嵌入式png进行纹理处理,只是创建了一个空的bitmapdata运行时无法在我的macOS Flash Player 11.8上运行

grabbed the AGALMiniAssembler.as from here : https://github.com/PrimaryFeather/Starling-Framework/blob/master/starling/src/com/adobe/utils/AGALMiniAssembler.as 抓起AGALMiniAssembler.as从这里: https://github.com/PrimaryFeather/Starling-Framework/blob/master/starling/src/com/adobe/utils/AGALMiniAssembler.as

package sandbox
{
    import com.adobe.utils.AGALMiniAssembler;
    import com.adobe.utils.PerspectiveMatrix3D;

    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.display.Stage3D;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.display3D.Context3D;
    import flash.display3D.Context3DProfile;
    import flash.display3D.Context3DProgramType;
    import flash.display3D.Context3DRenderMode;
    import flash.display3D.Context3DTextureFormat;
    import flash.display3D.Context3DVertexBufferFormat;
    import flash.display3D.IndexBuffer3D;
    import flash.display3D.Program3D;
    import flash.display3D.VertexBuffer3D;
    import flash.display3D.textures.Texture;
    import flash.events.Event;
    import flash.geom.Matrix3D;

    import core.Scene3D;

    [SWF(width="600",height="800",frameRate="60")]
    public class TestCompressedTexture extends Sprite
    {
        [Embed(source="../../assets/tex_cube.png")]
        private var TexCube:Class;
        private var _swfHeight:int;
        private var _swfWidth:int;
        public var context3D:Context3D;
        public var viewMatrix:Matrix3D = new Matrix3D();
        public var projectionMatrix:PerspectiveMatrix3D = new PerspectiveMatrix3D();

        public var meshIndexData:Vector.<uint> = Vector.<uint>
            ([
                0, 1, 2, 0, 2, 3,   
            ]);

        public var meshVertexData:Vector.<Number> = Vector.<Number>([
            //x,y,z     u,v    nx,ny,nz
            -1, -1,  1, 0, 0,  0, 0, 1,
            1, -1,  1, 1, 0,  0, 0, 1,
            1,  1,  1, 1, 1,  0, 0, 1,
            -1,  1,  1, 0, 1,  0, 0, 1,
        ]);
        private var indexBuffer:IndexBuffer3D;
        private var vertexBuffer:VertexBuffer3D;
        private var program:Program3D;
        private var _modelViewProjection:Matrix3D = new Matrix3D();
        private var modelMatrix:Matrix3D = new Matrix3D();
        private var texture:Texture;
        private var uvBuffer:VertexBuffer3D;

        public function TestCompressedTexture()
        {
            _swfHeight = 600;
            _swfWidth = 800;
            if (stage!=null){
                init();
            }else{
                addEventListener(Event.ADDED_TO_STAGE,init);
            }
            projectionMatrix.identity();
            projectionMatrix.perspectiveFieldOfViewRH(45.0,_swfWidth/_swfHeight,0.001,100.0);
            modelMatrix.identity();
            viewMatrix.identity();
            viewMatrix.prependTranslation(0,0,-5);
            super();
        }

        private function init(e:Event=null):void{
            if (hasEventListener(Event.ADDED_TO_STAGE))
                removeEventListener(Event.ADDED_TO_STAGE,init);
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            stage.stage3Ds[0].addEventListener(Event.CONTEXT3D_CREATE,onContext3DCreate);
            stage.stage3Ds[0].requestContext3D(Context3DRenderMode.AUTO,Context3DProfile.BASELINE_EXTENDED);
        }

        protected function onContext3DCreate(e:Event):void
        {
            removeEventListener(Event.ENTER_FRAME,enterFrame);
            var t:Stage3D = e.target as Stage3D;
            context3D = t.context3D;
            if (context3D == null){
                return;
            }
            context3D.enableErrorChecking = true;

            context3D.configureBackBuffer(_swfWidth,_swfHeight,0,true);
            dispatchEvent(new Event(Scene3D.SCENE3D_CREATED));

            createProgram();
            createTexture();
            createBuffer();
            addEventListener(Event.ENTER_FRAME,enterFrame);
        }

        public function createProgram():void{
            var vsa:AGALMiniAssembler = new AGALMiniAssembler();

            var vs:String = 
                "m44 op, va0, vc0\n" +
                "mov v0, va1\n" //uv
                ;
            var fs:String =
                "tex ft0, v0, fs0 <2d,repeat,nomip>\n"+
                "mov oc ft0 \n"
                ;
            program = vsa.assemble2(context3D,1,vs,fs);
            context3D.setProgram(program);
        }

        public function createBuffer():void{
            indexBuffer = context3D.createIndexBuffer(meshIndexData.length);
            indexBuffer.uploadFromVector(meshIndexData,0,meshIndexData.length);


            vertexBuffer = context3D.createVertexBuffer(meshVertexData.length/8,8);
            vertexBuffer.uploadFromVector(meshVertexData,0,meshVertexData.length /8);


        }

        public function createTexture():void{
//          texture = context3D.createTexture(512, 512, Context3DTextureFormat.BGRA, false);
            texture = context3D.createTexture(512, 512, Context3DTextureFormat.COMPRESSED, false);
//          var texCube:BitmapData = new TexCube().bitmapData;
//          trace(texCube.height,texCube.width);
            var bmd:BitmapData = new BitmapData(512,512);

            texture.uploadFromBitmapData(bmd);
        }

        protected function enterFrame(event:Event):void
        {
            context3D.clear();

            _modelViewProjection.identity();
            _modelViewProjection.append(modelMatrix);
            _modelViewProjection.append(viewMatrix);
            _modelViewProjection.append(projectionMatrix);
            // pass our matrix data to the shader program
            context3D.setProgramConstantsFromMatrix(
                Context3DProgramType.VERTEX,
                0, _modelViewProjection, true );

            context3D.setVertexBufferAt(0, vertexBuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
            context3D.setVertexBufferAt(1, vertexBuffer, 3, Context3DVertexBufferFormat.FLOAT_2);
            context3D.setTextureAt(0,texture);

            context3D.drawTriangles(indexBuffer);

            context3D.present();
        }
    }
}

While months too late for davyzhang, but for others who may land here: the maker of Starling, who has Adobe connections experimentally implemented the code, then learned from Adobe that the recently added run-time compression function is only available for desktop systems. 对于davyzhang来说,虽然还为时已晚,但对于其他可能落入此处的人来说:与Adobe建立联系的Starling的制造商通过实验实现了代码,然后从Adobe获悉,最近添加的运行时压缩功能仅适用于台式机系统。

https://github.com/PrimaryFeather/Starling-Framework/issues/153 https://github.com/PrimaryFeather/Starling-Framework/issues/153

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM