浏览代码

Plug bakeDiffuseLighting

/scriptablerenderloop-materialgraph
Paul Demeulenaere 8 年前
当前提交
8741cf58
共有 5 个文件被更改,包括 52 次插入9 次删除
  1. 51
      Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderloopMaterialGraph.cs
  2. 2
      Assets/ScriptableRenderLoop/HDRenderLoop/Material/Builtin/BuiltinData.cs
  3. 3
      Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/Lit.template
  4. 4
      Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/LitSharePass.template
  5. 1
      Assets/ShaderGenerator/ShaderGeneratorAttributes.cs

51
Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderloopMaterialGraph.cs


}
[Serializable]
public abstract class AbstractHDRenderLoopMasterNode : AbstractMasterNode, IMayRequireNormal, IMayRequireTangent
public abstract class AbstractHDRenderLoopMasterNode : AbstractMasterNode, IMayRequireNormal, IMayRequireTangent, IMayRequireMeshUV, IMayRequireWorldPosition
{
public AbstractHDRenderLoopMasterNode()
{

priority = attribute.priority,
displayName = attribute.displayName,
materialID = attribute.filter,
semantic = attribute.semantic,
shaderOutputName = field.Name,
valueType = valueType
};

foreach (var slot in slots)
{
if (slot.semantic == SurfaceDataAttributes.Semantic.BakeDiffuseLighting && !IncludeBuiltInLitData())
{
continue;
}
AddSlot(new MaterialSlot(slot.index, slot.displayName, slot.shaderOutputName, Graphing.SlotType.Input, slot.valueType, Vector4.zero));
}
}

return Requires(SurfaceDataAttributes.Semantic.Tangent);
}
public bool RequiresMeshUV(int index)
{
if (index == 1 || index == 2)
{
return Requires(SurfaceDataAttributes.Semantic.BakeDiffuseLighting);
}
return false;
}
public bool RequiresWorldPosition()
{
return Requires(SurfaceDataAttributes.Semantic.BakeDiffuseLighting);
}
private class Vayring
{
public string attributeName;

var vayrings = new List<Vayring>();
for (int iTexCoord = 0; iTexCoord < 4; ++iTexCoord)
{
if (needFragInputRegex.IsMatch("texCoord" + iTexCoord) || (iTexCoord == 0 && activeNodeList.OfType<IMayRequireMeshUV>().Any(x => x.RequiresMeshUV())))
if (needFragInputRegex.IsMatch("texCoord" + iTexCoord) || activeNodeList.OfType<IMayRequireMeshUV>().Any(x => x.RequiresMeshUV(iTexCoord)))
{
vayrings.Add(new Vayring()
{

type = SlotValueType.Vector2,
vertexCode = string.Format("output.texCoord{0} = input.texCoord{0};", iTexCoord),
pixelCode = string.Format("float4 {0} = float4(fragInput.texCoord{1}, 0, 0);", ShaderGeneratorNames.UV0.Replace("0", iTexCoord.ToString()), iTexCoord)
pixelCode = string.Format("float4 {0} = float4(fragInput.texCoord{1}, 0, 0);", ShaderGeneratorNames.UV[iTexCoord], iTexCoord)
});
}
}

}
}
var pixelShaderBodyVisitor = new ShaderGenerator();
var pixelShaderBodyVisitor = new ShaderGenerator[] { new ShaderGenerator(), new ShaderGenerator() };
(node as IGeneratesBodyCode).GenerateNodeCode(pixelShaderBodyVisitor, mode);
(node as IGeneratesBodyCode).GenerateNodeCode(pixelShaderBodyVisitor[0], mode);
}
foreach (var slot in GetInputSlots<MaterialSlot>())

var currentField = surfaceField != null ? surfaceField : builtinField;
string variableName = null;
int visitorIndex = 0;
var egdes = owner.GetEdges(slot.slotReference).ToArray();
if (egdes.Length == 1)
{

case SurfaceDataAttributes.Semantic.Tangent:
variableName = ShaderGeneratorNames.WorldSpaceTangent;
break;
case SurfaceDataAttributes.Semantic.BakeDiffuseLighting:
variableName = string.Format("SampleBakedGI({0}, surfaceData.normalWS, {1}, {2})", ShaderGeneratorNames.WorldSpacePosition, ShaderGeneratorNames.UV[1], ShaderGeneratorNames.UV[2]);
visitorIndex = 1; //it depends of surfaceData.normalWS, do it last
break;
default: break;
}
}

