Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Silverlode · Mar 24, 2015 at 02:32 PM · shadergraphicstexturesblit

Effects of Graphics.Blit to rendertexture are temporary

I am trying to build up a texture that represents where a player has visited on a level. (2d, topdown game).

The idea is to blit all the pixels currently in the player's LOS into a texture, so over time as he visits more parts of the level, more of this texture is white (visited) as opposed to black (unvisited). The blitting uses a shader that chooses the max value when comparing two colors (ie white over black).

When I try to blit this way, the RenderTexture ("knownmap"), only shows the most recent light radius, and not any historical ones from previous frames. I don't know why.

Camera Effect:

 //pass latest 'in LOS' pixels to shader
     material1.SetTexture("_LightMap", currentLOSAreaAsTexture);  
 //combine existing knownmap with latest 'in LOS' pixels 
     Graphics.Blit(knownmap, knownmap, material1);   
 //render to screen
     Graphics.Blit(knownmap, destination); 

 

Fragment Shader:

 float4 frag(vertexOutput i) : COLOR
         {            
     float4 known = tex2D(_MainTex, i.tex); 
     float4 light = tex2D(_LightMap, i.tex);
     return float4(max(light.r,known.r), max(light.g, known.g), 
 max(light.b, known.b), max(light.a, known.a));  

}

I've tried not blitting "knownmap" to itself, instead using an intermediate buffer, but same result. It's as if "knownmap" has reverted to a fully black texture at the start of each frame. How can I preserve the blitting effect across time?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Silverlode · Apr 02, 2015 at 01:55 PM

I still don't fully understand the situation, but I've managed to blit the way I wanted to. I was under the misconception that the rendertexture blitted to could only be written over and not actually contribute to the final image - but I've learned that blending is a case where this is not so. Using a very simple shader I have achieved what I was after

 Shader "AddTextures" {
 
     Properties{ 
         _MainTex("Texture 1", 2D) = "" 
         _Texture2("Texture 2", 2D) = "" 
     }
 
         SubShader{ Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" } 
         ZWrite Off 
         Blend One One 
         Pass{ 
             SetTexture[_MainTex] 
             SetTexture[_Texture2]
             { 
                 ConstantColor(0,0,0,[_Blend]) Combine texture + previous }
             } 
     
     }
 
 }


This was called in this manner,

    protected void Update()
         {
             materialKnown.SetTexture("_Texture2", lightmap);
             Graphics.Blit(black, knownmap, materialKnown);
 
         }

"black" is a wholly black texture pretty much just there to satify the Blit function signature.

Comment
Add comment · 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
0

Answer by raja-bala · Apr 03, 2015 at 11:52 AM

Good that you've figured a way around.

I thought I'd mention an alternative approach to doing this.

Have a secondary orthographic camera that looks down on your world; adjust its size to be as tight a fit.

On your player, attach a child game object with a mesh that approximates the viewing area.

Set it on another layer ("FogOfWar") and then make the ortho cam's mask only render this layer. Disable this layer from your main camera.

Create a RenderTexture and assign it to the ortho camera via script. Clear it once at the start. Set the clear flags to Don't clear

On the child game object, drag a material that outputs a color (say red).

You now have a mask of the players vision for your scene.

Code is always helpful, so here you go. It shows a simple minimap of spots the player has visited. I couldn't get the mask to blend with the minimap to create a fog of war effect for all the visited areas though.

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Blit into Texture2DArray slice not working in WebGL 0 Answers

OpenGL ES 2.0 support breaks simple shader 0 Answers

use RenderTexture on a sphere correctly? 1 Answer

Textures look terrible in game mode 1 Answer

How to (efficiently) make an image pixelated? 3 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