Compare commits

...

3 Commits

Author SHA1 Message Date
Jay 75d3631741 Shaders and debug command 2020-05-23 13:50:17 +01:00
Jay 689966c3b8 Merge branch 'master' of https://github.com/teverse/teverse 2020-05-23 10:39:09 +01:00
Jay b5ee4a3b24 Shaders for TevX 2020-05-23 10:39:04 +01:00
16 changed files with 221 additions and 62 deletions

39
core/3d/camera.lua Normal file
View File

@ -0,0 +1,39 @@
local keyMap = {
[tonumber(enums.keys.KEY_W)] = vector3(0, 0, 1),
[tonumber(enums.keys.KEY_S)] = vector3(0, 0, -1),
[tonumber(enums.keys.KEY_A)] = vector3(-1, 0, 0),
[tonumber(enums.keys.KEY_D)] = vector3(1, 0, 0),
[tonumber(enums.keys.KEY_Q)] = vector3(0, -1, 0),
[tonumber(enums.keys.KEY_E)] = vector3(0, 1, 0)
}
local moveStep = 0.3
local rotateStep = 0.01
local cam = teverse.scene.camera
local db = false
teverse.input:on("keyDown", function(key)
if db then return end
db = true
local mapped = keyMap[tonumber(key)]
if mapped then
while sleep() and teverse.input:isKeyDown(key) do
cam.position = cam.position + (cam.rotation * mapped * moveStep)
end
end
db = false
end)
teverse.input:on("mouseMoved", function( movement )
if teverse.input:isMouseButtonDown(3) then
local pitch = quaternion.euler(movement.y * rotateStep, 0, 0)
local yaw = quaternion.euler(0, movement.x * rotateStep, 0)
-- Applied seperately to avoid camera flipping on the wrong axis.
cam.rotation = yaw * cam.rotation;
cam.rotation = cam.rotation * pitch
end
end)

View File

@ -66,15 +66,10 @@ teverse.debug:on("print", function(msg)
end)
end)
if _TEV_VERSION_PATCH and _TEV_VERSION_PATCH >= 9 then
for _,v in pairs(teverse.debug:getOutputHistory()) do
addLog(v.message, v.time)
end
logContainer.canvasOffset = vector2(0, lastPos - 200)
else
print("History not supported")
for _,v in pairs(teverse.debug:getOutputHistory()) do
addLog(v.message, v.time)
end
logContainer.canvasOffset = vector2(0, lastPos - 200)
return container

View File

@ -22,14 +22,22 @@ if teverse.dev.localTevGit then
textAlpha = 0.5,
backgroundAlpha = 0
})
elseif _TEV_VERSION_PATCH and _TEV_VERSION_PATCH >= 12 then
teverse.input:on("keyUp", function(key)
if key == "KEY_F1" and not teverse.dev.localTevGit and teverse.input:isKeyDown("KEY_LSHIFT") then
teverse.dev:promptTevGit()
end
end)
end
local debug = false
teverse.input:on("keyUp", function(key)
if key == "KEY_F1" and not teverse.dev.localTevGit and teverse.input:isKeyDown("KEY_LSHIFT") then
teverse.dev:promptTevGit()
elseif key == "KEY_F2" and teverse.input:isKeyDown("KEY_LSHIFT") then
print("Reload")
teverse.dev:reloadAllShaders()
elseif key == "KEY_F12" and teverse.input:isKeyDown("KEY_LSHIFT") then
print("Debug")
debug = not debug
teverse.graphics:setDebug(debug)
end
end)
local settingsButton = teverse.construct("guiIcon", {
parent = teverse.coreInterface,
size = guiCoord(0, 35, 0, 35),

View File

@ -0,0 +1,18 @@
$input v_texcoord0
#include <teverse.sh>
SAMPLER2D(sColour, 0);
SAMPLER2D(sLight, 1);
uniform vec4 uniformAmbientColour;
vec3 colourAt(vec2 coord){
vec4 colour = toLinear(texture2D(sColour, coord) );
vec4 light = toLinear(texture2D(sLight, coord) );
return toGamma((colour * light) + (colour * uniformAmbientColour)).xyz;
}
void main()
{
gl_FragColor = vec4(colourAt(v_texcoord0), 1.0);
}

View File

@ -0,0 +1,4 @@
vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
vec3 a_position : POSITION;
vec2 a_texcoord0 : TEXCOORD0;

10
shaders/combine/vertex.sc Normal file
View File

@ -0,0 +1,10 @@
$input a_position, a_texcoord0
$output v_texcoord0
#include <teverse.sh>
void main()
{
gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0) );
v_texcoord0 = a_texcoord0;
}

View File

