浏览代码

Working HD / LW unlit from same shader file.

/main
Tim Cooper 7 年前
当前提交
f71d562a
共有 6 个文件被更改,包括 204 次插入199 次删除
  1. 356
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/HDPipeline/HDUnlitSubShader.cs
  2. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightWeightUnlitSubShader.cs
  3. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/MasterNodes/UnlitMasterNode.cs
  4. 16
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/HDUnlitPassForward.template
  5. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/HDPipeline/IUnlitSubShader.cs
  6. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/HDPipeline/IUnlitSubShader.cs.meta

356
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/HDPipeline/HDUnlitSubShader.cs


//using System.Collections.Generic;
//using System.IO;
//using System.Linq;
//using UnityEditor.Graphing;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor.Graphing;
//namespace UnityEditor.ShaderGraph
//{
// public class HDUnlitSubShader
// {
// struct Pass
// {
// public string Name;
// public string ShaderPassName;
// public string ShaderPassInclude;
// public List<int> VertexShaderSlots;
// public List<int> PixelShaderSlots;
// }
namespace UnityEditor.ShaderGraph
{
public class HDUnlitSubShader : IUnlitSubShader
{
struct Pass
{
public string Name;
public string ShaderPassName;
public string ShaderPassInclude;
public List<int> VertexShaderSlots;
public List<int> PixelShaderSlots;
}
// Pass m_UnlitPassForwardOnly = new Pass()
// {
// Name = "ForwardOnly",
// ShaderPassName = "SHADERPASS_FORWARD_UNLIT",
// ShaderPassInclude = "ShaderPassForwardUnlit",
// PixelShaderSlots = new List<int>()
// {
// UnlitMasterNode.ColorSlotId,
// UnlitMasterNode.AlphaSlotId
// }
// };
Pass m_UnlitPassForwardOnly = new Pass()
{
Name = "ForwardOnly",
ShaderPassName = "SHADERPASS_FORWARD_UNLIT",
ShaderPassInclude = "ShaderPassForwardUnlit",
PixelShaderSlots = new List<int>()
{
UnlitMasterNode.ColorSlotId,
UnlitMasterNode.AlphaSlotId
}
};
// Pass m_UnlitPassForwardDepthOnly = new Pass()
// {
// Name = "DepthForwardOnly",
// ShaderPassName = "SHADERPASS_DEPTH_ONLY",
// ShaderPassInclude = "ShaderPassDepthOnly",
// PixelShaderSlots = new List<int>()
// {
// UnlitMasterNode.ColorSlotId,
// UnlitMasterNode.AlphaSlotId
// }
// };
Pass m_UnlitPassForwardDepthOnly = new Pass()
{
Name = "DepthForwardOnly",
ShaderPassName = "SHADERPASS_DEPTH_ONLY",
ShaderPassInclude = "ShaderPassDepthOnly",
PixelShaderSlots = new List<int>()
{
UnlitMasterNode.ColorSlotId,
UnlitMasterNode.AlphaSlotId
}
};
// private static string GetShaderPassFromTemplate(UnlitMasterNode masterNode, Pass pass, GenerationMode mode)
// {
// var surfaceDescriptionFunction = new ShaderGenerator();
// var surfaceDescriptionStruct = new ShaderGenerator();
// var shaderFunctionVisitor = new ShaderGenerator();
// var surfaceInputs = new ShaderGenerator();
private static string GetShaderPassFromTemplate(UnlitMasterNode masterNode, Pass pass, GenerationMode mode)
{
var builder = new ShaderStringBuilder();
builder.IncreaseIndent();
builder.IncreaseIndent();
// var shaderProperties = new PropertyCollector();
var surfaceDescriptionFunction = new ShaderGenerator();
var surfaceDescriptionStruct = new ShaderGenerator();
var shaderFunctionVisitor = new ShaderGenerator();
var surfaceInputs = new ShaderGenerator();
var functionRegistry = new FunctionRegistry(builder);
// surfaceInputs.AddShaderChunk("struct SurfaceInputs{", false);
// surfaceInputs.Indent();
var shaderProperties = new PropertyCollector();
// var activeNodeList = ListPool<INode>.Get();
// NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, masterNode, NodeUtils.IncludeSelf.Include, pass.PixelShaderSlots);
surfaceInputs.AddShaderChunk("struct SurfaceInputs{", false);
surfaceInputs.Indent();
// var requirements = AbstractMaterialGraph.GetRequirements(activeNodeList);
var activeNodeList = ListPool<INode>.Get();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, masterNode, NodeUtils.IncludeSelf.Include, pass.PixelShaderSlots);
// /*
// ShaderGenerator.GenerateSpaceTranslationSurfaceInputs(requirements.requiresNormal, InterpolatorType.Normal, surfaceInputs);
// ShaderGenerator.GenerateSpaceTranslationSurfaceInputs(requirements.requiresTangent, InterpolatorType.Tangent, surfaceInputs);
// ShaderGenerator.GenerateSpaceTranslationSurfaceInputs(requirements.requiresBitangent, InterpolatorType.BiTangent, surfaceInputs);
// ShaderGenerator.GenerateSpaceTranslationSurfaceInputs(requirements.requiresViewDir, InterpolatorType.ViewDirection, surfaceInputs);
// ShaderGenerator.GenerateSpaceTranslationSurfaceInputs(requirements.requiresPosition, InterpolatorType.Position, surfaceInputs);*/
var requirements = ShaderGraphRequirements.FromNodes(activeNodeList);
/*
ShaderGenerator.GenerateSpaceTranslationSurfaceInputs(requirements.requiresNormal, InterpolatorType.Normal, surfaceInputs);
ShaderGenerator.GenerateSpaceTranslationSurfaceInputs(requirements.requiresTangent, InterpolatorType.Tangent, surfaceInputs);
ShaderGenerator.GenerateSpaceTranslationSurfaceInputs(requirements.requiresBitangent, InterpolatorType.BiTangent, surfaceInputs);
ShaderGenerator.GenerateSpaceTranslationSurfaceInputs(requirements.requiresViewDir, InterpolatorType.ViewDirection, surfaceInputs);
ShaderGenerator.GenerateSpaceTranslationSurfaceInputs(requirements.requiresPosition, InterpolatorType.Position, surfaceInputs);*/
// ShaderGenerator defines = new ShaderGenerator();
// defines.AddShaderChunk(string.Format("#define SHADERPASS {0}", pass.ShaderPassName), true);
// if (requirements.requiresVertexColor)
// surfaceInputs.AddShaderChunk(string.Format("float4 {0};", ShaderGeneratorNames.VertexColor), false);
ShaderGenerator defines = new ShaderGenerator();
defines.AddShaderChunk(string.Format("#define SHADERPASS {0}", pass.ShaderPassName), true);
// /*if (requirements.requiresScreenPosition)
// surfaceInputs.AddShaderChunk(string.Format("float4 {0};", ShaderGeneratorNames.ScreenPosition), false);*/
if (requirements.requiresVertexColor)
surfaceInputs.AddShaderChunk(string.Format("float4 {0};", ShaderGeneratorNames.VertexColor), false);
// foreach (var channel in requirements.requiresMeshUVs.Distinct())
// {
// surfaceInputs.AddShaderChunk(string.Format("half4 {0};", channel.GetUVName()), false);
// defines.AddShaderChunk("#define ATTRIBUTES_NEED_TEXCOORD" + (int)channel, true);
// defines.AddShaderChunk("#define VARYINGS_NEED_TEXCOORD" + (int)channel, true);
// }
/*if (requirements.requiresScreenPosition)
surfaceInputs.AddShaderChunk(string.Format("float4 {0};", ShaderGeneratorNames.ScreenPosition), false);*/
// surfaceInputs.Deindent();
// surfaceInputs.AddShaderChunk("};", false);
foreach (var channel in requirements.requiresMeshUVs.Distinct())
{
surfaceInputs.AddShaderChunk(string.Format("half4 {0};", channel.GetUVName()), false);
defines.AddShaderChunk("#define ATTRIBUTES_NEED_TEXCOORD" + (int)channel, true);
defines.AddShaderChunk("#define VARYINGS_NEED_TEXCOORD" + (int)channel, true);
}
// var slots = new List<MaterialSlot>();
// foreach (var id in pass.PixelShaderSlots)
// {
// var slot = masterNode.FindSlot<MaterialSlot>(id);
// if (slot != null)
// slots.Add(slot);
// }
// AbstractMaterialGraph.GenerateSurfaceDescriptionStruct(surfaceDescriptionStruct, slots, true);
surfaceInputs.Deindent();
surfaceInputs.AddShaderChunk("};", false);
// var usedSlots = new List<MaterialSlot>();
// foreach (var id in pass.PixelShaderSlots)
// usedSlots.Add(masterNode.FindSlot<MaterialSlot>(id));
var slots = new List<MaterialSlot>();
foreach (var id in pass.PixelShaderSlots)
{
var slot = masterNode.FindSlot<MaterialSlot>(id);
if (slot != null)
slots.Add(slot);
}
GraphUtil.GenerateSurfaceDescriptionStruct(surfaceDescriptionStruct, slots, true);
// AbstractMaterialGraph.GenerateSurfaceDescription(
// activeNodeList,
// masterNode,
// masterNode.owner as AbstractMaterialGraph,
// surfaceDescriptionFunction,
// shaderFunctionVisitor,
// shaderProperties,
// requirements,
// mode,
// "PopulateSurfaceData",
// "SurfaceDescription",
// null,
// null,
// usedSlots);
var usedSlots = new List<MaterialSlot>();
foreach (var id in pass.PixelShaderSlots)
usedSlots.Add(masterNode.FindSlot<MaterialSlot>(id));
// var graph = new ShaderGenerator();
// graph.AddShaderChunk(shaderFunctionVisitor.GetShaderString(2), false);
// graph.AddShaderChunk(surfaceInputs.GetShaderString(2), false);
// graph.AddShaderChunk(surfaceDescriptionStruct.GetShaderString(2), false);
// graph.AddShaderChunk(shaderProperties.GetPropertiesDeclaration(2), false);
// graph.AddShaderChunk(surfaceDescriptionFunction.GetShaderString(2), false);
GraphUtil.GenerateSurfaceDescription(
activeNodeList,
masterNode,
masterNode.owner as AbstractMaterialGraph,
surfaceDescriptionFunction,
functionRegistry,
shaderProperties,
requirements,
mode,
"PopulateSurfaceData",
"SurfaceDescription",
null,
usedSlots);
// var tagsVisitor = new ShaderGenerator();
// var blendingVisitor = new ShaderGenerator();
// var cullingVisitor = new ShaderGenerator();
// var zTestVisitor = new ShaderGenerator();
// var zWriteVisitor = new ShaderGenerator();
var graph = new ShaderGenerator();
graph.AddShaderChunk(shaderFunctionVisitor.GetShaderString(2), false);
graph.AddShaderChunk(surfaceInputs.GetShaderString(2), false);
graph.AddShaderChunk(surfaceDescriptionStruct.GetShaderString(2), false);
graph.AddShaderChunk(shaderProperties.GetPropertiesDeclaration(2), false);
graph.AddShaderChunk(surfaceDescriptionFunction.GetShaderString(2), false);
// var materialOptions = new SurfaceMaterialOptions();
// materialOptions.GetTags(tagsVisitor);
// materialOptions.GetBlend(blendingVisitor);
// materialOptions.GetCull(cullingVisitor);
// materialOptions.GetDepthTest(zTestVisitor);
// materialOptions.GetDepthWrite(zWriteVisitor);
// var localPixelShader = new ShaderGenerator();
// var localSurfaceInputs = new ShaderGenerator();
// var surfaceOutputRemap = new ShaderGenerator();
var tagsVisitor = new ShaderGenerator();
var blendingVisitor = new ShaderGenerator();
var cullingVisitor = new ShaderGenerator();
var zTestVisitor = new ShaderGenerator();
var zWriteVisitor = new ShaderGenerator();
// var reqs = ShaderGraphRequirements.none;
// reqs.requiresNormal |= NeededCoordinateSpace.World;
// reqs.requiresTangent |= NeededCoordinateSpace.World;
// reqs.requiresBitangent |= NeededCoordinateSpace.World;
// reqs.requiresPosition |= NeededCoordinateSpace.World;
// reqs.requiresViewDir |= NeededCoordinateSpace.World;
// foreach (var channel in requirements.requiresMeshUVs.Distinct())
// localSurfaceInputs.AddShaderChunk(string.Format("surfaceInput.{0} = {1};", channel.GetUVName(), string.Format("half4(input.texCoord{0}, 0, 0)", (int)channel)), false);
var materialOptions = new SurfaceMaterialOptions();
materialOptions.GetTags(tagsVisitor);
materialOptions.GetBlend(blendingVisitor);
materialOptions.GetCull(cullingVisitor);
materialOptions.GetDepthTest(zTestVisitor);
materialOptions.GetDepthWrite(zWriteVisitor);
// /*ShaderGenerator.GenerateStandardTransforms(
// 3,
// 10,
// interpolators,
// localVertexShader,
// localPixelShader,
// localSurfaceInputs,
// requirements,
// reqs,
// CoordinateSpace.World);*/
var localPixelShader = new ShaderGenerator();
var localSurfaceInputs = new ShaderGenerator();
var surfaceOutputRemap = new ShaderGenerator();
// var templateLocation = ShaderGenerator.GetTemplatePath("HDUnlitPassForward.template");
foreach (var channel in requirements.requiresMeshUVs.Distinct())
localSurfaceInputs.AddShaderChunk(string.Format("surfaceInput.{0} = {1};", channel.GetUVName(), string.Format("half4(input.texCoord{0}, 0, 0)", (int)channel)), false);
// foreach (var slot in usedSlots)
// {
// surfaceOutputRemap.AddShaderChunk(slot.shaderOutputName
// + " = surf."
// + slot.shaderOutputName + ";", true);
// }
var templateLocation = ShaderGenerator.GetTemplatePath("HDUnlitPassForward.template");
// if (!File.Exists(templateLocation))
// return string.Empty;
foreach (var slot in usedSlots)
{
surfaceOutputRemap.AddShaderChunk(slot.shaderOutputName
+ " = surf."
+ slot.shaderOutputName + ";", true);
}
// var subShaderTemplate = File.ReadAllText(templateLocation);
// var resultPass = subShaderTemplate.Replace("${Defines}", defines.GetShaderString(3));
// resultPass = resultPass.Replace("${Graph}", graph.GetShaderString(3));
// resultPass = resultPass.Replace("${LocalPixelShader}", localPixelShader.GetShaderString(3));
// resultPass = resultPass.Replace("${SurfaceInputs}", localSurfaceInputs.GetShaderString(3));
// resultPass = resultPass.Replace("${SurfaceOutputRemap}", surfaceOutputRemap.GetShaderString(3));
// resultPass = resultPass.Replace("${LightMode}", pass.Name);
// resultPass = resultPass.Replace("${ShaderPassInclude}", pass.ShaderPassInclude);
if (!File.Exists(templateLocation))
return string.Empty;
var subShaderTemplate = File.ReadAllText(templateLocation);
var resultPass = subShaderTemplate.Replace("${Defines}", defines.GetShaderString(3));
resultPass = resultPass.Replace("${Graph}", graph.GetShaderString(3));
resultPass = resultPass.Replace("${LocalPixelShader}", localPixelShader.GetShaderString(3));
resultPass = resultPass.Replace("${SurfaceInputs}", localSurfaceInputs.GetShaderString(3));
resultPass = resultPass.Replace("${SurfaceOutputRemap}", surfaceOutputRemap.GetShaderString(3));
resultPass = resultPass.Replace("${LightMode}", pass.Name);
resultPass = resultPass.Replace("${ShaderPassInclude}", pass.ShaderPassInclude);
// resultPass = resultPass.Replace("${Tags}", tagsVisitor.GetShaderString(2));
// resultPass = resultPass.Replace("${Blending}", blendingVisitor.GetShaderString(2));
// resultPass = resultPass.Replace("${Culling}", cullingVisitor.GetShaderString(2));
// resultPass = resultPass.Replace("${ZTest}", zTestVisitor.GetShaderString(2));
// resultPass = resultPass.Replace("${ZWrite}", zWriteVisitor.GetShaderString(2));
// resultPass = resultPass.Replace("${LOD}", "" + materialOptions.lod);
// return resultPass;
// }
resultPass = resultPass.Replace("${Tags}", tagsVisitor.GetShaderString(2));
resultPass = resultPass.Replace("${Blending}", blendingVisitor.GetShaderString(2));
resultPass = resultPass.Replace("${Culling}", cullingVisitor.GetShaderString(2));
resultPass = resultPass.Replace("${ZTest}", zTestVisitor.GetShaderString(2));
resultPass = resultPass.Replace("${ZWrite}", zWriteVisitor.GetShaderString(2));
resultPass = resultPass.Replace("${LOD}", "" + materialOptions.lod);
return resultPass;
}
// public IEnumerable<string> GetSubshader(UnlitMasterNode masterNode, GenerationMode mode)
// {
// var subShader = new ShaderGenerator();
// subShader.AddShaderChunk("SubShader", true);
// subShader.AddShaderChunk("{", true);
// subShader.Indent();
// subShader.AddShaderChunk("Tags{ \"RenderType\" = \"Opaque\" }", true);
public string GetSubshader(UnlitMasterNode masterNode, GenerationMode mode)
{
var subShader = new ShaderGenerator();
subShader.AddShaderChunk("SubShader", true);
subShader.AddShaderChunk("{", true);
subShader.Indent();
subShader.AddShaderChunk("Tags{ \"RenderType\" = \"Opaque\" }", true);
// subShader.AddShaderChunk(
// GetShaderPassFromTemplate(
// masterNode,
// m_UnlitPassForwardDepthOnly,
// mode),
// true);
subShader.AddShaderChunk(
GetShaderPassFromTemplate(
masterNode,
m_UnlitPassForwardDepthOnly,
mode),
true);
// subShader.AddShaderChunk(
// GetShaderPassFromTemplate(
// masterNode,
// m_UnlitPassForwardOnly,
// mode),
// true);
subShader.AddShaderChunk(
GetShaderPassFromTemplate(
masterNode,
m_UnlitPassForwardOnly,
mode),
true);
// subShader.Deindent();
// subShader.AddShaderChunk("}", true);
subShader.Deindent();
subShader.AddShaderChunk("}", true);
// return new[] { subShader.GetShaderString(0) };
// }
// }
//}
return subShader.GetShaderString(0);
}
}
}

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightWeightUnlitSubShader.cs


