From ed4c5cfe05e2b467e9e2de76692c8f1a106c93b9 Mon Sep 17 00:00:00 2001 From: Nikita Gavrilov Date: Sat, 24 Feb 2024 21:27:23 +0100 Subject: [PATCH] feat: remake shaders using shader graph, add masked blend mode --- materials/voxel_map.vmat | 27 ------- materials/voxels_opaque.vmat | 19 +++++ materials/voxels_translucent.vmat | 22 ++++++ prefabs/Chunk.prefab | 4 +- prefabs/VisualOnlyChunk.prefab | 4 +- scenes/main.scene | 4 +- shaders/voxels.shader | 108 --------------------------- shaders/voxels_opaque.shader | 112 ++++++++++++++++++++++++++++ shaders/voxels_opaque.shdrgrph | 43 +++++++++++ shaders/voxels_translucent.shader | 112 ++++++++++++++++++++++++++++ shaders/voxels_translucent.shdrgrph | 47 ++++++++++++ 11 files changed, 361 insertions(+), 141 deletions(-) delete mode 100644 materials/voxel_map.vmat create mode 100644 materials/voxels_opaque.vmat create mode 100644 materials/voxels_translucent.vmat delete mode 100644 shaders/voxels.shader create mode 100644 shaders/voxels_opaque.shader create mode 100644 shaders/voxels_opaque.shdrgrph create mode 100644 shaders/voxels_translucent.shader create mode 100644 shaders/voxels_translucent.shdrgrph diff --git a/materials/voxel_map.vmat b/materials/voxel_map.vmat deleted file mode 100644 index ec1982e..0000000 --- a/materials/voxel_map.vmat +++ /dev/null @@ -1,27 +0,0 @@ -// THIS FILE IS AUTO-GENERATED - -Layer0 -{ - shader "shaders/blendable.shader" - - //---- Texture Filtering ---- - F_TEXTURE_FILTERING 3 // Point Sample - - //---- Fog ---- - g_bFogEnabled "1" - - //---- Material A ---- - g_flTintColorA "[1.000000 1.000000 1.000000 0.000000]" - TextureAmbientOcclusionA "materials/default/default_ao.tga" - TextureBlendMaskA "materials/default/default_blend.tga" - TextureColorA "materials/default/default_color.tga" - TextureMetalnessA "materials/default/default_metal.tga" - TextureNormalA "materials/default/default_normal.tga" - TextureRoughnessA "materials/default/default_rough.tga" - TextureTintMaskA "materials/default/default.tga" - - DynamicParams - { - TextureColorA "color" - } -} \ No newline at end of file diff --git a/materials/voxels_opaque.vmat b/materials/voxels_opaque.vmat new file mode 100644 index 0000000..cc2f10f --- /dev/null +++ b/materials/voxels_opaque.vmat @@ -0,0 +1,19 @@ +// THIS FILE IS AUTO-GENERATED + +Layer0 +{ + shader "shaders/voxels_opaque.shader" + + //---- Texture Filtering ---- + F_TEXTURE_FILTERING 3 // Point Sample + + Texture "materials/lights/white_color.tga" + + //---- Fog ---- + g_bFogEnabled "1" + + DynamicParams + { + Texture "color" + } +} \ No newline at end of file diff --git a/materials/voxels_translucent.vmat b/materials/voxels_translucent.vmat new file mode 100644 index 0000000..5424b85 --- /dev/null +++ b/materials/voxels_translucent.vmat @@ -0,0 +1,22 @@ +// THIS FILE IS AUTO-GENERATED + +Layer0 +{ + shader "shaders/voxels_translucent.shader" + + //---- Texture Filtering ---- + F_TEXTURE_FILTERING 3 // Point Sample + + Texture "materials/default/default_color.tga" + + //---- Fog ---- + g_bFogEnabled "1" + + //---- Translucent ---- + g_flOpacityScale "1.000" + + DynamicParams + { + Texture "color" + } +} \ No newline at end of file diff --git a/prefabs/Chunk.prefab b/prefabs/Chunk.prefab index 1321b68..cf4a493 100644 --- a/prefabs/Chunk.prefab +++ b/prefabs/Chunk.prefab @@ -27,7 +27,7 @@ "component_type": "ModelCollider" }, "InteractionEnabled": true, - "OpaqueVoxelsMaterial": "materials/voxel_map.vmat", + "OpaqueVoxelsMaterial": "materials/voxels_opaque.vmat", "PhysicsCollider": { "_type": "component", "go": "1ce9c04a-c4c3-4e7b-864d-39896d8ed47a", @@ -35,7 +35,7 @@ }, "PhysicsEnabled": true, "RenderingEnabled": true, - "TranslucentVoxelsMaterial": "materials/voxel_map.vmat" + "TranslucentVoxelsMaterial": "materials/voxels_translucent.vmat" }, { "__type": "ModelCollider", diff --git a/prefabs/VisualOnlyChunk.prefab b/prefabs/VisualOnlyChunk.prefab index 317ee70..3e82759 100644 --- a/prefabs/VisualOnlyChunk.prefab +++ b/prefabs/VisualOnlyChunk.prefab @@ -27,7 +27,7 @@ "component_type": "ModelCollider" }, "InteractionEnabled": false, - "OpaqueVoxelsMaterial": "materials/voxel_map.vmat", + "OpaqueVoxelsMaterial": "materials/voxels_opaque.vmat", "PhysicsCollider": { "_type": "component", "go": "faa05274-45f8-4747-b98a-6132ac09b0e5", @@ -35,7 +35,7 @@ }, "PhysicsEnabled": false, "RenderingEnabled": true, - "TranslucentVoxelsMaterial": "materials/voxel_map.vmat" + "TranslucentVoxelsMaterial": "materials/voxels_translucent.vmat" }, { "__type": "ModelCollider", diff --git a/scenes/main.scene b/scenes/main.scene index 6ca4aa0..5f88336 100644 --- a/scenes/main.scene +++ b/scenes/main.scene @@ -571,7 +571,7 @@ "__type": "ModelRenderer", "BodyGroups": 18446744073709551615, "MaterialGroup": null, - "MaterialOverride": "materials/voxel_map.vmat", + "MaterialOverride": "materials/voxels_translucent.vmat", "Model": "models/dev/plane.vmdl", "RenderType": "On", "Tint": "1,1,1,1" @@ -610,6 +610,6 @@ }, "Title": "main", "Description": "", - "LastSaved": "2024-02-24T02:12:52.7880785\u002B01:00", + "LastSaved": "2024-02-24T20:41:15.2700211\u002B01:00", "__references": [] } \ No newline at end of file diff --git a/shaders/voxels.shader b/shaders/voxels.shader deleted file mode 100644 index 4a6c1ee..0000000 --- a/shaders/voxels.shader +++ /dev/null @@ -1,108 +0,0 @@ -HEADER -{ - Description = "Voxel Shader"; - Version = 1; -} - -FEATURES -{ - #include "common/features.hlsl" - - Feature( F_ALPHA_TEST, 0..1, "Translucent" ); - Feature( F_TRANSLUCENT, 0..1, "Translucent" ); - FeatureRule( Allow1( F_TRANSLUCENT, F_ALPHA_TEST ), "Translucent and Alpha Test are not compatible" ); - Feature( F_PREPASS_ALPHA_TEST, 0..1, "Translucent" ); -} - -MODES -{ - VrForward(); // Indicates this shader will be used for main rendering - ToolsVis( S_MODE_TOOLS_VIS ); // Ability to see in the editor - ToolsWireframe(S_MODE_TOOLS_WIREFRAME ); // Allows for mat_wireframe to work - //ToolsShadingComplexity("tools_shading_complexity.shader"); // Shows how expensive drawing is in debug view - Depth( S_MODE_DEPTH ); -} - -COMMON -{ - //#include "common/shared.hlsl" - - #define VS_INPUT_HAS_TANGENT_BASIS 1 - #define PS_INPUT_HAS_TANGENT_BASIS 1 - - #include "system.fxc" // This should always be the first include in COMMON - #include "sbox_shared.fxc" - float g_flVoxelSize < Default( 32.0 ); >; - - StaticCombo( S_ALPHA_TEST, F_ALPHA_TEST, Sys( ALL ) ); - StaticCombo( S_TRANSLUCENT, F_TRANSLUCENT, Sys( ALL ) ); -} - -struct VertexInput -{ - #include "common/vertexinput.hlsl" -}; - -struct PixelInput -{ - #include "common/pixelinput.hlsl" -}; - -VS -{ - #include "common/vertex.hlsl" - - PixelInput MainVs( VertexInput v ) - { - PixelInput i = ProcessVertex( v ); - return FinalizeVertex(i); - } -} - -PS -{ - - #define S_NON_DIRECTIONAL_DIFFUSE_LIGHTING 1 - float g_flTextureAtlasCellSize< Default(64.0f); >; - StaticCombo( S_MODE_DEPTH, 0..1, Sys( ALL ) ); - StaticCombo( S_MODE_TOOLS_WIREFRAME, 0..1, Sys( ALL ) ); - StaticCombo( S_DO_NOT_CAST_SHADOWS, F_DO_NOT_CAST_SHADOWS, Sys( ALL ) ); - - #if ( S_MODE_TOOLS_WIREFRAME ) - RenderState( FillMode, WIREFRAME ); - RenderState( SlopeScaleDepthBias, -0.5 ); - RenderState( DepthBiasClamp, -0.0005 ); - RenderState( DepthWriteEnable, false ); - #define DEPTH_STATE_ALREADY_SET - #endif - - #include "common/pixel.hlsl" - - - float g_flVoxelOpacity< Range(0.0f, 1.0f); Default(1.0f); >; - - //StaticCombo( S_ALPHA_TEST, F_ALPHA_TEST, Sys( ALL ) ); - - //RenderState( AlphaTestEnable, false ); - //RenderState(BlendEnable, false); - //RenderState(SrcBlend, SRC_ALPHA); - //RenderState(DstBlend, INV_SRC_ALPHA); - //RenderState(BlendOpAlpha, ADD); - //RenderState(SrcBlendAlpha, SRC_ALPHA); - //RenderState(DstBlendAlpha, INV_SRC_ALPHA); - //RenderState(AlphaTestFunc, LESS_EQUAL); - - //SamplerState g_sSampler0 < Filter( POINT ); AddressU( WRAP ); AddressV( WRAP ); >; - //CreateInputTexture2D( Texture_ps_0, Srgb, 8, "None", "_color", ",0/,0/0", Default4( 1.00, 1.00, 1.00, 1.00 ) ); - //Texture2D g_tTexture_ps_0 < Channel( RGBA, Box( Texture_ps_0 ), Srgb ); OutputFormat( RGBA8888 ); SrgbRead( True ); >; - - - float4 MainPs( PixelInput i ) : SV_Target0 - { - - Material m = Material::From( i ); - float4 color = ShadingModelStandard::Shade( i, m ); - //color.a = i.vVertexColor.a; - return color; - } -} diff --git a/shaders/voxels_opaque.shader b/shaders/voxels_opaque.shader new file mode 100644 index 0000000..4bab84e --- /dev/null +++ b/shaders/voxels_opaque.shader @@ -0,0 +1,112 @@ + +HEADER +{ + Description = ""; +} + +FEATURES +{ + #include "common/features.hlsl" +} + +MODES +{ + VrForward(); + Depth(); + ToolsVis( S_MODE_TOOLS_VIS ); + ToolsWireframe( "vr_tools_wireframe.shader" ); + ToolsShadingComplexity( "tools_shading_complexity.shader" ); +} + +COMMON +{ + #ifndef S_ALPHA_TEST + #define S_ALPHA_TEST 0 + #endif + #ifndef S_TRANSLUCENT + #define S_TRANSLUCENT 0 + #endif + + #include "common/shared.hlsl" + #include "procedural.hlsl" + + #define S_UV2 1 + #define CUSTOM_MATERIAL_INPUTS +} + +struct VertexInput +{ + #include "common/vertexinput.hlsl" + float4 vColor : COLOR0 < Semantic( Color ); >; +}; + +struct PixelInput +{ + #include "common/pixelinput.hlsl" + float3 vPositionOs : TEXCOORD14; + float3 vNormalOs : TEXCOORD15; + float4 vTangentUOs_flTangentVSign : TANGENT < Semantic( TangentU_SignV ); >; + float4 vColor : COLOR0; +}; + +VS +{ + #include "common/vertex.hlsl" + + PixelInput MainVs( VertexInput v ) + { + PixelInput i = ProcessVertex( v ); + i.vPositionOs = v.vPositionOs.xyz; + i.vColor = v.vColor; + + VS_DecodeObjectSpaceNormalAndTangent( v, i.vNormalOs, i.vTangentUOs_flTangentVSign ); + + return FinalizeVertex( i ); + } +} + +PS +{ + #include "common/pixel.hlsl" + + SamplerState g_sSampler0 < Filter( POINT ); AddressU( CLAMP ); AddressV( CLAMP ); >; + CreateInputTexture2D( Texture, Srgb, 8, "None", "_color", ",0/,0/0", Default4( 1.00, 1.00, 1.00, 1.00 ) ); + Texture2D g_tTexture < Channel( RGBA, Box( Texture ), Srgb ); OutputFormat( DXT5 ); SrgbRead( True ); >; + + float4 MainPs( PixelInput i ) : SV_Target0 + { + Material m = Material::Init(); + m.Albedo = float3( 1, 1, 1 ); + m.Normal = float3( 0, 0, 1 ); + m.Roughness = 1; + m.Metalness = 0; + m.AmbientOcclusion = 1; + m.TintMask = 1; + m.Opacity = 1; + m.Emission = float3( 0, 0, 0 ); + m.Transmission = 0; + + float4 l_0 = Tex2DS( g_tTexture, g_sSampler0, i.vTextureCoords.xy ); + + m.Albedo = l_0.xyz; + m.Opacity = 1; + m.Roughness = 1; + m.Metalness = 0; + m.AmbientOcclusion = 1; + + m.AmbientOcclusion = saturate( m.AmbientOcclusion ); + m.Roughness = saturate( m.Roughness ); + m.Metalness = saturate( m.Metalness ); + m.Opacity = saturate( m.Opacity ); + + // Result node takes normal as tangent space, convert it to world space now + m.Normal = TransformNormal( m.Normal, i.vNormalWs, i.vTangentUWs, i.vTangentVWs ); + + // for some toolvis shit + m.WorldTangentU = i.vTangentUWs; + m.WorldTangentV = i.vTangentVWs; + m.TextureCoords = i.vTextureCoords.xy; + + return ShadingModelStandard::Shade( i, m ); + } +} diff --git a/shaders/voxels_opaque.shdrgrph b/shaders/voxels_opaque.shdrgrph new file mode 100644 index 0000000..2efeeb9 --- /dev/null +++ b/shaders/voxels_opaque.shdrgrph @@ -0,0 +1,43 @@ +{ + "Model": "models/dev/plane.vmdl", + "Description": "", + "BlendMode": "Opaque", + "nodes": [ + { + "_class": "Result", + "DefaultOpacity": 1, + "DefaultRoughness": 1, + "DefaultMetalness": 0, + "DefaultAmbientOcclusion": 1, + "Identifier": "0", + "Position": "592,-96", + "Albedo": { + "Identifier": "1", + "Output": "Result" + } + }, + { + "_class": "TextureSampler", + "Image": "materials/lights/white_color.tga", + "Sampler": { + "Filter": "Point", + "AddressU": "Clamp", + "AddressV": "Clamp" + }, + "UI": { + "Name": "Texture", + "Default": "1,1,1,1", + "SrgbRead": true, + "PrimaryGroup": { + "Name": "" + }, + "SecondaryGroup": { + "Name": "" + }, + "CreateInput": "CreateInputTexture2D" + }, + "Identifier": "1", + "Position": "352,-96" + } + ] +} \ No newline at end of file diff --git a/shaders/voxels_translucent.shader b/shaders/voxels_translucent.shader new file mode 100644 index 0000000..2206b95 --- /dev/null +++ b/shaders/voxels_translucent.shader @@ -0,0 +1,112 @@ + +HEADER +{ + Description = ""; +} + +FEATURES +{ + #include "common/features.hlsl" +} + +MODES +{ + VrForward(); + Depth(); + ToolsVis( S_MODE_TOOLS_VIS ); + ToolsWireframe( "vr_tools_wireframe.shader" ); + ToolsShadingComplexity( "tools_shading_complexity.shader" ); +} + +COMMON +{ + #ifndef S_ALPHA_TEST + #define S_ALPHA_TEST 1 + #endif + #ifndef S_TRANSLUCENT + #define S_TRANSLUCENT 0 + #endif + + #include "common/shared.hlsl" + #include "procedural.hlsl" + + #define S_UV2 1 + #define CUSTOM_MATERIAL_INPUTS +} + +struct VertexInput +{ + #include "common/vertexinput.hlsl" + float4 vColor : COLOR0 < Semantic( Color ); >; +}; + +struct PixelInput +{ + #include "common/pixelinput.hlsl" + float3 vPositionOs : TEXCOORD14; + float3 vNormalOs : TEXCOORD15; + float4 vTangentUOs_flTangentVSign : TANGENT < Semantic( TangentU_SignV ); >; + float4 vColor : COLOR0; +}; + +VS +{ + #include "common/vertex.hlsl" + + PixelInput MainVs( VertexInput v ) + { + PixelInput i = ProcessVertex( v ); + i.vPositionOs = v.vPositionOs.xyz; + i.vColor = v.vColor; + + VS_DecodeObjectSpaceNormalAndTangent( v, i.vNormalOs, i.vTangentUOs_flTangentVSign ); + + return FinalizeVertex( i ); + } +} + +PS +{ + #include "common/pixel.hlsl" + + SamplerState g_sSampler0 < Filter( POINT ); AddressU( CLAMP ); AddressV( CLAMP ); >; + CreateInputTexture2D( Texture, Srgb, 8, "None", "_color", ",0/,0/0", Default4( 1.00, 1.00, 1.00, 1.00 ) ); + Texture2D g_tTexture < Channel( RGBA, Box( Texture ), Srgb ); OutputFormat( DXT5 ); SrgbRead( True ); >; + + float4 MainPs( PixelInput i ) : SV_Target0 + { + Material m = Material::Init(); + m.Albedo = float3( 1, 1, 1 ); + m.Normal = float3( 0, 0, 1 ); + m.Roughness = 1; + m.Metalness = 0; + m.AmbientOcclusion = 1; + m.TintMask = 1; + m.Opacity = 1; + m.Emission = float3( 0, 0, 0 ); + m.Transmission = 0; + + float4 l_0 = Tex2DS( g_tTexture, g_sSampler0, i.vTextureCoords.xy ); + + m.Albedo = l_0.xyz; + m.Opacity = l_0.a; + m.Roughness = 1; + m.Metalness = 0; + m.AmbientOcclusion = 1; + + m.AmbientOcclusion = saturate( m.AmbientOcclusion ); + m.Roughness = saturate( m.Roughness ); + m.Metalness = saturate( m.Metalness ); + m.Opacity = saturate( m.Opacity ); + + // Result node takes normal as tangent space, convert it to world space now + m.Normal = TransformNormal( m.Normal, i.vNormalWs, i.vTangentUWs, i.vTangentVWs ); + + // for some toolvis shit + m.WorldTangentU = i.vTangentUWs; + m.WorldTangentV = i.vTangentVWs; + m.TextureCoords = i.vTextureCoords.xy; + + return ShadingModelStandard::Shade( i, m ); + } +} diff --git a/shaders/voxels_translucent.shdrgrph b/shaders/voxels_translucent.shdrgrph new file mode 100644 index 0000000..0fbf8c7 --- /dev/null +++ b/shaders/voxels_translucent.shdrgrph @@ -0,0 +1,47 @@ +{ + "Model": "models/dev/plane.vmdl", + "Description": "", + "BlendMode": "Masked", + "nodes": [ + { + "_class": "Result", + "DefaultOpacity": 1, + "DefaultRoughness": 1, + "DefaultMetalness": 0, + "DefaultAmbientOcclusion": 1, + "Identifier": "0", + "Position": "432,-112", + "Albedo": { + "Identifier": "1", + "Output": "Result" + }, + "Opacity": { + "Identifier": "1", + "Output": "A" + } + }, + { + "_class": "TextureSampler", + "Image": "materials/lights/white_color.tga", + "Sampler": { + "Filter": "Point", + "AddressU": "Clamp", + "AddressV": "Clamp" + }, + "UI": { + "Name": "Texture", + "Default": "1,1,1,1", + "SrgbRead": true, + "PrimaryGroup": { + "Name": "" + }, + "SecondaryGroup": { + "Name": "" + }, + "CreateInput": "CreateInputTexture2D" + }, + "Identifier": "1", + "Position": "112,-112" + } + ] +} \ No newline at end of file