Addons 4.0 Update - Direct3D API Access!

Find news and updates about the Ashita project posted here.
Post Reply
User avatar
atom0s
Site Admin
Posts: 450
Joined: Sat May 14, 2016 5:13 pm

Addons 4.0 Update - Direct3D API Access!

Post by atom0s » Sun Jun 03, 2018 9:58 pm

Ashita v3 - Addons Update - Direct3D API Support!

I've pushed an update to the Addons plugin for Ashita v3 which enables addons to directly access the Direct3D device(s). This allows addons to directly interact with the graphics device giving them much more power to render custom things. (ie. the Minimap plugin could be done fully in an addon now.)

Here is a quick example of what this looks like in use:
Image

And the addon to do it:
  1. _addon.author = 'atom0s';
  2. _addon.version = '1.0';
  3. _addon.name = 'test';
  4.  
  5. require 'common';
  6. require 'd3d8';
  7.  
  8. local rot_x = 0;
  9. local rot_y = 0;
  10.  
  11. local MOOGLEVERTEX      = 0x102;    -- (D3DFVF_XYZ | D3DFVF_TEX1)
  12. local MOOGLEVERTEX_SIZE = 0x24;     -- (sizeof(vertices) / sizeof(t_MoogleVertex)) = (720 / 20)
  13. local MOOGLEVERTEX_TOTAL= 0x2D0;    -- MOOGLEVERTEX_SIZE * sizeof(t_MoogleVertex)) = (36 * 20)
  14. local vertices =
  15. {
  16.     { -1.0, -1.0, -1.0, 0.0, 1.0 },    -- Front face
  17.     { -1.0,  1.0, -1.0, 0.0, 0.0 },
  18.     { 1.0,  1.0, -1.0, 1.0, 0.0 },
  19.     { 1.0,  1.0, -1.0, 1.0, 0.0 },
  20.     { 1.0, -1.0, -1.0, 1.0, 1.0 },
  21.     { -1.0, -1.0, -1.0, 0.0, 1.0 },
  22.     { 1.0, -1.0,  1.0, 0.0, 1.0 },     -- Back face
  23.     { 1.0,  1.0,  1.0, 0.0, 0.0 },
  24.     { -1.0,  1.0,  1.0, 1.0, 0.0 },
  25.     { -1.0,  1.0,  1.0, 1.0, 0.0 },
  26.     { -1.0, -1.0,  1.0, 1.0, 1.0 },
  27.     { 1.0, -1.0,  1.0, 0.0, 1.0 },
  28.     { -1.0,  1.0, -1.0, 0.0, 1.0 },    -- Top face
  29.     { -1.0,  1.0,  1.0, 0.0, 0.0 },
  30.     { 1.0,  1.0,  1.0, 1.0, 0.0 },
  31.     { 1.0,  1.0,  1.0, 1.0, 0.0 },
  32.     { 1.0,  1.0, -1.0, 1.0, 1.0 },
  33.     { -1.0,  1.0, -1.0, 0.0, 1.0 },
  34.     { 1.0, -1.0, -1.0, 0.0, 1.0 },     -- Bottom face
  35.     { 1.0, -1.0,  1.0, 0.0, 0.0 },
  36.     { -1.0, -1.0,  1.0, 1.0, 0.0 },
  37.     { -1.0, -1.0,  1.0, 1.0, 0.0 },
  38.     { -1.0, -1.0, -1.0, 1.0, 1.0 },
  39.     { 1.0, -1.0, -1.0, 0.0, 1.0 },
  40.     { -1.0, -1.0,  1.0, 0.0, 1.0 },    -- Left face
  41.     { -1.0,  1.0,  1.0, 0.0, 0.0 },
  42.     { -1.0,  1.0, -1.0, 1.0, 0.0 },
  43.     { -1.0,  1.0, -1.0, 1.0, 0.0 },
  44.     { -1.0, -1.0, -1.0, 1.0, 1.0 },
  45.     { -1.0, -1.0,  1.0, 0.0, 1.0 },
  46.     { 1.0, -1.0, -1.0, 0.0, 1.0 },     -- Right face
  47.     { 1.0,  1.0, -1.0, 0.0, 0.0 },
  48.     { 1.0,  1.0,  1.0, 1.0, 0.0 },
  49.     { 1.0,  1.0,  1.0, 1.0, 0.0 },
  50.     { 1.0, -1.0,  1.0, 1.0, 1.0 },
  51.     { 1.0, -1.0, -1.0, 0.0, 1.0 },
  52. };
  53.  
  54. -- Load the texture from a local file..
  55. local hres, moogleTexture = ashita.d3dx.CreateTextureFromFileA(_addon.path .. '\\moogle.bmp');
  56. if (hres ~= 0) then
  57.     error('Failed to load moogle.bmp into a texture.');
  58. end
  59.  
  60. -- Create the moogle vertex buffer..
  61. local hres, moogleVertexBuffer = ashita.d3d8dev.CreateVertexBuffer(MOOGLEVERTEX_TOTAL, D3DUSAGE_WRITEONLY, MOOGLEVERTEX, D3DPOOL_MANAGED);
  62. if (hres ~= 0) then
  63.     error('Failed to create the moogle vertex buffer.');
  64. end
  65.  
  66. -- Lock the vertex buffer for writing..
  67. local hres, ptr = moogleVertexBuffer:Lock(0, 0, 0);
  68. if (hres ~= 0) then
  69.     error('Failed to lock the moogle vertex buffer.');
  70. end
  71.  
  72. -- Write the vertices to the vertex buffer..
  73. for k, v in pairs(vertices) do
  74.     -- Loop the entries data members..
  75.     for kk, vv in pairs(v) do
  76.         -- Write to the vertex buffer pointer..
  77.         --  This vertex buffer is just float data, so we can just write each value directly as a float
  78.         --  and step the pointer as we go..
  79.         ashita.memory.write_float(ptr, vv);
  80.         ptr = ptr + 4;
  81.     end
  82. end
  83.  
  84. -- Unlock the vertex buffer..
  85. moogleVertexBuffer:Unlock();
  86.  
  87. ----------------------------------------------------------------------------------------------------
  88. -- func: unload
  89. -- desc: Event called when the addon is being unloaded.
  90. ----------------------------------------------------------------------------------------------------
  91. ashita.register_event('unload', function()
  92.     if (moogleTexture ~= nil) then
  93.         moogleTexture:Release();
  94.     end
  95.     if (moogleVertexBuffer ~= nil) then
  96.         moogleVertexBuffer:Release();
  97.     end
  98. end)
  99.  
  100. ----------------------------------------------------------------------------------------------------
  101. -- func: render
  102. -- desc: Event called when the addon is being rendered.
  103. ----------------------------------------------------------------------------------------------------
  104. ashita.register_event('render', function()
  105.     if (moogleTexture == nil or moogleVertexBuffer == nil) then
  106.         return;
  107.     end
  108.  
  109.     -- Step the rotations to make the box spin..
  110.     rot_x = rot_x + 0.01;
  111.     rot_y = rot_y + 0.01;
  112.  
  113.     -- Prepare the render states for our box..
  114.     ashita.d3d8dev.SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
  115.     ashita.d3d8dev.SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  116.     ashita.d3d8dev.SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
  117.     ashita.d3d8dev.SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR);
  118.     ashita.d3d8dev.SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR);
  119.     ashita.d3d8dev.SetRenderState(D3DRS_LIGHTING, 0);
  120.     ashita.d3d8dev.SetRenderState(D3DRS_ZENABLE, 0);
  121.     ashita.d3d8dev.SetVertexShader(MOOGLEVERTEX);
  122.     ashita.d3d8dev.SetStreamSource(0, moogleVertexBuffer:Get(), 0x14); -- 0x14 = sizeof(t_MoogleVertex)
  123.     ashita.d3d8dev.SetTexture(0, moogleTexture:Get());
  124.  
  125.     -- Set the world transform..
  126.     local m = ashita.d3dx.MatrixIdentity(D3DXMATRIX());
  127.     local x = ashita.d3dx.MatrixIdentity(D3DXMATRIX());
  128.     local y = ashita.d3dx.MatrixIdentity(D3DXMATRIX());
  129.     x = ashita.d3dx.MatrixRotationX(x, rot_x);
  130.     y = ashita.d3dx.MatrixRotationY(y, rot_y);
  131.     m = ashita.d3dx.MatrixMultiply(m, x, y);
  132.     ashita.d3d8dev.SetTransform(D3DTS_WORLD, m);
  133.  
  134.     -- Draw the moogle box..
  135.     ashita.d3d8dev.DrawPrimitive(D3DPT_TRIANGLELIST, 0, 0x0C); -- 0x0C = MOOGLEVERTEX_SIZE / 3
  136. end);
IDirect3D8 can be accessed via: ashita.d3d8.
IDirect3DDevice8 can be accessed via: ashita.d3d8dev.
d3dx8 extensions can be accessed via: ashita.d3dx.

More info / docs coming soon for this as we are working on moving some of our site things around and such.

A new Lua lib was added for this as well named 'd3d8.lua'. It holds all of the D3D8 macro definitions from the .h files of the d3d8 sdk for C++. You can use either the direct values or the macro names in Lua as they are seen in the d3d8.lua file.
Lead Ashita Developer

Want to donate to say thanks?
https://www.paypal.me/atom0s
Post Reply