namespace UnityEditor.ShaderGraph
{
public class LightWeightUnlitSubShader
public class LightWeightUnlitSubShader : IUnlitSubShader
{
Pass m_UnlitPass = new Pass()
{

return resultPass;
}
public IEnumerable<string> GetSubshader(UnlitMasterNode masterNode, GenerationMode mode)
public string GetSubshader(UnlitMasterNode masterNode, GenerationMode mode)
{
var subShader = new ShaderGenerator();
subShader.AddShaderChunk("SubShader", true);

subShader.Deindent();
subShader.AddShaderChunk("}", true);
return new[] { subShader.GetShaderString(0) };
return subShader.GetShaderString(0);
}
}
}

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/MasterNodes/UnlitMasterNode.cs


finalShader.AddShaderChunk("}", false);
var lwSub = new LightWeightUnlitSubShader();
foreach (var subshader in lwSub.GetSubshader(this, mode))
finalShader.AddShaderChunk(subshader, true);
finalShader.AddShaderChunk(lwSub.GetSubshader(this, mode), true);
var hdSub = new HDUnlitSubShader();
finalShader.AddShaderChunk(hdSub.GetSubshader(this, mode), true);
finalShader.Deindent();
finalShader.AddShaderChunk("}", false);

16
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/HDUnlitPassForward.template


