浏览代码

Minimal loop wit forward shader

Not working currently miss C# side
/main
sebastienlagarde 8 年前
当前提交
aa06ca56
共有 5 个文件被更改,包括 103 次插入39 次删除
  1. 2
      Assets/ScriptableRenderLoop/UnityStandard/Shaders/Material/Material.hlsl
  2. 27
      Assets/ScriptableRenderLoop/UnityStandard/Shaders/ShaderVariables.hlsl
  3. 99
      Assets/ScriptableRenderLoop/UnityStandard/Shaders/UnityStandard.shader
  4. 10
      Assets/ScriptableRenderLoop/UnityStandard/UnityStandardRenderLoop.cs
  5. 4
      Assets/TestScenes/ForwardRenderLoop/ForwardRenderLoop.unity

2
Assets/ScriptableRenderLoop/UnityStandard/Shaders/Material/Material.hlsl


// SurfaceData and BSDFData
//-----------------------------------------------------------------------------
// Main structure that store the data from the standard shader (i.e user input)
// Main structure that store the user data (i.e user input of master node in material graph)
struct DisneyGGXSurfaceData
{
float3 diffuseColor;

27
Assets/ScriptableRenderLoop/UnityStandard/Shaders/ShaderVariables.hlsl


return unity_MatrixVP;
}
float3 TransformObjectToWorld(float3 position)
float3 TransformObjectToWorld(float3 positionOS)
return mul(GetObjectToWorldMatrix(), float4(position, 1.0));
return mul(GetObjectToWorldMatrix(), float4(positionOS, 1.0));
float3 TransformObjectToWorldDir(float3 dir)
float3 TransformObjectToWorldDir(float3 dirOS)
return normalize(mul((float3x3)GetObjectToWorldMatrix(), dir));
return normalize(mul((float3x3)GetObjectToWorldMatrix(), dirOS));
float3 TransformObjectToWorldNormal(float3 norm)
float3 TransformObjectToWorldNormal(float3 normalOS)
return UnityObjectToWorldDir(norm);
return UnityObjectToWorldDir(normalOS);
return normalize(mul(norm, (float3x3)GetWorldToObjectMatrix()));
return normalize(mul(normalOS, (float3x3)GetWorldToObjectMatrix()));
float4 TransformWorldToHClip(float3 pos)
float4 TransformWorldToHClip(float3 positionWS)
return mul(GetWorldToHClipMatrix(), float4(pos, 1.0));
return mul(GetWorldToHClipMatrix(), float4(positionWS, 1.0));
}
float3x3 CreateTangentToWorld(float3 normal, float3 tangent, float tangentSign)

float3 binormal = cross(normal, tangent) * sign;
return float3x3(tangent, binormal, normal);
return float3x3(tangent, binormal, normal);
}
// Computes world space view direction, from object space position
float3 GetWorldSpaceNormalizeViewDir(float3 positionWS)
{
return normalize(_WorldSpaceCameraPos.xyz - positionWS);
}

99
Assets/ScriptableRenderLoop/UnityStandard/Shaders/UnityStandard.shader


_DiffuseColor("Diffuse", Color) = (1,1,1,1)
_DiffuseMap("Diffuse", 2D) = "white" {}
_SpecColor("Specular", Color) = (0.2,0.2,0.2)
_SpecColor("Specular", Color) = (0.04,0.04,0.04)
_SpecMap("Specular", 2D) = "white" {}
_Smoothness("Smoothness", Range(0.0, 1.0)) = 0.5

#pragma vertex MainVS
#pragma fragment MainPS
float4 _DiffuseColor;
float4 _SpecColor;
float _Smoothness;
sampler2D _DiffuseMap;
sampler2D _NormalMap;
PunctualLightData _lightData[4];
float _LightCount;
//-------------------------------------------------------------------------------------
// Input functions

