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

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.

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.

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 :

void Update ()
    {
        Vector3 char_forward = _c.transform.forward ;
        GetComponent<Renderer>().material.SetVector("Player1_Forward", char_forward) ;
    }

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 :

Shader "Custom/FogOfWar" {
Properties {
    _Color ("Main Color", Color) = (1,1,1,1)
    _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    _FogRadius ("FogRadius", Float) = 1.0
    _FogMaxRadius ("FogMaxRadius", Float) = 1.0
    _Player1_Pos ("Player1_Pos", Vector) = (0,0,0,1)
    _Player1_Forward ("Player1_Forward", Vector) = (0,0,0,1)
}

SubShader {
    Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    LOD 200
    Blend SrcAlpha OneMinusSrcAlpha
    Cull Off

    CGPROGRAM
    #pragma surface surf Lambert vertex:vert

    sampler2D _MainTex;
    fixed4 _Color;
    float _FogRadius;
    float _FogMaxRadius;
    float4 _Player1_Forward;
    float4 _Player1_Pos;

    struct Input {
        float2 uv_MainTex;
        float2 location;
        float4 _Player1_Forward;
    };

    float powerForPos(float4 pos, float2 nearVertex);

    void vert(inout appdata_full vertexData, out Input outData) {
        float4 pos = mul(UNITY_MATRIX_MVP, vertexData.vertex);
        float4 posWorld = mul(_Object2World, vertexData.vertex);
        outData.uv_MainTex = vertexData.texcoord;
        outData.location = posWorld.xz;
    }

    void surf (Input IN, inout SurfaceOutput o) {
        fixed4 baseColor = tex2D(_MainTex, IN.uv_MainTex) * _Color;
       
        float alpha = (1.0 - (baseColor.a + powerForPos (_Player1_Pos, IN.location)));
        o.Albedo = baseColor.rgb;
        o.Alpha = alpha;
    }
   
    // return 0 if (pos - nearVertex) > _FogRadius
    float powerForPos(float4 pos, float2 nearVertex) {

        float atten = clamp(_FogRadius - length(pos.xz - nearVertex.xy), 0.0, _FogRadius);
       
        return (1.0/_FogMaxRadius)*atten/_FogRadius;
    }
    ENDCG
}

Fallback "Transparent/VertexLit"
}