Git Product home page Git Product logo

Comments (5)

projectBreakableAccAPI avatar projectBreakableAccAPI commented on July 18, 2024

Ok, so since I've left this problem I've grown much wiser and solved this misplaced position somewhat. My new problem is: how would I go about addressing each instance's position individually in the shader? I need to be able to get the voxel's shadow position in order to get accurate shading because right now they all share the exact same shade. Here's my new and improved, and yet still non-functional shader:

Shader "Instanced/distantVoxel"
{
	Properties
	{
		_MainTex("Albedo (RGB)", 2D) = "white" {}
	}
	SubShader
	{
		Tags{ "RenderType" = "Opaque" }
		LOD 200

		CGPROGRAM
		// Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it uses non-square matrices
		#pragma exclude_renderers gles
		// Physically based Standard lighting model
		#pragma surface surf Standard addshadow vertex:vert finalcolor:colorOverride
		#pragma instancing_options procedural:setup
		#pragma target 5.0
		#pragma multi_compile_instancing
		//#pragma multi_compile _ _MAIN_LIGHT_SHADOWS
		//#pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE

		sampler2D _MainTex;
		int sampledColor = 0;
		half4 colorSample;
		half shadow;

		#include "HLSLSupport.cginc"
		#include "UnityShadowLibrary.cginc"
		#include "AutoLight.cginc"
		#include "gpuinstance_includes.cginc"

		struct Input
		{
			float2 uv_MainTex;
			fixed4 color;
			float4 screenPos;
		};

		void setup()
		{
			do_instance_setup();
		}

		void vert(inout appdata_full v, out Input o)
		{
			UNITY_INITIALIZE_OUTPUT(Input, o);
			int id = get_instance_id();
			o.color = get_instance_color(id);
			#if defined (SHADOWS_SCREEN) && !defined(UNITY_NO_SCREENSPACE_SHADOWS) 
			{
				o.screenPos = mul(unity_WorldToShadow[0], mul(unity_ObjectToWorld, v.vertex));
			}
			#endif
		}

		half _DummyZero;

		void surf(Input IN, inout SurfaceOutputStandard o)
		{
			if (sampledColor < 1)
			{
				o.Emission = IN.screenPos.x * _DummyZero * shadow;
				o.Albedo = tex2D(_MainTex, IN.uv_MainTex);
				o.Alpha = 1;
				colorSample = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
				shadow = 1;
				#if defined (SHADOWS_SCREEN) && !defined(UNITY_NO_SCREENSPACE_SHADOWS) 
				{
					shadow = unitySampleShadow(IN.screenPos);
				}
				#endif
			}
		}

		void colorOverride(Input IN, SurfaceOutputStandard o, inout fixed4 color)
		{
			color = colorSample / (1 + shadow);
		}

		ENDCG
	}
	FallBack "Diffuse"
}

from gpuinstance.

projectBreakableAccAPI avatar projectBreakableAccAPI commented on July 18, 2024

Okay, I've basically abandoned that idea and am now just trying to make a shader that smooth transitions into the lower LOD by getting dimmer and diminishing its shadows based on distance. I thought this would be easy and I would be able to wrap up for the day but nope. When I try to get the distance of the camera from the instance's vertex I get 0s across the board. It seems that the vertex position cannot be converted to world space at all or something. Is there a solution I'm missing here? All I need is an accurate world position of the individual instance or at the very least, the distance between it and the camera.

My current, still nonfunctional code:

