Add distance fade to his shader
Hey , i am new to shades and wanted to create a wall ,that renders a only the part close to the player . I found what i wanted from this Question : http://answers.unity3d.com/questions/529814/how-to-have-2-different-objects-at-the-same-place.html
But other that a outline color i want to fade the texture away at the edges so that i get a type of feather effect.
How do i modify the code to archive this ?
Shader :
Shader "Custom/Proximity" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {} // Regular object texture
_PlayerPosition ("Player Position", vector) = (0,0,0,0) // The location of the player - will be set by script
_VisibleDistance ("Visibility Distance", float) = 10.0 // How close does the player have to be to make object visible
_OutlineWidth ("Outline Width", float) = 3.0 // Used to add an outline around visible area a la Mario Galaxy - http://www.youtube.com/watch?v=91raP59am9U
_OutlineColour ("Outline Colour", color) = (1.0,1.0,0.0,1.0) // Colour of the outline
}
SubShader {
Tags { "RenderType"="Transparent" "Queue"="Transparent"}
Pass {
Blend SrcAlpha OneMinusSrcAlpha
LOD 200
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// Access the shaderlab properties
uniform sampler2D _MainTex;
uniform float4 _PlayerPosition;
uniform float _VisibleDistance;
uniform float _OutlineWidth;
uniform fixed4 _OutlineColour;
// Input to vertex shader
struct vertexInput {
float4 vertex : POSITION;
float4 texcoord : TEXCOORD0;
};
// Input to fragment shader
struct vertexOutput {
float4 pos : SV_POSITION;
float4 position_in_world_space : TEXCOORD0;
float4 tex : TEXCOORD1;
};
// VERTEX SHADER
vertexOutput vert(vertexInput input)
{
vertexOutput output;
output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
output.position_in_world_space = mul(unity_ObjectToWorld, input.vertex);
output.tex = input.texcoord;
return output;
}
// FRAGMENT SHADER
float4 frag(vertexOutput input) : COLOR
{
// Calculate distance to player position
float dist = distance(input.position_in_world_space, _PlayerPosition);
// Return appropriate colour
if (dist < _VisibleDistance) {
return tex2D(_MainTex, float4(input.tex)); // Visible
}
else if (dist < _VisibleDistance + _OutlineWidth) {
return _OutlineColour; // Edge of visible range
}
else {
float4 tex = tex2D(_MainTex, float4(input.tex)); // Outside visible range
tex.a = 0;
return tex;
}
}
ENDCG
}
}
//FallBack "Diffuse"
}
The _PlayerPosition is given to shade by this code :
using UnityEngine;
using System.Collections;
public class ProximityXray : MonoBehaviour {
public Transform player;
Renderer render;
void Start () {
render = gameObject.GetComponent<Renderer>();
}
void Update () {
render.sharedMaterial.SetVector("_PlayerPosition", player.position);
}
}
Thanks
Answer by Glurth · Jan 29, 2017 at 05:24 PM
if (dist < _VisibleDistance) {
return tex2D(_MainTex, float4(input.tex));
You can adjust the Alpha value (transparency) of the color returned by tex2D. eg:
color= tex2D(_MainTex, float4(input.tex));
color.a = 1- (dist/_VisibleDistance); //set color alpha
return color;
In this example we set the opacity using the distance as fraction of the _VisibleDistance. An alpha result of 0 means completely transparent, a value of 1 mean completely opaque: which is why we subtract from 1. You will probably want to adjust this formula somewhat, so it doesn't start fading until some minimum distance.
Your answer
![](https://koobas.hobune.stream/wayback/20220612105900im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Transparent textures are still slightly darker, how do I achieve 100% transparency? 1 Answer
how to hide objects inside an invisible object? 0 Answers
Shader Transparency Issues 1 Answer
Shader not working properly in a build 0 Answers
HDRP Issue: Transparent + Distortion + object behind not showing through object 0 Answers