@ -1,12 +1,12 @@
$input v_worldPos, v_view, v_normal, v_tangent, v_bitangent, v_color0, v_color1
$input v_normal, v_colour, v_position, v_pbr
#include <teverse.sh>
#include <lighting.sh>
uniform vec4 u_lightRgbInnerR;
uniform vec4 u_camPos;
uniform vec4 uniformCameraPosition;
void main()
{
gl_FragColor = vec4(encodeNormalUint(v_normal), 0.0);
gl_FragData[0] = vec4(v_colour.xyz, v_pbr.x);
gl_FragData[1] = vec4(v_normal, v_pbr.y);
//gl_FragData[2] = vec4(v_position, 0.0);
}

View File

@ -1,6 +1,30 @@
#ifndef __LIGHTING_SH__
#define __LIGHTING_SH__
//
// Teverse will be rebuilding our PBR shaders shortly
// To optimise fully for Mobile and low end devices
//
float remapRoughness(float x)
{
return 2.0f * (1.0f / (1.0f - 0.5f + 0.001f) - 1.0f) * (pow(x, 2)) + 0.001f;
}
float schlick(float R0, float cos_theta)
{
float R = R0 + (1.0 - R0) * pow((1.0 - cos_theta), 5.0);
return R;
}
float roughSchlick2(float R0, float cos_theta, float roughness)
{
float area_under_curve = 1.0 / 6.0 * (5.0 * R0 + 1.0);
float new_area_under_curve = 1.0 / (6.0 * roughness + 6.0) * (5.0 * R0 + 1.0);
return schlick(R0, cos_theta) /
(1.0 + roughness) + (area_under_curve - new_area_under_curve);
}
const float PI = 3.1415;
@ -46,11 +70,11 @@ vec3 calcLight(vec3 _wpos, vec3 _normal, vec3 _view, vec3 _lightPos, float _ligh
float toClipSpaceDepth(float _depthTextureZ)
{
#if BGFX_SHADER_LANGUAGE_GLSL
#if GLSL
return _depthTextureZ * 2.0 - 1.0;
#else
return _depthTextureZ;
#endif // BGFX_SHADER_LANGUAGE_GLSL
#endif
}
vec3 clipToWorld(mat4 _invViewProj, vec3 _clipPos)
@ -70,7 +94,7 @@ float DistributionGGX(vec3 N, vec3 H, float roughness)
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
denom = PI * denom * denom;
return num / denom;
return NdotH2;
}
float GeometrySchlickGGX(float NdotV, float roughness)

View File

@ -1,24 +1,13 @@
vec3 v_worldPos : TEXCOORD1 = vec3(0.0, 0.0, 0.0);
vec3 v_view : TEXCOORD2 = vec3(0.0, 0.0, 0.0);
vec3 v_position : TEXCOORD3 = vec3(0.0, 0.0, 0.0);
vec3 v_view : TEXCOORD1 = vec3(0.0, 0.0, 0.0);
vec3 v_position : TEXCOORD2 = vec3(0.0, 0.0, 0.0);
vec4 v_colour : TEXCOORD3 = vec4(0.0, 0.0, 0.0, 0.0);
vec4 v_pbr : TEXCOORD4 = vec4(0.0, 0.0, 0.0, 0.0);
vec3 v_normal : NORMAL = vec3(0.0, 0.0, 1.0);
vec3 v_tangent : TANGENT = vec3(1.0, 0.0, 0.0);
vec3 v_bitangent : BINORMAL = vec3(0.0, 1.0, 0.0);
vec4 v_color0 : COLOR0 = vec4(1.0, 0.0, 0.0, 1.0);
vec4 v_color1 : COLOR1 = vec4(1.0, 0.0, 0.0, 1.0);
vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
vec3 a_position : POSITION;
vec4 a_normal : NORMAL;
vec4 a_tangent : TANGENT;
vec2 a_texcoord0 : TEXCOORD0;
vec4 a_color0 : COLOR0;
vec4 i_data0 : TEXCOORD7;
vec4 i_data1 : TEXCOORD6;
vec4 i_data2 : TEXCOORD5;
vec4 i_data3 : TEXCOORD4;
vec4 i_data4 : TEXCOORD3;
vec4 a_texcoord2 : TEXCOORD2;
ivec4 a_indices : BLENDINDICES;
vec4 a_weight : BLENDWEIGHT;
vec4 i_data4 : TEXCOORD3;

View File

