Skip to content

Commit

Permalink
fixed shader bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaysmito101 committed Nov 19, 2023
1 parent be65666 commit 658e778
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 26 deletions.
70 changes: 54 additions & 16 deletions Binaries/Data/shaders/generation/base_noise/noise_gen.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ uniform float u_NoiseOctaveStrengths[16];
uniform int u_NoiseOctaveStrengthsCount;
uniform int u_SlopeSmoothingRadius;
uniform vec2 u_TransformRange;
uniform float u_SlopeSamplingRadius;
uniform bool m_UseGaussianPreFilter;

//
// GLSL textureless classic 3D noise "cnoise",
Expand Down Expand Up @@ -146,31 +148,67 @@ uint PixelCoordToDataOffset(uint x, uint y)
return y * u_Resolution + x;
}

float calculateSlopeFactorAtCoord(uvec2 offsetv2)
float gaussianSample(uvec2 offset)
{
/*
const float filterMask[5][5] = {
{ 0.000229, 0.005977, 0.060598, 0.005977, 0.000229 },
{ 0.005977, 0.156150, 1.579180, 0.156150, 0.005977 },
{ 0.060598, 1.579180, 15.961800, 1.579180, 0.060598 },
{ 0.005977, 0.156150, 1.579180, 0.156150, 0.005977 },
{ 0.000229, 0.005977, 0.060598, 0.005977, 0.000229 }
};

float sum = 0.0;

for (int i = -2; i <= 2; i++)
{
for (int j = -2; j <= 2; j++)
{
ivec2 offsetiv2 = ivec2(offset.x + i, offset.y + j);
if (offsetiv2.x < 0 || offsetiv2.x >= u_Resolution || offsetiv2.y < 0 || offsetiv2.y >= u_Resolution) continue;
sum += dataSource[PixelCoordToDataOffset(offsetiv2.x, offsetiv2.y)] * filterMask[i + 2][j + 2];
}
}

return sum;
}

float calculateSlopeFactorAtCoord(uvec2 offsetb, uvec2 offsetc, float radius)
{
uvec2 offsetv2 = offsetb + offsetc;

if (offsetv2.x == 0) offsetv2.x = 1;
if (offsetv2.y == 0) offsetv2.y = 1;
if (offsetv2.x == u_Resolution - 1) offsetv2.x = u_Resolution - 2;
if (offsetv2.y == u_Resolution - 1) offsetv2.y = u_Resolution - 2;
*/


float T = 0.0f, B = 0.0f, L = 0.0f, R = 0.0f;

float C = dataSource[PixelCoordToDataOffset(offsetv2.x, offsetv2.y)];
float T = dataSource[PixelCoordToDataOffset(offsetv2.x, offsetv2.y - 1)];
float B = dataSource[PixelCoordToDataOffset(offsetv2.x, offsetv2.y + 1)];
float L = dataSource[PixelCoordToDataOffset(offsetv2.x - 1, offsetv2.y)];
float R = dataSource[PixelCoordToDataOffset(offsetv2.x + 1, offsetv2.y)];
if (m_UseGaussianPreFilter)
{
T = gaussianSample(offsetv2 + uvec2(0, -1));
B = gaussianSample(offsetv2 + uvec2(0, 1));
L = gaussianSample(offsetv2 + uvec2(-1, 0));
R = gaussianSample(offsetv2 + uvec2(1, 0));
}
else
{
T = dataSource[PixelCoordToDataOffset(offsetv2.x, offsetv2.y - 1)];
B = dataSource[PixelCoordToDataOffset(offsetv2.x, offsetv2.y + 1)];
L = dataSource[PixelCoordToDataOffset(offsetv2.x - 1, offsetv2.y)];
R = dataSource[PixelCoordToDataOffset(offsetv2.x + 1, offsetv2.y)];
}

float slopeFactor = 0.0f;

// calculate the slope factor

float dX = (R - L);
float dY = (B - T);
slopeFactor = sqrt(dX * dX + dY * dY);

float dX = (R - L) / 2.0f;
float dY = (B - T) / 2.0f;
slopeFactor = sqrt(dX * dX + dY * dY);

return clamp(slopeFactor * u_Resolution, 0.0, 1.0);
return slopeFactor * u_Resolution / radius;
}

float calculateSlopeFactor()
Expand All @@ -183,7 +221,7 @@ float calculateSlopeFactor()
{
for (int j = -u_SlopeSmoothingRadius; j <= u_SlopeSmoothingRadius; j++)
{
factor += calculateSlopeFactorAtCoord(offsetv2 + uvec2(i, j));
factor += calculateSlopeFactorAtCoord(offsetv2, uvec2(vec2(i, j) * u_SlopeSamplingRadius), u_SlopeSamplingRadius);
}
}

Expand All @@ -205,7 +243,7 @@ void main(void)
{
seed = texture(u_SeedTexture, uv).rgb;
}
seed = seed * u_Frequency + u_Offset + vec3(u_Seed);
seed = seed * u_Frequency + u_Offset + vec3(u_Seed % 100);

float n = 0.0f;
float frequency = 1.0f;
Expand All @@ -225,7 +263,7 @@ void main(void)

