包含此页的版本:
不含此页的版本:
要使用相机在场景中创建特定视点图像的组件。输出要么绘制到屏幕上,要么作为纹理捕获。更多信息
请参阅术语表在自定义通用渲染管线(Render Pipeline) 获取场景内容并将其显示在屏幕上的一系列作。Unity 允许您从预构建的渲染管道中进行选择,或编写自己的渲染管道。更多信息
请参阅术语表(URP)着色器在 GPU 上运行的程序。更多信息
请参阅术语表,请按照下列步骤作:
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"在HLSLPROGRAM在着色器文件中。这Core.hlsl文件导入ShaderVariablesFunction.hlsl文件。ShaderVariablesFunction.hlsl文件。| 方法 | 语法 | 描述 |
|---|---|---|
GetCameraPositionWS |
float3 GetCameraPositionWS() |
返回摄像机的世界空间位置。 |
GetScaledScreenParams |
float4 GetScaledScreenParams() |
返回屏幕的宽度和高度像素计算机图像中的最小单位。像素大小取决于您的屏幕分辨率。像素光照是在每个屏幕像素下计算的。更多信息 请参阅术语表. |
GetViewForwardDir |
float3 GetViewForwardDir() |
返回世界空间中视图的正向方向。 |
IsPerspectiveProjection |
bool IsPerspectiveProjection() |
返回true如果摄像机投影设置为透视。 |
LinearDepthToEyeDepth |
half LinearDepthToEyeDepth(half linearDepth) |
转换线性深度缓冲区 保存图像中每个像素的 z 值深度的内存存储,其中 z 值是投影平面中每个呈现像素的深度。更多信息 请参阅术语表值来查看深度。有关更多信息,请参阅摄像机和深度纹理。 |
TransformScreenUV |
void TransformScreenUV(inout float2 screenSpaceUV) |
如果 Unity 使用倒置坐标空间,则翻转屏幕空间位置的 y 坐标。您还可以同时输入uv,屏幕高度为float,因此该方法输出缩放到屏幕尺寸(以像素为单位)的位置。 |
以下 URP 着色器使用表示从表面到相机方向的颜色绘制对象表面。
Shader "Custom/DirectionToCamera"
{
SubShader
{
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct Attributes
{
float4 positionOS : POSITION;
float2 uv: TEXCOORD0;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float2 uv: TEXCOORD0;
float3 viewDirection : TEXCOORD2;
};
Varyings vert(Attributes IN)
{
Varyings OUT;
// Get the positions of the vertex in different coordinate spaces
VertexPositionInputs positions = GetVertexPositionInputs(IN.positionOS);
OUT.positionCS = positions.positionCS;
// Get the direction from the vertex to the camera, in world space
OUT.viewDirection = GetCameraPositionWS() - positions.positionWS.xyz;
return OUT;
}
half4 frag(Varyings IN) : SV_Target
{
// Set the fragment color to the direction vector
return float4(IN.viewDirection, 1);
}
ENDHLSL
}
}
}