Search Unity

Why i don't see the shader i created by script ?

Discussion in 'Shaders' started by Chocolade, Jul 5, 2017.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    933
    I'm using this wiki page:

    http://wiki.unity3d.com/index.php?title=Shield

    I have a spaceship object and i want the shield to be around the spaceship when the spaceship is in the middle of the shield.

    In the editor i did: Assets > Create > Shader > Standrad Surface Shader
    The double click on the created shader opened it in visual studio and added this code:

    Code (csharp):
    1.  
    2. Shader "Custom/NewSurfaceShader" {
    3.     Properties
    4.     {
    5.         _Color("_Color", Color) = (0.0,1.0,0.0,1.0)
    6.         _Inside("_Inside", Range(0.0,2.0)) = 0.0
    7.         _Rim("_Rim", Range(0.0,1.0)) = 1.2
    8.         _Texture("_Texture", 2D) = "white" {}
    9.     _Speed("_Speed", Range(0.5,5.0)) = 0.5
    10.         _Tile("_Tile", Range(1.0,10.0)) = 5.0
    11.         _Strength("_Strength", Range(0.0,5.0)) = 1.5
    12.     }
    13.  
    14.         SubShader
    15.     {
    16.         Tags
    17.     {
    18.         "Queue" = "Transparent"
    19.         "IgnoreProjector" = "True"
    20.         "RenderType" = "Transparent"
    21.  
    22.     }
    23.  
    24.  
    25.         Cull Back
    26.         ZWrite On
    27.         ZTest LEqual
    28.  
    29.  
    30.         CGPROGRAM
    31. #pragma surface surf BlinnPhongEditor alpha vertex:vert
    32.         //#pragma target 3.0
    33.  
    34.  
    35.         fixed4 _Color;
    36.     sampler2D _CameraDepthTexture;
    37.     fixed _Inside;
    38.     fixed _Rim;
    39.     sampler2D _Texture;
    40.     fixed _Speed;
    41.     fixed _Tile;
    42.     fixed _Strength;
    43.  
    44.     struct EditorSurfaceOutput
    45.     {
    46.         half3 Albedo;
    47.         half3 Normal;
    48.         half3 Emission;
    49.         half3 Gloss;
    50.         half Specular;
    51.         half Alpha;
    52.     };
    53.  
    54.     inline half4 LightingBlinnPhongEditor_PrePass(EditorSurfaceOutput s, half4 light)
    55.     {
    56.         half3 spec = light.a * s.Gloss;
    57.  
    58.         half4 c;
    59.  
    60.         c.rgb = (s.Albedo * light.rgb + light.rgb * spec);
    61.  
    62.         c.a = s.Alpha + Luminance(spec);
    63.  
    64.         return c;
    65.  
    66.  
    67.     }
    68.  
    69.     inline half4 LightingBlinnPhongEditor(EditorSurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
    70.     {
    71.         viewDir = normalize(viewDir);
    72.         half3 h = normalize(lightDir + viewDir);
    73.  
    74.         half diff = max(0, dot(s.Normal, lightDir));
    75.  
    76.         float nh = max(0, dot(s.Normal, h));
    77.         float3 spec = pow(nh, s.Specular*128.0) * s.Gloss;
    78.  
    79.         half4 res;
    80.         res.rgb = _LightColor0.rgb * (diff * atten * 2.0);
    81.         res.w = spec * Luminance(_LightColor0.rgb);
    82.  
    83.         return LightingBlinnPhongEditor_PrePass(s, res);
    84.     }
    85.  
    86.     struct Input
    87.     {
    88.         float4 screenPos;
    89.         float3 viewDir;
    90.         float2 uv_Texture;
    91.     };
    92.  
    93.  
    94.     void vert(inout appdata_full v, out Input o)
    95.     {
    96.         UNITY_INITIALIZE_OUTPUT(Input,o);
    97.     }
    98.  
    99.  
    100.     void surf(Input IN, inout EditorSurfaceOutput o)
    101.     {
    102.         o.Albedo = fixed3(0.0,0.0,0.0);
    103.         o.Normal = fixed3(0.0,0.0,1.0);
    104.         o.Emission = 0.0;
    105.         o.Gloss = 0.0;
    106.         o.Specular = 0.0;
    107.         o.Alpha = 1.0;
    108.         float4 ScreenDepthDiff0 = LinearEyeDepth(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(IN.screenPos)).r) - IN.screenPos.z;
    109.         float4 Saturate0 = fixed4(0.3,0.3,0.3,1.0);//
    110.         float4 Fresnel0_1_NoInput = fixed4(0,0,1,1);
    111.         float dNorm = 1.0 - dot(normalize(float4(IN.viewDir, 1.0).xyz), normalize(Fresnel0_1_NoInput.xyz));
    112.         float4 Fresnel0 = float4(dNorm,dNorm,dNorm,dNorm);
    113.         float4 Step0 = step(Fresnel0,float4(1.0, 1.0, 1.0, 1.0));
    114.         float4 Clamp0 = clamp(Step0,_Inside.xxxx,float4(1.0, 1.0, 1.0, 1.0));
    115.         float4 Pow0 = pow(Fresnel0,(_Rim).xxxx);
    116.         float4 Multiply5 = _Time * _Speed.xxxx;
    117.         float4 UV_Pan0 = float4((IN.uv_Texture.xyxy).x,(IN.uv_Texture.xyxy).y + Multiply5.x,(IN.uv_Texture.xyxy).z,(IN.uv_Texture.xyxy).w);
    118.         float4 Multiply1 = UV_Pan0 * _Tile.xxxx;
    119.         float4 Tex2D0 = tex2D(_Texture,Multiply1.xy);
    120.         float4 Multiply2 = Tex2D0 * _Strength.xxxx;
    121.         float4 Multiply0 = Pow0 * Multiply2;
    122.         float4 Multiply3 = Clamp0 * Multiply0;
    123.         float4 Multiply4 = Saturate0 * Multiply3;
    124.         o.Emission = Multiply3.xyz * _Color.rgb;
    125.         o.Alpha = Multiply3.w * _Color.a;
    126.  
    127.     }
    128.     ENDCG
    129.     }
    130.         Fallback "Diffuse"
    131. }
    132.  
    Then same way i created a csharp script file and entered this code:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class ShieldUvAnimation : MonoBehaviour {
    7.  
    8.     public GameObject iShield;
    9.     public float iSpeed;
    10.  
    11.     private Material mMaterial;
    12.     private float mTime;
    13.  
    14.     // Use this for initialization
    15.     void Start()
    16.     {
    17.         mMaterial = iShield.GetComponent<Renderer>().material;
    18.  
    19.         mTime = 0.0f;
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         mTime += Time.deltaTime * iSpeed;
    26.  
    27.         mMaterial.SetFloat("_Offset", Mathf.Repeat(mTime, 1.0f));
    28.     }
    29.  
    30. }
    31.  
    Then for testing i created a empty GameObject attached the csharp script to it.
    But now what should i do with the Shader ? I can't drag it anywhere to the empty GameObject.

    And also what should i put in the inspector of the empty GameObject in the script in the filed iShield ?


    Update

    What i tried now is:

    Ok i dragged the shader i created to a new material i created.
    Then i changed in the script the variable

    Code (csharp):
    1. private Material mMaterial;
    To be public

    Code (csharp):
    1. public Material mMaterial;
    Then i dragged the new material to the empty gameobject in the inspector to the filed mMaterial.
    And in the filed iShield i dragged there my spaceship object.

    I'm running the game no errors but i don't see any shield.
     
    Last edited: Jul 5, 2017