浏览代码

Creating mini shader graph library / fixing gamma -> linear colours

/main
Tim Cooper 7 年前
当前提交
ad906af4
共有 27 个文件被更改,包括 242 次插入100 次删除
  1. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/ColorMaterialSlot.cs
  2. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/ColorRGBMaterialSlot.cs
  3. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/ColorNode.cs
  4. 13
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/GraphUtil.cs
  5. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/ShaderGenerator.cs
  6. 9
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/DefaultShaderIncludes.cs
  7. 20
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightPBRForwardPass.template
  8. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/ShaderGraphLibrary.meta
  9. 14
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/ShaderGraphLibrary/Functions.hlsl
  10. 9
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/ShaderGraphLibrary/Functions.hlsl.meta
  11. 173
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/ShaderGraphLibrary/GeometricTools.hlsl
  12. 9
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/ShaderGraphLibrary/GeometricTools.hlsl.meta
  13. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/LightWeightShaderPaths.cs.meta
  14. 21
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/LightWeightShaderPaths.cs
  15. 26
      MaterialGraphProject/Assets/Beachball.mat
  16. 8
      MaterialGraphProject/Assets/Beachball.mat.meta
  17. 9
      MaterialGraphProject/Assets/ASDF.shader.meta
  18. 0
      /MaterialGraphProject/Assets/UnityShaderEditor/Editor/ShaderGraphLibrary/ShaderConfig.cs.hlsl
  19. 0
      /MaterialGraphProject/Assets/UnityShaderEditor/Editor/ShaderGraphLibrary/ShaderConfig.cs.hlsl.meta
  20. 0
      /MaterialGraphProject/Assets/UnityShaderEditor/Editor/ShaderGraphLibrary/ShaderVariables.hlsl
  21. 0
      /MaterialGraphProject/Assets/UnityShaderEditor/Editor/ShaderGraphLibrary/ShaderVariables.hlsl.meta
  22. 0
      /MaterialGraphProject/Assets/UnityShaderEditor/Editor/ShaderGraphLibrary/ShaderVariablesFunctions.hlsl
  23. 0
      /MaterialGraphProject/Assets/UnityShaderEditor/Editor/ShaderGraphLibrary/ShaderVariablesFunctions.hlsl.meta
  24. 0
      /MaterialGraphProject/Assets/UnityShaderEditor/Editor/ShaderGraphLibrary/ShaderVariablesMatrixDefsLegacyUnity.hlsl
  25. 0
      /MaterialGraphProject/Assets/UnityShaderEditor/Editor/ShaderGraphLibrary/ShaderVariablesMatrixDefsLegacyUnity.hlsl.meta

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/ColorMaterialSlot.cs


protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
return string.Format("IsGammaSpace() ? {0}4({1}, {2}, {3}, {4}) : {0}4 (GammaToLinearSpace({0}3({1}, {2}, {3})), {4})"
return string.Format("IsGammaSpace() ? {0}4({1}, {2}, {3}, {4}) : {0}4 (SRGBToLinear({0}3({1}, {2}, {3})), {4})"
, precision
, value.x
, value.y

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/ColorRGBMaterialSlot.cs


protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
return string.Format("IsGammaSpace() ? {0}3({1}, {2}, {3}) : GammaToLinearSpace({0}3({1}, {2}, {3}))"
return string.Format("IsGammaSpace() ? {0}3({1}, {2}, {3}) : SRGBToLinear({0}3({1}, {2}, {3}))"
, precision
, value.x
, value.y

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/ColorNode.cs


return;
visitor.AddShaderChunk(string.Format(
@"{0}4 {1} = IsGammaSpace() ? {0}4({2}, {3}, {4}, {5}) : {0}4(GammaToLinearSpace({0}3({2}, {3}, {4})), {5});"
@"{0}4 {1} = IsGammaSpace() ? {0}4({2}, {3}, {4}, {5}) : {0}4(SRGBToLinear({0}3({2}, {3}, {4})), {5});"
, precision
, GetVariableNameForNode()
, color.r

13
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/GraphUtil.cs


vertexInputs.AddShaderChunk(String.Format("float4 texcoord{0} : TEXCOORD{1};", ((int)channel).ToString(), vertexInputIndex.ToString()), false);
vertexInputIndex++;
}
vertexInputs.Deindent();
vertexInputs.AddShaderChunk("};", false);
}

finalBuilder.AppendLine(@"HLSLINCLUDE");
finalBuilder.AppendLine("#define USE_LEGACY_UNITY_MATRIX_VARIABLES");
finalBuilder.AppendLine(@"#include ""Common.hlsl""");
finalBuilder.AppendLine(@"#include ""Packing.hlsl""");
finalBuilder.AppendLine(@"#include ""ShaderVariables.hlsl""");
finalBuilder.AppendLine(@"#include ""ShaderVariablesFunctions.hlsl""");
finalBuilder.AppendLine(@"#include ""ShaderLibrary/Common.hlsl""");
finalBuilder.AppendLine(@"#include ""ShaderLibrary/Packing.hlsl""");
finalBuilder.AppendLine(@"#include ""ShaderLibrary/Color.hlsl""");
finalBuilder.AppendLine(@"#include ""ShaderGraphLibrary/Functions.hlsl""");
finalBuilder.AppendLine(@"#include ""ShaderGraphLibrary/ShaderVariables.hlsl""");
finalBuilder.AppendLine(@"#include ""ShaderGraphLibrary/ShaderVariablesFunctions.hlsl""");
finalBuilder.Concat(functionBuilder);
finalBuilder.AppendLines(vertexInputs.GetShaderString(0));
finalBuilder.AppendLines(surfaceInputs.GetShaderString(0));

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/ShaderGenerator.cs


Pass
{
HLSLPROGRAM
#define USE_LEGACY_UNITY_MATRIX_VARIABLES
#include ""Common.hlsl""
#include ""ShaderVariables.hlsl""
#include ""ShaderVariablesFunctions.hlsl""
#pragma vertex vert
#pragma fragment frag

9
MaterialGraphProject/Assets/UnityShaderEditor/Editor/DefaultShaderIncludes.cs


using System.IO;
namespace UnityEditor
{
internal static class DefaultShaderIncludes

{
return new[]
{
"Assets/ScriptableRenderPipeline/",
"Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders"
Path.GetFullPath("Packages/com.unity.render-pipelines.core"),
Path.GetFullPath("Packages/com.unity.render-pipelines.lightweight/Shaders"),
Path.GetFullPath("Packages/com.unity.render-pipelines.high-quality/Material/Unlit"),
Path.GetFullPath("Assets/UnityShaderEditor/Editor"),
Path.GetFullPath("Packages/com.unity.shadergraph/Editor"),
};
}
}

20
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightPBRForwardPass.template


${Culling}
${ZTest}
${ZWrite}
#pragma multi_compile _ _MAIN_LIGHT_COOKIE
#pragma multi_compile _MAIN_DIRECTIONAL_LIGHT _MAIN_SPOT_LIGHT
#pragma multi_compile _ _ADDITIONAL_LIGHTS

#pragma vertex vert
#pragma fragment frag
#include "ShaderLibrary/Color.hlsl"
#include "ShaderGraphLibrary/Functions.hlsl"
struct GraphVertexOutput
{
float4 clipPos : SV_POSITION;

};
GraphVertexOutput o = (GraphVertexOutput)0;
${VertexShader}

${SurfaceOutputRemap}
#if defined(UNITY_COLORSPACE_GAMMA)
#if defined(UNITY_COLORSPACE_GAMMA)
Albedo = Albedo * Albedo;
Emission = Emission * Emission;
#endif

normalWS,
WorldSpaceViewDirection,
indirectDiffuse,
IN.fogFactorAndVertexLight.yzw,
IN.fogFactorAndVertexLight.yzw,
Albedo,
Metallic,
Specular,

}
ENDHLSL
}
}

8
MaterialGraphProject/Assets/UnityShaderEditor/Editor/ShaderGraphLibrary.meta


fileFormatVersion: 2
guid: c1bc9b92d5eaa40f8a85c1f48ba87de9
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

14
MaterialGraphProject/Assets/UnityShaderEditor/Editor/ShaderGraphLibrary/Functions.hlsl


// UNITY_SHADER_NO_UPGRADE
#ifndef UNITY_SHADER_GRAPH_INCLUDED
#define UNITY_SHADER_GRAPH_INCLUDED
bool IsGammaSpace()
{
#ifdef UNITY_COLORSPACE_GAMMA
return true;
#else
return false;
#endif
}
#endif // UNITY_SHADER_GRAPH_INCLUDED

9
MaterialGraphProject/Assets/UnityShaderEditor/Editor/ShaderGraphLibrary/Functions.hlsl.meta


fileFormatVersion: 2
guid: 76c5c0b7561134f0ca526e50f239a4a2
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

173
MaterialGraphProject/Assets/UnityShaderEditor/Editor/ShaderGraphLibrary/GeometricTools.hlsl


#ifndef UNITY_GEOMETRICTOOLS_INCLUDED
#define UNITY_GEOMETRICTOOLS_INCLUDED
//-----------------------------------------------------------------------------
// Intersection functions
//-----------------------------------------------------------------------------
// return furthest near intersection in x and closest far intersection in y
// if (intersections.y > intersections.x) the ray hit the box, else it miss it
// Assume dir is normalize
float2 BoxRayIntersect(float3 start, float3 dir, float3 boxMin, float3 boxMax)
{
float3 invDir = 1.0 / dir;
// Find the ray intersection with box plane
float3 firstPlaneIntersect = (boxMin - start) * invDir;
float3 secondPlaneIntersect = (boxMax - start) * invDir;
// Get the closest/furthest of these intersections along the ray (Ok because x/0 give +inf and -x/0 give �inf )
float3 closestPlane = min(firstPlaneIntersect, secondPlaneIntersect);
float3 furthestPlane = max(firstPlaneIntersect, secondPlaneIntersect);
float2 intersections;
// Find the furthest near intersection
intersections.x = max(closestPlane.x, max(closestPlane.y, closestPlane.z));
// Find the closest far intersection
intersections.y = min(min(furthestPlane.x, furthestPlane.y), furthestPlane.z);
return intersections;
}
// This simplified version assume that we care about the result only when we are inside the box
// Assume dir is normalize
float BoxRayIntersectSimple(float3 start, float3 dir, float3 boxMin, float3 boxMax)
{
float3 invDir = 1.0 / dir;
// Find the ray intersection with box plane
float3 rbmin = (boxMin - start) * invDir;
float3 rbmax = (boxMax - start) * invDir;
float3 rbminmax = (dir > 0.0) ? rbmax : rbmin;
return min(min(rbminmax.x, rbminmax.y), rbminmax.z);
}
// Assume Sphere is at the origin (i.e start = position - spherePosition)
float2 SphereRayIntersect(float3 start, float3 dir, float radius, out bool intersect)
{
float a = dot(dir, dir);
float b = dot(dir, start) * 2.0;
float c = dot(start, start) - radius * radius;
float discriminant = b * b - 4.0 * a * c;
float2 intersections = float2(0.0, 0.0);
intersect = false;
if (discriminant < 0.0 || a == 0.0)
{
intersections.x = 0.0;
intersections.y = 0.0;
}
else
{
float sqrtDiscriminant = sqrt(discriminant);
intersections.x = (-b - sqrtDiscriminant) / (2.0 * a);
intersections.y = (-b + sqrtDiscriminant) / (2.0 * a);
intersect = true;
}
return intersections;
}
// This simplified version assume that we care about the result only when we are inside the sphere
// Assume Sphere is at the origin (i.e start = position - spherePosition) and dir is normalized
// Ref: http://http.developer.nvidia.com/GPUGems/gpugems_ch19.html
float SphereRayIntersectSimple(float3 start, float3 dir, float radius)
{
float b = dot(dir, start) * 2.0;
float c = dot(start, start) - radius * radius;
float discriminant = b * b - 4.0 * c;
return abs(sqrt(discriminant) - b) * 0.5;
}
float3 RayPlaneIntersect(in float3 rayOrigin, in float3 rayDirection, in float3 planeOrigin, in float3 planeNormal)
{
float dist = dot(planeNormal, planeOrigin - rayOrigin) / dot(planeNormal, rayDirection);
return rayOrigin + rayDirection * dist;
}
//-----------------------------------------------------------------------------
// Miscellaneous functions
//-----------------------------------------------------------------------------
// Box is AABB
float DistancePointBox(float3 position, float3 boxMin, float3 boxMax)
{
return length(max(max(position - boxMax, boxMin - position), float3(0.0, 0.0, 0.0)));
}
float3 ProjectPointOnPlane(float3 position, float3 planePosition, float3 planeNormal)
{
return position - (dot(position - planePosition, planeNormal) * planeNormal);
}
// Plane equation: {(a, b, c) = N, d = -dot(N, P)}.
// Returns the distance from the plane to the point 'p' along the normal.
// Positive -> in front (above), negative -> behind (below).
float DistanceFromPlane(float3 p, float4 plane)
{
return dot(float4(p, 1.0), plane);
}
// Returns 'true' if the triangle is outside of the frustum.
// 'epsilon' is the (negative) distance to (outside of) the frustum below which we cull the triangle.
bool CullTriangleFrustum(float3 p0, float3 p1, float3 p2, float epsilon, float4 frustumPlanes[6], int numPlanes)
{
bool outside = false;
for (int i = 0; i < numPlanes; i++)
{
// If all 3 points are behind any of the planes, we cull.
outside = outside || Max3(DistanceFromPlane(p0, frustumPlanes[i]),
DistanceFromPlane(p1, frustumPlanes[i]),
DistanceFromPlane(p2, frustumPlanes[i])) < epsilon;
}
return outside;
}
// Returns 'true' if the edge of the triangle is outside of the frustum.
// The edges are defined s.t. they are on the opposite side of the point with the given index.
// 'epsilon' is the (negative) distance to (outside of) the frustum below which we cull the triangle.
bool3 CullTriangleEdgesFrustum(float3 p0, float3 p1, float3 p2, float epsilon, float4 frustumPlanes[6], int numPlanes)
{
bool3 edgesOutside = false;
for (int i = 0; i < numPlanes; i++)
{
bool3 pointsOutside = bool3(DistanceFromPlane(p0, frustumPlanes[i]) < epsilon,
DistanceFromPlane(p1, frustumPlanes[i]) < epsilon,
DistanceFromPlane(p2, frustumPlanes[i]) < epsilon);
// If both points of the edge are behind any of the planes, we cull.
edgesOutside.x = edgesOutside.x || (pointsOutside.y && pointsOutside.z);
edgesOutside.y = edgesOutside.y || (pointsOutside.x && pointsOutside.z);
edgesOutside.z = edgesOutside.z || (pointsOutside.x && pointsOutside.y);
}
return edgesOutside;
}
// Returns 'true' if a triangle defined by 3 vertices is back-facing.
// 'epsilon' is the (negative) value of dot(N, V) below which we cull the triangle.
// 'winding' can be used to change the order: pass 1 for (p0 -> p1 -> p2), or -1 for (p0 -> p2 -> p1).
bool CullTriangleBackFace(float3 p0, float3 p1, float3 p2, float epsilon, float3 viewPos, float winding)
{
float3 edge1 = p1 - p0;
float3 edge2 = p2 - p0;
float3 N = cross(edge1, edge2);
float3 V = viewPos - p0;
float NdotV = dot(N, V) * winding;
// Optimize:
// NdotV / (length(N) * length(V)) < Epsilon
// NdotV < Epsilon * length(N) * length(V)
// NdotV < Epsilon * sqrt(dot(N, N)) * sqrt(dot(V, V))
// NdotV < Epsilon * sqrt(dot(N, N) * dot(V, V))
return NdotV < epsilon * sqrt(dot(N, N) * dot(V, V));
}
#endif // UNITY_GEOMETRICTOOLS_INCLUDED

9
MaterialGraphProject/Assets/UnityShaderEditor/Editor/ShaderGraphLibrary/GeometricTools.hlsl.meta


fileFormatVersion: 2
guid: 07d3f5482ecfe425f91b12cd32be4cbe
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/LightWeightShaderPaths.cs.meta


fileFormatVersion: 2
guid: cddd8645d2052584eaee32f4bfc5cc0b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

21
MaterialGraphProject/Assets/UnityShaderEditor/Editor/LightWeightShaderPaths.cs


using System.Linq;
using UnityEngine;
using System.IO;
namespace UnityEditor.Experimental.Rendering
{
static class LightweightIncludePaths
{
[ShaderIncludePath]
public static string[] GetPaths()
{
return new[]
{
Path.GetFullPath("Assets"),
Path.GetFullPath("Packages/com.unity.render-pipelines.core/ShaderLibrary"),
Path.GetFullPath("Packages/com.unity.render-pipelines.lightweight/Shaders"),
Path.GetFullPath("Packages/com.unity.render-pipelines.high-quality/Material/Unlit")
};
}
}
}

26
MaterialGraphProject/Assets/Beachball.mat


%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: Beachball
m_Shader: {fileID: 4800000, guid: b141c720617d37f45a2fd27af59ccf42, type: 3}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _Texture2D_A24EBBFA_Tex:
m_Texture: {fileID: 2800000, guid: e017404169662f041a969b0a924cc2ed, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats: []
m_Colors: []

8
MaterialGraphProject/Assets/Beachball.mat.meta


fileFormatVersion: 2
guid: b232af1498b13d14db58368a73b45744
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 2100000
userData:
assetBundleName:
assetBundleVariant:

9
MaterialGraphProject/Assets/ASDF.shader.meta


fileFormatVersion: 2
guid: 13688daeb472a81479c26aeef30f92e8
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

/MaterialGraphProject/Assets/ShaderConfig.cs.hlsl → /MaterialGraphProject/Assets/UnityShaderEditor/Editor/ShaderGraphLibrary/ShaderConfig.cs.hlsl

/MaterialGraphProject/Assets/ShaderConfig.cs.hlsl.meta → /MaterialGraphProject/Assets/UnityShaderEditor/Editor/ShaderGraphLibrary/ShaderConfig.cs.hlsl.meta

/MaterialGraphProject/Assets/ShaderVariables.hlsl → /MaterialGraphProject/Assets/UnityShaderEditor/Editor/ShaderGraphLibrary/ShaderVariables.hlsl

/MaterialGraphProject/Assets/ShaderVariables.hlsl.meta → /MaterialGraphProject/Assets/UnityShaderEditor/Editor/ShaderGraphLibrary/ShaderVariables.hlsl.meta

/MaterialGraphProject/Assets/ShaderVariablesFunctions.hlsl → /MaterialGraphProject/Assets/UnityShaderEditor/Editor/ShaderGraphLibrary/ShaderVariablesFunctions.hlsl

/MaterialGraphProject/Assets/ShaderVariablesFunctions.hlsl.meta → /MaterialGraphProject/Assets/UnityShaderEditor/Editor/ShaderGraphLibrary/ShaderVariablesFunctions.hlsl.meta

/MaterialGraphProject/Assets/ShaderVariablesMatrixDefsLegacyUnity.hlsl → /MaterialGraphProject/Assets/UnityShaderEditor/Editor/ShaderGraphLibrary/ShaderVariablesMatrixDefsLegacyUnity.hlsl

/MaterialGraphProject/Assets/ShaderVariablesMatrixDefsLegacyUnity.hlsl.meta → /MaterialGraphProject/Assets/UnityShaderEditor/Editor/ShaderGraphLibrary/ShaderVariablesMatrixDefsLegacyUnity.hlsl.meta

正在加载...
取消
保存