Skip to content

Commit

Permalink
Draggable texture preview
Browse files Browse the repository at this point in the history
  • Loading branch information
Auburn committed Aug 23, 2021
1 parent 929c439 commit 5ffc74b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
6 changes: 6 additions & 0 deletions NoiseTool/FastNoiseNodeEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,12 @@ FastNoise::OutputMinMax FastNoiseNodeEditor::GenerateNodePreviewNoise( FastNoise
Node::NoiseSize / -2, Node::NoiseSize / -2, 0,
Node::NoiseSize, Node::NoiseSize, 1,
mNodeFrequency, mNodeSeed );

case NoiseTexture::GenType_4D:
return gen->GenUniformGrid4D( noise,
Node::NoiseSize / -2, Node::NoiseSize / -2, 0, 0,
Node::NoiseSize, Node::NoiseSize, 1, 1,
mNodeFrequency, mNodeSeed );
}

return {};
Expand Down
62 changes: 54 additions & 8 deletions NoiseTool/NoiseTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ NoiseTexture::NoiseTexture()
mBuildData.frequency = 0.02f;
mBuildData.seed = 1337;
mBuildData.size = { -1, -1 };
mBuildData.offset = { 0, 0, 0 };
mBuildData.offset = {};
mBuildData.generationType = GenType_2D;

for( size_t i = 0; i < 2; i++ )
Expand Down Expand Up @@ -73,7 +73,7 @@ void NoiseTexture::Draw()
int texSize[2] = { mBuildData.size.x(), mBuildData.size.y() };
ImGui::SameLine();

if( ImGui::DragInt2( "Size", texSize, 2, 16, 8192 ) )
if( ImGui::DragInt2( "Size", texSize, 2, 4, 8192 ) )
{
ImVec2 delta( (float)(texSize[0] - mBuildData.size.x()), (float)(texSize[1] - mBuildData.size.y()) );

Expand All @@ -90,11 +90,50 @@ void NoiseTexture::Draw()
if( contentSize.x >= 1 && contentSize.y >= 1 &&
(edited || mBuildData.size.x() != (int)contentSize.x || mBuildData.size.y() != (int)contentSize.y) )
{
mBuildData.size = { (int)contentSize.x, (int)contentSize.y };
Vector2i newSize = { (int)contentSize.x, (int)contentSize.y };

mBuildData.offset.xy() -= Vector2( newSize - mBuildData.size ) / 2;
mBuildData.size = newSize;
ReGenerate( mBuildData.generator );
}

ImGuiIntegration::image( mNoiseTexture, Vector2( mNoiseTexture.imageSize( 0 ) ) );
ImGui::PushStyleColor( ImGuiCol_Button, 0 );
ImGui::PushStyleColor( ImGuiCol_ButtonActive, 0 );
ImGui::PushStyleColor( ImGuiCol_ButtonHovered, 0 );
ImGuiIntegration::imageButton( mNoiseTexture, Vector2( mNoiseTexture.imageSize( 0 ) ), {{},Vector2{1}}, 0 );
ImGui::PopStyleColor( 3 );

if( ImGui::IsItemHovered() )
{
Vector4 oldOffset = mBuildData.offset;

if( mBuildData.generationType != GenType_2DTiled && ImGui::IsMouseDragging( ImGuiMouseButton_Left ) )
{
Vector2 dragDelta( ImGui::GetMouseDragDelta( ImGuiMouseButton_Left ) );
ImGui::ResetMouseDragDelta( ImGuiMouseButton_Left );

mBuildData.offset.x() -= dragDelta.x();
mBuildData.offset.y() += dragDelta.y();
}
else if( (mBuildData.generationType == GenType_3D || mBuildData.generationType == GenType_4D)
&& ImGui::IsMouseDragging( ImGuiMouseButton_Right ) )
{
Vector2 dragDelta( ImGui::GetMouseDragDelta( ImGuiMouseButton_Right ) );
ImGui::ResetMouseDragDelta( ImGuiMouseButton_Right );

mBuildData.offset.z() -= dragDelta.x();

if( mBuildData.generationType == GenType_4D )
{
mBuildData.offset.w() -= dragDelta.y();
}
}

if( oldOffset != mBuildData.offset )
{
ReGenerate( mBuildData.generator );
}
}
}
ImGui::End();
}
Expand Down Expand Up @@ -136,7 +175,7 @@ void NoiseTexture::ReGenerate( FastNoise::SmartNodeArg<> generator )
NoiseTexture::TextureData NoiseTexture::BuildTexture( const BuildData& buildData )
{
static thread_local std::vector<float> noiseData;
noiseData.resize( buildData.size.x() * buildData.size.y() );
noiseData.resize( (size_t)buildData.size.x() * buildData.size.y() );

auto genRGB = FastNoise::New<FastNoise::ConvertRGBA8>( buildData.generator->GetSIMDLevel() );

Expand All @@ -146,8 +185,8 @@ NoiseTexture::TextureData NoiseTexture::BuildTexture( const BuildData& buildData
switch( buildData.generationType )
{
case GenType_2D:
minMax = genRGB->GenUniformGrid2D( noiseData.data(),
buildData.size.x() / -2, buildData.size.y() / -2,
minMax = genRGB->GenUniformGrid2D( noiseData.data(),
(int)buildData.offset.x(), (int)buildData.offset.y(),
buildData.size.x(), buildData.size.y(),
buildData.frequency, buildData.seed );
break;
Expand All @@ -160,10 +199,17 @@ NoiseTexture::TextureData NoiseTexture::BuildTexture( const BuildData& buildData

case GenType_3D:
minMax = genRGB->GenUniformGrid3D( noiseData.data(),
buildData.size.x() / -2, buildData.size.y() / -2, 0,
(int)buildData.offset.x(), (int)buildData.offset.y(), (int)buildData.offset.z(),
buildData.size.x(), buildData.size.y(), 1,
buildData.frequency, buildData.seed );
break;

case GenType_4D:
minMax = genRGB->GenUniformGrid4D( noiseData.data(),
(int)buildData.offset.x(), (int)buildData.offset.y(), (int)buildData.offset.z(), (int)buildData.offset.w(),
buildData.size.x(), buildData.size.y(), 1, 1,
buildData.frequency, buildData.seed );
break;
}

return TextureData( buildData.iteration, buildData.size, minMax, noiseData );
Expand Down
7 changes: 5 additions & 2 deletions NoiseTool/NoiseTexture.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <Magnum/GL/GL.h>
#include <Magnum/GL/Texture.h>
#include <Magnum/ImageView.h>
#include <Magnum/Math/Vector4.h>

#include "FastNoise/FastNoise.h"
#include "MultiThreadQueues.h"
Expand All @@ -22,13 +23,15 @@ namespace Magnum
GenType_2D,
GenType_2DTiled,
GenType_3D,
GenType_4D,
GenType_Count
};

inline static const char* GenTypeStrings =
"2D\0"
"2D Tiled\0"
"3D Slice\0";
"3D Slice\0"
"4D Slice\0";

NoiseTexture();
~NoiseTexture();
Expand All @@ -42,7 +45,7 @@ namespace Magnum
{
FastNoise::SmartNode<const FastNoise::Generator> generator;
Vector2i size;
Vector3 offset;
Vector4 offset;
float frequency;
int32_t seed;
uint64_t iteration;
Expand Down

0 comments on commit 5ffc74b

Please sign in to comment.