Last active
June 8, 2021 20:26
-
-
Save lhog/77f3fb10fed0c4e054b6c67eb24efeed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function widget:GetInfo() | |
return { | |
name = "Test Unitshapes Rendering", | |
version = "v0.2", | |
desc = "Test Unitshapes Rendering", | |
author = "ivand", | |
date = "2020", | |
license = "GPL", | |
layer = 0, | |
enabled = true, | |
} | |
end | |
local luaShaderDir = "LuaUI/Widgets/Include/" | |
local LuaShader = VFS.Include(luaShaderDir.."LuaShader.lua") | |
local vao | |
local unitShader, cmpShader | |
local vsSrc = [[ | |
#version 330 | |
#line 10023 | |
layout (location = 0) in vec3 pos; | |
layout (location = 1) in vec3 normal; | |
layout (location = 2) in vec3 T; | |
layout (location = 3) in vec3 B; | |
layout (location = 4) in vec4 uv; | |
layout (location = 5) in uint pieceIndex; | |
layout (location = 6) in uvec4 instData; | |
layout (location = 7) in vec3 instOffset; | |
#extension GL_ARB_uniform_buffer_object : require | |
#extension GL_ARB_shader_storage_buffer_object : require | |
#extension GL_ARB_shading_language_420pack: require | |
layout(std140, binding = 0) uniform UniformMatrixBuffer { | |
mat4 screenView; | |
mat4 screenProj; | |
mat4 screenViewProj; | |
mat4 cameraView; | |
mat4 cameraProj; | |
mat4 cameraViewProj; | |
mat4 cameraBillboard; | |
mat4 cameraViewInv; | |
mat4 cameraProjInv; | |
mat4 cameraViewProjInv; | |
mat4 shadowView; | |
mat4 shadowProj; | |
mat4 shadowViewProj; | |
//TODO: minimap matrices | |
}; | |
layout(std140, binding = 1) uniform UniformParamsBuffer { | |
vec3 rndVec3; //new every draw frame. | |
uint renderCaps; //various render booleans | |
vec4 timeInfo; //gameFrame, gameSeconds, drawFrame, frameTimeOffset | |
vec4 viewGeometry; //vsx, vsy, vpx, vpy | |
vec4 mapSize; //xz, xzPO2 | |
vec4 mapHeight; //height minCur, maxCur, minInit, maxInit | |
vec4 fogColor; //fog color | |
vec4 fogParams; //fog {start, end, 0.0, scale} | |
vec4 pad[7]; | |
vec4 mouseScreenPos; | |
vec4 mouseWorldPos; | |
vec4 teamColor[255]; | |
}; | |
/* | |
layout(std140, binding = 2) uniform FixedStateMatrices { | |
mat4 modelViewMat; | |
mat4 projectionMat; | |
mat4 textureMat; | |
mat4 modelViewProjectionMat; | |
}; | |
*/ | |
layout(std140, binding=0) readonly buffer MatrixBuffer { | |
mat4 mat[]; | |
}; | |
#define NORM2SNORM(value) (value * 2.0 - 1.0) | |
#define SNORM2NORM(value) (value * 0.5 + 0.5) | |
out vec2 vuv; | |
out vec3 col; | |
out vec3 myTeamColor; | |
mat4 mat4mix(mat4 a, mat4 b, float alpha) { | |
return (a * (1.0 - alpha) + b * alpha); | |
} | |
void main() { | |
uint baseIndex = instData.x; | |
mat4 modelMatrix = mat[baseIndex]; | |
//modelMatrix[3].xyz = mix(modelMatrix[3].xyz, mouseWorldPos.xyz, mouseWorldPos.w); | |
mat4 pieceMatrix = mat4mix(mat4(1.0), mat[baseIndex + pieceIndex + 1u], modelMatrix[3][3]); | |
//vec4 modelPos = modelMatrix * modelViewMat * pieceMatrix * vec4(pos, 1.0); | |
vec4 modelPos = modelMatrix * pieceMatrix * vec4(pos, 1.0); | |
modelPos.xyz += instOffset; | |
gl_Position = cameraViewProj * modelPos; | |
vuv = uv.xy; | |
col = vec3(float(pieceIndex) / 21.0); | |
myTeamColor = teamColor[instData.y].rgb; | |
} | |
]] | |
local fsSrc = [[ | |
#version 330 | |
#line 20064 | |
uniform sampler2D tex1; | |
uniform sampler2D tex2; | |
#extension GL_ARB_uniform_buffer_object : require | |
#extension GL_ARB_shading_language_420pack: require | |
in vec2 vuv; | |
in vec3 col; | |
in vec3 myTeamColor; | |
out vec4 fragColor; | |
void main() { | |
vec4 modelColor = texture(tex1, vuv.xy); | |
modelColor.rgb = mix(modelColor.rgb, myTeamColor.rgb, modelColor.a); | |
fragColor = vec4(modelColor.rgb, 1.0); | |
//fragColor = vec4(col.rgb, 1.0); | |
} | |
]] | |
local cmpSrc = [[ | |
#version 430 | |
#line 30129 | |
layout(local_size_x = 32, local_size_y = 32, local_size_z = 1) in; | |
void main() { | |
} | |
]] | |
local atlasID | |
local udefID = UnitDefNames["armcom"].id | |
local instVBO | |
function widget:Initialize() | |
local vertVBO = gl.GetVBO(GL.ARRAY_BUFFER, false) -- GL.ARRAY_BUFFER, false | |
local indxVBO = gl.GetVBO(GL.ELEMENT_ARRAY_BUFFER, false) -- GL.ARRAY_BUFFER, false | |
instVBO = gl.GetVBO(GL.ARRAY_BUFFER, false) | |
instVBO:Define(4, { | |
{id = 6, name = "instData", type = GL.UNSIGNED_INT, size = 4}, | |
{id = 7, name = "instOffset", size = 3}, | |
}) | |
local unitIDs = Spring.GetAllUnits() | |
--unitID = 12257 | |
--Spring.Echo("unitID = ", unitID) | |
vertVBO:ModelsVBO() | |
indxVBO:ModelsVBO() | |
instVBO:InstanceDataFromUnitIDs(unitIDs, 6) --6 is id {id = 6, name = "elemOffset", type = GL.UNSIGNED_INT, size = 4}, | |
instVBO:Upload({ | |
100, 0, 0, | |
-100, 0, 0, | |
0, 0, 100, | |
0, 0, -100, | |
}, 7) | |
Spring.Utilities.TableEcho(instVBO:Download(), "instVBO") | |
vertVBO:DumpDefinition() | |
local elemCount = vertVBO:GetBufferSize() | |
Spring.Echo(elemCount) | |
vao = gl.GetVAO() | |
vao:AttachVertexBuffer(vertVBO) | |
vao:AttachIndexBuffer(indxVBO) | |
vao:AttachInstanceBuffer(instVBO) | |
vao:AddUnitsToSubmission(unitIDs) | |
unitShader = LuaShader({ | |
vertex = vsSrc, | |
fragment = fsSrc, | |
uniformInt = { | |
tex1 = 0, | |
tex2 = 1, | |
}, | |
}, "Blabla") | |
local shaderCompiled = unitShader:Initialize() | |
cmpShader = LuaShader({ | |
compute = cmpSrc, | |
}, "Blabla2") | |
shaderCompiled = cmpShader:Initialize() | |
Spring.Echo("cmpShader ", shaderCompiled) | |
Spring.Echo("Hello") | |
end | |
--[[ | |
local vbo = gl.GetVBO(GL.ARRAY_BUFFER) | |
--local res = vbo:Upload({}, 0, 0) | |
local res = vbo:Define(100, | |
{ | |
{id = 0, name = "pos", size = 2}, --only update {x,z} once | |
{id = 1, name = "normals", size = 3, normalized = true}, --only update {x,z} once | |
{id = 2, name = "color", size = 4, type = GL.UNSIGNED_BYTE}, --only update {x,z} once | |
}) | |
vbo:DumpDefinition() | |
local res2 = vbo:Upload({1, 1, 0.33, 0.33, 0.33, 255, 255, 255, 255, 2, 2, 0.33, 0.33, 0.33, 128, 128, 128, 128}) | |
local res2 = vbo:Upload({0.66, 0.66, 0.66, 0.99, 0.99, 0.99}, 0, 1) | |
local resDL = vbo:Download(0, 2, 1) | |
Spring.Echo("VBO=", vbo, res, res2, res3) | |
Spring.Utilities.TableEcho(resDL, "resDL") | |
vbo:Delete() | |
]]-- | |
function widget:Shutdown() | |
if vao then | |
vao:Delete() | |
end | |
if unitShader then | |
unitShader:Finalize() | |
end | |
if cmpShader then | |
cmpShader:Finalize() | |
end | |
if atlasID then | |
Spring.Echo(gl.DeleteTextureAtlas(atlasID)) | |
end | |
end | |
function widget:DrawWorld() | |
--[[ | |
instVBO:Upload({ | |
100, 0, 0, | |
-100, 0, 0, | |
0, 0, 100, | |
0, 0, -100, | |
}, 7) | |
--]] | |
--gl.Blending(false) | |
gl.DepthMask(true) | |
gl.DepthTest(true) | |
gl.UnitShapeTextures(udefID, true) | |
gl.MatrixMode(GL.MODELVIEW) | |
gl.PushMatrix() | |
gl.LoadIdentity() | |
gl.Rotate(Spring.GetGameFrame(), 0, 1, 0) | |
--Spring.Echo(gl.GetMatrixData(GL.MODELVIEW)) | |
unitShader:Activate() | |
--vao:DrawElements(GL.TRIANGLES, 0, 0, 4, 0) | |
vao:Submit() | |
unitShader:Deactivate() | |
gl.UnitShapeTextures(udefID, false) | |
gl.PopMatrix() | |
cmpShader:Activate() | |
gl.DispatchCompute(1, 1, 1) | |
cmpShader:Deactivate() | |
end | |
function widget:DrawScreenEffects() | |
local vsx, vsy = widgetHandler:GetViewSizes() | |
--gl.Texture(0, atlasID) | |
--gl.TexRect(0, 0, vsx, vsy, false, true) | |
--gl.Texture(0, false) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment