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”.
MapShader.shader
Shader "Custom/MapShader" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Map (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.0
_Metallic ("Metallic", Range(0,1)) = 0.0
_VisRan ("Visibility Range", Range(0,10)) = 2.0
_LightSphere1_Pos("LightSphere1", vector) = (0,0,0,1)
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
float4 _LightSphere1_Pos;
struct Input {
float2 uv_MainTex;
float3 worldPos; //World Position;
};
fixed4 _Color;
half _Glossiness;
half _Metallic;
half _VisRan;
float4 _Posi;
void surf(Input IN, inout SurfaceOutputStandard o)
{
// RAYMARCH RESOLUTION (YOU CAN CHANGE THIS TO REDUCE THE
// JAGGED EDGE ARTIFACTS BUT DON'T OVERDO IT)
int rayRes = 60;
fixed4 cMap = tex2D(_MainTex, IN.uv_MainTex);
_Color = pow(_Color, 1.0 / 2.2); //REVERSE THE POSITION/COLOR VALUE FROM NON LINEAR GAMMA TO LINEAR
_Color.r -= 100.0;
_Color.g -= 100.0;
float2 vecio = float2(_Color.r, _Color.g);
float2 direction2 = (IN.worldPos.xz - vecio.xy) / (rayRes*10.0*_Color.b);
bool clear = true;
// This loop is UNROLLED during compilation and not DYNAMICALLY BRANCHED in runtime.
for (int count = 0; count <= rayRes; count++)
{
fixed cBlock = tex2D(_MainTex, IN.uv_MainTex - direction2 * count).a;
if (cBlock < 0.1)
{
clear = false;
}
}
float dist = (length(direction2)*rayRes*10.0) / (_VisRan);
if (clear)
{
float val = sin(_Time * 360 * 0.2)*0.2; // -0.2 to 0.2 f=0.2Hz
o.Albedo = cMap.rgb*pow(saturate(1.0 - dist), 2.0 + val);
}
else
{
o.Albedo = float3(0, 0, 0);
}
/*
// UNCOMMENT TO REVEAL RED CIRCLE BELOW LIGHT OBJECT FOR POSITION DEBUGGING REASONS
if(length(direction2) < 0.0001)
{
o.Albedo = float3(1,0,0);
}
*/
////// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = 1.0;
}
ENDCG
}
FallBack "Diffuse"
}
LightPos.cs
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class LightPos : MonoBehaviour {
// Use this for initialization
void Start ()
{
///empty...
}
// Update is called once per frame
void Update ()
{
// Set light position.
Vector3 pos = this.transform.position;
// Gamma transform due to TTS similar transformation and add 100
// to deal with negative position coordinates and avoid clamping.
pos.x = Mathf.Pow(pos.x+100, 2.2f);
pos.z = Mathf.Pow(pos.z+100, 2.2f);
// Set light position as color.
Renderer rend = GameObject.Find("A_Plane").GetComponent<Renderer>();
rend.sharedMaterial.color = new Color(pos.x, pos.z, 1, 1);
}
}
6677755–765118–MapShader.shader (2.68 KB)
6677755–765121–LightPos.cs (755 Bytes)