Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package;
- import openfl.display.GradientType;
- import openfl.display.Shape;
- import openfl.geom.Matrix;
- import openfl.Vector;
- import openfl.display.Sprite;
- import openfl.display.BitmapData;
- import openfl.display3D.*;
- import openfl.display3D.textures.Texture;
- import openfl.events.Event;
- import openfl.geom.Matrix3D;
- import openfl.geom.Vector3D;
- import openfl.Lib;
- import openfl.utils.AGALMiniAssembler;
- class Main extends Sprite {
- private var context3D:Context3D;
- private var program:Program3D;
- private var vertexBuffer:VertexBuffer3D;
- private var indexBuffer:IndexBuffer3D;
- private var texture:Texture;
- public function new() {
- super();
- if (stage != null)
- setup();
- else
- addEventListener(Event.ADDED_TO_STAGE, setup);
- }
- private function setup(e:Event = null):Void {
- removeEventListener(Event.ADDED_TO_STAGE, setup);
- 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);
- // Create a gradient shape
- final bitmapData:BitmapData = new BitmapData(256, 256, false);
- final shape:Shape = new Shape(); // Shape to draw the gradient
- final matrix:Matrix = new Matrix(); // Transform matrix for gradient
- final rotation = (Lib.getTimer()/1000) * Math.PI;
- // Create a horizontal gradient
- matrix.createGradientBox(256, 256, rotation, 0, 0);
- shape.graphics.beginGradientFill(GradientType.LINEAR, [0x00FF00, 0xFF0000], [1, 1], [0, 255], matrix);
- shape.graphics.drawRect(0, 0, 256, 256);
- shape.graphics.endFill();
- // Draw the shape onto the bitmap
- bitmapData.draw(shape);
- // Create texture from bitmap data
- texture = context3D.createTexture(256, 256, Context3DTextureFormat.BGRA, false);
- texture.uploadFromBitmapData(bitmapData);
- // Define vertices with texture coordinates
- var vertices:Vector<Float> = new Vector<Float>([
- -0.4, -0.4, 0, -1, -1, // x, y, z, u, v
- - 0.4, 0.4, 0, -1, 1,
- 0.4, 0.4, 0, 1, 1
- ]);
- vertexBuffer = context3D.createVertexBuffer(3, 5);
- vertexBuffer.uploadFromVector(vertices, 0, 3);
- var indices:Vector<UInt> = new Vector<UInt>([0, 1, 2]);
- indexBuffer = context3D.createIndexBuffer(3);
- indexBuffer.uploadFromVector(indices, 0, 3);
- /* OLD
- // Vertex and fragment shaders
- var vertexShaderAssembler:AGALMiniAssembler = new AGALMiniAssembler();
- vertexShaderAssembler.assemble(Context3DProgramType.VERTEX,
- "m44 vt0, va0, vc0\n" + // Transform vertex positions by the model-view-projection matrix and store in temp register vt0
- "mov op, vt0\n" + // Move transformed position to output position register (op)
- //"mov v0, vt0\n" // NEW: pass the given [-1, 1] uv for debugging
- "mov v0, va1" // OLD: pass texture coordinates
- );
- var fragmentShaderAssembler:AGALMiniAssembler = new AGALMiniAssembler();
- fragmentShaderAssembler.assemble(Context3DProgramType.FRAGMENT,
- "mul ft0, v0, fc0.yy\n" + // Scale from [-1,1] to [-0.5, 0.5]
- "add ft0, ft0, fc0.xx\n" + // Shift from [-0.5, 0.5] to [0, 1]
- "tex ft1, ft0.xy, fs0 <2d, linear, clamp, nomip>\n" + // Sample the texture
- "mov oc, ft1\n" // Output the sampled color
- ); */
- // Vertex and fragment shaders
- var vertexShaderAssembler:AGALMiniAssembler = new AGALMiniAssembler();
- vertexShaderAssembler.assemble(Context3DProgramType.VERTEX,
- "m44 vt0, va0, vc0\n" + // Transform vertex positions by the model-view-projection matrix and store in temp register vt0
- "mov op, vt0\n" + // Move transformed position to output position register (op)
- // Adjust UV coordinates from [-1,1] to [0, 1]
- "add vt1, va1, vc1.xx\n" + // vt1 = va1 + 1, shifting from [-1,1] to [0,2]
- // "add vt1, va1, vc1.xx\n" + // vt1 = va1 + 1, shifting from [-1,1] to [0,2] //BUG: Logicaly speaking THIS SHOULD WORK SO WHY ISNT IT
- "mul v0, vt1, vc1.yy\n" // v0 = vt1 * 0.5, scaling down to [0,1]
- );
- var fragmentShaderAssembler:AGALMiniAssembler = new AGALMiniAssembler();
- fragmentShaderAssembler.assemble(Context3DProgramType.FRAGMENT,
- "tex ft1, v0.xy, fs0 <2d, linear, clamp, nomip>\n" + // Sample the texture with normalized UVs
- "mov oc, ft1\n" // Output the sampled color
- );
- program = context3D.createProgram();
- program.upload(vertexShaderAssembler.agalcode, fragmentShaderAssembler.agalcode);
- }
- private function onRender(e:Event):Void {
- if (context3D == null)
- return;
- context3D.clear(0,.5,.75);
- context3D.setTextureAt(0, texture);
- context3D.setVertexBufferAt(0, vertexBuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
- context3D.setVertexBufferAt(1, vertexBuffer, 3, Context3DVertexBufferFormat.FLOAT_2);
- context3D.setProgram(program);
- var m:Matrix3D = new Matrix3D();
- m.appendRotation(Lib.getTimer() / 40, Vector3D.Z_AXIS);
- context3D.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, m, true);
- context3D.setProgramConstantsFromVector(Context3DProgramType.VERTEX, 1, new Vector<Float>([1.0, 0.5, 0, 0]), 1);
- context3D.drawTriangles(indexBuffer);
- context3D.present();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment