Lighting and shadows

Lighting is an essential element in our scenes, but as we have already seen, it takes some work to get it right. In this section, we will revisit lighting and also tackle adding shadows. Adding shadows to our objects will make them look like they are really there. We will start with adding shadows, so open up Unity and follow along:

  1. Create a new shader called UnlitShadowReceiver in the Assets/ARCoreDesign/Materials/Shaders folder.
  2. Double-click on the new shader to open it in your code editor.
  3. Select all the autogenerated code and delete it. Then, add the following code:
Shader "ARCoreDesign/UnlitShadowReceiver" 
{
Properties
{
_Color("Main Color", Color) = (1,1,1,1)
_MainTex("Base (RGB)", 2D) = "white" {}
_Cutoff("Cutout", Range(0,1)) = 0.5
}
SubShader
{
Pass
{
Alphatest Greater[_Cutoff] SetTexture[_MainTex]
}

Pass
{
Blend DstColor Zero Tags{ "LightMode" = "ForwardBase" }

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#pragma multi_compile_fwdbase
#include "AutoLight.cginc"

struct v2f
{
float4 pos : SV_POSITION; LIGHTING_COORDS(0,1)
};
v2f vert(appdata_base v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
TRANSFER_VERTEX_TO_FRAGMENT(o);
return o;
}
fixed4 frag(v2f i) : COLOR
{
float attenuation = LIGHT_ATTENUATION(i);
return attenuation;
}
ENDCG
}
}
Fallback "Transparent/Cutout/VertexLit"
}
  1. This shader is an example of a transparent shadow receiver. The shader works in two passes. In the first pass, we essentially clear the texture based on a cutoff alpha value. This allows us to turn an object transparent and still receive a shadow. The second pass draws the shadow using a vertex and fragment shader. Feel free to spend time studying this shader further.
As ARCore matures, there will likely be more versions of transparent shadow receivers available. Plan to search for other options or other ways to improve this form of shader in the future.
  1. Save the file and return to Unity.
  2. Create a new material in the Assets/ARCoreDesign/Materials folder and name it as UnlitShadowReceiver. Set the properties of the material, as shown in the following excerpt:
Setting the properties on the UnlitShadowReceiver material
  1. Select and drag the armchair prefab from the Assets/ARCoreDesign/Materials folder in the Project window and drop it in an open area of the Hierarchy window. We want to adjust our prefab a bit, and this is the easiest way.
  2. From the menu, select GameObject | 3D | Plane. Expand the armchair object and drag the Plane onto the 24 Ligne Roset Citta armchair child object.
  3. Select Plane and reset the position to (0, 0, 0) and scale to (0.1, 1, 0.1) on the Transform. Set the material to new UnlitShadowReceiver, as shown in the following excerpt:
Setting the Plane material to UnlitShadowReceiver
  1. Select the armchair object in the Hierarchy window and then in the Inspector window, click on the Apply button beside the Prefab properties to save the prefab. Leave the prefab in the scene for now, but we will want to delete it later.

We just created our transparent shadow receiver shader and then set it on a plane that we added to our prefab. We need to do this in order for our object, the armchair, to correctly cast a shadow on our new transparent receiver. Next, we need to turn on shadows, as the ARCore example has them disabled by default.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.104.238