An example of handling transparent objects.

This commit is contained in:
Robert Cupisz 2016-11-06 00:21:11 +01:00
parent 9d1e179371
commit fc99d4650f
7 changed files with 1448 additions and 2 deletions

View File

@ -0,0 +1,34 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: StandardAlphaBlended-VolumetricFog
m_Shader: {fileID: 4800000, guid: 9dca33cc83d85fd409e8d3dfc9db5676, type: 3}
m_ShaderKeywords:
m_LightmapFlags: 5
m_CustomRenderQueue: -1
stringTagMap: {}
m_SavedProperties:
serializedVersion: 2
m_TexEnvs:
- first:
name: _MainTex
second:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- first:
name: _Glossiness
second: 0.667
- first:
name: _Metallic
second: 0
m_Colors:
- first:
name: _Color
second: {r: 1, g: 1, b: 1, a: 0}

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: b92b0577b1e30bf4ab3b04c3e6a74994
timeCreated: 1478261393
licenseType: Pro
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,102 @@
Shader "Custom/StandardAlphaBlended-VolumetricFog"
{
Properties
{
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader
{
Tags {"Queue" = "Transparent" "RenderType"="Transparent" }
LOD 200
CGPROGRAM
#pragma surface surf Standard fullforwardshadows alpha finalcolor:ApplyFog
#pragma target 3.0
#pragma multi_compile _ VOLUMETRIC_FOG
#if VOLUMETRIC_FOG
#include "../../VolumetricFog/Shaders/VolumetricFog.cginc"
#endif
sampler2D _MainTex;
sampler2D _CameraDepthTexture;
struct Input {
float2 uv_MainTex;
float4 screenPos;
};
void ApplyFog(Input IN, SurfaceOutputStandard o, inout fixed4 color)
{
#if VOLUMETRIC_FOG
half3 uvscreen = IN.screenPos.xyz/IN.screenPos.w;
half linear01Depth = Linear01Depth(uvscreen.z);
fixed4 fog = Fog(linear01Depth, uvscreen.xy);
// Always apply fog attenuation - also in the forward add pass.
color.rgb *= fog.a;
// Alpha premultiply mode (used with alpha and Standard lighting function, or explicitly alpha:premul)
// uses source blend factor of One instead of SrcAlpha. `color` is compensated for it, so we need to compensate
// the amount of inscattering too. A note on why this works: below.
#if _ALPHAPREMULTIPLY_ON
fog.rgb *= o.Alpha;
#endif
// Add inscattering only once, so in forward base, but not forward add.
#ifndef UNITY_PASS_FORWARDADD
color.rgb += fog.rgb;
#endif
// So why does multiplying the inscattered light by alpha work?
// In other words: how did fog ever work, if opaque objects add all of the inscattered light
// between them and the camera, and then the transparencies add even more?
//
// This is our scene initially:
// scene |---is0---------------------------------------> camera
//
// And that's with the transparent object added in between the opaque stuff and the camera:
// scene |---is1---> transparent |---is2---------------> camera
//
// When rendering, we start with the opaque part of the scene and add all the light inscattered between that and the camera: is0.
// Then we add the transparent object. It does two things (let's consider the alpha premultiply version):
// - Dims whatever was behind it (including is0) by OneMinusSrcAlpha
// - Adds light inscattered in front of it (is2), multiplied by Alpha
//
// So all in all we end up with this much inscattered light:
// is0 * OneMinusSrcAlpha + is2 * Alpha
//
// Judging by the diagram, though, the correct amount should be:
// is1 * OneMinusSrcAlpha + is2
//
// Turns out the two expressions are equal - who would've thunk?
// is1 = is0 - is2
// (is0 - is2) * OneMinusSrcAlpha + is2
// is0 * OneMinusSrcAlpha - is2 * (1 - Alpha) + is2
// is0 * OneMinusSrcAlpha - is2 + is2 * Alpha + is2
// is0 * OneMinusSrcAlpha + is2 * Alpha
// I leave figuring out if the fog attenuation is correct as an exercise to the reader ;)
#endif
}
half _Glossiness;
half _Metallic;
fixed4 _Color;
void surf (Input IN, inout SurfaceOutputStandard o)
{
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Standard"
}

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 9dca33cc83d85fd409e8d3dfc9db5676
timeCreated: 1478225190
licenseType: Pro
ShaderImporter:
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 915ed33d3a042174eb51699251f87c5b
timeCreated: 1478261417
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -8,13 +8,21 @@ This repository contains some of the lighting effects implemented for the [Unity
##### Test scenes ##### Test scenes
![area light](http://i.imgur.com/HxwAefB.png) ![area light](http://i.imgur.com/HxwAefB.png)
Area light with shadows.
![tube light](http://i.imgur.com/MxS8Jus.png) ![tube light](http://i.imgur.com/MxS8Jus.png)
Two tube lights with some locally controlled fog density.
![tube light](http://i.imgur.com/ZSXoCYX.png) ![tube light](http://i.imgur.com/ZSXoCYX.png)
Tube light with a shadow plane and global fog noise + wind.
![transparency](http://i.imgur.com/Yr8Zj2S.png)
Transparent spheres and a point light.
System requirements System requirements
------------------- -------------------
- DX11 - DX11
- Tested in Unity 5.4.1, 5.4.2 and 5.5b10 - Unity 5.4.1
Usage notes Usage notes
----------- -----------
@ -42,7 +50,6 @@ Usage notes
Known issues and missing functionality Known issues and missing functionality
-------------------------------------- --------------------------------------
- Directional light support disabled for now, but should be added soon. - Directional light support disabled for now, but should be added soon.
- The fog can properly affect transparencies, but the standard shader requires a small patch for this to work. An example will be added soon.
- The froxel resolution is currently hardcoded and larger view distances will cause undersampling issues along the view axis. Should be fixed soon. In the meantime use the *Far Clip Max* setting to limit how far the volumetrics are calculated. - The froxel resolution is currently hardcoded and larger view distances will cause undersampling issues along the view axis. Should be fixed soon. In the meantime use the *Far Clip Max* setting to limit how far the volumetrics are calculated.
- The lights only work in deferred rendering and won't affect any objects rendered in forward. - The lights only work in deferred rendering and won't affect any objects rendered in forward.
- No scaling via the transform component - currently the size and shape of the lights can only be changed via the properties of the light component. Changing the scale in the transform component doesn't work. - No scaling via the transform component - currently the size and shape of the lights can only be changed via the properties of the light component. Changing the scale in the transform component doesn't work.