struct VSOutput
{
float4 positionHS : SV_POSITION;
float2 texCoord0 : TEXCOORD0;
float4 tangentToWorld[3] : TEXCOORD1; // [3x3:tangentToWorld | 1x3:viewDirForParallax]
float3 positionWS : TEXCOORD0;
float2 texCoord0 : TEXCOORD1;
float4 tangentToWorld[3] : TEXCOORD2; // [3x3:tangentToWorld | 1x3:viewDirForParallax]
VSOutput MainVS( VSInput i )
VSOutput MainVS(VSInput input)
VSOutput o;
VSOutput output;
float3 positionWS = TransformObjectToWorld(i.positionOS.xyz);
output.positionWS = TransformObjectToWorld(input.positionOS.xyz);
o.positionHS = TransformWorldToHClip(positionWS);
output.positionHS = TransformWorldToHClip(output.positionWS);
float3 normalWS = TransformObjectToWorldNormal(i.normalOS);
float3 normalWS = TransformObjectToWorldNormal(input.normalOS);
o.texCoord0 = i.uv0;
output.texCoord0 = input.uv0;
// #ifdef _TANGENT_TO_WORLD
float4 tangentWS = float4(TransformObjectToWorldDir(input.tangentOS.xyz), input.tangentOS.w);
float3x3 tangentToWorld = CreateTangentToWorld(normalWS, tangentWS.xyz, tangentWS.w);
output.tangentToWorld[0].xyz = tangentToWorld[0];
output.tangentToWorld[1].xyz = tangentToWorld[1];
output.tangentToWorld[2].xyz = tangentToWorld[2];
// #else
// output.tangentToWorld[0].xyz = 0;
// output.tangentToWorld[1].xyz = 0;
// output.tangentToWorld[2].xyz = normalWS;
// #endif
output.tangentToWorld[0].w = 0;
output.tangentToWorld[1].w = 0;
output.tangentToWorld[2].w = 0;
return output;
}
//-------------------------------------------------------------------------------------
// This function is either hand written or generate by the material graph
DisneyGGXSurfaceData GetSurfaceData(VSOutput input)
{
DisneyGGXSurfaceData data;
data.diffuseColor = tex2D(_DiffuseMap, input.texCoord0) * _DiffuseColor;
data.occlusion = 1.0;
#ifdef _TANGENT_TO_WORLD
float4 tangentWS = float4(TransformObjectToWorldDir(i.tangentOS.xyz), i.tangentOS.w);
data.specularColor = _SpecColor;
data.smoothness = _Smoothness;
float3x3 tangentToWorld = CreateTangentToWorld(normalWorld, tangentWorld.xyz, tangentWorld.w);
o.tangentToWorld[0].xyz = tangentToWorld[0];
o.tangentToWorld[1].xyz = tangentToWorld[1];
o.tangentToWorld[2].xyz = tangentToWorld[2];
#else
o.tangentToWorld[0].xyz = 0;
o.tangentToWorld[1].xyz = 0;
o.tangentToWorld[2].xyz = normalWS;
#endif
data.normal = UnpackNormalDXT5nm(tex2D(_NormalMap, input.texCoord0));
return o;
return data;
float4 MainPS( VSOutput i ) : SV_Target
//-------------------------------------------------------------------------------------
float4 MainPS(VSOutput input) : SV_Target
return float4( 1.0, 0.0, 0.0, 0.0 );
float3 V = GetWorldSpaceNormalizeViewDir(input.positionWS);
DisneyGGXSurfaceData surfaceData = GetSurfaceData(input);
DisneyGGXBSDFData BSDFData = ConvertSurfaceDataToBSDFData(surfaceData);
float4 outDiffuseLighting;
float4 outSpecularLighting;
for (int i = 0; i < _LightCount; ++i)
{
float4 diffuseLighting;
float4 specularLighting;
EvaluateBSDF_Punctual_DisneyGGX(V, input.positionWS, _lightData[i], BSDFData, diffuseLighting, specularLighting);
outDiffuseLighting += diffuseLighting;
outSpecularLighting += specularLighting;
}
return outDiffuseLighting + outSpecularLighting;
}
ENDCG

10
Assets/ScriptableRenderLoop/UnityStandard/UnityStandardRenderLoop.cs


}
//---------------------------------------------------------------------------------------------------------------------------------------------------
void UpdateLightConstants(ActiveLight[] activeLights, ref ShadowOutput shadow)
void UpdateLightConstants(ActiveLight[] activeLights /*, ref ShadowOutput shadow */)
for ( int nLight = 0; nLight < Math.Min(activeLights.Length, 4); nLight++ )
{
}
_LightCount
/*
int nNumLightsIncludingTooMany = 0;

renderLoop.SetupCameraProperties (camera);
//UpdateLightConstants(cullResults.culledLights, ref shadows);
UpdateLightConstants(cullResults.culledLights /*, ref shadows */);
DrawRendererSettings settings = new DrawRendererSettings (cullResults, camera, new ShaderPassName("Forward"));
settings.rendererConfiguration = RendererConfiguration.ConfigureOneLightProbePerRenderer | RendererConfiguration.ConfigureReflectionProbesProbePerRenderer;

4
Assets/TestScenes/ForwardRenderLoop/ForwardRenderLoop.unity


m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1409545155}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 26.23, y: 0.5, z: 11}
m_LocalPosition: {x: 26.23, y: 6.95, z: 11}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}

m_Script: {fileID: 11500000, guid: 92bb16b4ee20841929b24d6bd771738d, type: 3}
m_Name:
m_EditorClassIdentifier:
m_RenderLoop: {fileID: 11400000, guid: 873753be6b0da44d58b061c5ec3d12bc, type: 2}
m_RenderLoop: {fileID: 11400000, guid: bbb9d949788aad640a70f108f114474d, type: 2}
--- !u!1 &1758207072
GameObject:
m_ObjectHideFlags: 0

正在加载...
取消
保存