diff --git a/Assets/Materials/Shaders/BasicVoxelShader.shader b/Assets/Materials/Shaders/BasicVoxelShader.shader index 54cce939..7c4f0280 100644 --- a/Assets/Materials/Shaders/BasicVoxelShader.shader +++ b/Assets/Materials/Shaders/BasicVoxelShader.shader @@ -8,7 +8,7 @@ Shader "Custom/BasicVoxelShader" SubShader { - //Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "IgnoreProjector" = "True"} + Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "IgnoreProjector" = "True"} Pass { Name "Forward" @@ -21,8 +21,8 @@ Shader "Custom/BasicVoxelShader" Cull[_Cull] AlphaToMask[_AlphaToMask]*/ - CGPROGRAM - #pragma target 3.5 + HLSLPROGRAM + #pragma target 4.5 // use "vert" function as the vertex shader #pragma vertex VoxelForwardVertex // use "frag" function as the pixel (fragment) shader @@ -30,8 +30,6 @@ Shader "Custom/BasicVoxelShader" // texture arrays are not available everywhere, // only compile shader on platforms where they are #pragma require 2darray - - #include "UnityCG.cginc" /* GPU Instancing #pragma multi_compile_instancing @@ -41,20 +39,18 @@ Shader "Custom/BasicVoxelShader" //#pragma multi_compile_instancing #pragma multi_compile_instancing #pragma multi_compile _ DOTS_INSTANCING_ON - //#include_with_pragmas "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DOTS.hlsl - //#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl" - - //#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" - - //#include - //#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" - - //#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" + //#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl" + #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" + #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderVariablesFunctions.hlsl" + #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl" //uniform float _uvSizes[2 * 6]; // Needs to be set from C# + + struct Attributes { int packedData : POSITION; + UNITY_VERTEX_INPUT_INSTANCE_ID }; struct Varyings @@ -63,14 +59,28 @@ Shader "Custom/BasicVoxelShader" //float3 positionWS : TEXCOORD1; half3 normalWS : TEXCOORD1; float4 positionHCS : SV_POSITION; + UNITY_VERTEX_INPUT_INSTANCE_ID }; - + + #ifdef DOTS_INSTANCING_ON + UNITY_DOTS_INSTANCING_START(MaterialPropertyMetadata) + UNITY_DOTS_INSTANCED_PROP(float4, _Color) + UNITY_DOTS_INSTANCING_END(MaterialPropertyMetadata) + #define _Color UNITY_ACCESS_DOTS_INSTANCED_PROP_WITH_DEFAULT(float4, _Color) + #endif + Varyings VoxelForwardVertex(Attributes input) { + Varyings output = (Varyings)0; - + + UNITY_SETUP_INSTANCE_ID(input); + UNITY_TRANSFER_INSTANCE_ID(input, output); + + //float3 positionOS = float3(float(input.packedData&(255)), float((input.packedData >> 8)&(255)), float((input.packedData >> 16)&(255))); float4 positionOS = float4(float(input.packedData&(255)), float((input.packedData >> 8)&(255)), float((input.packedData >> 16)&(255)), 1); int normal = int((input.packedData >> 29)&(7)); + //float3 normalOS; float4 normalOS; if(normal == 0) @@ -115,21 +125,22 @@ Shader "Custom/BasicVoxelShader" //output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap); //output.positionWS.xyz = vertexInput.positionWS; - output.positionHCS = UnityObjectToClipPos(positionOS.xyz); + output.positionHCS = GetVertexPositionInputs(positionOS.xyz).positionCS; output.normalWS = normalOS; return output; } - UNITY_DECLARE_TEX2DARRAY(_ColourTextures); + //UNITY_DECLARE_TEX2DARRAY(_ColourTextures); half4 VoxelForwardFragment(Varyings input) : SV_Target0 { - return half4(UNITY_SAMPLE_TEX2DARRAY(_ColourTextures, input.uv).rgb, 1.0); + UNITY_SETUP_INSTANCE_ID(input); + return (1,1,1,1); } - ENDCG + ENDHLSL } diff --git a/Assets/Materials/Shaders/MyVoxelForwardPassVR.hlsl b/Assets/Materials/Shaders/MyVoxelForwardPassVR.hlsl new file mode 100644 index 00000000..3278d550 --- /dev/null +++ b/Assets/Materials/Shaders/MyVoxelForwardPassVR.hlsl @@ -0,0 +1,243 @@ +#ifndef UNIVERSAL_SIMPLE_LIT_PASS_INCLUDED +#define UNIVERSAL_SIMPLE_LIT_PASS_INCLUDED + +//#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" +#if defined(LOD_FADE_CROSSFADE) + #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/LODCrossFade.hlsl" +#endif + +struct Attributes +{ + int packedData : POSITION; + //float3 normalOS : NORMAL; + //float4 tangentOS : TANGENT; + float2 texcoord : TEXCOORD0; + float2 staticLightmapUV : TEXCOORD1; + float2 dynamicLightmapUV : TEXCOORD2; + UNITY_VERTEX_INPUT_INSTANCE_ID +}; + +struct Varyings +{ + float3 uv : TEXCOORD0; + + float3 positionWS : TEXCOORD1; // xyz: posWS + + #ifdef _NORMALMAP + half4 normalWS : TEXCOORD2; // xyz: normal, w: viewDir.x + half4 tangentWS : TEXCOORD3; // xyz: tangent, w: viewDir.y + half4 bitangentWS : TEXCOORD4; // xyz: bitangent, w: viewDir.z + #else + half3 normalWS : TEXCOORD2; + #endif + + #ifdef _ADDITIONAL_LIGHTS_VERTEX + half4 fogFactorAndVertexLight : TEXCOORD5; // x: fogFactor, yzw: vertex light + #else + half fogFactor : TEXCOORD5; + #endif + + #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR) + float4 shadowCoord : TEXCOORD6; + #endif + + DECLARE_LIGHTMAP_OR_SH(staticLightmapUV, vertexSH, 7); + +#ifdef DYNAMICLIGHTMAP_ON + float2 dynamicLightmapUV : TEXCOORD8; // Dynamic lightmap UVs +#endif + + float4 positionCS : SV_POSITION; + UNITY_VERTEX_INPUT_INSTANCE_ID + UNITY_VERTEX_OUTPUT_STEREO +}; + +void InitializeInputData(Varyings input, half3 normalTS, out InputData inputData) +{ + inputData = (InputData)0; + + inputData.positionWS = input.positionWS; + + #ifdef _NORMALMAP + half3 viewDirWS = half3(input.normalWS.w, input.tangentWS.w, input.bitangentWS.w); + inputData.tangentToWorld = half3x3(input.tangentWS.xyz, input.bitangentWS.xyz, input.normalWS.xyz); + inputData.normalWS = TransformTangentToWorld(normalTS, inputData.tangentToWorld); + #else + half3 viewDirWS = GetWorldSpaceNormalizeViewDir(inputData.positionWS); + inputData.normalWS = input.normalWS; + #endif + + inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS); + viewDirWS = SafeNormalize(viewDirWS); + + inputData.viewDirectionWS = viewDirWS; + + #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR) + inputData.shadowCoord = input.shadowCoord; + #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS) + inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS); + #else + inputData.shadowCoord = float4(0, 0, 0, 0); + #endif + + #ifdef _ADDITIONAL_LIGHTS_VERTEX + inputData.fogCoord = InitializeInputDataFog(float4(inputData.positionWS, 1.0), input.fogFactorAndVertexLight.x); + inputData.vertexLighting = input.fogFactorAndVertexLight.yzw; + #else + inputData.fogCoord = InitializeInputDataFog(float4(inputData.positionWS, 1.0), input.fogFactor); + inputData.vertexLighting = half3(0, 0, 0); + #endif + +#if defined(DYNAMICLIGHTMAP_ON) + inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.dynamicLightmapUV, input.vertexSH, inputData.normalWS); +#else + inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.vertexSH, inputData.normalWS); +#endif + + inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS); + inputData.shadowMask = SAMPLE_SHADOWMASK(input.staticLightmapUV); + + #if defined(DEBUG_DISPLAY) + #if defined(DYNAMICLIGHTMAP_ON) + inputData.dynamicLightmapUV = input.dynamicLightmapUV.xy; + #endif + #if defined(LIGHTMAP_ON) + inputData.staticLightmapUV = input.staticLightmapUV; + #else + inputData.vertexSH = input.vertexSH; + #endif + #endif +} + +/////////////////////////////////////////////////////////////////////////////// +// Vertex and Fragment functions // +/////////////////////////////////////////////////////////////////////////////// + +Varyings VoxelForwardVertex(Attributes input) +{ + Varyings output = (Varyings)0; + + UNITY_SETUP_INSTANCE_ID(input); + UNITY_TRANSFER_INSTANCE_ID(input, output); + UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output); + + float3 positionOS = float3(float(input.packedData&(255)), float((input.packedData >> 8)&(255)), float((input.packedData >> 16)&(255))); + int normal = int((input.packedData >> 29)&(7)); + float3 normalOS; + if(normal == 0) + { + normalOS = float4(0,1,0,0); + } + else if(normal == 1) + { + normalOS= float4(0,-1,0,0); + } + else if(normal == 2) + { + normalOS = float4(1,0,0,0); + } + else if(normal == 3) + { + normalOS = float4(-1,0,0,0); + } + else if(normal == 4) + { + normalOS = float4(0,0,1,0); + } + else + { + normalOS = float4(0,0,-1,0); + } + float4 tangentOS = float4(0,0,0,1); + VertexPositionInputs vertexInput = GetVertexPositionInputs(positionOS); + VertexNormalInputs normalInput = GetVertexNormalInputs(normalOS, tangentOS); + + // 5 bits for textureID + output.uv.z = int((input.packedData >> 24)&(31)); + if (normal < 2) + { + output.uv.xy = positionOS.xz * float2(_uvSizes[output.uv.z], _uvSizes[output.uv.z+1]); + } + else + { + output.uv.xy = (normal < 4 ? positionOS.zy : positionOS.xy) * float2(_uvSizes[output.uv.z], _uvSizes[output.uv.z+1]); + } + +#if defined(_FOG_FRAGMENT) + half fogFactor = 0; +#else + half fogFactor = ComputeFogFactor(vertexInput.positionCS.z); +#endif + + //output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap); + output.positionWS.xyz = vertexInput.positionWS; + output.positionCS = vertexInput.positionCS; + +#ifdef _NORMALMAP + half3 viewDirWS = GetWorldSpaceViewDir(vertexInput.positionWS); + output.normalWS = half4(normalInput.normalWS, viewDirWS.x); + output.tangentWS = half4(normalInput.tangentWS, viewDirWS.y); + output.bitangentWS = half4(normalInput.bitangentWS, viewDirWS.z); +#else + output.normalWS = NormalizeNormalPerVertex(normalInput.normalWS); +#endif + + OUTPUT_LIGHTMAP_UV(input.staticLightmapUV, unity_LightmapST, output.staticLightmapUV); +#ifdef DYNAMICLIGHTMAP_ON + output.dynamicLightmapUV = input.dynamicLightmapUV.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw; +#endif + OUTPUT_SH(output.normalWS.xyz, output.vertexSH); + + #ifdef _ADDITIONAL_LIGHTS_VERTEX + half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS); + output.fogFactorAndVertexLight = half4(fogFactor, vertexLight); + #else + output.fogFactor = fogFactor; + #endif + + #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR) + output.shadowCoord = GetShadowCoord(vertexInput); + #endif + + return output; +} + +void VoxelForwardFragment( + Varyings input + , out half4 outColor : SV_Target0 +#ifdef _WRITE_RENDERING_LAYERS + , out float4 outRenderingLayers : SV_Target1 +#endif +) +{ + UNITY_SETUP_INSTANCE_ID(input); + UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input); + + SurfaceData surfaceData; + InitializeSimpleLitSurfaceData(input.uv, surfaceData); + +#ifdef LOD_FADE_CROSSFADE + LODFadeCrossFade(input.positionCS); +#endif + + InputData inputData; + InitializeInputData(input, surfaceData.normalTS, inputData); + SETUP_DEBUG_TEXTURE_DATA(inputData, input.uv, _BaseMap); + +#ifdef _DBUFFER + ApplyDecalToSurfaceData(input.positionCS, surfaceData, inputData); +#endif + + half4 color = UniversalFragmentBlinnPhong(inputData, surfaceData); + color.rgb = MixFog(color.rgb, inputData.fogCoord); + color.a = OutputAlpha(color.a, IsSurfaceTypeTransparent(_Surface)); + + outColor = color; + +#ifdef _WRITE_RENDERING_LAYERS + uint renderingLayers = GetMeshRenderingLayer(); + outRenderingLayers = float4(EncodeMeshRenderingLayer(renderingLayers), 0, 0, 0); +#endif +} + +#endif diff --git a/Assets/Materials/Shaders/MyVoxelShaderInputVR.hlsl b/Assets/Materials/Shaders/MyVoxelShaderInputVR.hlsl new file mode 100644 index 00000000..8179fcf2 --- /dev/null +++ b/Assets/Materials/Shaders/MyVoxelShaderInputVR.hlsl @@ -0,0 +1,68 @@ +//#ifndef UNIVERSAL_SIMPLE_LIT_INPUT_INCLUDED +//#define UNIVERSAL_SIMPLE_LIT_INPUT_INCLUDED + +//#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" +#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceInput.hlsl" + +CBUFFER_START(UnityPerMaterial) + half4 _BaseColor; + half4 _SpecColor; + half4 _EmissionColor; + half _Cutoff; + half _Surface; +CBUFFER_END + +#ifdef UNITY_DOTS_INSTANCING_ENABLED + UNITY_DOTS_INSTANCING_START(MaterialPropertyMetadata) + UNITY_DOTS_INSTANCED_PROP(float4, _BaseColor) + UNITY_DOTS_INSTANCED_PROP(float4, _SpecColor) + UNITY_DOTS_INSTANCED_PROP(float4, _EmissionColor) + UNITY_DOTS_INSTANCED_PROP(float , _Cutoff) + UNITY_DOTS_INSTANCED_PROP(float , _Surface) + UNITY_DOTS_INSTANCING_END(MaterialPropertyMetadata) + + #define _BaseColor UNITY_ACCESS_DOTS_INSTANCED_PROP_WITH_DEFAULT(float4 , _BaseColor) + #define _SpecColor UNITY_ACCESS_DOTS_INSTANCED_PROP_WITH_DEFAULT(float4 , _SpecColor) + #define _EmissionColor UNITY_ACCESS_DOTS_INSTANCED_PROP_WITH_DEFAULT(float4 , _EmissionColor) + #define _Cutoff UNITY_ACCESS_DOTS_INSTANCED_PROP_WITH_DEFAULT(float , _Cutoff) + #define _Surface UNITY_ACCESS_DOTS_INSTANCED_PROP_WITH_DEFAULT(float , _Surface) +#endif + +TEXTURE2D(_SpecGlossMap); SAMPLER(sampler_SpecGlossMap); +half4 SampleSpecularSmoothness(float2 uv, half alpha, half4 specColor, TEXTURE2D_PARAM(specMap, sampler_specMap)) +{ + half4 specularSmoothness = half4(0, 0, 0, 1); +#ifdef _SPECGLOSSMAP + specularSmoothness = SAMPLE_TEXTURE2D(specMap, sampler_specMap, uv) * specColor; +#elif defined(_SPECULAR_COLOR) + specularSmoothness = specColor; +#endif + +#ifdef _GLOSSINESS_FROM_BASE_ALPHA + specularSmoothness.a = alpha; +#endif + + return specularSmoothness; +} + +inline void InitializeSimpleLitSurfaceData(float3 uv, out SurfaceData outSurfaceData) +{ + outSurfaceData = (SurfaceData)0; + + half4 albedoAlpha = half4(UNITY_SAMPLE_TEX2DARRAY(_ColourTextures, uv).rgb, 1.0) * _BaseColor; + outSurfaceData.alpha = albedoAlpha.a * _BaseColor.a; + outSurfaceData.alpha = AlphaDiscard(outSurfaceData.alpha, _Cutoff); + + outSurfaceData.albedo = albedoAlpha.rgb * _BaseColor.rgb; + outSurfaceData.albedo = AlphaModulate(outSurfaceData.albedo, outSurfaceData.alpha); + + half4 specularSmoothness = SampleSpecularSmoothness(uv, outSurfaceData.alpha, _SpecColor, TEXTURE2D_ARGS(_SpecGlossMap, sampler_SpecGlossMap)); + outSurfaceData.metallic = 0.0; // unused + outSurfaceData.specular = specularSmoothness.rgb; + outSurfaceData.smoothness = specularSmoothness.a; + outSurfaceData.normalTS = SampleNormal(uv, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap)); + outSurfaceData.occlusion = 1.0; + outSurfaceData.emission = SampleEmission(uv, _EmissionColor.rgb, TEXTURE2D_ARGS(_EmissionMap, sampler_EmissionMap)); +} + +//#endif diff --git a/Assets/Materials/Shaders/MyVoxelShaderVR.shader b/Assets/Materials/Shaders/MyVoxelShaderVR.shader new file mode 100644 index 00000000..83260e06 --- /dev/null +++ b/Assets/Materials/Shaders/MyVoxelShaderVR.shader @@ -0,0 +1,252 @@ +Shader "Custom/MyVoxelShaderVR" +{ + Properties + { + _ColourTextures ("Textures", 2DArray) = "" {} + [MainColor] _BaseColor("Base Color", Color) = (1, 1, 1, 1) + + //_Cutoff("Alpha Clipping", Range(0.0, 1.0)) = 0.5 + + //_Smoothness("Smoothness", Range(0.0, 1.0)) = 0.5 + _SpecColor("Specular Color", Color) = (0.5, 0.5, 0.5, 0.5) + _SpecGlossMap("Specular Map", 2D) = "white" {} + //_SmoothnessSource("Smoothness Source", Float) = 0.0 + _SpecularHighlights("Specular Highlights", Float) = 1.0 + + [HideInInspector] _BumpScale("Scale", Float) = 1.0 + [NoScaleOffset] _BumpMap("Normal Map", 2D) = "bump" {} + + [HDR] _EmissionColor("Emission Color", Color) = (0,0,0) + [NoScaleOffset]_EmissionMap("Emission Map", 2D) = "white" {} + + // Blending state + _Surface("__surface", Float) = 0.0 + _Blend("__blend", Float) = 0.0 + _Cull("__cull", Float) = 2.0 + //[ToggleUI] _AlphaClip("__clip", Float) = 0.0 + [HideInInspector] _SrcBlend("__src", Float) = 1.0 + [HideInInspector] _DstBlend("__dst", Float) = 0.0 + [HideInInspector] _SrcBlendAlpha("__srcA", Float) = 1.0 + [HideInInspector] _DstBlendAlpha("__dstA", Float) = 0.0 + [HideInInspector] _ZWrite("__zw", Float) = 1.0 + [HideInInspector] _BlendModePreserveSpecular("_BlendModePreserveSpecular", Float) = 1.0 + [HideInInspector] _AlphaToMask("__alphaToMask", Float) = 0.0 + + [ToggleUI] _ReceiveShadows("Receive Shadows", Float) = 1.0 + // Editmode props + _QueueOffset("Queue offset", Float) = 0.0 + } + + SubShader + { + + Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "IgnoreProjector" = "True"} + Pass + { + Name "Forward" + Tags + { + "LightMode" = "UniversalForward" + } + Blend[_SrcBlend][_DstBlend], [_SrcBlendAlpha][_DstBlendAlpha] + ZWrite[_ZWrite] + Cull[_Cull] + AlphaToMask[_AlphaToMask] + + HLSLPROGRAM + #pragma target 4.5 + // use "vert" function as the vertex shader + #pragma vertex VoxelForwardVertex + // use "frag" function as the pixel (fragment) shader + #pragma fragment VoxelForwardFragment + // texture arrays are not available everywhere, + // only compile shader on platforms where they are + #pragma require 2darray + + // Universal Pipeline keywords + #pragma multi_compile _ _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE _MAIN_LIGHT_SHADOWS_SCREEN + //#pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS + //#pragma multi_compile _ EVALUATE_SH_MIXED EVALUATE_SH_VERTEX + //#pragma multi_compile _ LIGHTMAP_SHADOW_MIXING + //#pragma multi_compile _ SHADOWS_SHADOWMASK + #pragma multi_compile_fragment _ _ADDITIONAL_LIGHT_SHADOWS + //#pragma multi_compile_fragment _ _SHADOWS_SOFT + //#pragma multi_compile_fragment _ _SCREEN_SPACE_OCCLUSION + //#pragma multi_compile_fragment _ _DBUFFER_MRT1 _DBUFFER_MRT2 _DBUFFER_MRT3 + //#pragma multi_compile_fragment _ _LIGHT_LAYERS + //#pragma multi_compile_fragment _ _LIGHT_COOKIES + #pragma multi_compile _ _FORWARD_PLUS + //#include_with_pragmas "Packages/com.unity.render-pipelines.universal/ShaderLibrary/RenderingLayers.hlsl" + + // GPU Instancing + #pragma multi_compile_instancing + #pragma instancing_options renderinglayer + #pragma multi_compile _ DOTS_INSTANCING_ON + //#include_with_pragmas "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DOTS.hlsl" + + //#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl" + + #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" + #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" + #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderVariablesFunctions.hlsl" + #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl" + + #include "Assets/Materials/Shaders/texarrays.hlsl" + + //#include + + //#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" + + #define BUMP_SCALE_NOT_SUPPORTED 1 + + UNITY_DECLARE_TEX2DARRAY(_ColourTextures); + uniform float _uvSizes[2 * 6]; // Needs to be set from C# + + #include "Assets/Materials/Shaders/MyVoxelShaderInputVR.hlsl" + #include "Assets/Materials/Shaders/MyVoxelForwardPassVR.hlsl" + ENDHLSL + } + + Pass + { + Name "ShadowCaster" + Tags + { + "LightMode" = "ShadowCaster" + } + + // ------------------------------------- + // Render State Commands + ZWrite On + ZTest LEqual + ColorMask 0 + Cull [_Cull] + + HLSLPROGRAM + #pragma target 4.5 + // use "vert" function as the vertex shader + #pragma vertex shadow_vert + // use "frag" function as the pixel (fragment) shader + #pragma fragment shadow_frag + // GPU Instancing + #pragma multi_compile_instancing + #pragma instancing_options renderinglayer + #pragma multi_compile _ DOTS_INSTANCING_ON + + #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" + #include "Assets/Materials/Shaders/texarrays.hlsl" + //#include + + UNITY_DECLARE_TEX2DARRAY(_ColourTextures); + uniform float _uvSizes[2 * 6]; // Needs to be set from C# + #include "Assets/Materials/Shaders/MyVoxelShaderInputVR.hlsl" + + // vertex shader inputs + struct Attributes + { + int aData : POSITION; + UNITY_VERTEX_INPUT_INSTANCE_ID + }; + + // vertex shader outputs + struct Varyings + { + float4 positionHCS : SV_POSITION; + UNITY_VERTEX_INPUT_INSTANCE_ID + }; + + + // vertex shader + Varyings shadow_vert (Attributes IN) + { + Varyings OUT; + UNITY_SETUP_INSTANCE_ID(IN); + UNITY_TRANSFER_INSTANCE_ID(IN, OUT); + + // Get the chunk-relative position from the first 24 bits + float3 position = float3(float(IN.aData&(255)), float((IN.aData >> 8)&(255)), float((IN.aData >> 16)&(255))); + float4 positionWS = mul(UNITY_MATRIX_MVP, float4(position, 1.0)); // Use MVP matrix to get global position + OUT.positionHCS = positionWS; + return OUT; + } + + half4 shadow_frag (Varyings IN) : SV_Target + { + UNITY_SETUP_INSTANCE_ID(IN); + return 0; + } + ENDHLSL + } + + + // This pass is used when drawing to a _CameraNormalsTexture texture + // Without it, the skybox has lower depth and this object is not drawn! + Pass + { + Name "DepthNormals" + Tags + { + "LightMode" = "DepthNormals" + } + + // ------------------------------------- + // Render State Commands + ZWrite On + Cull[_Cull] + + HLSLPROGRAM + #pragma target 4.5 + #pragma vertex depth_vert + #pragma fragment depth_frag + + // GPU Instancing + #pragma multi_compile_instancing + #pragma instancing_options renderinglayer + #pragma multi_compile _ DOTS_INSTANCING_ON + + #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" + #include "Assets/Materials/Shaders/texarrays.hlsl" + //#include + UNITY_DECLARE_TEX2DARRAY(_ColourTextures); + uniform float _uvSizes[2 * 6]; // Needs to be set from C# + #include "Assets/Materials/Shaders/MyVoxelShaderInputVR.hlsl" + + // vertex shader inputs + struct Attributes + { + int aData : POSITION; + UNITY_VERTEX_INPUT_INSTANCE_ID + }; + + // vertex shader outputs + struct Varyings + { + float4 positionHCS : SV_POSITION; + UNITY_VERTEX_INPUT_INSTANCE_ID + }; + + + // vertex shader + Varyings depth_vert (Attributes IN) + { + Varyings OUT; + UNITY_SETUP_INSTANCE_ID(IN); + UNITY_TRANSFER_INSTANCE_ID(IN, OUT); + + // Get the chunk-relative position from the first 24 bits + float3 position = float3(float(IN.aData&(255)), float((IN.aData >> 8)&(255)), float((IN.aData >> 16)&(255))); + // float4 positionWS = mul(UNITY_MATRIX_MVP, float4(position, 1.0)); // Use MVP matrix to get global position + OUT.positionHCS = TransformObjectToHClip(position); + return OUT; + } + + half4 depth_frag (Varyings IN) : SV_Target + { + UNITY_SETUP_INSTANCE_ID(IN); + return 0; + } + ENDHLSL + } + } + +} diff --git a/Assets/Materials/Shaders/VoxelForwardPass.hlsl b/Assets/Materials/Shaders/VoxelForwardPass.hlsl index 5e184bc9..3278d550 100644 --- a/Assets/Materials/Shaders/VoxelForwardPass.hlsl +++ b/Assets/Materials/Shaders/VoxelForwardPass.hlsl @@ -1,7 +1,7 @@ #ifndef UNIVERSAL_SIMPLE_LIT_PASS_INCLUDED #define UNIVERSAL_SIMPLE_LIT_PASS_INCLUDED -#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" +//#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" #if defined(LOD_FADE_CROSSFADE) #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/LODCrossFade.hlsl" #endif diff --git a/Assets/Materials/Shaders/VoxelShader.shader b/Assets/Materials/Shaders/VoxelShader.shader index ad7aa059..ffd1ee79 100644 --- a/Assets/Materials/Shaders/VoxelShader.shader +++ b/Assets/Materials/Shaders/VoxelShader.shader @@ -76,7 +76,7 @@ Shader "Custom/VoxelShader" //#pragma multi_compile_fragment _ _LIGHT_LAYERS //#pragma multi_compile_fragment _ _LIGHT_COOKIES #pragma multi_compile _ _FORWARD_PLUS - #include_with_pragmas "Packages/com.unity.render-pipelines.universal/ShaderLibrary/RenderingLayers.hlsl" + //#include_with_pragmas "Packages/com.unity.render-pipelines.universal/ShaderLibrary/RenderingLayers.hlsl" // GPU Instancing #pragma multi_compile_instancing @@ -86,15 +86,16 @@ Shader "Custom/VoxelShader" //#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl" - #include #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" + #include - #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" + //#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" #define BUMP_SCALE_NOT_SUPPORTED 1 UNITY_DECLARE_TEX2DARRAY(_ColourTextures); uniform float _uvSizes[2 * 6]; // Needs to be set from C# + #include "Assets/Materials/Shaders/VoxelShaderInput.hlsl" #include "Assets/Materials/Shaders/VoxelForwardPass.hlsl" ENDHLSL diff --git a/Assets/Materials/Shaders/VoxelShaderInput.hlsl b/Assets/Materials/Shaders/VoxelShaderInput.hlsl index 4440bda5..8179fcf2 100644 --- a/Assets/Materials/Shaders/VoxelShaderInput.hlsl +++ b/Assets/Materials/Shaders/VoxelShaderInput.hlsl @@ -1,7 +1,7 @@ //#ifndef UNIVERSAL_SIMPLE_LIT_INPUT_INCLUDED //#define UNIVERSAL_SIMPLE_LIT_INPUT_INCLUDED -#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" +//#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceInput.hlsl" CBUFFER_START(UnityPerMaterial) diff --git a/Assets/Materials/Shaders/texarrays.hlsl b/Assets/Materials/Shaders/texarrays.hlsl new file mode 100644 index 00000000..5346641b --- /dev/null +++ b/Assets/Materials/Shaders/texarrays.hlsl @@ -0,0 +1,7 @@ +#ifndef INCLUDED_TEXARRAYS +#define INCLUDED_TEXARRAYS + +#define UNITY_SAMPLE_TEX2DARRAY(tex,coord) tex.Sample (sampler##tex,coord) +#define UNITY_DECLARE_TEX2DARRAY(tex) Texture2DArray tex; SamplerState sampler##tex + +#endif //ifndef INCLUDED_TEXARRAYS \ No newline at end of file diff --git a/Assets/Scripts/Terrain/TerrainConfig.cs b/Assets/Scripts/Terrain/TerrainConfig.cs index e21b619e..ded0a28f 100644 --- a/Assets/Scripts/Terrain/TerrainConfig.cs +++ b/Assets/Scripts/Terrain/TerrainConfig.cs @@ -10,9 +10,20 @@ public static class Env public const int PLAYER_VIEW_RANGE = 2; public const int TERRAIN_SPAWN_RANGE = 3; public const int MAX_COL_PER_TICK = 10; - + #region DO NOT CHANGE THESE! + public static int getTerrainSpawnRange() + { + //when Variable renderdistance is desired, add + // public static readonly CommandLineParser.IntArgument renderDistance = new CommandLineParser.IntArgument("-renderDist", 10); + // to Opencraft-2/Packages/PolkaDOTS/Runtime/Configuration/Applicationconfig.cs + //and uncomment this line: + // return ApplicationConfig.renderDistance.Value; + //Because this breaks polkaDOTS' abstraction model, this is not desired as default + return TERRAIN_SPAWN_RANGE; + } + public const float CAMERA_Y_OFFSET = 0.5f; public const int AREA_POW_2 = AREA_POW << 1; diff --git a/Assets/Scripts/Terrain/TerrainToSpawnSystem.cs b/Assets/Scripts/Terrain/TerrainToSpawnSystem.cs index 88c87200..f7912116 100644 --- a/Assets/Scripts/Terrain/TerrainToSpawnSystem.cs +++ b/Assets/Scripts/Terrain/TerrainToSpawnSystem.cs @@ -6,6 +6,9 @@ using Unity.Burst; using Unity.Collections; using Unity.Profiling; +using UnityEngine; +using System.Linq; +using Unity.Collections.LowLevel.Unsafe; namespace Opencraft.Terrain { @@ -44,7 +47,10 @@ public void OnUpdate(ref SystemState state) markerPlayerTerrainGenCheck.Begin(); // todo - make this parallel var areasPlayersCloseTo = new NativeHashSet(32, Allocator.TempJob); - int viewRange = Env.TERRAIN_SPAWN_RANGE; + int viewRange = Env.getTerrainSpawnRange(); + + NativeArray ChunksToSpawnSnapshot = chunksToSpawnBuffer.ToNativeArray(Allocator.Temp); + foreach (var transform in SystemAPI.Query().WithAll()) { var pos = transform.Position; @@ -54,13 +60,17 @@ public void OnUpdate(ref SystemState state) var viewRangeSide = (viewRange +1+ viewRange); // Set of areas forming cube around players current area NativeHashSet nearbyColumns = new NativeHashSet(viewRangeSide * viewRangeSide, Allocator.Temp); + for (int i = -viewRange; i < viewRange; i++) { //for (int j = -viewRange; j < viewRange; j++) //{ for (int k = -viewRange; k < viewRange; k++) { - nearbyColumns.Add(playerColumn + new int2(i,k)); + int2 targetCol = playerColumn + new int2(i, k); + + nearbyColumns.Add(targetCol); + } //} } @@ -76,7 +86,10 @@ public void OnUpdate(ref SystemState state) // Copy the nearby areas of this player that aren't yet spawned to the global hashset areasPlayersCloseTo.UnionWith(nearbyColumns); } + + // Mark areas that need to be spawned + areasPlayersCloseTo.ExceptWith(ChunksToSpawnSnapshot); chunksToSpawnBuffer.AddRange(areasPlayersCloseTo.ToNativeArray(Allocator.Temp)); areasPlayersCloseTo.Dispose(); markerPlayerTerrainGenCheck.End();