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 /
avatar image
0
Question by The-Gabo · Mar 08, 2020 at 11:29 PM · shaderoptimization

Can I make this world reveal shader have more than 1 source of reveal ?

Hey,

I'm currently making a level where objets appear around you.

There's the shader that takes care of this : I think it's not too costly. And i also think it doesn't need lighting.

 Shader "Reveal/Diffuse" {
 
     Properties{
 
         _Color("Color", Color) = (1,1,1,1)
         _MainTex("Albedo (RGB)", 2D) = "white" {}
     
         // Dissolve properties
         _DissolveTexture("Dissolve Texture", 2D) = "white" {}
         _Progression("Progression", Range(0,100)) = 0.0
     }
 
         SubShader{
         Tags { "RenderType" = "Opaque" }
 
         CGPROGRAM
         #pragma surface surf Standard fullforwardshadows
 
         #pragma target 3.0
         fixed4 _Color;
         sampler2D _MainTex;
 
         //Dissolve properties
         float3 _PlayerPos;
         float _Progression;
         sampler2D _DissolveTexture;
 
         struct Input {
             float2 uv_MainTex;
             float3 worldPos;
         };
 
         void surf(Input IN, inout SurfaceOutputStandard o) {
 
             // Dissolve function
             half dissolve_value = tex2D(_DissolveTexture, IN.uv_MainTex).x;
             float dist = distance(_PlayerPos, IN.worldPos);
 
             //clip(dissolve_value - dist / _Progression);
 
             // Basic shader function
             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
 
             o.Albedo = c.rgb;
             }
             ENDCG
         }
             FallBack "Diffuse"
 }


The thing is, this shader only can handle one position "source". That means that if for example i want an objets to be revealed by both the player and a bullet for example, this shader wouldn't work.

The system I need was, that when a second source hits the renderer, I duplicate it, and give a second source to that renderer.

It's very dirty, and costly. Am I making my self clear, and is there a way to give this shader multiple sources of revelation.

Thanks,

gabriel

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

Answer by Namey5 · Mar 09, 2020 at 02:05 AM

You can pass in an array of positions instead, and loop through those to find the closest distance to use;

  Shader "Reveal/Diffuse" {
  
      Properties{
  
          _Color("Color", Color) = (1,1,1,1)
          _MainTex("Albedo (RGB)", 2D) = "white" {}
      
          // Dissolve properties
          _DissolveTexture("Dissolve Texture", 2D) = "white" {}
          _Progression("Progression", Range(0,100)) = 0.0
      }
  
          SubShader{
          Tags { "RenderType" = "Opaque" }
  
          CGPROGRAM
          #pragma surface surf Standard fullforwardshadows
  
          #pragma target 3.0
          fixed4 _Color;
          sampler2D _MainTex;
  
          //Dissolve properties
          #define MAX_POSITIONS 30
          float3 _DissolvePositions[MAX_POSITIONS];
          int _TotalPositions;
          float _Progression;
          sampler2D _DissolveTexture;
  
          struct Input {
              float2 uv_MainTex;
              float3 worldPos;
          };
  
          void surf(Input IN, inout SurfaceOutputStandard o) {
  
              // Dissolve function
              half dissolve_value = tex2D(_DissolveTexture, IN.uv_MainTex).x;
              float dist = distance (_DissolvePositions[0], IN.worldPos);
 
              for (int i = 1; i < min (MAX_POSITIONS, _TotalPositions); i++)
                  dist = min (distance (_DissolvePositions[i], IN.worldPos), dist);
  
              //clip(dissolve_value - dist / _Progression);
  
              // Basic shader function
              fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
  
              o.Albedo = c.rgb;
              }
              ENDCG
          }
          FallBack "Diffuse"
  }

In the above, rather than passing a single position, we pass an array of positions. The MAX_POSITIONS macro defines the maximum number of positions you could have at one time. You can change this number, but it has to be there because arrays in shaders must be of a constant length (known at compile time). However, if we have less positions than that, we only want to perform operations on the values we have, so we also have to pass in the number of positions we actually want to use. In your script, this can be done like so;

 //Let's say there are 10 objects you wish to have an effect on this shader;
 Vector3[] positions = new Vector3[10];
 ...
 //Code to fill the above array
 ...
 //Then, instead of passing a single vector to the shader, we pass the whole array;
 mat.SetVectorArray ("_DissolvePositions", positions);
 //Also pass the length of the array;
 mat.SetInt ("_TotalPositions", positions.Length);
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

194 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

Related Questions

Best transparent shader for Android devices 1 Answer

Eliminate CPU spike from shader load? 1 Answer

transparency shader faster then diffuse... 0 Answers

How to configure to eliminate the unused shaders? 0 Answers

Can the Cull option in shader improve perfomances ? 2 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