diff options
| author | jussi | 2025-08-30 16:41:05 +0300 |
|---|---|---|
| committer | jussi | 2025-08-30 16:41:05 +0300 |
| commit | de672a85d2778c47fce0d412cea787405388330c (patch) | |
| tree | fcae06a78397f52bc819c99cdbeaa417c48cf57b /examples/instancing/main.lua | |
| parent | 15deeccc4bcbe5b68f002f8cc91ee4ed8ced68fb (diff) | |
| download | reilua-enhanced-de672a85d2778c47fce0d412cea787405388330c.tar.gz reilua-enhanced-de672a85d2778c47fce0d412cea787405388330c.tar.bz2 reilua-enhanced-de672a85d2778c47fce0d412cea787405388330c.zip | |
DrawMeshInstanced takes transforms as Buffer.
Diffstat (limited to 'examples/instancing/main.lua')
| -rw-r--r-- | examples/instancing/main.lua | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/examples/instancing/main.lua b/examples/instancing/main.lua index 1966479..9e14383 100644 --- a/examples/instancing/main.lua +++ b/examples/instancing/main.lua @@ -8,10 +8,14 @@ Modified by Jussi Viitala (@nullstare) for ReiLua style. ]] +package.path = package.path..";"..RL.GetBasePath().."../resources/lib/?.lua" + +Matrix = require( "matrix" ) + local MAX_INSTANCES = 10000 local cube -local transforms = {} +local transformsBuf local camera local shader @@ -41,6 +45,9 @@ function RL.init() -- Define mesh to be instanced cube = RL.GenMeshCube( { 1, 1, 1 } ) + transformsBuf = RL.LoadBufferFormatted( MAX_INSTANCES * 16, RL.BUFFER_FLOAT, 0 ) + local bufPos = 0 + -- Translate and rotate cubes randomly for i = 1, MAX_INSTANCES do local translation = RL.MatrixTranslate( { math.random( -50, 50 ), math.random( -50, 50 ), math.random( -50, 50 ) } ) @@ -48,7 +55,8 @@ function RL.init() local angle = math.rad( math.random( 0, 10 ) ) local rotation = RL.MatrixRotate( axis, angle ) - table.insert( transforms, RL.MatrixMultiply( rotation, translation ) ) + RL.SetBufferData( transformsBuf, bufPos, Matrix:temp( RL.MatrixMultiply( rotation, translation ) ):arr() ) + bufPos = bufPos + 16 end -- Load lighting shader @@ -101,7 +109,7 @@ function RL.draw() -- Draw meshes instanced using material containing instancing shader (RED + lighting), -- transforms[] for the instances should be provided, they are dynamically -- updated in GPU every frame, so we can animate the different mesh instances - RL.DrawMeshInstanced( cube, matInstances, transforms, MAX_INSTANCES ) + RL.DrawMeshInstanced( cube, matInstances, transformsBuf, MAX_INSTANCES ) -- Draw cube mesh with default material (BLUE) RL.DrawMesh( cube, matDefault, RL.MatrixTranslate( { 10.0, 0.0, 0.0 } ) ) @@ -113,4 +121,5 @@ end function RL.exit() RL.UnloadMaterial( matInstances, true ) RL.UnloadMesh( cube ) + RL.UnloadBuffer( transformsBuf ) end |
