Skip to content

Commit

Permalink
➕ Addition Node & Sample Node
Browse files Browse the repository at this point in the history
  • Loading branch information
wobbier committed Mar 5, 2024
1 parent 36d3656 commit 64c0659
Show file tree
Hide file tree
Showing 13 changed files with 413 additions and 58 deletions.
14 changes: 10 additions & 4 deletions Modules/Moonlight/Source/Graphics/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,22 @@ namespace Moonlight
, Tiling( 1.f, 1.f )
, TypeName( MaterialTypeName )
{
if( ShaderPath.length() > 0 )
{
MeshShader = ShaderCommand( ShaderPath );
}
LoadShader( ShaderPath );
}

Material::~Material()
{
}

void Material::LoadShader( const std::string& inShaderName )
{
ShaderName = inShaderName;
if( inShaderName.length() > 0 )
{
MeshShader = ShaderCommand( inShaderName );
}
}

const bool Material::IsTransparent() const
{
if( RenderMode == RenderingMode::Transparent )
Expand Down
3 changes: 3 additions & 0 deletions Modules/Moonlight/Source/Graphics/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ namespace Moonlight
Material() = delete;
virtual ~Material();

void LoadShader( const std::string& inShaderName );

const bool IsTransparent() const;
void SetRenderMode( RenderingMode newMode );

Expand All @@ -76,6 +78,7 @@ namespace Moonlight
RenderingMode RenderMode = RenderingMode::Opaque;
Vector3 DiffuseColor;
Vector2 Tiling;
std::string ShaderName;

Moonlight::ShaderCommand MeshShader;
const std::string& GetTypeName() const;
Expand Down
1 change: 1 addition & 0 deletions Modules/Moonlight/Source/Graphics/ShaderFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ struct ShaderFileMetadata
//
std::string nameNoExt = fileName.substr( 0, fileName.rfind( "." ) );
std::string progArgs = "-f \"" + localFolder + fileName + "\"" + " -o \"" + localFolder + fileName + "." + GetExtension2() + "\" --varyingdef \"" + localFolder + nameNoExt + ".var\" --platform windows -p " + shaderType + " --type " + exportType;
progArgs += " -i \"" + Path( "Engine/Assets/Shaders" ).FullPath + "\"";
// ./shaderc -f ../../../Assets/Shaders/vs_cubes.shader -o ../../../Assets/Shaders/dummy.bin --varyingdef ./varying.def.sc --platform windows -p vs_5_0 --type vertex

PlatformUtils::SystemCall( shadercPath, progArgs );
Expand Down
35 changes: 35 additions & 0 deletions Modules/Moonlight/Source/Materials/DiffuseMaterial.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,40 @@ class DiffuseMaterial
bgfx::UniformHandle s_diffuse;
bgfx::UniformHandle s_tiling;
};
class ShaderGraphMaterial
: public Moonlight::Material
{
public:
ShaderGraphMaterial()
: Moonlight::Material( "ShaderGraphMaterial" )
, s_diffuse( BGFX_INVALID_HANDLE )
, s_tiling( BGFX_INVALID_HANDLE )
{

}

void Init() override
{
s_diffuse = bgfx::createUniform( "s_diffuse", bgfx::UniformType::Vec4 );
s_tiling = bgfx::createUniform( "s_tiling", bgfx::UniformType::Vec4 );
}

virtual void Use() final
{
bgfx::setUniform( s_diffuse, &DiffuseColor.x );
bgfx::setUniform( s_tiling, &Tiling.x );
}

SharedPtr<Material> CreateInstance() final
{
SharedPtr<ShaderGraphMaterial> ptr = MakeShared<ShaderGraphMaterial>( *this );

return ptr;
}
private:
bgfx::UniformHandle s_diffuse;
bgfx::UniformHandle s_tiling;
};

class WhiteMaterial
: public Moonlight::Material
Expand Down Expand Up @@ -71,4 +105,5 @@ class WhiteMaterial
};

ME_REGISTER_MATERIAL_NAME( DiffuseMaterial, "Diffuse" )
ME_REGISTER_MATERIAL_NAME( ShaderGraphMaterial, "ShaderGraphMaterial" )
ME_REGISTER_MATERIAL_NAME( WhiteMaterial, "White" )
18 changes: 18 additions & 0 deletions Source/Components/Graphics/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,24 @@ void Mesh::OnEditorInspect()

