Comments (5)
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.
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.
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.
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.
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)
- Android Doesn't Work At All HOT 2
- Changing the Layer of Instances? HOT 1
- Diffirent Shader HOT 1
- Appending Position is 1 frame late? HOT 5
- How do textured instances work? Is there a better way to reparent instances in batches? HOT 8
- Not an issue - Amazing Project! Would love to have a URP version, even payed! HOT 9
- "Tried to parent a child to an unitialized parent" error on a parent that's already been initialized multiple frames ago. HOT 3
- Packaging iOS to draw real-time point cloud will report an error,How to solve this problem? HOT 1
- How to bake and play a Transform-Animation
- WebGL Build Error HOT 2
- Enabling/Disabling Animations For Certain Bones HOT 6
- This Project is the Bomb! Thank you! HOT 10
- Camera Controls Wonky (always rotating with mouse) HOT 1
- High GPU Usage for Culling? HOT 10
- Univeral Render Pipeline support HOT 1
- Help with positioning HOT 7
- Changing render distance of instances HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gpuinstance.