Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to make object receive lighting?

Discussion in 'Shaders' started by liuyu, Feb 6, 2015.

  1. liuyu

    liuyu

    Joined:
    Oct 14, 2014
    Posts:
    28
    Hi,
    There are three object: plane, sphere, capsule. The plane and sphere use default shader "Diffuse", the capsule use my shader "TestShader".
    To building PC exe, and using DirectiX tool: PIX to analyze the draw sequeces.
    1. No lighting Plane
    2. No lighting sphere
    3. No lighting capsule
    4. Lighting plane (Alpha one one)
    5. Lighting sphere (Alpha one one)
    Why there isn't Lighting capsule.
    Is there need some codes in TestShader to lighting? Thanks.
    The project MyProj7 is: http://pan.baidu.com/s/1dDD3F93
    It is 4.6.2.
     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    I didn't look at the project, but it probably lies in the fact that alpha blended objects don't receive (or cast) shadows.
     
  3. liuyu

    liuyu

    Joined:
    Oct 14, 2014
    Posts:
    28
  4. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Yes, it can be downloaded, but I'm not going to. Just post your shader code here.
     
    bloomingdedalus likes this.
  5. liuyu

    liuyu

    Joined:
    Oct 14, 2014
    Posts:
    28
    The code is very simple:
    Code (CSharp):
    1. Shader "TestShader" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white" {}
    4.     }
    5.     SubShader {
    6.         Tags { "RenderType"="Opaque" }
    7.         LOD 200
    8.         Pass
    9.         {      
    10.             Tags {"LightMode" = "ForwardBase"}
    11.            
    12.             CGPROGRAM
    13.             #pragma multi_compile_fwdbase
    14.             #pragma vertex Vert_T
    15.             #pragma fragment Frag_T
    16.             #pragma fragmentoption ARB_precision_hint_fastest
    17.            
    18.             uniform sampler2D _MainTex;
    19.             float4 _MainTex_ST;
    20.  
    21.             struct PX_T_invs
    22.             {
    23.                 float4 vertex : POSITION;
    24.                 half2 texcoord : TEXCOORD0;
    25.             };      
    26.             struct PX_T_outvs
    27.             {
    28.                 float4 pos : POSITION;
    29.                 half2 texcoord : TEXCOORD0;
    30.             };
    31.  
    32.             PX_T_outvs Vert_T(PX_T_invs i)
    33.             {
    34.                 PX_T_outvs o;              
    35.                 o.pos = mul (UNITY_MATRIX_MVP, i.vertex);
    36.                 o.texcoord = i.texcoord;          
    37.                 return o;
    38.             }
    39.             fixed4 Frag_T( PX_T_outvs i ) : COLOR
    40.             {
    41.                 fixed4 colorTex = tex2D(_MainTex, i.texcoord);
    42.                 return colorTex;
    43.             }
    44.        
    45.             ENDCG
    46.         }
    47.     }
    48.     FallBack "Diffuse"
    49. }
    I know there aren't any codes about point light, but the sphere draw two times.
    The first time --- no lighting - just like the capsule using "TestShader"
    And the second time is lighting the sphere, like deffered shading's point light.
    So I want to know how to let the "TestShader" can lighting object like the "Diffuse" shader.
     
  6. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Normally you first render the object distance from the perspective of the lights. You'll use this later for the shadows.

    Next in deferred mode, you render the objects in to the g-buffer.

    The you can accumulate the lighting (with shadows) based on the information from the g-buffer.

    The issue I see here is that the lighting mode is set to forward base. This means it's skipped in the deferred pass and the geometry won't be in the g-buffer.
     
  7. liuyu

    liuyu

    Joined:
    Oct 14, 2014
    Posts:
    28
    No, there isn't need g-buffer. You can download the "MyProj7.rar", and building it to pc. Using PIX of directx to perf the draw sequence. You will find that the plane and sphere draw twice, and the capsule draw only once. The second drawing of plane and sphere is point lighting.
    So I want to know why the plane and sphere can draw twice. Thank you.