if( MeshMaterial )
{
if( ImGui::Button( "Select Shader" ) )
{
RequestAssetSelectionEvent evt( [this]( Path selectedAsset ) {
std::string path = selectedAsset.GetLocalPathString();
size_t pos = path.rfind( selectedAsset.GetExtension() );
if( pos != std::string::npos ) {
path.erase( pos-1, path.length() );
}
MeshMaterial->LoadShader( path );
}, AssetType::Shader );
evt.Fire();
}

if( MeshMaterial->MeshShader.IsLoaded() && ImGui::Button( "Reload Shader" ) )
{
MeshMaterial->LoadShader( MeshMaterial->ShaderName );
}

bool transparent = MeshMaterial->IsTransparent();
//if (ImGui::TreeNodeEx("Material", ImGuiTreeNodeFlags_DefaultOpen))
{
Expand Down
18 changes: 17 additions & 1 deletion Tools/ShaderEditor/Source/Core/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include "File.h"
#include "Math\Vector3.h"
#include "ShaderWriter.h"
#include "Pointers.h"
#include "Math\Vector4.h"
#include "Math\Vector2.h"

namespace ed = ax::NodeEditor;
namespace util = ax::NodeEditor::Utilities;
Expand All @@ -22,6 +25,8 @@ enum class PinType
Object,
Function,
Delegate,
Texture,
Vector2,
Vector3Type,
Vector4,
};
Expand All @@ -42,8 +47,9 @@ enum class NodeType
};

struct Node;
namespace Moonlight { class Texture; }

using PinData = std::variant<int, bool, float, Vector3>;
using PinData = std::variant<int, bool, float, Vector2, Vector3, Vector4, SharedPtr<Moonlight::Texture>>;

struct Pin
{
Expand Down Expand Up @@ -108,6 +114,16 @@ struct Node
virtual void OnExport( ShaderWriter& inFile )
{
}

bool ExportLinkedPin( int inPinNumber, ShaderWriter& inFile )
{
if( Inputs[inPinNumber].LinkedInput )
{
Inputs[inPinNumber].LinkedInput->Node->OnExport( inFile );
return true;
}
return false;
}
};

struct Link
Expand Down
22 changes: 21 additions & 1 deletion Tools/ShaderEditor/Source/Core/ShaderWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

ShaderWriter::ShaderWriter( const std::string& inShaderName )
{
m_shaderFile = File( Path( inShaderName ) );
m_shaderFile = File( Path( "../../../Assets/Shaders/" + inShaderName));
m_shaderFile.Reset();
}

Expand Down Expand Up @@ -31,3 +31,23 @@ void ShaderWriter::WriteToDisk()
m_shaderFile.Write();
}

void ShaderWriter::WriteVector( Vector3 inValue )
{
std::string var = "v3_" + std::to_string( ID++ );
WriteLine( "vec3 " + var + " = vec3( " + std::to_string( inValue.x ) + ", " + std::to_string( inValue.y ) + ", " + std::to_string( inValue.z ) + " );" );
LastVariable = var;
}

void ShaderWriter::WriteFloat( float inValue )
{
std::string var = "f_" + std::to_string( ID++ );
WriteLine( "float " + var + " = " + std::to_string( inValue ) + ";" );
LastVariable = var;
}

void ShaderWriter::WriteInt( int inValue )
{
std::string var = "i_" + std::to_string( ID++ );
WriteLine( "float " + var + " = " + std::to_string( inValue ) + ";" );
LastVariable = var;
}
5 changes: 5 additions & 0 deletions Tools/ShaderEditor/Source/Core/ShaderWriter.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include <string>
#include "File.h"
#include "Math\Vector3.h"

class ShaderWriter
{
Expand All @@ -12,6 +13,10 @@ class ShaderWriter
void Append( const std::string& inText );
void WriteToDisk();

void WriteVector( Vector3 inValue );
void WriteFloat( float inValue );
void WriteInt( int inValue );

int Tabs = 0;

std::string LastVariable;
Expand Down
Loading

0 comments on commit 64c0659

Please sign in to comment.