- Home /
Can worldspace UI Elements cast a shadow?
I am currently making my start menu, and am aiming for something somewhat unique, I tried to reasearch this but came up with nothing, I did have the Text Shadow component, but it looked awful once I added my hover animations that stretch and bounce the selected text, which the text shadow followed, I am looking for something that can cast an actual shadow on the ground below the text and acts like a normal shadow when the animations play. Don't be afraid to answer no.. If there is no way to do it, I will still accept as a correct answer.
Answer by Namey5 · Mar 19, 2020 at 04:09 AM
It definitely used to be possible, but after some quick tests it would appear that the shadow system auto-culls all UI (for good reason), making shadow-casting essentially impossible. The only real way to do this would be to use actual renderers. For text, the legacy 3D text-mesh component still works, so you can apply a custom shader and material to it to cast shadows. Here's a shader that will do that;
// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
Shader "GUI/Shadowed Text Shader"
{
Properties
{
_MainTex ("Font Texture", 2D) = "white" {}
_Color ("Text Color", Color) = (1,1,1,1)
}
SubShader
{
Tags {
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
}
Cull Off
Pass
{
Lighting Off ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile _ UNITY_SINGLE_PASS_STEREO STEREO_INSTANCING_ON STEREO_MULTIVIEW_ON
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
UNITY_VERTEX_OUTPUT_STEREO
};
sampler2D _MainTex;
uniform float4 _MainTex_ST;
uniform fixed4 _Color;
v2f vert (appdata_t v)
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.vertex = UnityObjectToClipPos(v.vertex);
o.color = v.color * _Color;
o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = i.color;
col.a *= tex2D(_MainTex, i.texcoord).a;
return col;
}
ENDCG
}
Pass
{
Tags { "LightMode" = "ShadowCaster" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 2.0
#pragma multi_compile_shadowcaster
#pragma multi_compile_instancing // allow instanced shadow pass for most of the shaders
#include "UnityCG.cginc"
struct v2f
{
V2F_SHADOW_CASTER;
float2 uv : TEXCOORD1;
UNITY_VERTEX_OUTPUT_STEREO
};
uniform float4 _MainTex_ST;
v2f vert (appdata_base v)
{
v2f o;
UNITY_SETUP_INSTANCE_ID (v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO (o);
TRANSFER_SHADOW_CASTER_NORMALOFFSET (o)
o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
return o;
}
uniform sampler2D _MainTex;
//uniform fixed _Cutoff;
uniform fixed4 _Color;
float4 frag (v2f i) : SV_Target
{
fixed4 texcol = tex2D (_MainTex, i.uv);
clip (texcol.a* _Color.a - 0.5);
SHADOW_CASTER_FRAGMENT (i)
}
ENDCG
}
}
}
Unfortunately, this won't work straight up because the TextMesh system assumes you are using the auto-generated font material. To get it working properly, you'll also need to attach this script;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(TextMesh)), ExecuteInEditMode]
public class ShadowTextSetup : MonoBehaviour
{
private TextMesh textMesh;
new private MeshRenderer renderer;
public Texture fontTexture { get; private set; }
private void OnEnable ()
{
if (!TryGetComponent<TextMesh>(out textMesh) || !TryGetComponent<MeshRenderer>(out renderer))
return;
fontTexture = textMesh.font.material.GetTexture ("_MainTex");
renderer.sharedMaterial.SetTexture ("_MainTex", fontTexture);
}
}
Thank you so much for a detailed response I will defintley try this out, interested to see how this looks, again thanks so much! greatly appreciated :)
Your answer
Follow this Question
Related Questions
How to handle UI/Space mixed scene? 2 Answers
World space buttons and preventing UI touch passthrough 0 Answers
Is it possible to make world space UI cast shadow on geometry in the scene? 0 Answers
3rd party UI - World Canvas or Screen Space Camera? 0 Answers
translating world position to (scaled) world-space canvas 0 Answers