Search Unity

Question Shader property doesn't work from instanced prefab from asset bundle.

Discussion in 'General Graphics' started by EOMedvis, Mar 27, 2021.

  1. EOMedvis

    EOMedvis

    Joined:
    Feb 19, 2019
    Posts:
    94
    I'm having a problem getting a shader property to work on an object that I instanced prefab from an asset bundle. The same shader works when the object is dragged directly into the scene via the prefab without the asset bundle.

    As shown below, the shader is just a simple standard shader with a emission overlay to give it a glowing effect:

    Code (CSharp):
    1. Shader "Custom/SurfaceGlow"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Color", Color) = (1,1,1,1)
    6.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    7.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    8.         _Metallic ("Metallic", Range(0,1)) = 0.0
    9.         _BumpMap ("Bumpmap", 2D) = "bump" {}
    10.         _Emission ("Glow Color", Color) = (0.0,1.0,0.0,1.0)
    11.         _GlowStr ("Glow Strength", Range(0.0,1.0)) = 0
    12.     }
    13.     SubShader
    14.     {
    15.         Tags { "RenderType"="Opaque" }
    16.         LOD 200
    17.  
    18.         CGPROGRAM
    19.         // Physically based Standard lighting model, and enable shadows on all light types
    20.         #pragma surface surf Standard fullforwardshadows
    21.  
    22.         // Use shader model 3.0 target, to get nicer looking lighting
    23.         #pragma target 3.0
    24.  
    25.         sampler2D _MainTex;
    26.         sampler2D _BumpMap;
    27.  
    28.         struct Input
    29.         {
    30.             float2 uv_MainTex;
    31.             float2 uv_BumpMap;
    32.             float3 viewDir;
    33.         };
    34.  
    35.         half _Glossiness;
    36.         half _Metallic;
    37.         fixed4 _Color;
    38.         float4 _Emission;
    39.         float _GlowStr;
    40.  
    41.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    42.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    43.         // #pragma instancing_options assumeuniformscaling
    44.         UNITY_INSTANCING_BUFFER_START(Props)
    45.             // put more per-instance properties here
    46.         UNITY_INSTANCING_BUFFER_END(Props)
    47.  
    48.         void surf (Input IN, inout SurfaceOutputStandard o)
    49.         {
    50.             // Albedo comes from a texture tinted by color
    51.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    52.             o.Albedo = c.rgb;
    53.             o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    54.             half glow;
    55.  
    56.             if(_GlowStr == 0)
    57.             {
    58.                 glow = 1.0;              
    59.             }          
    60.             else
    61.             {
    62.                 glow = 1.0 - saturate(dot(normalize(IN.viewDir), o.Normal));
    63.             }
    64.  
    65.             o.Emission = _Emission.rgb * pow(_GlowStr, glow);          
    66.             o.Metallic = _Metallic;
    67.             o.Smoothness = _Glossiness;
    68.             o.Alpha = c.a;          
    69.         }
    70.         ENDCG
    71.     }
    72.     FallBack "Diffuse"
    73. }
    74.  
    I have a simple material for a gameobject using this shader.

    When I drag the prefab directly into the scene, whether the game is running or not, I select the prefab object with the material and set the slider controls, the material glows as intended. So I know the shader works:

    upload_2021-3-27_16-47-19.png

    but when the object from an asset bundle containing the prefab, and I do the same by selecting the instanced game object and setting its material directly, the slider does nothing:

    upload_2021-3-27_16-50-10.png

    My goal is to set the glow value by script during runtime, but I'm unable to do so because the instanced object ignores the setting in the shader, even when I directly select the instance game object and set its material property directly in-game with the slider.

    What am I overlooking here? Thanks in advance for your help. :)
     

    Attached Files:

    Javen99 likes this.
  2. EOMedvis

    EOMedvis

    Joined:
    Feb 19, 2019
    Posts:
    94