I am developing an ARFoundation application for Android mobile devices using Unity game engine.
I want to render the shadow of the AR virtual object placed on the AR plane where it is placed. Then I want to access the pixels of the screen inside a C# script in Unity to do some computer vision-based postprocessing. I am already accessing the raw AR frame by using the CpuImageSample.cs script provided by ARFoundation (.cs).
For rendering shadows on the AR planes, I am using the free version of the Shadow Receiver URP asset () which provides shader graphs for rendering shadows.
I selected the transparent shadow receiver as the shader to be used with AR plane materials. This shader graph has the following properties in the inspector.
I tried to access the unity_ShadowMasks property from a C# script in Unity, but it is always null. This is the code of my C# script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CustomPlaneShadowScript : MonoBehaviour
{
private Renderer planeShadowRenderer;
public void Awake()
{
planeShadowRenderer = GetComponent<Renderer>();
}
// Start is called before the first frame update
void Start()
{
Texture shadowMasks = planeShadowRenderer.material.getTexture("unity_ShadowMasks");
if (shadowMasks != null)
{
Debug.Log("Successfully accessed unity_ShadowMasks");
}
else
{
Debug.LogWarning("unity_ShadowMasks is null");
}
}
// Update is called once per frame
void Update()
{
}
}
I read online about this and one of the solutions that popped up were adding a render texture somehow and accessing the shadows from there. But I'm not sure how that works and I couldn't find clear steps.
Any help would be greatly appreciated!
I am developing an ARFoundation application for Android mobile devices using Unity game engine.
I want to render the shadow of the AR virtual object placed on the AR plane where it is placed. Then I want to access the pixels of the screen inside a C# script in Unity to do some computer vision-based postprocessing. I am already accessing the raw AR frame by using the CpuImageSample.cs script provided by ARFoundation (https://github.com/Unity-Technologies/arfoundation-samples/blob/master/Assets/Scripts/CpuImageSample.cs).
For rendering shadows on the AR planes, I am using the free version of the Shadow Receiver URP asset (https://assetstore.unity.com/packages/vfx/shaders/shadow-receiver-urp-free-ar-shadows-228074?aid=1100ljSxt) which provides shader graphs for rendering shadows.
I selected the transparent shadow receiver as the shader to be used with AR plane materials. This shader graph has the following properties in the inspector.
I tried to access the unity_ShadowMasks property from a C# script in Unity, but it is always null. This is the code of my C# script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CustomPlaneShadowScript : MonoBehaviour
{
private Renderer planeShadowRenderer;
public void Awake()
{
planeShadowRenderer = GetComponent<Renderer>();
}
// Start is called before the first frame update
void Start()
{
Texture shadowMasks = planeShadowRenderer.material.getTexture("unity_ShadowMasks");
if (shadowMasks != null)
{
Debug.Log("Successfully accessed unity_ShadowMasks");
}
else
{
Debug.LogWarning("unity_ShadowMasks is null");
}
}
// Update is called once per frame
void Update()
{
}
}
I read online about this and one of the solutions that popped up were adding a render texture somehow and accessing the shadows from there. But I'm not sure how that works and I couldn't find clear steps.
Any help would be greatly appreciated!
Share Improve this question asked Jan 24 at 11:09 salamsalam 469 bronze badges1 Answer
Reset to default 0I cannot walk you through all the steps needed for this, but I can guide you in the right direction.
You'd want to use a separate camera that renders only the shadows onto a ARGB32 Render Texture. You can use a culling mask to render only the shadows. I'm not familiar with the shaders you listed, but you may be able to set it to render to a Render Texture directly. Optionally, be sure to use a solid color that doesn't look like a shadow for the non-shadow (transparent) pixels. You can also read the transparency values, but that might make things more difficult.
Once in the Render Texture, you can walk over all the pixels of the render texture, and find out which ones are colored in the 'shadow' color - and which ones have your 'transparent' color.