Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by DkSker · Oct 27, 2021 at 07:33 PM · shaderrendertexturertsfog of war

Shaders making raw image invisible

Problem: I am currently implementing fog of war into my RTS 2D game. I read an article written by Andrew Huang on how to implement this, however, in order to render the actual 'fog' he used a projector which I realized would not work with 2D games (At this stage, I have the correct RenderTexture that will update in runtime to work with). My initial idea is to setup a quad mesh in front of my camera and use a shader to render the textures onto the quad, however, I have no idea how to achieve that, as I am a completely beginner at shaders. Currently, my idea is to use a world space canvas with 2 Raw images. When the textures are assigned correctly, (and without assigning a material to the raw image), the raw image will show pink color where a character is (I have sphere mesh on each character to simulate the "FIeld of view"). However, when I attached this material with this shader (Shader Script below, written by Andrew Huang), the raw image is completely invisible. On my raw images, I also have 2 scripts that help smooth out the fog of war effect (See below too).

(I am completely new to shaders, but my current solution is to rewrite the shaders myself, but I have no idea how to start or which direction to head towards)

Shader Script: Shader "Custom/FogProjection" { Properties { _PrevTexture("Previous Texture", 2D) = "white" {} _CurrTexture("Current Texture", 2D) = "white" {} _Color("Color", Color) = (0, 0, 0, 0) _Blend("Blend", Float) = 0 _FCurr("CurrFloat", Float) = 0 } SubShader { Tags { "Queue" = "Transparent+100" } // to cover other transparent non-z-write things

             Pass
             {
                 ZWrite Off
                 Blend SrcAlpha OneMinusSrcAlpha
                 ZTest Equal
 
                 CGPROGRAM
                 #pragma vertex vert
                 #pragma fragment frag
 
                 #include "UnityCG.cginc"
 
                 struct appdata
                 {
                     float4 vertex : POSITION;
                     float4 uv : TEXCOORD0;
                 };
 
                 struct v2f
                 {
                     float4 uv : TEXCOORD0;
                     float4 vertex : SV_POSITION;
                 };
 
 
                 float4x4 unity_Projector;
                 sampler2D _CurrTexture;
                 sampler2D _PrevTexture;
                 fixed4 _Color;
                 float _Blend;
                 float _FCurr;
 
                 v2f vert(appdata v)
                 {
                     v2f o;
                     o.vertex = UnityObjectToClipPos(v.vertex);
                     o.uv = mul(unity_Projector, v.vertex);
                     return o;
                 }
 
                 fixed4 frag(v2f i) : SV_Target
                 {
                     float aPrev = tex2Dproj(_PrevTexture, i.uv).a;
                     float aCurr = tex2Dproj(_CurrTexture, i.uv).a;
                     fixed a = lerp(aPrev, aCurr, _Blend);
                     // weird things happen to minimap if alpha value gets negative
                     _Color.a = max(0, _Color.a - a);
                     
                     return _Color;
                 }
                 ENDCG
             }
         }
 }

The blend/smooth script on the raw image: using System.Collections; using UnityEngine; using UnityEngine.UI;

 public class FogRaw : MonoBehaviour
 {
     public Material projectorMaterial;
     public float blendSpeed;
     public int textureScale;
 
     public RenderTexture fogTexture;
 
     private RenderTexture prevTexture;
     private RenderTexture currTexture;
     private RawImage screen;
 
     private float blendAmount;
 
     private void Awake()
     {
         screen = GetComponent<RawImage>();
         screen.enabled = true;
 
         prevTexture = GenerateTexture();
         currTexture = GenerateTexture();
 
         // Projector materials aren't instanced, resulting in the material asset getting changed.
         // Instance it here to prevent us from having to check in or discard these changes manually.
         screen.material = new Material(projectorMaterial);
 
         screen.material.SetTexture("_PrevTexture", prevTexture);
         screen.material.SetTexture("_CurrTexture", currTexture);
 
         StartNewBlend();
     }
 
     RenderTexture GenerateTexture()
     {
         RenderTexture rt = new RenderTexture(
             fogTexture.width * textureScale,
             fogTexture.height * textureScale,
             0,
             fogTexture.format)
         { filterMode = FilterMode.Bilinear };
         rt.antiAliasing = fogTexture.antiAliasing;
 
         return rt;
     }
 
     public void StartNewBlend()
     {
         StopCoroutine(BlendFog());
         blendAmount = 0;
         print(screen.material.GetColor("_Color").a);
         // Swap the textures
         Graphics.Blit(currTexture, prevTexture);
         Graphics.Blit(fogTexture, currTexture);
 
         StartCoroutine(BlendFog());
     }
 
     IEnumerator BlendFog()
     {
         while (blendAmount < 1)
         {
             // increase the interpolation amount
             blendAmount += Time.deltaTime * blendSpeed;
             // Set the blend property so the shader knows how much to lerp
             // by when checking the alpha value
             screen.material.SetFloat("_Blend", blendAmount);
             yield return null;
         }
         // once finished blending, swap the textures and start a new blend
         StartNewBlend();
     }
 }

I desperately need help, I have been stuck on this for a few days now. Appreciate any feedbacks :D

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

0 Replies

· Add your reply
  • Sort: 

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

197 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 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

How to show render texture in a 2D game (Fog of War). 1 Answer

Fog of War Explored Area Gray out / un-updated areas 0 Answers

RTS fog of war texture remapping shader,Fog of war plane texture remapping shader HDRP 0 Answers

Render scene depth to a texture 4 Answers

Mulit-Pass Custom Shader Workflow 1 Answer


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