Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Multiple "circles that reveal texture" for dummies

Discussion in 'Shaders' started by Slybianco, Jan 1, 2021.

  1. Slybianco

    Slybianco

    Joined:
    Jan 1, 2021
    Posts:
    3
    Greetings, I'm trying to modify a Shader, provided by Ioannis Athanasiou in this video:


    What I would like to achieve is having multiple objects that will reveal multiple part of a provided texture. There are two main files, shader and a .cs script. The script is used to assign the "light" to an object, which, I can move around to reveal the texture. Unfortunately I can only work with Unity 2019.1.x since Tabletop Simulator is supporting only that version. I haven't studied shaders... so, is anyone willing to help me out? Currently, this works very fine, only with a "light".

    Code (CSharp):
    1. Shader "Custom/MapShader" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _MainTex ("Map (RGB)", 2D) = "white" {}
    5.         _Glossiness ("Smoothness", Range(0,1)) = 0.0
    6.         _Metallic ("Metallic", Range(0,1)) = 0.0
    7.         _VisRan ("Visibility Range", Range(0,10)) = 2.0
    8.         _LightSphere1_Pos("LightSphere1", vector) = (0,0,0,1)
    9.     }
    10.     SubShader {
    11.         Tags { "RenderType"="Opaque" }
    12.         LOD 200
    13.  
    14.         CGPROGRAM
    15.         // Physically based Standard lighting model, and enable shadows on all light types
    16.         #pragma surface surf Standard fullforwardshadows
    17.  
    18.         // Use shader model 3.0 target, to get nicer looking lighting
    19.         #pragma target 3.0
    20.  
    21.         sampler2D _MainTex;
    22.         float4 _LightSphere1_Pos;
    23.  
    24.         struct Input {
    25.             float2 uv_MainTex;
    26.             float3 worldPos; //World Position;
    27.         };
    28.  
    29.         fixed4 _Color;
    30.         half _Glossiness;
    31.         half _Metallic;
    32.         half _VisRan;
    33.  
    34.  
    35.         float4 _Posi;
    36.  
    37.  
    38.         void surf(Input IN, inout SurfaceOutputStandard o)
    39.         {
    40.             // RAYMARCH RESOLUTION (YOU CAN CHANGE THIS TO REDUCE THE
    41.             // JAGGED EDGE ARTIFACTS BUT DON'T OVERDO IT)
    42.             int rayRes = 60;
    43.  
    44.             fixed4 cMap = tex2D(_MainTex, IN.uv_MainTex);
    45.  
    46.             _Color = pow(_Color, 1.0 / 2.2); //REVERSE THE POSITION/COLOR VALUE FROM NON LINEAR GAMMA TO LINEAR
    47.             _Color.r -= 100.0;
    48.             _Color.g -= 100.0;
    49.  
    50.             float2 vecio = float2(_Color.r, _Color.g);
    51.             float2 direction2 = (IN.worldPos.xz - vecio.xy) / (rayRes*10.0*_Color.b);
    52.  
    53.             bool clear = true;
    54.  
    55.             // This loop is UNROLLED during compilation and not DYNAMICALLY BRANCHED in runtime.
    56.             for (int count = 0; count <= rayRes; count++)
    57.             {
    58.                 fixed cBlock = tex2D(_MainTex, IN.uv_MainTex - direction2 * count).a;
    59.  
    60.                 if (cBlock < 0.1)
    61.                 {
    62.                     clear = false;
    63.                 }
    64.             }
    65.  
    66.             float dist = (length(direction2)*rayRes*10.0) / (_VisRan);
    67.             if (clear)
    68.             {
    69.                 float val = sin(_Time * 360 * 0.2)*0.2; // -0.2 to 0.2 f=0.2Hz
    70.                 o.Albedo = cMap.rgb*pow(saturate(1.0 - dist), 2.0 + val);
    71.             }
    72.             else
    73.             {
    74.                 o.Albedo = float3(0, 0, 0);
    75.             }
    76.  
    77.             /*
    78.             // UNCOMMENT TO REVEAL RED CIRCLE BELOW LIGHT OBJECT FOR POSITION DEBUGGING REASONS
    79.             if(length(direction2) < 0.0001)
    80.             {
    81.                 o.Albedo = float3(1,0,0);
    82.             }
    83.             */
    84.  
    85.             ////// Metallic and smoothness come from slider variables
    86.             o.Metallic = _Metallic;
    87.             o.Smoothness = _Glossiness;
    88.             o.Alpha = 1.0;
    89.         }
    90.  
    91.         ENDCG
    92.     }
    93.     FallBack "Diffuse"
    94. }
    95.  


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [ExecuteInEditMode]
    5. public class LightPos : MonoBehaviour {
    6.     // Use this for initialization
    7.     void Start ()
    8.     {
    9.         ///empty...
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update ()
    14.     {
    15.         // Set light position.
    16.         Vector3 pos = this.transform.position;
    17.        
    18.         // Gamma transform due to TTS similar transformation and add 100
    19.         // to deal with negative position coordinates and avoid clamping.
    20.         pos.x = Mathf.Pow(pos.x+100, 2.2f);
    21.         pos.z = Mathf.Pow(pos.z+100, 2.2f);
    22.  
    23.         // Set light position as color.
    24.         Renderer rend = GameObject.Find("A_Plane").GetComponent<Renderer>();
    25.         rend.sharedMaterial.color = new Color(pos.x, pos.z, 1, 1);
    26.  
    27.     }
    28. }
     

    Attached Files:

  2. Slybianco

    Slybianco

    Joined:
    Jan 1, 2021
    Posts:
    3
    Found a solution, no need for further help, thanks anyway
     
  3. falconfetus8

    falconfetus8

    Joined:
    Feb 4, 2013
    Posts:
    17
    What was your solution? Don't leave us hanging!
     
  4. Slybianco

    Slybianco

    Joined:
    Jan 1, 2021
    Posts:
    3
    Sure,

    First thing, you need to attach the script: "BurocraziaLuci.cs" to the object which has the texture you want to hide.

    Then create some objects you want to move around and attach "LightPos.cs". This object need to have a new tag, "Lucina";

    In the end, Below the Script field there's a value called "numero"; you can set it's value from 0 to 3. There are four lights in our project. Each light must have unique value.

    Further info on how to import this to tabletop simulator, will be shared once we have a solution for that too
     

    Attached Files: