Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Coloring lot of people by object position via shader for batching

Discussion in 'Shaders' started by EwigeDreamer, May 6, 2019.

  1. EwigeDreamer

    EwigeDreamer

    Joined:
    Sep 29, 2017
    Posts:
    3
    hi)
    i create shader for coloring static people. a lot. material with this shader must be bathing, cause all materials of my people has similar properties. but...

    in Scene view all looks like great. in Game view all objects has one color =(
    screen.PNG
    what's my problem? what am I doing wrong?
    i use Unity 2018.3.14f1
    shader code:
    Code (CSharp):
    1. Shader "Custom/MaskedColorPeopleView"
    2. {
    3.     Properties
    4.     {
    5.         _Tex1 ("Albedo (RGB)", 2D) = "white" {}
    6.  
    7.         _NoiseMap ("Albedo (RGB)", 2D) = "gray" {}
    8.  
    9.         _SkinGrad ("Albedo (RGB)", 2D) = "white" {}
    10.         _ClothGrad ("Albedo (RGB)", 2D) = "white" {}
    11.     }
    12.     SubShader
    13.     {
    14.         Tags { "Queue" = "Transparent" "RenderType"="Opaque" "PreviewType"="plane"}
    15.         LOD 200
    16.    
    17.         Pass
    18.         {
    19.             CGPROGRAM
    20.             #pragma vertex vert
    21.             #pragma fragment frag
    22.    
    23.             #include "UnityCG.cginc"
    24.    
    25.             sampler2D _Tex1;
    26.             sampler2D _NoiseMap;
    27.             sampler2D _SkinGrad;
    28.             sampler2D _ClothGrad;
    29.  
    30.             struct Input
    31.             {
    32.                 float2 uv_MainTex;
    33.                 float4 objPos;
    34.             };
    35.    
    36.             struct appdata
    37.             {
    38.                 float4 vertex : POSITION;
    39.                 float2 uv : TEXCOORD0;
    40.             };
    41.  
    42.             struct v2f
    43.             {
    44.                 float4 vertex : SV_POSITION;
    45.                 float2 uv : TEXCOORD0;
    46.                 float4 worldPos : COLOR;
    47.             };
    48.        
    49.             v2f vert (appdata v)
    50.             {
    51.                 v2f o;
    52.                 o.vertex = UnityObjectToClipPos(v.vertex);
    53.                 o.uv = v.uv;
    54.                 o.worldPos = mul(unity_ObjectToWorld, float4(0,0,0,1));
    55.                 return o;
    56.             }
    57.        
    58.             fixed4 frag (v2f i) : SV_Target
    59.             {
    60.                 float3 pos = fmod(abs(i.worldPos / 10), fixed3(1,1,1));
    61.                 fixed4 noiseMap = tex2D(_NoiseMap, float2(pos.r, pos.b));
    62.            
    63.                 fixed4 tex1 = tex2D(_Tex1, i.uv);
    64.  
    65.                 fixed4 skinColor = tex2D(_SkinGrad, float2(noiseMap.r, 0.5)) * tex1.r * tex1.g;
    66.                 fixed4 shirtColor = tex2D(_ClothGrad, float2(noiseMap.g, 0.5)) * tex1.r * tex1.b;
    67.                 fixed4 pantsColor = tex2D(_ClothGrad, float2(noiseMap.b, 0.5)) * tex1.r * tex1.a;
    68.  
    69.                 fixed4 col = skinColor + shirtColor + pantsColor;
    70.                 col.a = 1;
    71.                 return col;
    72.             }
    73.             ENDCG
    74.         }
    75.     }
    76.     FallBack "Diffuse"
    77. }
    78.  
    аssets in attachments.
    PS: I hope I will not be banned .. I just create spectators in my game..
     

    Attached Files:

    Last edited: May 6, 2019
  2. sylon

    sylon

    Joined:
    Mar 5, 2017
    Posts:
    242
    I suspect your objects are being combined by static batching.
    In this case Unity will combine those objects into 1 which will mess up your position data.
     
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    5,186
    can test it by adding this inside Tags {}

    Code (CSharp):
    1. "DisableBatching"="True"
     
  4. EwigeDreamer

    EwigeDreamer

    Joined:
    Sep 29, 2017
    Posts:
    3
    Yep, the propblem in batching.. idea for perfomance, but perfomanse will destroy idea) thanks)