Search Unity

How to shape this shader more like a triangle than a circle ?

Discussion in 'Shaders' started by dumasflo, Nov 27, 2014.

  1. dumasflo

    dumasflo

    Joined:
    Nov 27, 2014
    Posts:
    2
    Hi,

    I'm working on a third person game, and I'm trying to make a sight vision triangle (or cone-shaped).

    I followed a tutorial to make a shader of fog of war which is what I want actually, but it shapes a circle. Is there a possibility to draw a cone-shaped ?

    Video of tutorial with the script of the shader :


    Screenshot of my current prototype :

    What I want :


    Thanks for help, if you need some more information, just ask.
     
  2. vizigr0u

    vizigr0u

    Joined:
    Nov 27, 2014
    Posts:
    1
    We discussed this in private but I'm assuming posting about it could help people.
    My approach would be to keep the same principle, passing more information to the shader: the "forward" vector (where the direction of the vision cone) and the angle of the cone (or this can be hardcoded in the shader).

    In the tutorial, a comment says "return 0 if pos - nearvertex > fogRadius", thats is "keep black every point outside the radius", you need to implement it such as it ALSO return 0 if the angle between the forward vector and the vector defined by (pos - player position) is superior to a predefined angle.
    I haven't really messed with Unity shaders for a while so I won't risk a bad implementation but you get the idea.
     
  3. dumasflo

    dumasflo

    Joined:
    Nov 27, 2014
    Posts:
    2
    Yup,

    So it seems like the first thing to do is to get the transform.forward of my character, and to send it into the shader script.

    To do so i tried to add this in my c# script :

    Code (CSharp):
    1.    
    2. void Update ()
    3.     {
    4.         Vector3 char_forward = _c.transform.forward ;
    5.         GetComponent<Renderer>().material.SetVector("Player1_Forward", char_forward) ;
    6.     }
    7.  
    and to declare it in the shader :


    And I get this when I try to read the result :


    What i miss ??

    This is the full shader script :
    Code (CSharp):
    1. Shader "Custom/FogOfWar" {
    2. Properties {
    3.     _Color ("Main Color", Color) = (1,1,1,1)
    4.     _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    5.     _FogRadius ("FogRadius", Float) = 1.0
    6.     _FogMaxRadius ("FogMaxRadius", Float) = 1.0
    7.     _Player1_Pos ("Player1_Pos", Vector) = (0,0,0,1)
    8.     _Player1_Forward ("Player1_Forward", Vector) = (0,0,0,1)
    9. }
    10.  
    11. SubShader {
    12.     Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    13.     LOD 200
    14.     Blend SrcAlpha OneMinusSrcAlpha
    15.     Cull Off
    16.  
    17.     CGPROGRAM
    18.     #pragma surface surf Lambert vertex:vert
    19.  
    20.     sampler2D _MainTex;
    21.     fixed4 _Color;
    22.     float _FogRadius;
    23.     float _FogMaxRadius;
    24.     float4 _Player1_Forward;
    25.     float4 _Player1_Pos;
    26.  
    27.     struct Input {
    28.         float2 uv_MainTex;
    29.         float2 location;
    30.         float4 _Player1_Forward;
    31.     };
    32.  
    33.     float powerForPos(float4 pos, float2 nearVertex);
    34.  
    35.     void vert(inout appdata_full vertexData, out Input outData) {
    36.         float4 pos = mul(UNITY_MATRIX_MVP, vertexData.vertex);
    37.         float4 posWorld = mul(_Object2World, vertexData.vertex);
    38.         outData.uv_MainTex = vertexData.texcoord;
    39.         outData.location = posWorld.xz;
    40.     }
    41.  
    42.     void surf (Input IN, inout SurfaceOutput o) {
    43.         fixed4 baseColor = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    44.        
    45.         float alpha = (1.0 - (baseColor.a + powerForPos (_Player1_Pos, IN.location)));
    46.         o.Albedo = baseColor.rgb;
    47.         o.Alpha = alpha;
    48.     }
    49.    
    50.     // return 0 if (pos - nearVertex) > _FogRadius
    51.     float powerForPos(float4 pos, float2 nearVertex) {
    52.  
    53.         float atten = clamp(_FogRadius - length(pos.xz - nearVertex.xy), 0.0, _FogRadius);
    54.        
    55.         return (1.0/_FogMaxRadius)*atten/_FogRadius;
    56.     }
    57.     ENDCG
    58. }
    59.  
    60. Fallback "Transparent/VertexLit"
    61. }
    62.