How to render to a Bitmap?

Software: Away3D 4.x

ranoya, Newbie
Posted: 18 August 2012 01:52 AM   Total Posts: 29

Have anyone tried to render to a Bitmap? I first tried to use a simple trick:
.
.
bitmapdata.draw(myView3D);
bitmap:Bitmap = new Bitmap(bitmapdata);
addChild(bitmap);
.
.
but nothing appears.

then i tried this:

myView3D.render();
context = satge.stages3d[0].context3D;
context.drawToBitmapData(bitmapdata);

Then i get a runtime error that says to clear the buffers before drawing to BitmapData. Any idea?

   

Richard Olsson, Administrator
Posted: 18 August 2012 10:22 AM   Total Posts: 1192   [ # 1 ]

The renderToBitmapData() has to happen at a very specific point in the render pipeline. You can’t just do it outside of the render() call.

Use View3D.renderer.queueSnapshot() to queue the rendering of the scene to a bitmap data. It will happen during the next View3D.render(), and will have completed by the time render() returns.

   

ranoya, Newbie
Posted: 18 August 2012 12:17 PM   Total Posts: 29   [ # 2 ]

Thanks, Richard. It works perfectly. I was using away3d 4 beta, and this function didn´t exist for me.

My intention is to implement an object that reflects the image of other objects, like the ReflectivePlane in away3d´s version 3.

Is it possible to create a second view that shares the scene of the first, with another camera, and render from the both cameras at the same time?

   

Richard Olsson, Administrator
Posted: 18 August 2012 12:22 PM   Total Posts: 1192   [ # 3 ]

If you want to achieve real-time reflection you should really not go via a BitmapData. That will require the 3D front buffer to be downloaded from the GPU to the CPU memory, and then again from CPU to GPU for the next render. That is incredibly slow.

You should render the view to a texture, and then use that texture on the object you want to make reflective. The simplest way would probably be to hack View3D a little to render twice inside the render() function, similarly to how it’s done in the StereoView3D available in the stereofeature branch on GitHub.

There is also a reflection feature similar to what you’re requesting in the pipeline which is coming in 4.1.

   

ranoya, Newbie
Posted: 18 August 2012 12:53 PM   Total Posts: 29   [ # 4 ]

Thanks. I will try this way, while 4.1 is not ready.

Then, the texture is always in GPU memory, right?

I think I´ll need to use perspective projection to map the rendered texture to the mirror´s UVData, frame by frame. Do you think it will be a hard work for the CPU? Or this task could be done by a Vertex Shader?

Maybe I will wait for 4.1…

   

Richard Olsson, Administrator
Posted: 20 August 2012 06:59 AM   Total Posts: 1192   [ # 5 ]

To hack it you would just move the camera, render to a texture, move the camera back to it’s actual position and render again, this time using the aforementioned texture for the reflective surface. All projection calculations (except where to move the camera to) will be taken care of by Away3D, and there is no need for any vertex shader work at all.

   

ranoya, Newbie
Posted: 20 August 2012 11:23 AM   Total Posts: 29   [ # 6 ]

Referring to the Camera positions, no problem. I know enough linear algebra to make it works. I think the more difficult part of the problem is that, for each position of the camera relative to the mirror, I need to remake the UV map of the rectangular mesh that represents my mirror, because the four vertices of this recangle will be, at each frame, at different positions in the rendered texture map. Is there some function that could help me with this task?
Also, the just way i found to render to texture, inside away3d, is by calling the executeRender() or draw() methods of the renderer object of the view3d. The only problem is that these functions need an EntityCollector object as a parameter, and this object is a view3d´s private attribute. Must I extend the View3d class and make this attribute public? Or maybe I need to create a new EntityCollector object?
Thanks a lot for your help, Richard.

   

Richard Olsson, Administrator
Posted: 21 August 2012 02:48 PM   Total Posts: 1192   [ # 7 ]

If I were you I would have a look at both the old ReflectivePlane in 3.x, and the “stereofeature” branch of the Away3D 4.x GitHub repository, as I mentioned earlier.

Plane primitives in Away3D already have UV coordinates. The general pattern of what happens in ReflectivePlane should therefor be 100% transferable to your situation. The StereoView3D in the stereofeature branch is built the way that is necessary to render twice within a single call to render(). Note though that it does require some minor changes to View3D. You can see which exactly by looking at the diff between the master branch and stereofeature for the relevant files.

The real-time reflection that is being developed for 4.1 actually uses a cube-map, so can reflect in all directions and with respect for normal maps et c.

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X