Shader "Instanced/voxelFade"
{
	Properties
	{
		_MainTex("Albedo (RGB)", 2D) = "white" {}
		_FadeEnd("Fade End", Color) = (1, 1, 1, 1)
		_FadeRate("Rate of Fade", float) = 0
		_BegDist("Beginning Distance", float) = 0
	}
		SubShader{
		Tags{ "RenderType" = "Opaque" }
		LOD 200

		CGPROGRAM
			// Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it uses non-square matrices
	#pragma exclude_renderers gles
			// Physically based Standard lighting model
	#pragma surface surf Standard addshadow vertex:vert finalcolor:colorOverride
	#pragma instancing_options procedural:setup
	#pragma target 5.0
	#pragma multi_compile_instancing

		sampler2D _MainTex;
		float4 _FadeEnd;
		float _FadeRate;
		float _BegDist;
		float endDistance;

	#include "gpuinstance_includes.cginc"

		struct Input
		{
			float2 uv_MainTex;
			fixed4 color;
		};

		void setup()
		{
		do_instance_setup();
		}

		void vert(inout appdata_full v, out Input o)
		{
		UNITY_INITIALIZE_OUTPUT(Input, o);
		int id = get_instance_id();
		o.color = get_instance_color(id);
		endDistance = distance(_WorldSpaceCameraPos, mul(unity_ObjectToWorld, v.vertex));
		}

		void surf(Input IN, inout SurfaceOutputStandard o)
		{
			fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
			o.Albedo = c.rgb;
			o.Alpha = c.a;
		}

		void colorOverride(Input IN, SurfaceOutputStandard o, inout fixed4 color)
		{
			endDistance = clamp((endDistance - _BegDist) * _FadeRate, 0, 1);
			color = color * (1 - endDistance) + (IN.color * endDistance) * _FadeEnd;
		}

		ENDCG
		}
			FallBack "Diffuse"
}

from gpuinstance.

projectBreakableAccAPI avatar projectBreakableAccAPI commented on July 18, 2024

Well it seems that I've done it, here it is for anyone else who needs this weird, oddly specific shader or at least one for reference:

Shader "Instanced/voxelFade"
{
	Properties
	{
		_MainTex("Albedo (RGB)", 2D) = "white" {}
		_FadeEnd("Fade End", Color) = (1, 1, 1, 1)
		_FadeRate("Rate of Fade", float) = 0
		_BegDist("Beginning Distance", float) = 0
	}
		SubShader{
		Tags{ "RenderType" = "Opaque" }
		LOD 200

		CGPROGRAM
		// Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it uses non-square matrices
		#pragma exclude_renderers gles
		// Physically based Standard lighting model
		#pragma surface surf Standard addshadow vertex:vert finalcolor:colorOverride
		#pragma instancing_options procedural:setup
		#pragma target 5.0
		#pragma multi_compile_instancing

		sampler2D _MainTex;
		float4 _FadeEnd;
		float _FadeRate;
		float _BegDist;

		#include "gpuinstance_includes.cginc"

		struct Input
		{
			float2 uv_MainTex;
			fixed4 color;
			float3 worldPos;
		};

		void setup()
		{
			do_instance_setup();
		}

		void vert(inout appdata_full v, out Input o)
		{
			UNITY_INITIALIZE_OUTPUT(Input, o);
			int id = get_instance_id();
			o.color = get_instance_color(id);
		}

		void surf(Input IN, inout SurfaceOutputStandard o)
		{
			fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
			o.Albedo = c.rgb;
			o.Alpha = c.a;
		}

		void colorOverride(Input IN, SurfaceOutputStandard o, inout fixed4 color)
		{
			float endDistance = distance(IN.worldPos, _WorldSpaceCameraPos);
			endDistance = clamp((endDistance - _BegDist) * _FadeRate, 0, 1);
			color = color * (1 - endDistance) + (IN.color * endDistance) * _FadeEnd;
		}

		ENDCG
		}
			FallBack "Diffuse"
}

I still want to come up with a solution for the shadow thing, now that I know how to get the voxel's position, but unfortunately, I still can't quite figure it out, let me know if you have any luck, and thanks in advance for reading this incoherency :)

from gpuinstance.

mkrebser avatar mkrebser commented on July 18, 2024

You are trying to blend out shadows based on distance?
Don't know very much about unity's shadow/lighting model .It will probably depend on your lighting as well. Perhaps look into custom lighting models: https://docs.unity3d.com/Manual/SL-SurfaceShaderLighting.html

I think I wrote a shader once for a voxel type game using a custom lighting model

I believe you can also just turn off shadows for different mesh for each LOD in the mesh settings. I typically only enable shadows for like the first 2 lods

from gpuinstance.

projectBreakableAccAPI avatar projectBreakableAccAPI commented on July 18, 2024

Thank you for the insight, my solution to the voxel's world position was the surface shader function float3 worldPos. This returns an accurate world position for the instance. I also made another shader for 3 sided cubes that always have their sides facing the camera, which was surprisingly hard, but I got it working. This question can be closed now, but I'm going to open another one because I have another question :/

from gpuinstance.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.