if (!string.IsNullOrEmpty(variableName))
{
pixelShaderBodyVisitor.AddShaderChunk(string.Format("{0}.{1} = {2};", surfaceField != null ? "surfaceData" : "builtinData", slotOutputName, variableName), false);
pixelShaderBodyVisitor[visitorIndex].AddShaderChunk(string.Format("{0}.{1} = {2};", surfaceField != null ? "surfaceData" : "builtinData", slotOutputName, variableName), false);
}
}

var enumValue = Enum.ToObject(materialIdField.FieldType, GetMatchingMaterialID()).ToString();
var define = string.Format("{0}_{1}", materialIdField.Name, ShaderGeneratorHelper.InsertUnderscore(enumValue));
define = define.ToUpper();
pixelShaderBodyVisitor.AddShaderChunk(string.Format("surfaceData.{0} = {1};", materialIdField.Name, define), false);
pixelShaderBodyVisitor[1].AddShaderChunk(string.Format("surfaceData.{0} = {1};", materialIdField.Name, define), false);
resultShader = resultShader.Replace("${PixelShaderBody}", pixelShaderBodyVisitor.GetShaderString(1));
resultShader = resultShader.Replace("${PixelShaderBody}", pixelShaderBodyVisitor.Select(o => o.GetShaderString(1)).Aggregate((a, b) => a + b));
resultShader = resultShader.Replace("${VertexAttributes}", vertexAttributeVisitor.GetShaderString(1));
resultShader = resultShader.Replace("${PackedVaryingAttributes}", packedVarying.GetShaderString(1));
resultShader = resultShader.Replace("${PackingVaryingCode}", packInterpolatorVisitor.GetShaderString(1));

protected abstract int GetMatchingMaterialID();
protected abstract string GetTemplateText();
protected abstract string GetTemplatePassText();
protected abstract bool IncludeBuiltInLitData();
}
[Serializable]

return typeof(Lit.SurfaceData);
}
protected sealed override bool IncludeBuiltInLitData()
{
return true;
}
protected sealed override string GetTemplateText()
{
var templatePath = "Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/Lit.template";

protected override int GetMatchingMaterialID()
{
return -1;
}
protected sealed override bool IncludeBuiltInLitData()
{
return false;
}
protected sealed override string GetTemplateText()

2
Assets/ScriptableRenderLoop/HDRenderLoop/Material/Builtin/BuiltinData.cs


// We would prefer to split lighting and material information but for performance reasons,
// those lighting information are fill
// at the same time than material information.
[SurfaceDataAttributes("Bake Diffuse Lighting")]
[SurfaceDataAttributes("Bake Diffuse Lighting", 0, null, SurfaceDataAttributes.Semantic.BakeDiffuseLighting)]
public Vector3 bakeDiffuseLighting; // This is the result of sampling lightmap/lightprobe/proxyvolume
[SurfaceDataAttributes("Emissive Color")]

3
Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/Lit.template


#define UNITY_MATERIAL_LIT // Need to be define before including Material.hlsl
#pragma shader_feature _ALPHATEST_ON
#pragma shader_feature _ _DOUBLESIDED_LIGHTING_FLIP _DOUBLESIDED_LIGHTING_MIRROR
#pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
#pragma multi_compile DIRLIGHTMAP_OFF DIRLIGHTMAP_COMBINED
#pragma multi_compile DYNAMICLIGHTMAP_OFF DYNAMICLIGHTMAP_ON
//-------------------------------------------------------------------------------------
// Include

4
Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/LitSharePass.template


#ifdef UNITY_MATERIAL_LIT
#include "Assets/ScriptableRenderLoop/HDRenderLoop/Material/MaterialUtilities.hlsl"
#endif
#define UnpackNormal(x) UnpackNormalAG(x, 1.0)
#if SHADERPASS == SHADERPASS_LIGHT_TRANSPORT

1
Assets/ShaderGenerator/ShaderGeneratorAttributes.cs


Tangent,
Opacity,
AmbientOcclusion,
BakeDiffuseLighting,
}
public string displayName { get; private set; }

正在加载...
取消
保存