Guest User

Untitled

a guest
May 5th, 2024
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 5.30 KB | None | 0 0
  1. package;
  2.  
  3. import openfl.display.GradientType;
  4. import openfl.display.Shape;
  5. import openfl.geom.Matrix;
  6. import openfl.Vector;
  7. import openfl.display.Sprite;
  8. import openfl.display.BitmapData;
  9. import openfl.display3D.*;
  10. import openfl.display3D.textures.Texture;
  11. import openfl.events.Event;
  12. import openfl.geom.Matrix3D;
  13. import openfl.geom.Vector3D;
  14. import openfl.Lib;
  15. import openfl.utils.AGALMiniAssembler;
  16.  
  17. class Main extends Sprite {
  18.     private var context3D:Context3D;
  19.     private var program:Program3D;
  20.     private var vertexBuffer:VertexBuffer3D;
  21.     private var indexBuffer:IndexBuffer3D;
  22.     private var texture:Texture;
  23.  
  24.     public function new() {
  25.         super();
  26.         if (stage != null)
  27.             setup();
  28.         else
  29.             addEventListener(Event.ADDED_TO_STAGE, setup);
  30.     }
  31.  
  32.     private function setup(e:Event = null):Void {
  33.         removeEventListener(Event.ADDED_TO_STAGE, setup);
  34.         stage.stage3Ds[0].addEventListener(Event.CONTEXT3D_CREATE, initStage3D);
  35.         stage.stage3Ds[0].requestContext3D();
  36.         addEventListener(Event.ENTER_FRAME, onRender);
  37.     }
  38.  
  39.     private function initStage3D(e:Event):Void {
  40.         context3D = stage.stage3Ds[0].context3D;
  41.         context3D.configureBackBuffer(stage.stageWidth, stage.stageHeight, 0, false);
  42.  
  43.         // Create a gradient shape
  44.  
  45.         final bitmapData:BitmapData = new BitmapData(256, 256, false);
  46.         final shape:Shape = new Shape(); // Shape to draw the gradient
  47.         final matrix:Matrix = new Matrix(); // Transform matrix for gradient
  48.         final rotation = (Lib.getTimer()/1000) * Math.PI;
  49.  
  50.         // Create a horizontal gradient
  51.         matrix.createGradientBox(256, 256, rotation, 0, 0);
  52.         shape.graphics.beginGradientFill(GradientType.LINEAR, [0x00FF00, 0xFF0000], [1, 1], [0, 255], matrix);
  53.         shape.graphics.drawRect(0, 0, 256, 256);
  54.         shape.graphics.endFill();
  55.    
  56.         // Draw the shape onto the bitmap
  57.         bitmapData.draw(shape);
  58.    
  59.         // Create texture from bitmap data
  60.         texture = context3D.createTexture(256, 256, Context3DTextureFormat.BGRA, false);
  61.         texture.uploadFromBitmapData(bitmapData);
  62.  
  63.         // Define vertices with texture coordinates
  64.         var vertices:Vector<Float> = new Vector<Float>([
  65.              -0.4, -0.4, 0, -1, -1, // x, y, z, u, v
  66.             - 0.4,  0.4, 0, -1, 1,
  67.               0.4,  0.4, 0, 1, 1
  68.         ]);
  69.  
  70.         vertexBuffer = context3D.createVertexBuffer(3, 5);
  71.         vertexBuffer.uploadFromVector(vertices, 0, 3);
  72.  
  73.         var indices:Vector<UInt> = new Vector<UInt>([0, 1, 2]);
  74.         indexBuffer = context3D.createIndexBuffer(3);
  75.         indexBuffer.uploadFromVector(indices, 0, 3);
  76.  
  77.         /* OLD
  78.         // Vertex and fragment shaders
  79.         var vertexShaderAssembler:AGALMiniAssembler = new AGALMiniAssembler();
  80.         vertexShaderAssembler.assemble(Context3DProgramType.VERTEX,
  81.             "m44 vt0, va0, vc0\n" +     // Transform vertex positions by the model-view-projection matrix and store in temp register vt0
  82.             "mov op, vt0\n" +           // Move transformed position to output position register (op)
  83.             //"mov v0, vt0\n"           // NEW: pass the given [-1, 1] uv for debugging
  84.             "mov v0, va1"               // OLD: pass texture coordinates
  85.         );
  86.  
  87.         var fragmentShaderAssembler:AGALMiniAssembler = new AGALMiniAssembler();
  88.         fragmentShaderAssembler.assemble(Context3DProgramType.FRAGMENT,
  89.             "mul ft0, v0, fc0.yy\n" +                               // Scale from [-1,1] to [-0.5, 0.5]
  90.             "add ft0, ft0, fc0.xx\n" +                              // Shift from [-0.5, 0.5] to [0, 1]
  91.             "tex ft1, ft0.xy, fs0 <2d, linear, clamp, nomip>\n" +   // Sample the texture
  92.             "mov oc, ft1\n"                                         // Output the sampled color
  93.         );  */
  94.  
  95.         // Vertex and fragment shaders
  96.         var vertexShaderAssembler:AGALMiniAssembler = new AGALMiniAssembler();
  97.         vertexShaderAssembler.assemble(Context3DProgramType.VERTEX,
  98.             "m44 vt0, va0, vc0\n" +     // Transform vertex positions by the model-view-projection matrix and store in temp register vt0
  99.             "mov op, vt0\n" +           // Move transformed position to output position register (op)
  100.            
  101.             // Adjust UV coordinates from [-1,1] to [0, 1]
  102.             "add vt1, va1, vc1.xx\n" +   // vt1 = va1 + 1, shifting from [-1,1] to [0,2]
  103. //          "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           
  104.             "mul v0, vt1, vc1.yy\n"      // v0 = vt1 * 0.5, scaling down to [0,1]
  105.         );
  106.        
  107.         var fragmentShaderAssembler:AGALMiniAssembler = new AGALMiniAssembler();
  108.         fragmentShaderAssembler.assemble(Context3DProgramType.FRAGMENT,
  109.             "tex ft1, v0.xy, fs0 <2d, linear, clamp, nomip>\n" +  // Sample the texture with normalized UVs
  110.             "mov oc, ft1\n"                                       // Output the sampled color
  111.         );     
  112.  
  113.         program = context3D.createProgram();
  114.         program.upload(vertexShaderAssembler.agalcode, fragmentShaderAssembler.agalcode);
  115.     }
  116.  
  117.     private function onRender(e:Event):Void {
  118.         if (context3D == null)
  119.             return;
  120.  
  121.         context3D.clear(0,.5,.75);
  122.  
  123.         context3D.setTextureAt(0, texture);
  124.         context3D.setVertexBufferAt(0, vertexBuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
  125.         context3D.setVertexBufferAt(1, vertexBuffer, 3, Context3DVertexBufferFormat.FLOAT_2);
  126.         context3D.setProgram(program);
  127.  
  128.  
  129.         var m:Matrix3D = new Matrix3D();
  130.         m.appendRotation(Lib.getTimer() / 40, Vector3D.Z_AXIS);
  131.         context3D.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, m, true);
  132.         context3D.setProgramConstantsFromVector(Context3DProgramType.VERTEX, 1, new Vector<Float>([1.0, 0.5, 0, 0]), 1);
  133.  
  134.         context3D.drawTriangles(indexBuffer);
  135.         context3D.present();
  136.     }
  137. }
  138.  
Advertisement
Add Comment
Please, Sign In to add comment