Skip to content

Instantly share code, notes, and snippets.

@lhog
Created October 30, 2021 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lhog/385f8e1174606bac190ecc996ab0b5cf to your computer and use it in GitHub Desktop.
Save lhog/385f8e1174606bac190ecc996ab0b5cf to your computer and use it in GitHub Desktop.
function widget:GetInfo()
return {
name = "Unitshapes Rendering",
version = "v0.2",
desc = "Unitshapes Rendering",
author = "ivand",
date = "2020",
license = "GPL",
layer = 0,
enabled = false,
}
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
//__ENGINEUNIFORMBUFFERDEFS__
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;
void main() {
uint baseIndex = instData.x;
mat4 modelMatrix = mat[baseIndex];
mat4 pieceMatrix = mat4mix(mat4(1.0), mat[baseIndex + pieceIndex], modelMatrix[3][3]); // the +1u is not needed here?
vec4 modelPos = modelMatrix * pieceMatrix * vec4(pos, 1.0);
modelPos.xyz += vec3(0.0, 10.0, 0.0) + mouseWorldPos.xyz + instOffset; //instOffset;
gl_Position = cameraViewProj * modelPos;
vuv = uv.xy;
col = vec3(float(pieceIndex) / 21.0);
uint teamIndex = (instData.y & 0x000000FFu); //leftmost ubyte is teamIndex
myTeamColor = teamColor[teamIndex].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 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(600, {
{id = 6, name = "instData", type = GL.UNSIGNED_INT, size = 4},
{id = 7, name = "instOffset", size = 3},
})
local unitIDs = Spring.GetAllUnits()
local communitdefid = UnitDefNames["armcom"].id
-- local pwunitdefid = UnitDefNames["corcom"].id
--unitID = 12257
--Spring.Echo("unitID = ", unitID)
vertVBO:ModelsVBO()
indxVBO:ModelsVBO()
--instVBO:InstanceDataFromUnitDefIDs(unitIDs, 6) --6 is id {id = 6, name = "elemOffset", type = GL.UNSIGNED_INT, size = 4},
instVBO:InstanceDataFromUnitDefIDs({communitdefid}, 6) --6 is id {id = 6, name = "elemOffset", type = GL.UNSIGNED_INT, size = 4},
instVBO:Upload({
0, 0, -10,
}, 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:AddUnitDefsToSubmission(communitdefid)
--vao:AddUnitsToSubmission(unitIDs)
local engineUniformBufferDefs = LuaShader.GetEngineUniformBufferDefs()
vsSrc = vsSrc:gsub("//__ENGINEUNIFORMBUFFERDEFS__", engineUniformBufferDefs)
fsSrc = fsSrc:gsub("//__ENGINEUNIFORMBUFFERDEFS__", engineUniformBufferDefs)
unitShader = LuaShader({
vertex = vsSrc,
fragment = fsSrc,
uniformInt = {
tex1 = 0,
tex2 = 1,
},
}, "Blabla")
local shaderCompiled = unitShader:Initialize()
--Spring.Echo("Hello")
end
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()
gl.Culling(GL.BACK)
gl.DepthMask(true)
gl.DepthTest(true)
gl.UnitShapeTextures(udefID, true)
unitShader:Activate()
--vao:DrawElements(GL.TRIANGLES, 0, 0, -1)
vao:Submit()
unitShader:Deactivate()
gl.UnitShapeTextures(udefID, false)
gl.PolygonOffset ( false )
end
function widget:DrawScreenEffects()
local vsx, vsy = widgetHandler:GetViewSizes()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment