Unity3d 仅渲染长方体内的点云数据
我正在尝试使用着色器仅渲染3d框内的点云数据。 但是,点云数据着色器使用几何体,剪辑框着色器使用曲面,所以我不知道如何将这两者结合起来 点云数据着色器Unity3d 仅渲染长方体内的点云数据,unity3d,shader,Unity3d,Shader,我正在尝试使用着色器仅渲染3d框内的点云数据。 但是,点云数据着色器使用几何体,剪辑框着色器使用曲面,所以我不知道如何将这两者结合起来 点云数据着色器 要获取片段着色器中像素的世界位置,必须将其通过顶点和几何体着色器: 顶点=>几何体 struct vertexOut { float4 pos : SV_POSITION; float4 color : COLOR0; float3 normal : NORMAL; float r : TEXCOORD0; //
要获取片段着色器中像素的世界位置,必须将其通过顶点和几何体着色器: 顶点=>几何体
struct vertexOut {
float4 pos : SV_POSITION;
float4 color : COLOR0;
float3 normal : NORMAL;
float r : TEXCOORD0; // not sure if this is good to do lol
float3 worldPos : TEXCOORD1;
};
vertexOut vert(vertexIn i) {
vertexOut o;
...
// calculate world position
o.worldPos = mul(unity_ObjectToWorld, i.pos);
return o;
}
几何体=>碎片
(由于只需围绕顶点创建一个小三角形,因此可以使用原始顶点的三角形近似新顶点的世界位置。如果不需要,则必须在循环中计算3个单独的世界位置。)
以及片段着色器的\u WorldToBox
属性。
您还需要c#scipt将矩阵传递给着色器。着色器错误:下标“worldPos”无效。我确实在geom参考中添加了代码。但是,我不知道如何处理IN.worldPos。抱歉,我只是Unity shader的初学者。您需要计算像素的世界位置并显式传递它。我更新了答案,但目前无法测试。谢谢你的帮助!!但是仍然有相同的错误:下标“worldPos”无效,带有“float3-boxPosition=mul(_-WorldToBox,float4(IN.worldPos,1));”线路。将剪切代码添加到void geom(点顶点在[1]中),如果我在.worldPos中更改为在[0].worldPos中,则我有不同的着色器错误:无法将表达式映射到代码为“clip(boxPosition+0.5)”的gs_4_0指令集我认为顶点着色器没有片段引用。天哪,莫利…它正在工作!!谢谢你的帮助。我非常感谢你的回答!我不知道片段引用与片段着色器一起工作。
Shader "Custom/ClipBox" {
Properties{
_MainTex("Albedo (RGB)", 2D) = "white" {}
_Glossiness("Smoothness", Range(0,1)) = 0.5
_Metallic("Metallic", Range(0,1)) = 0.0
}
SubShader{
Tags { "RenderType" = "Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Standard fullforwardshadows addshadow
#pragma target 3.0
sampler2D _MainTex;
half _Glossiness;
half _Metallic;
float4x4 _WorldToBox;
struct Input {
float2 uv_MainTex;
float3 worldPos;
};
void surf(Input IN, inout SurfaceOutputStandard o) {
float3 boxPosition = mul(_WorldToBox, float4(IN.worldPos, 1));
clip(boxPosition + 0.5);
clip(0.5 - boxPosition);
fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
o.Albedo = c.rgb;
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
o.Alpha = 0.0f;
}
ENDCG
}
FallBack "Diffuse"
}
struct vertexOut {
float4 pos : SV_POSITION;
float4 color : COLOR0;
float3 normal : NORMAL;
float r : TEXCOORD0; // not sure if this is good to do lol
float3 worldPos : TEXCOORD1;
};
vertexOut vert(vertexIn i) {
vertexOut o;
...
// calculate world position
o.worldPos = mul(unity_ObjectToWorld, i.pos);
return o;
}
struct geomOut {
float4 pos : POSITION;
float4 color : COLO0R;
float3 normal : NORMAL;
float3 worldPos : TEXCOORD0;
};
void geom(point vertexOut IN[1], inout TriangleStream<geomOut> OutputStream) {
...
for (int i = 0; i < 3; i++) {
p[i] = mul(r,p[i]); // apply rotation
p[i].x *= _ScreenParams.y / _ScreenParams.x; // make square
OUT.pos = IN[0].pos + float4(p[i],0,0) / 2.;
// Simply use the input vertex world position. This might result in unclear cube edges.
OUT.worldPos = IN[0].worldPos;
OutputStream.Append(OUT);
}
}
float3 boxPosition = mul(_WorldToBox, float4(IN.worldPos, 1));
clip(boxPosition + 0.5);
clip(0.5 - boxPosition);