diff --git a/actors.xml b/actors.xml index 2adbbe7..7cf954c 100644 --- a/actors.xml +++ b/actors.xml @@ -1,6 +1,7 @@ - + + \ No newline at end of file diff --git a/main.xml b/main.xml index 45bd14e..34aeb25 100644 --- a/main.xml +++ b/main.xml @@ -285,6 +285,14 @@ return currentActor ~= nil end + function oat._actor.hasShader() + return oat._actor.cond() and (currentActor.frag ~= nil or currentActor.vert ~= nil) + end + + function oat._actor.noShader() + return oat._actor.cond() and not oat._actor.hasShader() + end + function oat._actor.type() return currentActor.type end @@ -293,6 +301,14 @@ return currentActor.file end + function oat._actor.frag() + return currentActor.frag or 'nop.frag' + end + + function oat._actor.vert() + return currentActor.vert or 'nop.vert' + end + function oat._actor.font() return currentActor.font end @@ -423,6 +439,43 @@ return actor end + local function isShaderCode(str) + return string.find(str or '', '\n') + end + + function Shader(frag, vert) + if actorsInitialized then error('uranium: cannot create an actor during runtime!!', 2) end + local actor = createProxyActor('RageShaderProgram') + + local fragFile = frag + local vertFile = vert + + local isFragShaderCode = isShaderCode(frag) + local isVertShaderCode = isShaderCode(vert) + + if isFragShaderCode then fragFile = nil end + if isVertShaderCode then vertFile = nil end + + if (frag and vert) and ((isFragShaderCode and not isVertShaderCode) or (not isFragShaderCode and isVertShaderCode)) then + error('uranium: cannot create a shader with 1 shader file and 1 shader code block', 2) + end + + table.insert(actorQueue, { + type = 'Sprite', + frag = fragFile and ('../' .. fragFile) or 'nop.frag', + vert = vertFile and ('../' .. vertFile) or 'nop.vert', + init = function(a) + actor.__lock(a:GetShader()) + + -- shader code stuff + if isFragShaderCode or isVertShaderCode then + a:GetShader():compile(vert or '', frag or '') + end + end + }) + return actor + end + function Model(file) if actorsInitialized then error('uranium: cannot create an actor during runtime!!', 2) end if not file then error('uranium: cannot create a Model without a file', 2) end diff --git a/nop.frag b/nop.frag new file mode 100755 index 0000000..f9acfe0 --- /dev/null +++ b/nop.frag @@ -0,0 +1,9 @@ +#version 120 + +uniform sampler2D sampler0; +varying vec2 textureCoord; +varying vec4 color; + +void main (void) { + gl_FragColor = texture2D( sampler0, textureCoord ) * color; +} diff --git a/nop.vert b/nop.vert new file mode 100755 index 0000000..a1083a1 --- /dev/null +++ b/nop.vert @@ -0,0 +1,21 @@ +#version 120 + +attribute vec4 TextureMatrixScale; +varying vec3 position; +varying vec3 normal; +varying vec4 color; +varying vec2 textureCoord; +varying vec2 imageCoord; +uniform vec2 textureSize; +uniform vec2 imageSize; +uniform mat4 textureMatrix; +void main() { + normal = gl_NormalMatrix * gl_Normal * vec3(1.0, -1.0, 1.0); + gl_Position = (gl_ModelViewProjectionMatrix * gl_Vertex); + position = gl_Vertex.xyz; + gl_TexCoord[0] = (textureMatrix * gl_MultiTexCoord0 * TextureMatrixScale) + (gl_MultiTexCoord0 * (vec4(1)-TextureMatrixScale)); + textureCoord = ((textureMatrix * gl_MultiTexCoord0 * TextureMatrixScale) + (gl_MultiTexCoord0 * (vec4(1)-TextureMatrixScale))).xy; + imageCoord = textureCoord * textureSize / imageSize; + gl_FrontColor = gl_Color; + color = gl_Color; +} diff --git a/typings.lua b/typings.lua index 8cfaea5..bbfe5c9 100644 --- a/typings.lua +++ b/typings.lua @@ -41,6 +41,11 @@ function ActorSound(file) end ---@return ActorFrameTexture --- Defines an ActorFrameTexture actor. function ActorFrameTexture() end +---@param frag string | nil +---@param vert string | nil +---@return RageShaderProgram +--- Defines a shader. `frag` and `vert` can either be filenames or shader code. +function Shader(frag, vert) end ---@param actor Actor --- Resets an actor to its initial state