Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by AkshayVenkataramanan · Jan 29, 2017 at 04:01 PM · shadermaterialtransparency

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AkshayVenkataramanan · Jan 29, 2017 at 06:14 PM 0
Share

Thanks , That did it !!!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

121 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges