Skip to content

Instantly share code, notes, and snippets.

@PrimaryFeather
Created June 29, 2017 13:50
Show Gist options
  • Save PrimaryFeather/e3fa56ef341e0f246f6bb42831b3ef38 to your computer and use it in GitHub Desktop.
Save PrimaryFeather/e3fa56ef341e0f246f6bb42831b3ef38 to your computer and use it in GitHub Desktop.
This Stage3D sample demonstrates that the camera is still being accessed after disposing a video texture.
package stage3d
{
import com.adobe.utils.AGALMiniAssembler;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display3D.Context3D;
import flash.display3D.Context3DProfile;
import flash.display3D.Context3DProgramType;
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.display3D.textures.TextureBase;
import flash.display3D.textures.VideoTexture;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.geom.Matrix3D;
import flash.geom.Vector3D;
import flash.media.Camera;
import flash.ui.Keyboard;
import flash.utils.getTimer;
public class VideoTextureBug extends Sprite
{
private var _texture:TextureBase;
private var _context3D:Context3D;
private var _program:Program3D;
private var _vertexBuffer:VertexBuffer3D;
private var _indexBuffer:IndexBuffer3D;
public function VideoTextureBug()
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event=null):void
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.stage3Ds[0].addEventListener(Event.CONTEXT3D_CREATE, initMolehill);
stage.stage3Ds[0].requestContext3D("auto", Context3DProfile.BASELINE);
addEventListener(Event.ENTER_FRAME, onRender);
}
private function initMolehill(e:Event):void
{
_context3D = stage.stage3Ds[0].context3D;
_context3D.configureBackBuffer(stage.stageWidth, stage.stageHeight, 1, true);
var vertices:Vector.<Number> = Vector.<Number>([
-0.3,-0.3, 0, 0, 0, // x, y, z, u, v
-0.3, 0.3, 0, 1, 0,
0.3, 0.3, 0, 0, 1]);
// Create VertexBuffer3D. 3 vertices, of 5 Numbers each
_vertexBuffer = _context3D.createVertexBuffer(3, 5);
// Upload VertexBuffer3D to GPU. Offset 0, 3 vertices
_vertexBuffer.uploadFromVector(vertices, 0, 3);
var indices:Vector.<uint> = Vector.<uint>([0, 1, 2]);
// Create IndexBuffer3D. Total of 3 indices. 1 triangle of 3 vertices
_indexBuffer = _context3D.createIndexBuffer(3);
// Upload IndexBuffer3D to GPU. Offset 0, count 3
_indexBuffer.uploadFromVector (indices, 0, 3);
_texture = createVideoTexture();
var vertexShaderAssembler : AGALMiniAssembler = new AGALMiniAssembler();
vertexShaderAssembler.assemble( Context3DProgramType.VERTEX,
"m44 op, va0, vc0\n" + // pos to clipspace
"mov v0, va1" // copy UV
);
var fragmentShaderAssembler : AGALMiniAssembler= new AGALMiniAssembler();
fragmentShaderAssembler.assemble( Context3DProgramType.FRAGMENT,
"tex ft1, v0, fs0 <2d>\n" +
"mov oc, ft1"
);
_program = _context3D.createProgram();
_program.upload( vertexShaderAssembler.agalcode, fragmentShaderAssembler.agalcode);
}
private function createBitmapTexture():TextureBase
{
var bitmapData:BitmapData = new BitmapData(256, 256, false, 0xff00ff);
var texture:Texture = _context3D.createTexture(bitmapData.width, bitmapData.height, Context3DTextureFormat.BGRA, false);
texture.uploadFromBitmapData(bitmapData);
return texture;
}
private function createVideoTexture():TextureBase
{
var camera:Camera = Camera.getCamera();
var texture:VideoTexture = _context3D.createVideoTexture();
texture.attachCamera(camera);
return texture;
}
private function onRender(e:Event):void
{
if ( !_context3D )
return;
_context3D.clear ();
// vertex position to attribute register 0
_context3D.setVertexBufferAt (0, _vertexBuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
// UV to attribute register 1
_context3D.setVertexBufferAt(1, _vertexBuffer, 3, Context3DVertexBufferFormat.FLOAT_2);
// assign texture to texture sampler 0
_context3D.setTextureAt(0, _texture);
// assign shader program
_context3D.setProgram(_program);
var m:Matrix3D = new Matrix3D();
m.appendRotation(getTimer()/40, Vector3D.Z_AXIS);
_context3D.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, m, true);
_context3D.drawTriangles(_indexBuffer);
_context3D.present();
}
private function onKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.X)
{
// (_texture as VideoTexture).attachCamera(null); // <- must be called
_texture.dispose();
_texture = createBitmapTexture();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment