参考连接: Unity Shader中获取模型中心点的世界坐标_unity获取物体坐标_VirtualCreator的博客-CSDN博客
shader中获得模型原点的方式:
float3 center = float3(unity_ObjectToWorld[0].w, unity_ObjectToWorld[1].w, unity_ObjectToWorld[2].w);
float3 center = float3(unity_ObjectToWorld._m03, unity_ObjectToWorld._m13, unity_ObjectToWorld._m23);
float3 center = mul(unity_ObjectToWorld , float(0,0,0,1)).xyz;
float3 center = unity_ObjectToWorld._14_24_34;
原点指的是哪里:
实际是当前材质球挂接的Renderer的mesh定义的坐标原点,但是有两种情况:
1 Renderer是MeshRender
原点就是当前mesh的坐标原点.
当前body的mesh , 坐标中心就是原点
2 SkinnedMeshRender
是RootBone所指游戏对象的坐标点。 可以自行测试
验证方式
项目需要我需要验证的是y轴的坐标
下面是验证的shader, 挂接在要验证的mesh上.文章来源:https://www.toymoban.com/news/detail-645527.html
所输出的结果, 模型越黑的地方就是0点。文章来源地址https://www.toymoban.com/news/detail-645527.html
Shader "Unlit/shader_center"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
CBUFFER_START(UnityPerMaterial)
float4 _MainTex_ST;
CBUFFER_END
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
struct Attributes
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct Varyings
{
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
float3 worldPos : TEXCOORD1;
};
Varyings vert (Attributes v)
{
Varyings o;
VertexPositionInputs vertexInput = GetVertexPositionInputs(v.vertex);
o.vertex = vertexInput.positionCS;
o.worldPos = vertexInput.positionWS;
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
float4 frag (Varyings i) : SV_Target
{
float3 center = float3(unity_ObjectToWorld[0].w, unity_ObjectToWorld[1].w, unity_ObjectToWorld[2].w);
float co =abs(center.y - i.worldPos.y);
return co;
// sample the texture
float4 col = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex ,i.uv);
return col;
}
ENDHLSL
}
}
}
到了这里,关于unity shader中获得模型中心点 + 中心点详解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!