Skip to content

Commit

Permalink
Align to v0.21.0 of lacking engine (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
mokiat authored Oct 6, 2024
1 parent c476a0e commit 32e042c
Show file tree
Hide file tree
Showing 11 changed files with 233 additions and 196 deletions.
22 changes: 12 additions & 10 deletions game/builder_geometry.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
)

func (b *shaderBuilder) BuildGeometryCode(constraints graphics.GeometryConstraints, shader *lsl.Shader) renderapi.ProgramCode {
// TODO: Verify matching varyings between vertex and fragment
return render.ProgramCode{
VertexCode: b.buildGeometryVertexCode(constraints, shader),
FragmentCode: b.buildGeometryFragmentCode(constraints, shader),
Expand All @@ -19,8 +18,8 @@ func (b *shaderBuilder) BuildGeometryCode(constraints graphics.GeometryConstrain
func (b *shaderBuilder) buildGeometryVertexCode(constraints graphics.GeometryConstraints, _ *lsl.Shader) string {
var settings struct {
UseArmature bool
UseTangents bool
UseNormals bool
UseTangents bool
UseTexCoords bool
UseVertexColoring bool
}
Expand All @@ -30,26 +29,28 @@ func (b *shaderBuilder) buildGeometryVertexCode(constraints graphics.GeometryCon
if constraints.HasNormals {
settings.UseNormals = true
}
if constraints.HasTangents {
settings.UseTangents = true
}
if constraints.HasTexCoords {
settings.UseTexCoords = true
}
if constraints.HasVertexColors {
settings.UseVertexColoring = true
}
// TODO: Add support for varying

// TODO: Add support for varying
// TODO: Add support for position output

// TODO: Do actual shader translation
result := construct("custom_geometry.vert.glsl", settings)
// fmt.Println("Vertex code:\n", result)
return result

return construct("custom_geometry.vert.glsl", settings)
}

func (b *shaderBuilder) buildGeometryFragmentCode(constraints graphics.GeometryConstraints, shader *lsl.Shader) string {
var settings struct {
UseArmature bool
UseNormals bool
UseTangents bool
UseTexCoords bool
UseVertexColoring bool

Expand All @@ -66,6 +67,9 @@ func (b *shaderBuilder) buildGeometryFragmentCode(constraints graphics.GeometryC
if constraints.HasNormals {
settings.UseNormals = true
}
if constraints.HasTangents {
settings.UseTangents = true
}
if constraints.HasTexCoords {
settings.UseTexCoords = true
}
Expand All @@ -79,7 +83,5 @@ func (b *shaderBuilder) buildGeometryFragmentCode(constraints graphics.GeometryC
settings.VaryingLines = lines.VaryingLines
settings.CodeLines = lines.CodeLines

result := construct("custom_geometry.frag.glsl", settings)
// fmt.Println("Fragment code:\n", result)
return result
return construct("custom_geometry.frag.glsl", settings)
}
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ require (
github.com/mokiat/gblob v0.3.0
github.com/mokiat/gog v0.13.1
github.com/mokiat/gomath v0.9.0
github.com/mokiat/lacking v0.20.1
golang.org/x/image v0.20.0
github.com/mokiat/lacking v0.21.0
golang.org/x/image v0.21.0
)

require (
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/exp v0.0.0-20241004190924-225e2abe05e6 // indirect
golang.org/x/text v0.19.0 // indirect
)
32 changes: 16 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1v
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/pprof v0.0.0-20240910150728-a0b0bb1d4134 h1:c5FlPPgxOn7kJz3VoPLkQYQXGBS3EklQ4Zfi57uOuqQ=
github.com/google/pprof v0.0.0-20240910150728-a0b0bb1d4134/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144=
github.com/google/pprof v0.0.0-20241001023024-f4c0cfd0cf1d h1:Jaz2JzpQaQXyET0AjLBXShrthbpqMkhGiEfkcQAiAUs=
github.com/google/pprof v0.0.0-20241001023024-f4c0cfd0cf1d/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144=
github.com/hajimehoshi/go-mp3 v0.3.4 h1:NUP7pBYH8OguP4diaTZ9wJbUbk3tC0KlfzsEpWmYj68=
github.com/hajimehoshi/go-mp3 v0.3.4/go.mod h1:fRtZraRFcWb0pu7ok0LqyFhCUrPeMsGRSVop0eemFmo=
github.com/hajimehoshi/oto/v2 v2.3.1/go.mod h1:seWLbgHH7AyUMYKfKYT9pg7PhUu9/SisyJvNTT+ASQo=
Expand All @@ -22,26 +22,26 @@ github.com/mokiat/gog v0.13.1 h1:KHE6CsyRrfIpvfnAtjLT9P9u9eOHwBaTDuCDwWQgu98=
github.com/mokiat/gog v0.13.1/go.mod h1:VZTey/yYMWG4vGWbrjIHwsjyodlw5UARL3DVwSHOSyw=
github.com/mokiat/gomath v0.9.0 h1:dzhjuCouMIcw9BYOfWF70WbA8MlgyP0li9W1Be3HDfc=
github.com/mokiat/gomath v0.9.0/go.mod h1:RiglRYxDOMcucDUBYLCN16wIsMfSHXtCuGkQ9gLcghE=
github.com/mokiat/lacking v0.20.1 h1:4wXS3xnOpzkfW99ufSBoADIekcG2r79ppTm42XdPVQc=
github.com/mokiat/lacking v0.20.1/go.mod h1:le2Y7Q7SGcS7YBVTu9MoLLbuhSNdlCcSlixNksihRPI=
github.com/mokiat/lacking v0.21.0 h1:WKd1RN9fzUrLOb4Ak3ifQWotEao+NdZ/hxeifxdtbGo=
github.com/mokiat/lacking v0.21.0/go.mod h1:Rq998sSqVuqvvO9iAzg4VBdvMAIefFH3VoGqs7Nwrnk=
github.com/onsi/ginkgo/v2 v2.20.2 h1:7NVCeyIWROIAheY21RLS+3j2bb52W0W82tkberYytp4=
github.com/onsi/ginkgo/v2 v2.20.2/go.mod h1:K9gyxPIlb+aIvnZ8bd9Ak+YP18w3APlR+5coaZoE2ag=
github.com/onsi/gomega v1.34.2 h1:pNCwDkzrsv7MS9kpaQvVb1aVLahQXyJ/Tv5oAZMI3i8=
github.com/onsi/gomega v1.34.2/go.mod h1:v1xfxRgk0KIsG+QOdm7p8UosrOzPYRo60fd3B/1Dukc=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 h1:e66Fs6Z+fZTbFBAxKfP3PALWBtpfqks2bwGcexMxgtk=
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0/go.mod h1:2TbTHSBQa924w8M6Xs1QcRcFwyucIwBGpK1p2f1YFFY=
golang.org/x/image v0.20.0 h1:7cVCUjQwfL18gyBJOmYvptfSHS8Fb3YUDtfLIZ7Nbpw=
golang.org/x/image v0.20.0/go.mod h1:0a88To4CYVBAHp5FXJm8o7QbUl37Vd85ply1vyD8auM=
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
golang.org/x/exp v0.0.0-20241004190924-225e2abe05e6 h1:1wqE9dj9NpSm04INVsJhhEUzhuDVjbcyKH91sVyPATw=
golang.org/x/exp v0.0.0-20241004190924-225e2abe05e6/go.mod h1:NQtJDoLvd6faHhE7m4T/1IY708gDefGGjR/iUW8yQQ8=
golang.org/x/image v0.21.0 h1:c5qV36ajHpdj4Qi0GnE0jUc/yuo33OLFaa0d+crTD5s=
golang.org/x/image v0.21.0/go.mod h1:vUbsLavqK/W303ZroQQVKQ+Af3Yl6Uz1Ppu5J/cLz78=
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/tools v0.25.0 h1:oFU9pkj/iJgs+0DT+VMHrx+oBKs/LJMV+Uvg78sl+fE=
golang.org/x/tools v0.25.0/go.mod h1:/vtpO8WL1N9cQC3FN5zPqb//fRXskFHbLKk4OW1Q7rg=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/tools v0.26.0 h1:v/60pFQmzmT9ExmjDv2gGIfi3OqfKoEP6I5+umXlbnQ=
golang.org/x/tools v0.26.0/go.mod h1:TPVVj70c7JJ3WCazhD8OdXcZg/og+b9+tH/KxylGwH0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
51 changes: 24 additions & 27 deletions internal/shader/game/ambient_light.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -14,54 +14,54 @@ uniform samplerCube refractionTextureIn;

/*template "lighting.glsl"*/

struct ambientFresnelInput
struct AmbientFresnelInput
{
vec3 reflectanceF0;
vec3 reflectance_f0;
vec3 normal;
vec3 viewDirection;
float roughness;
};

vec3 calculateAmbientFresnel(ambientFresnelInput i)
vec3 calc_fresnel_ambient(AmbientFresnelInput i)
{
float normViewDot = clamp(dot(i.normal, i.viewDirection), 0.0, 1.0);
return i.reflectanceF0 + (max(vec3(1.0 - i.roughness), i.reflectanceF0) - i.reflectanceF0) * pow(1.0 - normViewDot, 5.0);
float norm_dot_view = min(abs(dot(i.normal, i.viewDirection)), 1.0);
return i.reflectance_f0 + (max(vec3(1.0 - i.roughness), i.reflectance_f0) - i.reflectance_f0) * pow(1.0 - norm_dot_view, 5.0);
}

struct ambientSetup {
vec3 baseColor;
float metallic;
float roughness;
vec3 reflectedColor;
vec3 refractedColor;
vec3 viewDirection;
vec3 normal;
};

vec3 calculateAmbientHDR(ambientSetup s)
{
vec3 fresnel = calculateAmbientFresnel(ambientFresnelInput(
s.reflectedColor,
vec3 albedo = s.baseColor * (1.0 - s.metallic);
vec3 irradiance_light = texture(refractionTextureIn, -s.normal).xyz;
vec3 refraction_brdf = irradiance_light * (albedo / pi);

vec3 reflection_f0 = mix(dielectric_reflectance, s.baseColor, s.metallic);
vec3 fresnel = calc_fresnel_ambient(AmbientFresnelInput(
reflection_f0,
s.normal,
s.viewDirection,
s.roughness
));

vec3 lightDirection = reflect(s.viewDirection, s.normal);
vec3 reflectedLightIntensity = pow(mix(
pow(texture(refractionTextureIn, lightDirection) / (2.0 * pi), vec4(0.25)),
pow(texture(reflectionTextureIn, lightDirection), vec4(0.25)),
pow(1.0 - s.roughness, 4.0)
), vec4(4.0)).xyz;
float geometry = calculateGeometry(geometryInput(
vec3 light_dir = reflect(-s.viewDirection, s.normal);
float max_lod = log2(float(textureSize(reflectionTextureIn, 0).x)) - 1.0;
float alpha = s.roughness * s.roughness;
vec3 reflected_light = textureLod(reflectionTextureIn, -light_dir, alpha * max_lod).xyz / (2.0 * pi);
float geometry = calc_geometry(GeometryInput(
s.normal,
s.viewDirection,
s.roughness
));
vec3 reflectedHDR = fresnel * s.reflectedColor * reflectedLightIntensity * geometry;
float normalization_factor = 1.0; // works best
vec3 reflection_brdf = reflected_light * geometry / normalization_factor;

vec3 refractedLightIntensity = texture(refractionTextureIn, -s.normal).xyz;
vec3 refractedHDR = (vec3(1.0) - fresnel) * s.refractedColor * refractedLightIntensity / (2.0 * pi);

return (reflectedHDR + refractedHDR);
return mix(refraction_brdf, reflection_brdf, fresnel);
}

void main()
Expand All @@ -79,13 +79,10 @@ void main()
float metalness = albedoMetalness.w;
float roughness = normalRoughness.w;

vec3 refractedColor = baseColor * (1.0 - metalness);
vec3 reflectedColor = mix(vec3(0.02), baseColor, metalness);

vec3 hdr = calculateAmbientHDR(ambientSetup(
baseColor,
metalness,
roughness,
reflectedColor,
refractedColor,
normalize(cameraPosition - worldPosition),
normal
));
Expand Down
45 changes: 24 additions & 21 deletions internal/shader/game/custom_geometry.frag.glsl
Original file line number Diff line number Diff line change
@@ -1,45 +1,48 @@
/*template "version.glsl"*/
/* template "version.glsl" */

layout(location = 0) out vec4 fbColor0Out; // color + metallic
layout(location = 1) out vec4 fbColor1Out; // normal + roughness

/*range $line := .TextureLines */
/* range $line := .TextureLines */
/* $line */
/*end*/
/* end */

/*if .UniformLines */
/* if .UniformLines */
layout (std140) uniform Material
{
/*range $line := .UniformLines */
/* range $line := .UniformLines */
/* $line */
/*end*/
/* end */
};
/*end*/
/* end */

/*template "ubo_camera.glsl"*/
/* template "ubo_camera.glsl" */

smooth in vec3 normalInOut;
/*if .UseTexCoords*/
smooth in vec3 tangentInOut;
smooth in vec2 texCoordInOut;
/*end*/
/*if .UseVertexColoring*/
smooth in vec4 colorInOut;
/*end*/

void main()
vec3 mapNormal(vec3 texel, float scale)
{
/* if not .UseVertexColoring */
vec4 colorInOut = vec4(1.0, 1.0, 1.0, 1.0);
/* end */
vec3 ls_normal = (texel * 2.0 - vec3(1.0)) * vec3(scale, scale, 1.0);
vec3 ws_normal = normalize(normalInOut);
vec3 ws_tangent = normalize(tangentInOut);
vec3 ws_bitangent = normalize(cross(ws_normal, ws_tangent));
mat3 tbn = mat3(ws_tangent, ws_bitangent, ws_normal);
return tbn * normalize(ls_normal);
}

vec4 color = vec4(1.0, 1.0, 1.0, 1.0);
float metallic = 0.0;
void main()
{
vec3 normal = normalize(normalInOut);
float roughness = 0.0;
vec4 color = colorInOut;
float metallic = 0.0;
float roughness = 1.0;

/*range $line := .CodeLines */
/* range $line := .CodeLines */
/* $line */
/*end*/
/* end */

fbColor0Out = vec4(color.xyz, metallic);
fbColor1Out = vec4(normal, roughness);
Expand Down
Loading

0 comments on commit 32e042c

Please sign in to comment.