From 9f5d740c11a0cdb596ce298a70898b901983e350 Mon Sep 17 00:00:00 2001 From: Eddie Date: Mon, 10 Jun 2024 16:36:26 +0300 Subject: [PATCH] terrain update --- include/limitless/core/indexed_stream.hpp | 1 + shaders/terrain/terrain.glsl | 92 ++++++++++++++++++----- 2 files changed, 76 insertions(+), 17 deletions(-) diff --git a/include/limitless/core/indexed_stream.hpp b/include/limitless/core/indexed_stream.hpp index b2925c32..dc3c4fff 100644 --- a/include/limitless/core/indexed_stream.hpp +++ b/include/limitless/core/indexed_stream.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include namespace Limitless { template diff --git a/shaders/terrain/terrain.glsl b/shaders/terrain/terrain.glsl index 841fbe29..fde83e2b 100644 --- a/shaders/terrain/terrain.glsl +++ b/shaders/terrain/terrain.glsl @@ -13,24 +13,82 @@ vec3 MacroContrast(float variation) { return mix(variation_contrast, vec3(1.0), vec3(variation)); } +#define _2xSqrt3 3.46410161514 + +vec2 hashUv( vec2 p) +{ + return fract ( sin (( p ) * mat2 (127.1 , 311.7 , 269.5 , 183.3)) + *43758.5453) ; +} + +void triangleGrid(vec2 uv, +out float w1, out float w2, out float w3, +out ivec2 v1, out ivec2 v2, out ivec2 v3) +{ + const mat2 skewedGridTransform = mat2(1.0, 0.0, -0.57735027, 1.15470054); + vec2 skewedUv = skewedGridTransform * uv * _2xSqrt3; + + ivec2 vBase = ivec2(floor(skewedUv)); + vec3 temp = vec3(fract(skewedUv), 0); + temp.z = 1.0 - temp.x - temp.y; + if (temp.z > 0.0) { + w1 = temp.z; + w2 = temp.y; + w3 = temp.x; + v1 = vBase; + v2 = vBase + ivec2(0, 1); + v3 = vBase + ivec2(1, 0); + } else { + w1 = -temp.z; + w2 = 1.0 - temp.y; + w3 = 1.0 - temp.x; + v1 = vBase + ivec2(1, 1); + v2 = vBase + ivec2(1, 0); + v3 = vBase + ivec2(0, 1); + } +} + +vec3 StochasticTexture(vec2 uv, float index, sampler2DArray s) +{ + float w1 = 0, w2 = 0, w3 = 0; + ivec2 v1, v2, v3; + triangleGrid(uv, w1, w2, w3, v1, v2, v3); + + vec2 uv1 = uv + hashUv(v1); + vec2 uv2 = uv + hashUv(v2); + vec2 uv3 = uv + hashUv(v3); + + vec2 duvdx = dFdx(uv); + vec2 duvdy = dFdy(uv); + + vec3 T1 = textureGrad(s, vec3(uv1, index), duvdx, duvdy).rgb; + vec3 T2 = textureGrad(s, vec3(uv2, index), duvdx, duvdy).rgb; + vec3 T3 = textureGrad(s, vec3(uv3, index), duvdx, duvdy).rgb; + + vec3 col = w1 * T1 + w2 * T2 + w3 * T3; + return col; +} + void calculateTerrain(inout MaterialContext mctx) { - vec2 uv2 = getVertexUV1() * 4.0; + vec2 terrain_uv = (getVertexPosition().xz / 25.6) * 256.0; + vec3 variation = MacroContrast(MacroVariation()); - vec3 diffuse = texture(_terrain_diffuse_texture, vec3(uv2, getVertexTileCurrent())).rgb * vec3(1.0, 2.0, 1.0); - vec3 normal = texture(_terrain_normal_texture, vec3(uv2, getVertexTileCurrent())).xyz; - vec3 orm = texture(_terrain_orm_texture, vec3(uv2, getVertexTileCurrent())).rgb; + + vec3 diffuse = StochasticTexture(terrain_uv, getVertexTileCurrent(), _terrain_diffuse_texture); + vec3 normal = StochasticTexture(terrain_uv, getVertexTileCurrent(), _terrain_normal_texture); + vec3 orm = StochasticTexture(terrain_uv, getVertexTileCurrent(), _terrain_orm_texture); vec2 uv = getVertexUV(); float vectors[8] = { - smoothstep(0.0, 1.0, length(uv)), - smoothstep(0.0, 1.0, uv.y), - smoothstep(0.0, 1.0, length(vec2(1.0 - uv.x, uv.y))), - smoothstep(0.0, 1.0, uv.x), - smoothstep(0.0, 1.0, 1.0 - uv.x), - smoothstep(0.0, 1.0, length(vec2(uv.x, 1.0 - uv.y))), - smoothstep(0.0, 1.0, 1.0 - uv.y), - smoothstep(0.0, 1.0, length(vec2(1.0 - uv.x, 1.0 - uv.y))) + smoothstep(0.0, 1.0, length(uv)), + smoothstep(0.0, 1.0, uv.y), + smoothstep(0.0, 1.0, length(vec2(1.0 - uv.x, uv.y))), + smoothstep(0.0, 1.0, uv.x), + smoothstep(0.0, 1.0, 1.0 - uv.x), + smoothstep(0.0, 1.0, length(vec2(uv.x, 1.0 - uv.y))), + smoothstep(0.0, 1.0, 1.0 - uv.y), + smoothstep(0.0, 1.0, length(vec2(1.0 - uv.x, 1.0 - uv.y))) }; uint mask[8]; @@ -44,9 +102,9 @@ void calculateTerrain(inout MaterialContext mctx) { // shoud we blend if (mask[i] == 1u) { // fetch adjacent type - vec3 adjacent_diffuse = texture(_terrain_diffuse_texture, vec3(uv2, types[i])).rgb; - vec3 adjacent_normal = texture(_terrain_normal_texture, vec3(uv2, types[i])).xyz; - vec3 adjacent_orm = texture(_terrain_orm_texture, vec3(uv2, types[i])).rgb; + vec3 adjacent_diffuse = StochasticTexture(terrain_uv, types[i], _terrain_diffuse_texture).rgb; + vec3 adjacent_normal = StochasticTexture(terrain_uv, types[i], _terrain_normal_texture).xyz; + vec3 adjacent_orm = StochasticTexture(terrain_uv, types[i], _terrain_orm_texture).rgb; // mix diffuse = mix(diffuse, adjacent_diffuse, (1.0 - vectors[i])); @@ -55,12 +113,12 @@ void calculateTerrain(inout MaterialContext mctx) { } } - diffuse *= variation; +// diffuse *= variation; // store result mctx.color.rgb = diffuse; mctx.normal = normalize(normal); mctx.ao = orm.r; mctx.roughness = orm.g; - mctx.metallic =orm.b; + mctx.metallic = orm.b; } \ No newline at end of file