Why can't unity stop light from passing through walls etc?

A Flare on a light gets stopped by colliders so why does the same not work for Directional lighting etc should it not be the same principle?

Turn on shadows?

And no, they are not the same. Flares are CPU side: a single line (a ray) is calculated between the camera and the flare source and checked against (usually simplified) collision shapes that are currently in view. If the line intersects with anything, the flare is disabled. Because it’s just one line it’s pretty fast, but you don’t want to do hundreds of these.

Light shadows have to have some way of testing the position of every pixel on screen against the light source or light direction and looking for things that get between it. This means millions of line calculations, and you’re testing against much more complex shapes (polygon meshes instead of simple boxes) and you can’t limit it to just things that can be seen as the surface that’s blocking light might be out of view. This is what raytracing is.

Instead we use an approximation called shadow maps. These render out the distance of surfaces from the point of view of the light (a depth map, which is what shadow maps actually are), usually at a much reduced resolution, and each pixel on screen tests to see if it’s further from the light than what was recorded in distance in that approximate direction.

When i turn on shadows then my meshes become transparent unless i turn off receive shadows and then there are unrealistic shadows inside the building as well , i want it lightless inside and setup a series f lights to mimic sunlight coming through windows etc but i also need the light on outside as the outside can be viewed from inside which complicates matters

What do you mean they become transparent? Sounds like you have deferred rendering turned on but aren’t using deferred shaders (ie: just the standard shader) which can cause some weird problems. That or your shaders are old and don’t have proper the shadowcaster pass needed for 5.0.

Could you post an image with the problem? I read your posts a couple of times and I’m not sure I understand what you mean.

Here u can see the shadows of a few houses.Not deferred lighting if u turn of recieve shadows it stops it .The mesh is one sided using a 2 sided version of the standard shader.

It’s probably the shader.

In your shader where it has “#pragma surface Standard” add “addshadow” to the end. The shadow caster & receiver pass is by default single sided so it won’t properly receive or cast shadows if you just make a surface shader and set Cull off. Adding “addshadow” will make sure it generates a shadow shader with the settings you have.

Hi bgolus
I can’t find a line like that here is the shader code it is from a unity asset package called Morph3d.I don’t understand shaders even a tiny bit.Could you input the line in the correct space for me thx.

Shader "Morph3D/Standard - Double Sided"
{
    Properties
    {
        _Color("Color", Color) = (1,1,1,1)
        _MainTex("Albedo", 2D) = "white" {}
      
        _Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5

        _Glossiness("Smoothness", Range(0.0, 1.0)) = 0.5
        [Gamma] _Metallic("Metallic", Range(0.0, 1.0)) = 0.0
        _MetallicGlossMap("Metallic", 2D) = "white" {}

        _BumpScale("Scale", Float) = 1.0
        _BumpMap("Normal Map", 2D) = "bump" {}

        _Parallax ("Height Scale", Range (0.005, 0.08)) = 0.02
        _ParallaxMap ("Height Map", 2D) = "black" {}

        _OcclusionStrength("Strength", Range(0.0, 1.0)) = 1.0
        _OcclusionMap("Occlusion", 2D) = "white" {}

        _EmissionColor("Color", Color) = (0,0,0)
        _EmissionMap("Emission", 2D) = "white" {}
      
        _DetailMask("Detail Mask", 2D) = "white" {}

        _DetailAlbedoMap("Detail Albedo x2", 2D) = "grey" {}
        _DetailNormalMapScale("Scale", Float) = 1.0
        _DetailNormalMap("Normal Map", 2D) = "bump" {}

        [Enum(UV0,0,UV1,1)] _UVSec ("UV Set for secondary textures", Float) = 0

        // UI-only data
        [HideInInspector] _EmissionScaleUI("Scale", Float) = 0.0
        [HideInInspector] _EmissionColorUI("Color", Color) = (1,1,1)

        // Blending state
        [HideInInspector] _Mode ("__mode", Float) = 0.0
        [HideInInspector] _SrcBlend ("__src", Float) = 1.0
        [HideInInspector] _DstBlend ("__dst", Float) = 0.0
        [HideInInspector] _ZWrite ("__zw", Float) = 1.0
    }

    CGINCLUDE
        #define UNITY_SETUP_BRDF_INPUT MetallicSetup
    ENDCG

    SubShader
    {
        Tags { "RenderType"="Opaque" "PerformanceChecks"="False" }
        LOD 300
  Cull Off

        // ------------------------------------------------------------------
        //  Base forward pass (directional light, emission, lightmaps, ...)
        Pass
        {
            Name "FORWARD"
            Tags { "LightMode" = "ForwardBase" }
          
            Cull Off

            Blend [_SrcBlend] [_DstBlend]
            ZWrite [_ZWrite]

            CGPROGRAM
            #pragma target 3.0
            // TEMPORARY: GLES2.0 temporarily disabled to prevent errors spam on devices without textureCubeLodEXT
            #pragma exclude_renderers gles
          
            // -------------------------------------
                  
            #pragma shader_feature _NORMALMAP
            #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
            #pragma shader_feature _EMISSION
            #pragma shader_feature _METALLICGLOSSMAP
            #pragma shader_feature ___ _DETAIL_MULX2
            #pragma shader_feature _PARALLAXMAP
          
            #pragma multi_compile_fwdbase
            #pragma multi_compile_fog
              
            #pragma vertex vertForwardBase
            #pragma fragment fragForwardBase

            #include "UnityStandardCore.cginc"

            ENDCG
        }
        // ------------------------------------------------------------------
        //  Additive forward pass (one light per pass)
        Pass
        {
            Name "FORWARD_DELTA"
          
            Tags { "LightMode" = "ForwardAdd" }
            Blend [_SrcBlend] One
            Fog { Color (0,0,0,0) } // in additive pass fog should be black
            ZWrite Off
            ZTest LEqual

            CGPROGRAM
            #pragma target 3.0
            // GLES2.0 temporarily disabled to prevent errors spam on devices without textureCubeLodEXT
            #pragma exclude_renderers gles

            // -------------------------------------

          
            #pragma shader_feature _NORMALMAP
            #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
            #pragma shader_feature _METALLICGLOSSMAP
            #pragma shader_feature ___ _DETAIL_MULX2
            #pragma shader_feature _PARALLAXMAP
          
            #pragma multi_compile_fwdadd_fullshadows
            #pragma multi_compile_fog
          
            #pragma vertex vertForwardAdd
            #pragma fragment fragForwardAdd

            #include "UnityStandardCore.cginc"

            ENDCG
        }
        // ------------------------------------------------------------------
        //  Shadow rendering pass
        Pass {
            Name "ShadowCaster"
            Tags { "LightMode" = "ShadowCaster" }
          
            ZWrite On ZTest LEqual

            CGPROGRAM
            #pragma target 3.0
            // TEMPORARY: GLES2.0 temporarily disabled to prevent errors spam on devices without textureCubeLodEXT
            #pragma exclude_renderers gles
          
            // -------------------------------------


            #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
            #pragma multi_compile_shadowcaster

            #pragma vertex vertShadowCaster
            #pragma fragment fragShadowCaster

            #include "UnityStandardShadow.cginc"

            ENDCG
        }
        // ------------------------------------------------------------------
        //  Deferred pass
        Pass
        {
            Name "DEFERRED"
          
            Cull Off
            Tags { "LightMode" = "Deferred" }

            CGPROGRAM
            #pragma target 3.0
            // TEMPORARY: GLES2.0 temporarily disabled to prevent errors spam on devices without textureCubeLodEXT
            #pragma exclude_renderers nomrt gles
          

            // -------------------------------------

            #pragma shader_feature _NORMALMAP
            #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
            #pragma shader_feature _EMISSION
            #pragma shader_feature _METALLICGLOSSMAP
            #pragma shader_feature ___ _DETAIL_MULX2
            #pragma shader_feature _PARALLAXMAP

            #pragma multi_compile ___ UNITY_HDR_ON
            #pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
            #pragma multi_compile DIRLIGHTMAP_OFF DIRLIGHTMAP_COMBINED DIRLIGHTMAP_SEPARATE
            #pragma multi_compile DYNAMICLIGHTMAP_OFF DYNAMICLIGHTMAP_ON
          
            #pragma vertex vertDeferred
            #pragma fragment fragDeferred

            #include "UnityStandardCore.cginc"

            ENDCG
        }

        // ------------------------------------------------------------------
        // Extracts information for lightmapping, GI (emission, albedo, ...)
        // This pass it not used during regular rendering.
        Pass
        {
            Name "META"
            Tags { "LightMode"="Meta" }


            CGPROGRAM
            #pragma vertex vert_meta
            #pragma fragment frag_meta

            #pragma shader_feature _EMISSION
            #pragma shader_feature _METALLICGLOSSMAP
            #pragma shader_feature ___ _DETAIL_MULX2

            #include "UnityStandardMeta.cginc"
            ENDCG
        }
    }

    SubShader
    {
        Tags { "RenderType"="Opaque" "PerformanceChecks"="False" }
        LOD 150
         Cull Off
        // ------------------------------------------------------------------
        //  Base forward pass (directional light, emission, lightmaps, ...)
        Pass
        {
            Name "FORWARD"
          
            Cull Off
            Tags { "LightMode" = "ForwardBase" }

            Blend [_SrcBlend] [_DstBlend]
            ZWrite [_ZWrite]

            CGPROGRAM
            #pragma target 2.0
          
            #pragma shader_feature _NORMALMAP
            #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
            #pragma shader_feature _EMISSION
            #pragma shader_feature _METALLICGLOSSMAP
            #pragma shader_feature ___ _DETAIL_MULX2
            // SM2.0: NOT SUPPORTED shader_feature _PARALLAXMAP

            #pragma skip_variants SHADOWS_SOFT DIRLIGHTMAP_COMBINED DIRLIGHTMAP_SEPARATE

            #pragma multi_compile_fwdbase
            #pragma multi_compile_fog
  
            #pragma vertex vertForwardBase
            #pragma fragment fragForwardBase

            #include "UnityStandardCore.cginc"

            ENDCG
        }
        // ------------------------------------------------------------------
        //  Additive forward pass (one light per pass)
        Pass
        {
            Name "FORWARD_DELTA"
          
            Tags { "LightMode" = "ForwardAdd" }
            Blend [_SrcBlend] One
            Fog { Color (0,0,0,0) } // in additive pass fog should be black
            ZWrite Off
            ZTest LEqual
          
            CGPROGRAM
            #pragma target 2.0

            #pragma shader_feature _NORMALMAP
            #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
            #pragma shader_feature _METALLICGLOSSMAP
            #pragma shader_feature ___ _DETAIL_MULX2
            // SM2.0: NOT SUPPORTED shader_feature _PARALLAXMAP
            #pragma skip_variants SHADOWS_SOFT
          
            #pragma multi_compile_fwdadd_fullshadows
            #pragma multi_compile_fog
          
            #pragma vertex vertForwardAdd
            #pragma fragment fragForwardAdd

            #include "UnityStandardCore.cginc"

            ENDCG
        }
        // ------------------------------------------------------------------
        //  Shadow rendering pass
        Pass {
            Name "ShadowCaster"
            Tags { "LightMode" = "ShadowCaster" }
          
            ZWrite On ZTest LEqual

            CGPROGRAM
            #pragma target 2.0

            #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
            #pragma skip_variants SHADOWS_SOFT
            #pragma multi_compile_shadowcaster

            #pragma vertex vertShadowCaster
            #pragma fragment fragShadowCaster

            #include "UnityStandardShadow.cginc"

            ENDCG
        }

        // ------------------------------------------------------------------
        // Extracts information for lightmapping, GI (emission, albedo, ...)
        // This pass it not used during regular rendering.
        Pass
        {
            Name "META"
            Tags { "LightMode"="Meta" }

            Cull Off

            CGPROGRAM
            #pragma vertex vert_meta
            #pragma fragment frag_meta

            #pragma shader_feature _EMISSION
            #pragma shader_feature _METALLICGLOSSMAP
            #pragma shader_feature ___ _DETAIL_MULX2

            #include "UnityStandardMeta.cginc"
            ENDCG
        }
    }

    FallBack "VertexLit"
    CustomEditor "StandardShaderGUI"
}