@ -1,5 +1,5 @@
$input a_position, a_normal, a_tangent, a_color0, i_data0, i_data1, i_data2, i_data3, i_data4, a_texcoord2
$output v_worldPos, v_view, v_normal, v_tangent, v_bitangent, v_color0, v_color1
$input a_position, a_normal, i_data0, i_data1, i_data2, i_data3, i_data4
$output v_normal, v_colour, v_position, v_pbr
/*
* Portions of this file may have been directly taken or adapted from the following open sourced projects:
@ -10,6 +10,7 @@ $output v_worldPos, v_view, v_normal, v_tangent, v_bitangent, v_color0, v_color1
*/
#include <teverse.sh>
BUFFER_RO(normalData, vec4, 2);
void main()
{
@ -19,29 +20,20 @@ void main()
model[2] = i_data2;
model[3] = i_data3;
// metalness, roughness, 0, 0
v_pbr = vec4(normalData[gl_InstanceID * 3].w, i_data4.w, 0.0, 0.0);
mat3 normalMatrix;
normalMatrix[0] = normalData[gl_InstanceID * 3].xyz;
normalMatrix[1] = normalData[gl_InstanceID * 3 + 1].xyz;
normalMatrix[2] = normalData[gl_InstanceID * 3 + 2].xyz;
vec3 wpos = instMul(model, vec4(a_position, 1.0) ).xyz;
gl_Position = mul(u_viewProj, vec4(wpos, 1.0) );
v_worldPos = wpos;
mat3 modelIT = calculateInverseTranspose(u_model[0]);
vec4 normal = a_normal * 2.0 - 1.0;
vec4 tangent = a_tangent * 2.0 - 1.0;
vec3 wnormal = normalize(mul(modelIT, normal.xyz ));
wnormal = instMul(model, vec4(wnormal, 1.0) ).xyz;
vec3 wtangent = normalize(mul(modelIT, tangent.xyz ));
vec3 view = mul(u_view, vec4(wpos, 0.0) ).xyz;
v_view = view; //mul(view, tbn);
v_normal = normalize(wnormal);
v_tangent = wtangent;
v_bitangent = vec3(0.0, 0.0, 0.0);
//v_position = a_position;
v_color0 = i_data4;
v_color1 = a_texcoord2;
vec3 normal = a_normal.xyz * 2.0 - 1.0;
vec3 wnormal = instMul(normalMatrix, vec4(normal.xyz, 0.0));
v_normal = encodeNormalUint(normalize(wnormal.xyz));
v_colour = i_data4;
v_position = gl_Position;
}

View File

@ -0,0 +1,60 @@
$input v_texcoord0, lightDirection, lightColour
//
// Teverse will be rebuilding our PBR shaders shortly
// To optimise fully for Mobile and low end devices
//
#include <teverse.sh>
#include <../../instancedGeometry/lighting.sh>
SAMPLER2D(sColour, 0);
SAMPLER2D(sNormal, 1);
SAMPLER2D(sDepth, 2);
uniform mat4 uniformMtx;
uniform vec4 uniformCameraPosition;
void main()
{
vec4 colour = texture2D(sColour, v_texcoord0);
vec4 normal = texture2D(sNormal, v_texcoord0);
normal.xyz = decodeNormalUint(normal.xyz);
float depth = toClipSpaceDepth(texture2D(sDepth, v_texcoord0).x);
vec3 lightDir = normalize(-lightDirection);
float metallic = colour.w;
float roughness = normal.w;
vec3 clip = vec3(v_texcoord0 * 2.0 - 1.0, depth);
#if !GLSL
clip.y = -clip.y;
#endif
vec3 wpos = clipToWorld(uniformMtx, clip);
vec3 V = normalize(uniformCameraPosition.xyz - wpos);
vec3 F0 = vec3(0.04);
F0 = mix(F0, colour, metallic);
vec3 L = lightDir;
vec3 H = normalize(V + L);
// cook-torrance brdf
float NDF = DistributionGGX(normal.xyz, H, roughness);
float G = GeometrySmith(normal.xyz, V, L, roughness);
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);
vec3 kS = F;
vec3 kD = vec3(1.0) - kS;
kD *= 1.0 - metallic;
vec3 numerator = NDF * G * F;
float denominator = 4.0 * max(dot(normal.xyz, V), 0.0) * max(dot(normal.xyz, L), 0.0);
vec3 specular = numerator / max(denominator, 0.001);
// add to outgoing radiance Lo
float NdotL = max(dot(normal.xyz, L), 0.0);
gl_FragColor = vec4(((kD * colour / PI + specular) * lightColour * NdotL), 1.0);
}

View File

@ -0,0 +1,8 @@
vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
vec3 lightDirection : TEXCOORD1 = vec3(0.0, 0.0, 0.0);
vec3 lightColour : TEXCOORD2 = vec3(0.0, 0.0, 0.0);
vec3 a_position : POSITION;
vec2 a_texcoord0 : TEXCOORD0;
vec4 i_data0 : TEXCOORD7;
vec4 i_data1 : TEXCOORD6;

View File

@ -0,0 +1,12 @@
$input a_position, a_texcoord0, i_data0, i_data1
$output v_texcoord0, lightDirection, lightColour
#include <teverse.sh>
void main()
{
gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0) );
v_texcoord0 = a_texcoord0;
lightColour = i_data0.xyz;
lightDirection = i_data1.xyz;
}

0
shaders/ssao/fragment.sc Normal file
View File

View File

0
shaders/ssao/vertex.sc Normal file
View File