自定义SRP
- 自定义RenderPipelineAsset:
作用:自定义SRP配置文件,可以创建、配置管线和管线参数。
public class MyRenderPipelineAsset : RenderPipelineAsset
{
protected override RenderPipeline CreatePipeline()
{
return new MyRenderPipeline(this);
}
#if UNITY_EDITOR
public static MyRenderPipelineAsset Create()
{
return = CreateInstance<MyRenderPipelineAsset>();
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1812")]
internal class CreateMyPipelineAsset : EndNameEditAction
{
public override void Action(int instanceId, string pathName, string resourceFile)
{
AssetDatabase.CreateAsset(Create(), pathName);
}
}
[MenuItem("Assets/Create/Rendering/My Render Pipeline/Pipeline Asset", priority = CoreUtils.assetCreateMenuPriority1)]
static void CreateLightweightPipeline()
{
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, CreateInstance<CreateMyPipelineAsset>(), "MyRenderPiplineAsset.asset", null, null);
}
#endif
}
- 自定义RenderPipeline:
作用:负责渲染场景
public class MyRenderPipeline : RenderPipeline
{
public MyRenderPipeline(MyRenderPipelineAsset asset)
{}
protected override void Render(ScriptableRenderContext context, Camera[] cameras)
{
BeginFrameRendering(cameras);
CommandBuffer commandBuffer = CommandBufferPool.Get();
foreach (var camera in cameras)
{
context.SetupCameraProperties(camera);
BeginCameraRendering(camera);
ScriptableCullingParameters cullingParameters;
camera.TryGetCullingParameters(out cullingParameters);
var cullingResults = context.Cull(ref cullingParameters);
commandBuffer.SetRenderTarget(BuiltinRenderTextureType.CameraTarget);
commandBuffer.ClearRenderTarget(true, true, new Color(0, 0, 0));
context.ExecuteCommandBuffer(commandBuffer);
commandBuffer.Clear();
var drawingSettings = new DrawingSettings(new ShaderTagId("MyPass"), new SortingSettings(camera));
var filteringSettings = FilteringSettings.defaultValue;
context.DrawRenderers(cullingResults, ref drawingSettings, ref filteringSettings);
context.DrawSkybox(camera);
ScriptableRenderContext.EmitWorldGeometryForSceneView(camera);
EndCameraRendering(context, camera);
}
context.Submit();
CommandBufferPool.Release(commandBuffer);
EndFrameRendering(context, cameras);
}
}
struct DrawingSettings{
bool enableDynamicBatching;
bool enableInstancing;
int mainLightIndex;
Material overrideMaterial;
int overrideMaterialPassIndex;
PerObjectData perObjectData;
SortingSettings sortingSettings;
}
enum PerObjectData{
None,
LightProbe,
ReflectionProbes,
LightProbeProxyVolume,
Lightmaps,
LightData,
MotionVectors,
LightIndices,
ReflectionProbeData,
OcclusionProbe,
OcclusionProbeProxyVolume,
ShadowMask,
}
struct SortingSettings{
Vector3 cameraPosition;
SortingCriteria criteria;
Vector3 customAxis;
DistanceMetric distanceMetric;
Matrix4x4 worldToCameraMatrix;
}
enum SortingCriteria{
None,
SortingLayer,
RenderQueue,
BackToFront,
QuantizedFrontToBack,
OptimizeStateChanges,
CanvasOrder,
RendererPriority,
CommonOpaque,
CommonTransparent,
}
enum DistanceMetric{
Perspective,
Orthographic,
CustomAxis,
}
struct FilterSettings{
bool excludeMotionVectorObjects,
int layerMask,
int renderingLayerMask,
RenderQueueRange renderQueueRange,
SortingLayerRange sortingLayerRange,
}
Shader "Custom/MyShader"{
Properties {}
SubShader {
Tags { "RenderType" = "Opaque" }
Pass{
Tags { "LightMode"="MyPass" }
HLSLPROGRAM
#pragma vertex VertexMain
#pragma fragment FragmentMain
#include "Packages/com.unity.render-pipelines.lightweight/ShaderLibrary/Core.hlsl"
struct VertexInput {
float4 position : POSITION;
};
struct VertexOut {
float4 position : SV_POSITION;
};
VertexOut VertexMain(VertexInput input) {
VertexOut vertexOut;
vertexOut.position = TransformObjectToHClip(input.position.xyz);
return vertexOut;
}
float4 FragmentMain() : SV_TARGET{
return float4(1,0,0,1);
}
ENDHLSL
}
}
}
- Shader中的矩阵变换:
都在Core RP Library/ShaderLibrary/SpaceTransform.hlsl中定义
|
|
|
float4x4 GetObjectToWorldMatrix() |
UNITY_MATRIX_M |
世界矩阵 |
float4x4 GetWorldToObjectMatrix() |
UNITY_MATRIX_I_M |
世界逆矩阵 |
float4x4 GetWorldToViewMatrix() |
UNITY_MATRIX_V |
视角矩阵 |
float4x4 GetWorldToHClipMatrix() |
UNITY_MATRIX_VP |
视角投影矩阵 |
float4x4 GetViewToHClipMatrix() |
UNITY_MATRIX_P |
投影矩阵 |
float4x4 GetOddNegativeScale() |
unity_WorldTransformParams.w |
|
float4x4 TransformObjectToWorld(float3 positionOS) |
|
本地到世界 |
float3 TransformWorldToObject(float3 positionWS) |
|
世界到本地 |
float3 TransformWorldToView(float3 positionWS) |
|
世界到视角 |
float4 TransformObjectToHClip(float3 positionOS) |
|
本地到投影 |
float4 TransformWorldToHClip(float3 positionWS) |
|
世界到投影 |
float4 TransformWViewToHClip(float3 positionVS) |
|
视角到投影 |
real3 TransformObjectToWorldDir(real3 dirOS) |
|
|
real3 TransformWorldToObjectDir(real3 dirWS) |
|
|
real3 TransformWorldToViewDir(real3 dirWS) |
|
|
real3 TransformWorldToHClipDir(real3 directionWS) |
|
|
float3 TransformObjectToWorldNormal(float3 normalOS) |
|
|
real3x3 CreateTangentToWorld(real3 normal, real3 tangent, real flipSign) |
|
|
real3 TransformTangentToWorld(real3 dirTS, real3x3 tangentToWorld) |
|
|
real3 TransformWorldToTangent(real3 dirWS, real3x3 tangentToWorld) |
|
|
real3 TransformTangentToObject(real3 dirTS, real3x3 tangentToWorld) |
|
|
real3 TransformObjectToTangent(real3 dirOS, real3x3 tangentToWorld) |
|
|