#pragma vertex Vert
#pragma fragment Frag
#pragma glsl
#pragma debug
#include "ShaderLibrary/common.hlsl"
#include "../../ShaderVariables.hlsl"
#include "../../ShaderPass/FragInputs.hlsl"
#include "../../ShaderPass/ShaderPass.cs.hlsl"
#include "CoreRP/ShaderLibrary/common.hlsl"
#include "HDRP/ShaderVariables.hlsl"
#include "HDRP/ShaderPass/FragInputs.hlsl"
#include "HDRP/ShaderPass/ShaderPass.cs.hlsl"
#include "../../Material/Material.hlsl"
#include "HDRP/Material/Material.hlsl"
#include "../../ShaderPass/VaryingMesh.hlsl"
#include "HDRP/ShaderPass/VaryingMesh.hlsl"
${Graph}

builtinData.depthOffset = 0.0;
}
#include "../../ShaderPass/${ShaderPassInclude}.hlsl"
#include "HDRP/ShaderPass/${ShaderPassInclude}.hlsl"
ENDHLSL
}

8
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/HDPipeline/IUnlitSubShader.cs


using System;
namespace UnityEditor.ShaderGraph {
public interface IUnlitSubShader
{
string GetSubshader(UnlitMasterNode masterNode, GenerationMode mode);
}
}

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/HDPipeline/IUnlitSubShader.cs.meta


fileFormatVersion: 2
guid: 1f0e14eed65d43342acaeb7f9e597621
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存