StarlingGraphics / Starling-Extension-Graphics

flash.display.Graphics style extension for the Starling Flash GPU rendering framework
https://github.com/StarlingGraphics/Starling-Extension-Graphics/wiki
MIT License
285 stars 88 forks source link

beginTextureFill() from atlas texture behaves wierd #26

Closed hardcoremore closed 10 years ago

hardcoremore commented 11 years ago

When I do this:

var texture:Texture = Assets.atlas.getTexture( "SimpleCrate" );

displayObject.graphics.beginTextureFill( texture ); displayObject.graphics.moveTo( 0, 0 );

displayObject.graphics.lineTo( 150, 0 ); displayObject.graphics.lineTo( 150, 150 ); displayObject.graphics.lineTo( 0, 150 ); displayObject.graphics.endFill();

And that draws fine except that it draws entire game atlas png scaled into 150x150 pixel box instead of just normal crate image texture.

Thanks, Caslav

ekeeper commented 11 years ago

A little solution:

        var sh:Shape = new Shape();
        var m:Matrix = new Matrix();

        var texS:SubTexture = tex as SubTexture;
        var clip:Rectangle = texS.clipping;
        var widthScale:Number = texS.parent.width/texS.root.width;
        var heightScale:Number = texS.parent.height/tex.root.height;

        m.scale(clip.width * widthScale, clip.height * heightScale);
        m.translate(clip.x * widthScale, clip.y * heightScale);

        sh.graphics.beginTextureFill( tex, m );
        sh.graphics.lineStyle( 1, 0xffffff );
        sh.graphics.drawRect(0,0,tex.width,tex.height);
        sh.graphics.drawRect(0,0,tex.width * 1.5,tex.height * 1.5);
        sh.x = 200;
        sh.y = 50;
        addChild( sh );

But we have another issue here if drawing will have width/height larger than width/height of a texture - shape will filled of another part of the atlas :(

HoecusPocus commented 11 years ago

Hi there, any luck with getting this to work so it repeats the texture from an atlas? Like if you were to draw ground from part of a texture atlas...?

Do you think it's possible to fill a RenderTexture like this from a source subTexture?

jonathanrpace commented 11 years ago

I don't think it's possible to repeat a sub-texture, no. As far as the GPU is concerned, it's still dealing with a single texture (the full atlas), and there's no way for a shader to 'repeat just this bit'.

If you want repeating textures you'll either have to stop using an atlas, or break apart your geometry into multiple quads - with each quad's uv's setup to only reference a particular part of the atlas (which you could achieve with the 'matrix' property on the beginFill() call).

makc commented 11 years ago

there's no way for a shader to 'repeat just this bit'.

I'd disagree. I don't have ready code at hand but frc agal opcodes might help.

HoecusPocus commented 11 years ago

I'm a complete novice as far as shader stuff goes. There's no way I can afford to extract loads of textures into single items. There would be too many state changes I'm sure... I'd like to keep all my background / level items in one textureAtlas, then draw arbitrary polygons of shapes, using a segment of the textureAtlas for each shape.

I wonder how using this atlas is made possible?

http://www.google.co.uk/imgres?imgurl=http://www.gamerendering.com/wp-content/uploads/textureatlas.jpg&imgrefurl=http://www.gamerendering.com/2009/12/08/texture-atlas/&h=400&w=400&sz=67&tbnid=zjReuNHL54qnzM:&tbnh=90&tbnw=90&zoom=1&usg=__dU8Mdt2PcL0KZiyk3nohnKONQFk=&docid=Ea8N2zguAQkkUM&sa=X&ei=qGebUfn-C-TC0QXooIGwDQ&ved=0CEQQ9QEwAg&dur=508

HoecusPocus commented 11 years ago

Page 10 of this article discusses the repeating of a subtexture, and says that it is possible using ddx and ddy shader methods... http://http.download.nvidia.com/developer/NVTextureSuite/Atlas_Tools/Texture_Atlas_Whitepaper.pdf

However I've no idea how to implement this.

jonathanrpace commented 11 years ago

makc - good point. You could divide the uv by whatever repeat interval you want, then take the fractional part and add a bit of offsetting to get a value that only repeats in a sub-section of the 0-1 uv space. Didn't think of that.

Hoecus - Afraid I've got no plans to implement this feature into the library (at least not in the near future - work is killer ATM), however the material system is open enough to allow for custom shaders to get plugged in (you'd need to start using 'beginMaterialFill' for this to work).