Okay, so if you look through that file there are a number of passes, and Cull Off is set on “FORWARD”, “DEFERRED” and “META”, but none of the others. That’s bad because it means shadows will be wrong on back faces (they won’t cast and won’t properly receive shadows) and secondary lights won’t work on back faces.

It should be pretty easy to fix (and someone should report it to Morph 3d). Look for the two lines that say “LOD 300” and “LOD 150” and add “Cull Off” (with out quotes) to the line after them.

edit: One thing I should mention, double sided shaders like this will have the normal maps inverted on the back faces. If you’re doing hard surface modelling like buildings (as it appears you are) you really shouldn’t use this shader! If your models have holes in them because of back face culling you should be flipping those faces or adding extra geometry.

Thx bgolus that did the trick. Now to try sort out the original issue which is how to create a dark room with one sided meshes whatever side the sun is shining is bright on the inside any clue how i can do that with this shader or is it not possible?

The solution is not to use one sided walls, or even two sided walls that are infinitely thin. You have to have an exterior wall of some kind to block the light. It comes down to the comment I made at the start that we’re doing an approximation of shadows, so an infinitely thin wall will bleed light a little bit along that edge of the shadow approximation.

You can just put in really basic exterior walls set to Cast Shadows > Shadows Only and they won’t render but will block light.

1 Like

Ok thx I will try that i guess will have to use unity cube and squash it down rather than the plane or can the plane work?

Planes will work, but so would a giant cube all the way around your room (it’ll be invisible and you should remove the collider so it doesn’t matter if it’s covering the hallway in etc.)

Yea i did the cube thing but one problem it casts shadow on the wall that the sun is shining on which obviously is not good and if i put it inside the wall it is no better than before what was his faces swapping method u mentioned is there some code around that can test it?Don’t really want to duplicate the geometry as i will have 100’s of houses in view and need to keep polycount down .