How to draw with position to render texture

25 views
Skip to first unread message

Ice

unread,
May 7, 2018, 9:02:17 AM5/7/18
to StageXL
Hei

I'm trying to use a render texture for my project but everything i draw ends up at position (0, 0).
This is my sample code:

BitmapData data1 = resourceManager.getBitmapData("spaceship1");
BitmapData data2 = resourceManager.getBitmapData("ship3");

Bitmap bitmap1 = new Bitmap(data1);
bitmap1.x = 100;
Bitmap bitmap2 = new Bitmap(data2);
bitmap2.x = 400;

var renderTexture = new RenderTexture(1280, 720, Color.Transparent);
var canvas = renderTexture.canvas;
var matrix = renderTexture.quad.drawMatrix;
var renderContext = new RenderContextCanvas(canvas);
var renderState = new RenderState(renderContext, matrix);

bitmap1.render(renderState);
bitmap2.render(renderState);

var bitmapData = new BitmapData.fromRenderTextureQuad(renderTexture.quad);
Bitmap bitmap3 = new Bitmap(bitmapData);

stage.addChild(bitmap3);


the bitmap1.x = 100; and the bitmap2.x = 400; is ignored and they end up at (0, 0)
I tried alternatives like attaching them to Sprite and then setting position on the sprite instead but with same result.

How do i render something at position (X,Y) on a render texture?

Bernhard Pichler

unread,
May 12, 2018, 4:44:27 AM5/12/18
to StageXL
Hi,

If i understand it correctly your solution should work just fine. You add the Bitmaps to a Sprite  (this way the x/y coordinates of the Bitmaps are relative to the sprite). Next you don't render the Bitmaps but the Sprite instead. 

Btw. there is a different solution too (which uses your approach in the background). The BitmapData class has a method called "draw". Here you can draw any DisplayObject (or BitmapData) to a BitmapData. The method also takes an optional "matrix" parameter to transform the rendered object in any way.

Hope this helps,
Bernhard
Reply all
Reply to author
Forward
0 new messages