Skip to content

Instantly share code, notes, and snippets.

@PrimaryFeather
Created September 20, 2018 06:51
Show Gist options
  • Save PrimaryFeather/a04017ed6b7816c0349426e1354a0cac to your computer and use it in GitHub Desktop.
Save PrimaryFeather/a04017ed6b7816c0349426e1354a0cac to your computer and use it in GitHub Desktop.
A very basic Stage3D example.
package stage3d
{
import com.adobe.utils.AGALMiniAssembler;
import flash.display.Sprite;
import flash.display3D.Context3D;
import flash.display3D.Context3DProgramType;
import flash.display3D.Context3DVertexBufferFormat;
import flash.display3D.IndexBuffer3D;
import flash.display3D.Program3D;
import flash.display3D.VertexBuffer3D;
import flash.events.Event;
import flash.geom.Matrix3D;
import flash.geom.Vector3D;
import flash.utils.getTimer;
public class HelloTriangleColored extends Sprite
{
private var _context3D:Context3D;
private var _program:Program3D;
private var _vertexBuffer:VertexBuffer3D;
private var _indexBuffer:IndexBuffer3D;
public function HelloTriangleColored()
{
if (stage) setup();
else addEventListener(Event.ADDED_TO_STAGE, setup);
}
private function setup(e:Event=null):void
{
stage.stage3Ds[0].addEventListener(Event.CONTEXT3D_CREATE, initStage3D);
stage.stage3Ds[0].requestContext3D();
addEventListener(Event.ENTER_FRAME, onRender);
}
private function initStage3D(e:Event):void
{
_context3D = stage.stage3Ds[0].context3D;
_context3D.configureBackBuffer(stage.stageWidth, stage.stageHeight, 0, false);
var vertices:Vector.<Number> = Vector.<Number>([
-0.4,-0.4, 0, 1, 0, 0, // x, y, z, r, g, b
-0.4, 0.4, 0, 0, 1, 0,
0.4, 0.4, 0, 0, 0, 1]);
// Create VertexBuffer3D. 3 vertices, of 6 Numbers each
_vertexBuffer = _context3D.createVertexBuffer(3, 6);
// 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);
var vertexShaderAssembler:AGALMiniAssembler = new AGALMiniAssembler();
vertexShaderAssembler.assemble(Context3DProgramType.VERTEX,
"m44 op, va0, vc0\n" + // pos to clipspace
"mov v0, va1" // copy color
);
var fragmentShaderAssembler:AGALMiniAssembler= new AGALMiniAssembler();
fragmentShaderAssembler.assemble(Context3DProgramType.FRAGMENT,
"mov oc, v0"
);
_program = _context3D.createProgram();
_program.upload(vertexShaderAssembler.agalcode, fragmentShaderAssembler.agalcode);
}
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);
// color to attribute register 1
_context3D.setVertexBufferAt(1, _vertexBuffer, 3, Context3DVertexBufferFormat.FLOAT_3);
// 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();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment