Skip to content

Commit

Permalink
Finished Mask Editor
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaysmito101 committed Nov 21, 2023
1 parent e471cef commit c800a84
Show file tree
Hide file tree
Showing 13 changed files with 252 additions and 32 deletions.
11 changes: 10 additions & 1 deletion Binaries/Data/shaders/generation/biome_mixer/simple_mixer.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ layout(std430, binding = 1) buffer DataTargetBuffer
uniform int u_Resolution;
uniform int u_Mode;
uniform float u_Strength;
uniform bool u_UseBiomeMask;
uniform sampler2D u_BiomeMask;

uint PixelCoordToDataOffset(uint x, uint y)
{
Expand All @@ -40,7 +42,14 @@ void main(void)
}
else if (u_Mode == 1)
{
dataTarget[offset] = dataTarget[offset] + u_Strength * dataSource[offset];
float factor = 1.0f;
if (u_UseBiomeMask)
{
vec2 uv = offsetv2 / float(u_Resolution);
factor = texture(u_BiomeMask, uv).x;
}

dataTarget[offset] = dataTarget[offset] + u_Strength * dataSource[offset] * factor;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ uint PixelCoordToDataOffset(uint x, uint y)
return y * u_Resolution + x;
}


void transferData()
{
uvec2 offsetv2 = gl_GlobalInvocationID.xy;
Expand Down
38 changes: 38 additions & 0 deletions Binaries/Data/shaders/generation/utils/mask_editor.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#version 430 core

// work group size
layout(local_size_x = 16, local_size_y = 16, local_size_z = 1) in;

// texture for mask
layout(binding = 0, rgba32f) uniform image2D u_MaskTexture;


uniform int u_Resolution;
uniform vec2 u_BrushPosition;
uniform vec4 u_BrushSettings0;
uniform int u_Mode;


float calculateFallOff(in vec2 uv)
{
float distanceVal = length(uv - u_BrushPosition);
return smoothstep(u_BrushSettings0.y * (1.0f - u_BrushSettings0.z), u_BrushSettings0.y, distanceVal);
}

void main()
{
ivec2 offset = ivec2(gl_GlobalInvocationID.xy);
vec4 oriVal = imageLoad(u_MaskTexture, offset);

vec2 uv = offset / float(u_Resolution);

// u_BrushSettings0 = strength, size, falloff, reserved
float val = mix(u_BrushSettings0.x, 0.0f, calculateFallOff(uv));

if (u_Mode == 1)
val = clamp(oriVal.x - val, 0.0f, 1.0f);
else
val = clamp(oriVal.x + val, 0.0f, 1.0f);

imageStore(u_MaskTexture, offset, vec4(val, val, val, 1.0f));
}
14 changes: 14 additions & 0 deletions Binaries/Data/shaders/object_mode/frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ uniform vec2 u_MousePos;
uniform vec2 u_ViewportResolution;
uniform bool u_RequiresDrawBrush;
uniform vec4 u_BrushSettings0;
uniform vec3 u_MaskColor;
uniform bool u_DrawMask;
uniform sampler2D u_MaskTexture;

int PixelCoordToDataOffset(int x, int y)
{
Expand Down Expand Up @@ -124,13 +127,24 @@ void main()
if(u_EnableSkyLight) outputColor = outputColor + irradiance * 0.4f * u_SkyLightIntensity;
outputColor = aces(outputColor);
outputColor = pow(outputColor, vec3(1.0f/2.2f));


if (u_DrawMask)
{
vec3 maskColor = texture(u_MaskTexture, fragmentInput.texCoord).rgb;
outputColor *= (maskColor.r * u_MaskColor + 0.1f);
}

if(u_RequiresDrawBrush)
{
const vec3 brushColor = vec3(1.0f, 0.0f, 0.0f);
// u_BrushSettings0.z = radius, u_BrushSettings0.w = hardness
float distanceVal = length(fragmentInput.texCoord - u_BrushSettings0.xy);
float falloff = smoothstep(u_BrushSettings0.z * (1.0f - u_BrushSettings0.w), u_BrushSettings0.z, distanceVal);
outputColor *= mix(brushColor, vec3(1.0), falloff);

}


FragColor = vec4(outputColor, 1.0f);
}
14 changes: 2 additions & 12 deletions TerraForge3D/include/Generators/BiomeCustomBaseShape.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "Generators/GeneratorTexture.h"
#include "Exporters/Serializer.h"
#include "Misc/CustomInspector.h"
#include "Renderer/ObjectRenderer.h"

class ApplicationState;

Expand All @@ -15,17 +16,6 @@ enum BiomeCustomBaseShapeEditMode
BiomeCustomBaseShapeEditMode_Count
};

struct BiomeCustomBaseShapeDrawSettings
{
float m_BrushSize = 0.2f;
float m_BrushStrength = 0.5f;
float m_BrushFalloff = 0.5f;
float m_BrushPositionX = 0.0f;
float m_BrushPositionY = 0.0f;
float m_BrushRotation = 0.0f;
int m_BrushMode = 0;
};

class BiomeCustomBaseShape
{
public:
Expand Down Expand Up @@ -58,5 +48,5 @@ class BiomeCustomBaseShape
std::shared_ptr<GeneratorTexture> m_PreviewTexture;
BiomeCustomBaseShapeEditMode m_EditMode = BiomeCustomBaseShapeEditMode_Draw;
float m_CalculationTime = 0.0f;
BiomeCustomBaseShapeDrawSettings m_DrawSettings;
DrawBrushSettings m_DrawSettings;
};
3 changes: 2 additions & 1 deletion TerraForge3D/include/Generators/BiomeManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ class BiomeManager
inline const bool IsEnabled() const { return m_IsEnabled; }
inline const char* GetBiomeName() const { return m_BiomeName; }
inline const float GetCalculationTime() const { return m_CalculationTime; }
inline const bool IsUpdationRequired() const { return m_RequireUpdation && m_IsEnabled; }
inline const bool IsUpdationRequired() const { return m_RequireUpdation; }
inline const bool IsUsingCustomBaseShape() const { return m_UseCustomBaseShape; }
inline GeneratorData* GetBiomeData() const { return m_Data.get(); }
inline const ImVec4& GetColor() const { return m_Color; }
inline const int GetFiltersCount() const { return 0; }
inline const std::vector<int>& GetFilters() const { return m_Filters; }
inline const std::string& GetBiomeID() const { return m_BiomeID; }
inline void SetName(const std::string& name) { strcpy(m_BiomeName, name.c_str()); }
inline GeneratorTexture* GetMaskTexture() const { return m_MaskEditor->GetTexture(); }

bool AddBaseShapeGenerator(const std::string& config);
bool LoadUpResources();
Expand Down
21 changes: 18 additions & 3 deletions TerraForge3D/include/Generators/MaskEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,40 @@
#include "Generators/GeneratorData.h"
#include "Generators/GeneratorTexture.h"
#include "Exporters/Serializer.h"
#include "Misc/CustomInspector.h"
#include "Renderer/ObjectRenderer.h"

class ApplicationState;

class MaskEditor
{
public:

MaskEditor(ApplicationState* state);
MaskEditor(ApplicationState* state, glm::vec3 vizColor);
~MaskEditor();

void Resize(int size);

bool ShowSettings();

bool ApplyDrawingShaders();

inline void SetVizColor( float r, float g, float b ) { m_VizColor = glm::vec3(r, g, b); }

inline GeneratorTexture* GetTexture() const { return m_Texture.get(); }

private:
ApplicationState* m_AppState = nullptr;

std::shared_ptr<GeneratorTexture> m_GeneratorTexture;
std::shared_ptr<ComputeShader> m_Shader;
std::shared_ptr<GeneratorTexture> m_Texture;

glm::vec3 m_VizColor = glm::vec3(0.2f, 0.2f, 0.2f);

DrawBrushSettings m_DrawSettings;

int m_Size = 256;
bool m_RequireUpdation = true;
bool m_IsEditing = false;

static MaskEditor* s_CurrentlyEditingMaskEditor;
};
1 change: 1 addition & 0 deletions TerraForge3D/include/Generators/SimpleBiomeMixer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct SimpleBiomeMixerSettings
{
bool enabled = true;
float strength = 1.0f;
bool useBiomeMask = false;
};

class SimpleBiomeMixer
Expand Down
19 changes: 17 additions & 2 deletions TerraForge3D/include/Renderer/ObjectRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@

struct BiomeCustomBaseShapeDrawSettings;


struct DrawBrushSettings
{
float m_BrushSize = 0.2f;
float m_BrushStrength = 0.5f;
float m_BrushFalloff = 0.5f;
float m_BrushPositionX = 0.0f;
float m_BrushPositionY = 0.0f;
float m_BrushRotation = 0.0f;
int m_BrushMode = 0;

int32_t m_MaskTexture = -1;
glm::vec3 m_MaskColor = glm::vec3(1.0f);
};

class ObjectRenderer : public RendererBase
{
public:
Expand All @@ -14,13 +29,13 @@ class ObjectRenderer : public RendererBase

virtual void Render(RendererViewport* viewport) override;
virtual void ShowSettings() override;
inline void SetCustomBaseShapeDrawSettings(BiomeCustomBaseShapeDrawSettings* settings) { m_CustomBaseShapeDrawSettings = settings; }
inline void SetCustomBaseShapeDrawSettings(DrawBrushSettings* settings) { m_DrawBrushSettings = settings; }

private:
virtual void ReloadShaders() override;

private:
bool m_InvertNormals = false;
std::shared_ptr<ShaderStorageBuffer> m_SharedMemoryBuffer;
BiomeCustomBaseShapeDrawSettings* m_CustomBaseShapeDrawSettings = nullptr;
DrawBrushSettings* m_DrawBrushSettings = nullptr;
};
7 changes: 5 additions & 2 deletions TerraForge3D/src/Generators/BiomeManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ bool BiomeManager::LoadUpResources()
m_BaseNoiseGenerator = std::make_shared<BiomeBaseNoiseGenerator>(m_AppState);
m_DEMBaseShapeGenerator = std::make_shared<DEMBaseShapeGenerator>(m_AppState);
m_CustomBaseShape = std::make_shared<BiomeCustomBaseShape>(m_AppState);
m_MaskEditor = std::make_shared<MaskEditor>(m_AppState);
m_MaskEditor = std::make_shared<MaskEditor>(m_AppState, glm::vec3(m_Color.x, m_Color.y, m_Color.z));
return true;
}

Expand Down Expand Up @@ -152,7 +152,10 @@ bool BiomeManager::ShowGeneralSettings()
ImGui::PushID(m_BiomeID.data());
ImGui::InputText("Biome Name", m_BiomeName, sizeof(m_BiomeName));
BIOME_UI_PROPERTY(ImGui::Checkbox("Enabled", &m_IsEnabled));
ImGui::ColorEdit3("Biome Color", reinterpret_cast<float*>(&m_Color));
if (ImGui::ColorEdit3("Biome Color", reinterpret_cast<float*>(&m_Color)))
{
m_MaskEditor->SetVizColor(m_Color.x, m_Color.y, m_Color.z);
}

BIOME_UI_PROPERTY(m_MaskEditor->ShowSettings());

Expand Down
Loading

0 comments on commit c800a84

Please sign in to comment.