if ( u_MixMethod == 0 ) dataTarget[offset] = dataSource[offset] + n;
else if ( u_MixMethod == 1 ) dataTarget[offset] = dataSource[offset] * n;
else if ( u_MixMethod == 2 ) dataTarget[offset] = dataSource[offset] * n + n;
else if ( u_MixMethod == 2 ) dataTarget[offset] = dataSource[offset] * n + dataSource[offset];
else if ( u_MixMethod == 3 ) dataTarget[offset] = n;
else dataTarget[offset] = dataSource[offset];

Expand Down
14 changes: 7 additions & 7 deletions Binaries/Data/shaders/generation/base_shape/classic.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
{
"Name": "Strength",
"Type": "Float",
"Default": 1.84,
"Default": 1.0,
"Widget": "Drag",
"Sensitivity": 0.01
},
{
"Name": "Scale",
"Type": "Float",
"Default": 0.04,
"Default": 1.0,
"Widget": "Drag",
"Sensitivity": 0.001
},
{
"Name": "Levels",
"Type": "Int",
"Default": 2,
"Default": 1,
"Widget": "Slider",
"Constraints": [1.0, 6.0, 0.0, 0.0]
"Constraints": [0.0, 6.0, 0.0, 0.0]
},
{
"Name": "Seed",
Expand Down Expand Up @@ -164,7 +164,7 @@ float cnoise(vec3 P)

float evaluateBaseShape(vec2 uv, vec3 seed)
{
seed += u_Offset;
seed = (seed + u_Offset) * u_Scale + vec3(u_Seed);
float n = 0.0f;
for(int i = 0 ; i < u_Levels ; i++)
{
Expand All @@ -173,8 +173,8 @@ float evaluateBaseShape(vec2 uv, vec3 seed)
cnoise(seed + vec3(3.0f, 4.0f, 0.0f))
);
}
n = cnoise(seed * u_Scale + vec3(u_Seed));
n = cnoise(seed);
if(u_AbsoluteValue) n = abs(n);
if(u_SquareValue) n = n * n;
return n * u_Strength * 10.0f;
return n * u_Strength;
}
13 changes: 10 additions & 3 deletions TerraForge3D/src/Generators/BiomeBaseNoiseGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ BiomeBaseNoiseGenerator::BiomeBaseNoiseGenerator(ApplicationState* appState)
m_NoiseOctaveStrengths[0] = m_NoiseOctaveStrengths[1] = 0.0f;

{

m_Inspector->AddIntegerVariable("Seed", 152);
m_Inspector->AddSeedWidget("Seed", "Seed");

Expand All @@ -44,16 +43,22 @@ BiomeBaseNoiseGenerator::BiomeBaseNoiseGenerator(ApplicationState* appState)
m_Inspector->AddDragWidget("Offset", "Offset", 0.0f, 0.0f, 0.01f);

m_Inspector->AddIntegerVariable("MixMethod");
m_Inspector->AddDropdownWidget("Mix Method", "MixMethod", { "Add", "Multiply", "Add & Multiply", "Set" });
m_Inspector->AddDropdownWidget("Mix Method", "MixMethod", { "Add", "Multiply", "Add & Multiply", "Set", "None"});

m_Inspector->AddIntegerVariable("TransformFactor", 1);
m_Inspector->AddDropdownWidget("Transform Factor", "TransformFactor", { "None", "Slope", "Height" });

m_Inspector->AddIntegerVariable("SlopeSmoothingRadius", 3);
m_Inspector->AddSliderWidget("Slope Smoothing Radius", "SlopeSmoothingRadius", 0, 10).SetRenderOnCondition("TransformFactor", 1);
m_Inspector->AddSliderWidget("Slope Smoothing Radius", "SlopeSmoothingRadius", 0, 20).SetRenderOnCondition("TransformFactor", 1);

m_Inspector->AddFloatVariable("SlopeSamplingRadius", 3);
m_Inspector->AddSliderWidget("Slope Sampling Radius", "SlopeSamplingRadius", 1.0, 10.0).SetRenderOnCondition("TransformFactor", 1);

m_Inspector->AddVector2Variable("TransformRange", { 0.0f, 1.0f });
m_Inspector->AddDragWidget("Transform Range", "TransformRange", 0.0f, 0.0f, 0.01f);

m_Inspector->AddBoolVariable("UseGaussianPreFilter", false);
m_Inspector->AddCheckboxWidget("Use Gaussian Pre Filter", "UseGaussianPreFilter");
}
}

Expand Down Expand Up @@ -110,6 +115,8 @@ void BiomeBaseNoiseGenerator::Update(GeneratorData* sourceBuffer, GeneratorData*
m_Shader->SetUniform1i("u_SlopeSmoothingRadius", values.at("SlopeSmoothingRadius").GetInt());
m_Shader->SetUniform2f("u_TransformRange", values.at("TransformRange").GetVector2());
m_Shader->SetUniform1i("u_Seed", values.at("Seed").GetInt());
m_Shader->SetUniform1f("u_SlopeSamplingRadius", values.at("SlopeSamplingRadius").GetFloat());
m_Shader->SetUniform1i("u_UseGaussianPreFilter", values.at("UseGaussianPreFilter").GetInt());
for (int i = 0; i < BIOME_BASE_NOISE_OCTAVE_COUNT; i++)
{
m_Shader->SetUniform1f("u_NoiseOctaveStrengths[" + std::to_string(i) + "]", m_NoiseOctaveStrengths[i]);
Expand Down

0 comments on commit 658e778

Please sign in to comment.