mirror of
https://github.com/muskit/H3VR-TNH-Quality-of-Life-Improvements.git
synced 2026-06-02 20:24:26 -07:00
Initial commit
This commit is contained in:
@@ -0,0 +1,288 @@
|
||||
// Alloy Physical Shader Framework
|
||||
// Copyright 2013-2017 RUST LLC.
|
||||
// http://www.alloy.rustltd.com/
|
||||
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
||||
[ExecuteInEditMode]
|
||||
[ImageEffectAllowedInSceneView]
|
||||
[RequireComponent(typeof (Camera))]
|
||||
[AddComponentMenu(AlloyUtils.ComponentMenu + "Effects Manager")]
|
||||
public class AlloyEffectsManager : MonoBehaviour {
|
||||
|
||||
// Arbitrary range multiplier.
|
||||
private const float c_blurWdith = 0.15f;
|
||||
private const float c_blurDepthDifferenceMultiplier = 100.0f;
|
||||
private const string c_copyTransmissionBufferName = "AlloyCopyTransmission";
|
||||
private const string c_blurNormalsBufferName = "AlloyBlurNormals";
|
||||
private const CameraEvent c_copyTransmissionEvent = CameraEvent.AfterGBuffer;
|
||||
private const CameraEvent c_blurNormalsEvent = CameraEvent.BeforeLighting;
|
||||
|
||||
[Serializable]
|
||||
public struct SkinSettingsData {
|
||||
public bool Enabled;
|
||||
public Texture2D Lut;
|
||||
|
||||
[Range(0.0f, 1.0f)]
|
||||
public float Weight;
|
||||
|
||||
[Range(0.01f, 1.0f)]
|
||||
public float MaskCutoff;
|
||||
|
||||
[Range(0.0f, 1.0f)]
|
||||
public float Bias;
|
||||
|
||||
[Range(0.0f, 1.0f)]
|
||||
public float Scale;
|
||||
|
||||
[Range(0.0f, 1.0f)]
|
||||
public float BumpBlur;
|
||||
|
||||
public Vector3 Absorption;
|
||||
public Vector3 AoColorBleed;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public struct TransmissionSettingsData {
|
||||
public bool Enabled;
|
||||
|
||||
[Range(0.0f, 1.0f)]
|
||||
public float Weight;
|
||||
|
||||
[Range(0.0f, 1.0f)]
|
||||
public float ShadowWeight;
|
||||
|
||||
[Range(0.0f, 1.0f)]
|
||||
[Tooltip("Amount that the transmission is distorted by surface normals.")]
|
||||
public float BumpDistortion;
|
||||
|
||||
[MinValue(1.0f)]
|
||||
public float Falloff;
|
||||
}
|
||||
|
||||
public SkinSettingsData SkinSettings = new SkinSettingsData() {
|
||||
Enabled = true,
|
||||
Weight = 1.0f,
|
||||
MaskCutoff = 0.1f,
|
||||
Bias = 0.0f,
|
||||
Scale = 1.0f,
|
||||
BumpBlur = 0.7f,
|
||||
Absorption = new Vector3(-8.0f, -40.0f, -64.0f),
|
||||
AoColorBleed = new Vector3(0.4f, 0.15f, 0.13f),
|
||||
};
|
||||
|
||||
public TransmissionSettingsData TransmissionSettings = new TransmissionSettingsData {
|
||||
Enabled = true,
|
||||
Weight = 1.0f,
|
||||
ShadowWeight = 0.5f,
|
||||
BumpDistortion = 0.05f,
|
||||
Falloff = 1.0f
|
||||
};
|
||||
|
||||
// LUT
|
||||
[HideInInspector] public Texture2D SkinLut;
|
||||
|
||||
// Shaders
|
||||
[HideInInspector] public Shader TransmissionBlitShader;
|
||||
[HideInInspector] public Shader BlurNormalsShader;
|
||||
|
||||
// Private
|
||||
private Material m_deferredTransmissionBlitMaterial;
|
||||
private Material m_deferredBlurredNormalsMaterial;
|
||||
|
||||
private Camera m_camera;
|
||||
private bool m_isTransmissionEnabled;
|
||||
private bool m_isScatteringEnabled;
|
||||
|
||||
private CommandBuffer m_copyTransmission;
|
||||
private CommandBuffer m_renderBlurredNormals;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
private int lastWidth = 0;
|
||||
private int lastHeight = 0;
|
||||
#endif
|
||||
|
||||
private void Awake() {
|
||||
m_camera = GetComponent<Camera>();
|
||||
}
|
||||
|
||||
private void Reset() {
|
||||
ResetCommandBuffers();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
private void Update() {
|
||||
if (lastWidth != m_camera.pixelWidth
|
||||
|| lastHeight != m_camera.pixelHeight) {
|
||||
ResetCommandBuffers();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
private void OnEnable() {
|
||||
ResetCommandBuffers();
|
||||
}
|
||||
|
||||
private void OnDisable() {
|
||||
DestroyCommandBuffers();
|
||||
}
|
||||
|
||||
private void OnDestroy() {
|
||||
DestroyCommandBuffers();
|
||||
}
|
||||
|
||||
public void Refresh() {
|
||||
bool scatteringEnabled = SkinSettings.Enabled;
|
||||
bool transmissionEnabled = TransmissionSettings.Enabled || scatteringEnabled;
|
||||
|
||||
if (m_isTransmissionEnabled == transmissionEnabled
|
||||
&& m_isScatteringEnabled == scatteringEnabled) {
|
||||
RefreshProperties();
|
||||
}
|
||||
else {
|
||||
ResetCommandBuffers();
|
||||
}
|
||||
}
|
||||
|
||||
// Per camera properties.
|
||||
private void RefreshProperties() {
|
||||
if (m_isTransmissionEnabled || m_isScatteringEnabled) {
|
||||
float transmissionWeight = m_isTransmissionEnabled ? Mathf.GammaToLinearSpace(TransmissionSettings.Weight) : 0.0f;
|
||||
|
||||
Shader.SetGlobalVector("_DeferredTransmissionParams",
|
||||
new Vector4(transmissionWeight, TransmissionSettings.Falloff, TransmissionSettings.BumpDistortion, TransmissionSettings.ShadowWeight));
|
||||
|
||||
if (m_isScatteringEnabled) {
|
||||
// Blur shaders.
|
||||
float distanceToProjectionWindow = 1.0f / Mathf.Tan(0.5f * Mathf.Deg2Rad * m_camera.fieldOfView);
|
||||
float blurStepScale = c_blurWdith * distanceToProjectionWindow;
|
||||
float blurDepthDifferenceScale = c_blurDepthDifferenceMultiplier * distanceToProjectionWindow;
|
||||
|
||||
Shader.SetGlobalVector("_DeferredBlurredNormalsParams", new Vector2(blurStepScale, blurDepthDifferenceScale));
|
||||
|
||||
// Material shaders.
|
||||
var absorption = SkinSettings.Absorption;
|
||||
var aoColorBleed = SkinSettings.AoColorBleed;
|
||||
|
||||
Shader.SetGlobalTexture("_DeferredSkinLut", SkinSettings.Lut);
|
||||
Shader.SetGlobalVector("_DeferredSkinParams", new Vector3(SkinSettings.Weight, 1.0f / SkinSettings.MaskCutoff, SkinSettings.BumpBlur));
|
||||
Shader.SetGlobalVector("_DeferredSkinTransmissionAbsorption", new Vector4(absorption.x, absorption.y, absorption.z, SkinSettings.Bias));
|
||||
Shader.SetGlobalVector("_DeferredSkinColorBleedAoWeights", new Vector4(aoColorBleed.x, aoColorBleed.y, aoColorBleed.z, SkinSettings.Scale));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ResetCommandBuffers() {
|
||||
m_isScatteringEnabled = SkinSettings.Enabled;
|
||||
m_isTransmissionEnabled = TransmissionSettings.Enabled || m_isScatteringEnabled;
|
||||
|
||||
if (SkinSettings.Lut == null) {
|
||||
SkinSettings.Lut = SkinLut;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
EditorUtility.SetDirty(this);
|
||||
#endif
|
||||
}
|
||||
|
||||
DestroyCommandBuffers();
|
||||
|
||||
if ((m_isTransmissionEnabled || m_isScatteringEnabled)
|
||||
&& m_camera != null
|
||||
&& TransmissionBlitShader != null) {
|
||||
int outputRT = Shader.PropertyToID("_DeferredPlusBuffer");
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// Reference for when screen size changes.
|
||||
lastWidth = m_camera.pixelWidth;
|
||||
lastHeight = m_camera.pixelHeight;
|
||||
#endif
|
||||
|
||||
m_deferredTransmissionBlitMaterial = new Material(TransmissionBlitShader);
|
||||
m_deferredTransmissionBlitMaterial.hideFlags = HideFlags.HideAndDontSave;
|
||||
|
||||
// Copy Gbuffer emission buffer so we can get at the alpha channel for transmission.
|
||||
m_copyTransmission = new CommandBuffer();
|
||||
m_copyTransmission.name = c_copyTransmissionBufferName;
|
||||
|
||||
if (!m_isScatteringEnabled) {
|
||||
// Copy transmission from emission buffer alpha.
|
||||
m_copyTransmission.GetTemporaryRT(outputRT, -1, -1, 0, FilterMode.Point, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
|
||||
m_copyTransmission.Blit(BuiltinRenderTextureType.CameraTarget, outputRT, m_deferredTransmissionBlitMaterial);
|
||||
m_copyTransmission.ReleaseTemporaryRT(outputRT);
|
||||
}
|
||||
else if (BlurNormalsShader != null) {
|
||||
int halfWidth = m_camera.pixelWidth / 2;
|
||||
int halfHeight = m_camera.pixelHeight / 2;
|
||||
int pingRT = Shader.PropertyToID("_DeferredBlurredNormalPingBuffer");
|
||||
int pongRT = Shader.PropertyToID("_DeferredBlurredNormalPongBuffer");
|
||||
|
||||
// Bind emission buffer to be copied in blurred normal upsample pass.
|
||||
m_copyTransmission.SetGlobalTexture("_DeferredTransmissionBuffer", BuiltinRenderTextureType.CameraTarget);
|
||||
|
||||
// Blur normals and copy transmission.
|
||||
m_deferredBlurredNormalsMaterial = new Material(BlurNormalsShader);
|
||||
m_deferredBlurredNormalsMaterial.hideFlags = HideFlags.HideAndDontSave;
|
||||
|
||||
m_renderBlurredNormals = new CommandBuffer();
|
||||
m_renderBlurredNormals.name = c_blurNormalsBufferName;
|
||||
|
||||
// RGBA8 target has sufficient precision for normals that are smooth and diffuse-only.
|
||||
m_renderBlurredNormals.GetTemporaryRT(outputRT, -1, -1, 0, FilterMode.Point, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
|
||||
m_renderBlurredNormals.GetTemporaryRT(pingRT, halfWidth, halfHeight, 0, FilterMode.Point, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear);
|
||||
m_renderBlurredNormals.GetTemporaryRT(pongRT, halfWidth, halfHeight, 0, FilterMode.Point, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear);
|
||||
|
||||
// Downsample, Blur X, Blur Y, Upsample.
|
||||
m_renderBlurredNormals.Blit(BuiltinRenderTextureType.GBuffer2, pingRT, m_deferredBlurredNormalsMaterial, 0);
|
||||
m_renderBlurredNormals.Blit(pingRT, pongRT, m_deferredBlurredNormalsMaterial, 1);
|
||||
m_renderBlurredNormals.Blit(pongRT, pingRT, m_deferredBlurredNormalsMaterial, 2);
|
||||
m_renderBlurredNormals.Blit(pingRT, outputRT, m_deferredBlurredNormalsMaterial, 3);
|
||||
|
||||
// Cleanup.
|
||||
m_renderBlurredNormals.ReleaseTemporaryRT(outputRT);
|
||||
m_renderBlurredNormals.ReleaseTemporaryRT(pingRT);
|
||||
m_renderBlurredNormals.ReleaseTemporaryRT(pongRT);
|
||||
|
||||
// Need depth texture for depth-aware upsample.
|
||||
m_camera.depthTextureMode |= DepthTextureMode.Depth;
|
||||
m_camera.AddCommandBuffer(c_blurNormalsEvent, m_renderBlurredNormals);
|
||||
}
|
||||
|
||||
m_camera.AddCommandBuffer(c_copyTransmissionEvent, m_copyTransmission);
|
||||
}
|
||||
|
||||
RefreshProperties();
|
||||
|
||||
#if UNITY_EDITOR
|
||||
EditorUtility.SetDirty(m_camera);
|
||||
#endif
|
||||
}
|
||||
|
||||
private void DestroyCommandBuffers() {
|
||||
if (m_copyTransmission != null) {
|
||||
m_camera.RemoveCommandBuffer(c_copyTransmissionEvent, m_copyTransmission);
|
||||
}
|
||||
|
||||
if (m_renderBlurredNormals != null) {
|
||||
m_camera.RemoveCommandBuffer(c_blurNormalsEvent, m_renderBlurredNormals);
|
||||
}
|
||||
|
||||
if (m_deferredTransmissionBlitMaterial != null) {
|
||||
DestroyImmediate(m_deferredTransmissionBlitMaterial);
|
||||
}
|
||||
|
||||
if (m_deferredBlurredNormalsMaterial != null) {
|
||||
DestroyImmediate(m_deferredBlurredNormalsMaterial);
|
||||
}
|
||||
|
||||
m_copyTransmission = null;
|
||||
m_renderBlurredNormals = null;
|
||||
m_deferredTransmissionBlitMaterial = null;
|
||||
m_deferredBlurredNormalsMaterial = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bf5b97b13ec5bae41ac96a39caf39874
|
||||
timeCreated: 1487764061
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences:
|
||||
- SkinLut: {fileID: 2800000, guid: d13510bb2be49aa40a66a0101efb6a36, type: 3}
|
||||
- TransmissionBlitShader: {fileID: 4800000, guid: 4bbeef29a84b4ed498f2d4a7d9fcefc4,
|
||||
type: 3}
|
||||
- BlurNormalsShader: {fileID: 4800000, guid: 5b34ef975c671984fb9c93adc7042982, type: 3}
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c7a3813b9ad39d847b4acc42619b3256
|
||||
folderAsset: yes
|
||||
timeCreated: 1445386286
|
||||
licenseType: Pro
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,39 @@
|
||||
// Alloy Physical Shader Framework
|
||||
// Copyright 2013-2017 RUST LLC.
|
||||
// http://www.alloy.rustltd.com/
|
||||
using System;
|
||||
using System.Linq;
|
||||
using MeatKit;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
[CustomEditor(typeof(Camera))]
|
||||
[CanEditMultipleObjects]
|
||||
public class AlloyCameraEditor : Editor {
|
||||
Editor m_editor;
|
||||
|
||||
Type GetTypeGlobal(string typeName) {
|
||||
return AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypesSafe()).FirstOrDefault(t => t.Name == typeName);
|
||||
}
|
||||
|
||||
void OnEnable() {
|
||||
m_editor = CreateEditor(targets, GetTypeGlobal("CameraEditor"));
|
||||
}
|
||||
|
||||
void OnDisable() {
|
||||
DestroyImmediate(m_editor);
|
||||
}
|
||||
|
||||
public override void OnInspectorGUI() {
|
||||
m_editor.OnInspectorGUI();
|
||||
bool anyMissing = targets.Any(c => ((Camera)c).GetComponent<AlloyEffectsManager>() == null);
|
||||
|
||||
if (anyMissing) {
|
||||
if (GUILayout.Button("Convert to Alloy Effects Manager", EditorStyles.toolbarButton)) {
|
||||
foreach (Camera camera in targets) {
|
||||
Undo.AddComponent<AlloyEffectsManager>(camera.gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dc5a924e062584c4b994b26019b1b30c
|
||||
timeCreated: 1491670701
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,142 @@
|
||||
// Alloy Physical Shader Framework
|
||||
// Copyright 2013-2017 RUST LLC.
|
||||
// http://www.alloy.rustltd.com/
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Alloy;
|
||||
using UnityEditor;
|
||||
using UnityEditor.AnimatedValues;
|
||||
using UnityEngine;
|
||||
|
||||
[CustomEditor(typeof(AlloyEffectsManager))]
|
||||
public class AlloyEffectsManagerEditor : Editor {
|
||||
private const string c_skinTabName = "SkinTab";
|
||||
private const string c_transmissionTabName = "TransmissionTab";
|
||||
private static Color s_scatterColor = new Color(0.49f, 0.36f, 0.16f);
|
||||
private static Color s_transmissionColor = new Color(0.49f, 0.46f, 0.16f);
|
||||
|
||||
private AlloyTabGroup m_tabGroup;
|
||||
private AnimBool m_skinGroup;
|
||||
private AnimBool m_transmissionGroup;
|
||||
private List<AlloyTabAdd> m_tabAdd = new List<AlloyTabAdd>();
|
||||
private GenericMenu m_menu;
|
||||
|
||||
private void OnEnable() {
|
||||
m_tabGroup = AlloyTabGroup.GetTabGroup();
|
||||
m_skinGroup = new AnimBool(m_tabGroup.IsOpen(c_skinTabName));
|
||||
m_transmissionGroup = new AnimBool(m_tabGroup.IsOpen(c_transmissionTabName));
|
||||
}
|
||||
|
||||
public override void OnInspectorGUI() {
|
||||
serializedObject.Update();
|
||||
m_tabAdd.Clear();
|
||||
|
||||
var skinEnabled = serializedObject.FindProperty("SkinSettings.Enabled");
|
||||
|
||||
if (skinEnabled.boolValue && !skinEnabled.hasMultipleDifferentValues) {
|
||||
bool removed;
|
||||
m_skinGroup.target = m_tabGroup.TabArea("Skin Scattering", s_scatterColor, true, out removed,
|
||||
c_skinTabName);
|
||||
|
||||
if (EditorGUILayout.BeginFadeGroup(m_skinGroup.faded)) {
|
||||
var prop = serializedObject.FindProperty("SkinSettings");
|
||||
prop.Next(true); //skip flag
|
||||
prop.Next(true); //skip enabled
|
||||
|
||||
DrawRemainingProp(prop);
|
||||
}
|
||||
|
||||
EditorGUILayout.EndFadeGroup();
|
||||
|
||||
if (removed) {
|
||||
skinEnabled.boolValue = false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
m_tabAdd.Add(new AlloyTabAdd {
|
||||
Color = s_scatterColor,
|
||||
Name = "Skin Scattering",
|
||||
Enable = EnableSkin
|
||||
});
|
||||
}
|
||||
|
||||
var transEnabled = serializedObject.FindProperty("TransmissionSettings.Enabled");
|
||||
|
||||
if (transEnabled.boolValue && !transEnabled.hasMultipleDifferentValues) {
|
||||
bool removed;
|
||||
m_transmissionGroup.target = m_tabGroup.TabArea("Transmission", s_transmissionColor, true, out removed,
|
||||
c_transmissionTabName);
|
||||
|
||||
if (EditorGUILayout.BeginFadeGroup(m_transmissionGroup.faded)) {
|
||||
var prop = serializedObject.FindProperty("TransmissionSettings");
|
||||
prop.Next(true); //skip flag
|
||||
prop.Next(true);//skip enabled
|
||||
DrawRemainingProp(prop);
|
||||
}
|
||||
|
||||
EditorGUILayout.EndFadeGroup();
|
||||
|
||||
if (removed) {
|
||||
transEnabled.boolValue = false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
m_tabAdd.Add(new AlloyTabAdd {
|
||||
Color = s_transmissionColor,
|
||||
Name = "Transmission",
|
||||
Enable = EnableTransmission
|
||||
});
|
||||
}
|
||||
|
||||
if (m_skinGroup.isAnimating || m_transmissionGroup.isAnimating) {
|
||||
Repaint();
|
||||
}
|
||||
|
||||
AlloyEditor.DrawAddTabGUI(m_tabAdd);
|
||||
serializedObject.ApplyModifiedProperties();
|
||||
|
||||
if (GUI.changed) {
|
||||
var deferredRendererPlus = serializedObject.targetObject as AlloyEffectsManager;
|
||||
|
||||
if (deferredRendererPlus != null) {
|
||||
deferredRendererPlus.Refresh();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void EnableTransmission() {
|
||||
foreach (AlloyEffectsManager rend in targets) {
|
||||
rend.TransmissionSettings.Enabled = true;
|
||||
EditorUtility.SetDirty(rend);
|
||||
rend.Refresh();
|
||||
}
|
||||
|
||||
m_transmissionGroup.value = false;
|
||||
m_transmissionGroup.target = true;
|
||||
m_tabGroup.SetOpen(c_transmissionTabName, true);
|
||||
}
|
||||
|
||||
private void EnableSkin() {
|
||||
foreach (AlloyEffectsManager rend in targets) {
|
||||
rend.SkinSettings.Enabled = true;
|
||||
EditorUtility.SetDirty(rend);
|
||||
rend.Refresh();
|
||||
}
|
||||
|
||||
m_skinGroup.value = false;
|
||||
m_skinGroup.target = true;
|
||||
m_tabGroup.SetOpen(c_skinTabName, true);
|
||||
}
|
||||
|
||||
private static void DrawRemainingProp(SerializedProperty prop) {
|
||||
int depth = prop.depth;
|
||||
|
||||
while (true) {
|
||||
bool child = EditorGUILayout.PropertyField(prop, true);
|
||||
|
||||
if (!prop.Next(child) || prop.depth < depth)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4d4b26109848fff4daa6b5974417de47
|
||||
timeCreated: 1445386295
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 979804faf9172f54dbdbf0974928665d
|
||||
folderAsset: yes
|
||||
timeCreated: 1439605207
|
||||
licenseType: Pro
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,232 @@
|
||||
// Alloy Physical Shader Framework
|
||||
// Copyright 2013-2017 RUST LLC.
|
||||
// http://www.alloy.rustltd.com/
|
||||
|
||||
Shader "Hidden/Alloy/Blur Normals" {
|
||||
Properties {
|
||||
_MainTex ("Render Input", 2D) = "white" {}
|
||||
}
|
||||
SubShader {
|
||||
ZTest Always Cull Off ZWrite Off Fog { Mode Off }
|
||||
|
||||
CGINCLUDE
|
||||
#pragma target 3.0
|
||||
#pragma exclude_renderers gles
|
||||
|
||||
#include "Assets/Alloy/Shaders/Framework/Utility.cginc"
|
||||
|
||||
#include "HLSLSupport.cginc"
|
||||
#include "UnityCG.cginc"
|
||||
#include "UnityDeferredLibrary.cginc"
|
||||
|
||||
// Screen-space diffusion
|
||||
// cf http://www.iryoku.com/screen-space-subsurface-scattering
|
||||
// cf http://uaasoftware.com/xi/PDSS/diffuseShader.fx
|
||||
|
||||
// blurWidth = 0.15
|
||||
// blurDepthDifferenceMultiplier = 100
|
||||
// distanceToProjectionWindow = 1 / tan(radians(FoV) / 2);
|
||||
// blurStepScale = blurWidth * distanceToProjectionWindow
|
||||
// blurDepthDifferenceScale = blurDepthDifferenceMultiplier * distanceToProjectionWindow
|
||||
|
||||
/// Number of downsampling texture taps.
|
||||
#define A_NUM_DOWNSAMPLE_TAPS 4
|
||||
|
||||
/// Downsampling texture coordinate offset directions.
|
||||
static const float2 A_DOWNSAMPLE_OFFSETS[A_NUM_DOWNSAMPLE_TAPS] = {
|
||||
float2(0.0f, 0.0f),
|
||||
float2(1.0f, 0.0f),
|
||||
float2(0.0f, 1.0f),
|
||||
float2(1.0f, 1.0f)
|
||||
};
|
||||
|
||||
/// Number of blur texture taps.
|
||||
#define A_NUM_BLUR_TAPS 7
|
||||
|
||||
/// Gaussian Distribution blur texture coordinate offsets.
|
||||
static const float A_BLUR_OFFSETS[A_NUM_BLUR_TAPS] = {
|
||||
0.0f, -3.0f, -2.0f, -1.0f, 1.0f, 2.0f, 3.0f
|
||||
};
|
||||
|
||||
/// Gaussian Distribution blur sample weights.
|
||||
static const half A_BLUR_WEIGHTS[A_NUM_BLUR_TAPS] = {
|
||||
0.199471h, 0.0647588h, 0.120985h, 0.176033h, 0.176033h, 0.120985h, 0.0647588h
|
||||
};
|
||||
|
||||
/// Number of upsampling texture taps.
|
||||
#define A_NUM_UPSAMPLE_TAPS 4
|
||||
|
||||
/// Upsampling texture coordinate offset directions.
|
||||
static const float2 A_UPSAMPLE_OFFSETS[A_NUM_UPSAMPLE_TAPS] = {
|
||||
float2(0.0f, 1.0f),
|
||||
float2(1.0f, 0.0f),
|
||||
float2(-1.0f, 0.0f),
|
||||
float2(0.0f, -1.0f)
|
||||
};
|
||||
|
||||
/// (X: blurStepScale, Y: blurDepthDifferenceScale).
|
||||
float2 _DeferredBlurredNormalsParams;
|
||||
|
||||
// G-Buffer LAB (transmission in alpha).
|
||||
sampler2D _DeferredTransmissionBuffer;
|
||||
|
||||
/// Pass source texture.
|
||||
sampler2D _MainTex;
|
||||
|
||||
/// Pass source texture (X: Tiling X, Y: Tiling Y, Z: Offset X, W: Offset Y).
|
||||
float4 _MainTex_ST;
|
||||
|
||||
/// Pass source texture (X: 1 / width, Y: 1 / height, Z: width, W: height).
|
||||
float4 _MainTex_TexelSize;
|
||||
|
||||
/// Normal buffer.
|
||||
sampler2D _CameraGBufferTexture2;
|
||||
|
||||
/// Interpolates offet normals to center normals at edge discontinuities.
|
||||
/// @param normalDepth Offset sample (XYZ: Normals, W: Depth).
|
||||
/// @param normalDepthM Center sample (XYZ: Normals, W: Depth).
|
||||
/// @return Edge-corrected normal.
|
||||
half3 aEdgeCorrectNormal(
|
||||
half4 normalDepth,
|
||||
half4 normalDepthM)
|
||||
{
|
||||
// Cheaper than a 5-way blend, which requires inverts and an accumulator.
|
||||
half alpha = saturate(_DeferredBlurredNormalsParams.y * abs(normalDepth.w - normalDepthM.w));
|
||||
return lerp(normalDepth.xyz, normalDepthM.xyz, alpha);
|
||||
}
|
||||
|
||||
/// Downsamples the G-Buffer normals and depth to 1/2 resolution.
|
||||
/// @param IN Vertex input.
|
||||
/// @return Downsampled image (XYZ: Normals, W: Nearest Depth).
|
||||
half4 aDownsample(
|
||||
v2f_img IN)
|
||||
{
|
||||
half depth = 1.0h;
|
||||
half3 normal = 0.0h;
|
||||
|
||||
UNITY_UNROLL
|
||||
for (int i = 1; i < A_NUM_DOWNSAMPLE_TAPS; i++) {
|
||||
float2 coord = UnityStereoScreenSpaceUVAdjust(A_DOWNSAMPLE_OFFSETS[i] * _MainTex_TexelSize.xy + IN.uv, _MainTex_ST);
|
||||
float4 sampleUv = float4(coord, 0.0f, 0.0f);
|
||||
|
||||
// Pre-combine sample weight with normal scale-bias unpack.
|
||||
// 0.25 * (normal * 2 - 1) = normal * 0.5 - 0.25
|
||||
normal += tex2Dlod(_MainTex, sampleUv).xyz * 0.5h - 0.25h;
|
||||
|
||||
// Use projection depth directly to avoid linearizing cost per sample.
|
||||
depth = min(depth, SAMPLE_DEPTH_TEXTURE_LOD(_CameraDepthTexture, sampleUv));
|
||||
}
|
||||
|
||||
// Export unpacked normals and linear depth for subsequent passes.
|
||||
return half4(normal, LinearEyeDepth(depth));
|
||||
}
|
||||
|
||||
/// Blur the source image along the specified axis.
|
||||
/// @param IN Vertex input.
|
||||
/// @param axis Axis on which to blur.
|
||||
/// @return (XYZ: Blurred Normals, W: Sharp Depth).
|
||||
half4 aBlurAxis(
|
||||
v2f_img IN,
|
||||
float2 axis)
|
||||
{
|
||||
// Gaussian Blur.
|
||||
half4 normalDepthM = tex2Dlod(_MainTex, float4(UnityStereoScreenSpaceUVAdjust(IN.uv, _MainTex_ST), 0.0f, 0.0f));
|
||||
float scale = _DeferredBlurredNormalsParams.x / normalDepthM.w;
|
||||
float2 finalStep = scale * axis * _MainTex_TexelSize.xy;
|
||||
half3 output = A_BLUR_WEIGHTS[0] * normalDepthM.xyz;
|
||||
|
||||
UNITY_UNROLL
|
||||
for (int i = 1; i < A_NUM_BLUR_TAPS; i++) {
|
||||
float2 coord = UnityStereoScreenSpaceUVAdjust(A_BLUR_OFFSETS[i] * finalStep + IN.uv, _MainTex_ST);
|
||||
half4 normalDepth = tex2Dlod(_MainTex, float4(coord, 0.0f, 0.0f));
|
||||
|
||||
// Lerp back to middle sample when blur sample crosses an edge.
|
||||
output += A_BLUR_WEIGHTS[i] * aEdgeCorrectNormal(normalDepth, normalDepthM);
|
||||
}
|
||||
|
||||
// Transfer original depth for subsequent passes.
|
||||
return half4(output, normalDepthM.w);
|
||||
}
|
||||
|
||||
/// Upsamples the blurred normals.
|
||||
/// @param IN Vertex input.
|
||||
/// @return Upsampled, packed blurred normals.
|
||||
half4 aUpsample(
|
||||
v2f_img IN)
|
||||
{
|
||||
// Cross Bilateral Upsample filter.
|
||||
half4 normalDepthM;
|
||||
half4 output = 0.0h;
|
||||
float4 sampleUv = float4(UnityStereoScreenSpaceUVAdjust(IN.uv, _MainTex_ST), 0.0f, 0.0f);
|
||||
|
||||
normalDepthM.xyz = tex2Dlod(_CameraGBufferTexture2, sampleUv).xyz * 2.0f - 1.0f;
|
||||
normalDepthM.w = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_LOD(_CameraDepthTexture, sampleUv));
|
||||
|
||||
UNITY_UNROLL
|
||||
for (int i = 0; i < A_NUM_UPSAMPLE_TAPS; i++) {
|
||||
float2 coord = UnityStereoScreenSpaceUVAdjust(A_UPSAMPLE_OFFSETS[i] * _MainTex_TexelSize.xy + IN.uv, _MainTex_ST);
|
||||
half4 normalDepth = tex2Dlod(_MainTex, float4(coord, 0.0f, 0.0f));
|
||||
|
||||
output.xyz += 0.25h * aEdgeCorrectNormal(normalDepth, normalDepthM);
|
||||
}
|
||||
|
||||
// Pack normals and transmission for RGBA8 storage.
|
||||
output.xyz = normalize(output.xyz) * 0.5h + 0.5h;
|
||||
output.w = tex2Dlod(_DeferredTransmissionBuffer, sampleUv).a;
|
||||
return output;
|
||||
}
|
||||
ENDCG
|
||||
|
||||
Pass {
|
||||
CGPROGRAM
|
||||
#pragma target 3.0
|
||||
#pragma exclude_renderers gles
|
||||
#pragma vertex vert_img
|
||||
#pragma fragment frag
|
||||
|
||||
half4 frag(v2f_img IN) : SV_Target {
|
||||
return aDownsample(IN);
|
||||
}
|
||||
ENDCG
|
||||
}
|
||||
|
||||
Pass {
|
||||
CGPROGRAM
|
||||
#pragma target 3.0
|
||||
#pragma exclude_renderers gles
|
||||
#pragma vertex vert_img
|
||||
#pragma fragment frag
|
||||
|
||||
half4 frag(v2f_img IN) : SV_Target {
|
||||
return aBlurAxis(IN, float2(1.0f, 0.0f));
|
||||
}
|
||||
ENDCG
|
||||
}
|
||||
|
||||
Pass {
|
||||
CGPROGRAM
|
||||
#pragma target 3.0
|
||||
#pragma exclude_renderers gles
|
||||
#pragma vertex vert_img
|
||||
#pragma fragment frag
|
||||
|
||||
half4 frag(v2f_img IN) : SV_Target {
|
||||
return aBlurAxis(IN, float2(0.0f, 1.0f));
|
||||
}
|
||||
ENDCG
|
||||
}
|
||||
|
||||
Pass {
|
||||
CGPROGRAM
|
||||
#pragma target 3.0
|
||||
#pragma exclude_renderers gles
|
||||
#pragma vertex vert_img
|
||||
#pragma fragment frag
|
||||
|
||||
half4 frag(v2f_img IN) : SV_Target {
|
||||
return aUpsample(IN);
|
||||
}
|
||||
ENDCG
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5b34ef975c671984fb9c93adc7042982
|
||||
timeCreated: 1439655606
|
||||
licenseType: Pro
|
||||
ShaderImporter:
|
||||
defaultTextures: []
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,33 @@
|
||||
// Alloy Physical Shader Framework
|
||||
// Copyright 2013-2017 RUST LLC.
|
||||
// http://www.alloy.rustltd.com/
|
||||
|
||||
Shader "Hidden/Alloy/Transmission Blit" {
|
||||
Properties {
|
||||
_MainTex ("Render Input", 2D) = "white" {}
|
||||
}
|
||||
SubShader {
|
||||
ZTest Always
|
||||
Cull Off
|
||||
ZWrite Off
|
||||
Fog { Mode Off }
|
||||
|
||||
Pass {
|
||||
CGPROGRAM
|
||||
#pragma target 3.0
|
||||
#pragma exclude_renderers gles
|
||||
#pragma vertex vert_img
|
||||
#pragma fragment frag
|
||||
|
||||
#include "UnityCG.cginc"
|
||||
|
||||
sampler2D _MainTex;
|
||||
float4 _MainTex_ST;
|
||||
|
||||
float4 frag(v2f_img IN) : SV_Target {
|
||||
return tex2Dlod(_MainTex, float4(UnityStereoScreenSpaceUVAdjust(IN.uv, _MainTex_ST), 0.0f, 0.0f));
|
||||
}
|
||||
ENDCG
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4bbeef29a84b4ed498f2d4a7d9fcefc4
|
||||
timeCreated: 1439605218
|
||||
licenseType: Pro
|
||||
ShaderImporter:
|
||||
defaultTextures: []
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user