Дано : модель с анимацией звезд облетающих 3д "чашку".
Задача : собрать сцену с AR (таргет изображение на чашке ) так ,чтобы звезды залетающие за реальный объект-таргет не отрисовывались
Решение :
для AR используем бесплатный QCAR image target (vuforia) , что позволяет нам привязаться к координатам реального объекта.
далее создадим объект по размеру сопостовимый с реальным и разместим внтри вращающейся части. Чтобы было нагляднее добавил изображение сцены
Остается последний шаг скрыть звезды находящиеся ЗА объектом и скрыть сам объект тк наводимся мы на реальную "чашку"
Нам понадобится соответствующий шэйдер
Автор тут
Синтаксис:
Используется glsl
Shader "Matte" {
Properties {
}
SubShader {
Tags {
"Queue"="Background"
"IgnoreProjector"="False"
"RenderType"="Opaque"
}
Cull Off
ZWrite On
ZTest LEqual
ColorMask RGBA
Blend One One
Fog {
}
CGPROGRAM
#pragma surface surf BlinnPhongEditor vertex:vert
#pragma target 2.0
struct EditorSurfaceOutput {
half3 Albedo;
half3 Normal;
half3 Emission;
half3 Gloss;
half Specular;
half Alpha;
half4 Custom;
};
inline half4 LightingBlinnPhongEditor_PrePass (EditorSurfaceOutput s, half4 light) {
half3 spec = light.a * s.Gloss;
half4 c;
c.rgb = (s.Albedo * light.rgb + light.rgb * spec);
c.a = s.Alpha;
return c;
}
inline half4 LightingBlinnPhongEditor (EditorSurfaceOutput s, half3 lightDir, half3 viewDir, half atten) {
half3 h = normalize (lightDir + viewDir);
half diff = max (0, dot ( lightDir, s.Normal ));
float nh = max (0, dot (s.Normal, h));
float spec = pow (nh, s.Specular*128.0);
half4 res;
res.rgb = _LightColor0.rgb * diff;
res.w = spec * Luminance (_LightColor0.rgb);
res *= atten * 2.0;
return LightingBlinnPhongEditor_PrePass( s, res );
}
struct Input {
float4 color : COLOR;
};
void vert (inout appdata_full v, out Input o) {
float4 VertexOutputMaster0_0_NoInput = float4(0,0,0,0);
float4 VertexOutputMaster0_1_NoInput = float4(0,0,0,0);
float4 VertexOutputMaster0_2_NoInput = float4(0,0,0,0);
float4 VertexOutputMaster0_3_NoInput = float4(0,0,0,0);
}
void surf (Input IN, inout EditorSurfaceOutput o) {
o.Normal = float3(0.0,0.0,1.0);
o.Alpha = 1.0;
o.Albedo = 0.0;
o.Emission = 0.0;
o.Gloss = 0.0;
o.Specular = 0.0;
o.Custom = 0.0;
float4 Master0_0_NoInput = float4(0,0,0,0);
float4 Master0_1_NoInput = float4(0,0,1,1);
float4 Master0_2_NoInput = float4(0,0,0,0);
float4 Master0_3_NoInput = float4(0,0,0,0);
float4 Master0_4_NoInput = float4(0,0,0,0);
float4 Master0_5_NoInput = float4(1,1,1,1);
float4 Master0_7_NoInput = float4(0,0,0,0);
float4 Master0_6_NoInput = float4(1,1,1,1);
o.Normal = normalize(o.Normal);
}
ENDCG
}
Fallback "Diffuse"
}
Properties {
}
SubShader {
Tags {
"Queue"="Background"
"IgnoreProjector"="False"
"RenderType"="Opaque"
}
Cull Off
ZWrite On
ZTest LEqual
ColorMask RGBA
Blend One One
Fog {
}
CGPROGRAM
#pragma surface surf BlinnPhongEditor vertex:vert
#pragma target 2.0
struct EditorSurfaceOutput {
half3 Albedo;
half3 Normal;
half3 Emission;
half3 Gloss;
half Specular;
half Alpha;
half4 Custom;
};
inline half4 LightingBlinnPhongEditor_PrePass (EditorSurfaceOutput s, half4 light) {
half3 spec = light.a * s.Gloss;
half4 c;
c.rgb = (s.Albedo * light.rgb + light.rgb * spec);
c.a = s.Alpha;
return c;
}
inline half4 LightingBlinnPhongEditor (EditorSurfaceOutput s, half3 lightDir, half3 viewDir, half atten) {
half3 h = normalize (lightDir + viewDir);
half diff = max (0, dot ( lightDir, s.Normal ));
float nh = max (0, dot (s.Normal, h));
float spec = pow (nh, s.Specular*128.0);
half4 res;
res.rgb = _LightColor0.rgb * diff;
res.w = spec * Luminance (_LightColor0.rgb);
res *= atten * 2.0;
return LightingBlinnPhongEditor_PrePass( s, res );
}
struct Input {
float4 color : COLOR;
};
void vert (inout appdata_full v, out Input o) {
float4 VertexOutputMaster0_0_NoInput = float4(0,0,0,0);
float4 VertexOutputMaster0_1_NoInput = float4(0,0,0,0);
float4 VertexOutputMaster0_2_NoInput = float4(0,0,0,0);
float4 VertexOutputMaster0_3_NoInput = float4(0,0,0,0);
}
void surf (Input IN, inout EditorSurfaceOutput o) {
o.Normal = float3(0.0,0.0,1.0);
o.Alpha = 1.0;
o.Albedo = 0.0;
o.Emission = 0.0;
o.Gloss = 0.0;
o.Specular = 0.0;
o.Custom = 0.0;
float4 Master0_0_NoInput = float4(0,0,0,0);
float4 Master0_1_NoInput = float4(0,0,1,1);
float4 Master0_2_NoInput = float4(0,0,0,0);
float4 Master0_3_NoInput = float4(0,0,0,0);
float4 Master0_4_NoInput = float4(0,0,0,0);
float4 Master0_5_NoInput = float4(1,1,1,1);
float4 Master0_7_NoInput = float4(0,0,0,0);
float4 Master0_6_NoInput = float4(1,1,1,1);
o.Normal = normalize(o.Normal);
}
ENDCG
}
Fallback "Diffuse"
}
примерный результат выглядит следующим образом. Звезды заходят за модель и "пропадают"
Немного коряво ибо у самого нет под рукой реального таргета ,но это я поправлю...
ЗЫ Модераторам : если ошибся разделом перенесите куда следует.