Search Unity

Location Based Opacity in Unity

Discussion in 'Shaders' started by Ippokratis, Mar 22, 2015.

  1. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
    A fellow developer in a Greek Unity 3d facebook group showed this video and asked how to recreate it in unity


    After some time, Renaldas Zioma showed up and explained the principle.

    I made a small project to demonstrate the technique, available here https://www.dropbox.com/s/77udpj0l5wk65ap/location based opacity.7z?dl=0

    And a screenshot : transp.PNG

    I wish to thanks Renaldas Zioma for supporting our local community.
    -Ippokratis
     
    Last edited: Apr 26, 2017
    peter_field, junglemason, ReJ and 4 others like this.
  2. kebrus

    kebrus

    Joined:
    Oct 10, 2011
    Posts:
    415
    Really cool effect, I'm trying to think of a place for it to be useful but got nothing, how have people been using this?

    thx for sharing
     
  3. konsnos

    konsnos

    Joined:
    Feb 13, 2012
    Posts:
    121
    A shield for example, or a teleportation device, or a fireball which constantly expands and burns everything in it.... :)
     
    kebrus likes this.
  4. junglemason

    junglemason

    Joined:
    Dec 30, 2010
    Posts:
    69
    Nice! I was just looking for this exact thing. I'm making a conical holographic projection in which things are moving in and out of the projected cone (and of course we only want to see what is within the cone).
     
  5. cadpeople

    cadpeople

    Joined:
    Nov 6, 2014
    Posts:
    21
    Sorry for the slight necro-bump, but does anyone know how I could reverse the effect? So everything BUT the sphere area is visible? And... How hard would it be to change the area form a sphere to something else? Cylindwer, cube, mabye even mesh? I know barely anythign about writing shaders, and I can't find anything like this; that isn't outdated :/
     
    Last edited: Oct 26, 2015
  6. peter_field

    peter_field

    Joined:
    Sep 4, 2013
    Posts:
    6
    This is so awesome! I would also be interested to see if it could work with different shapes but its exciting to know the effect can be created in unity regardless.

    The dropbox folder with the example files is empty though so I cant see how the effect was achieved. Would it be possible to make these example files available again? I would love to dive in and use this feature in a gameplay prototype. :)
     
  7. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
    Hi, thanks for letting me know that the link was down.
    I updated the link, it works now.
    Kind regards
     
  8. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    This is a modification of standard Surface Shader to achieve that.

    Code (CSharp):
    1. Shader "Test/ClipSphereShader" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    6.         _Metallic ("Metallic", Range(0,1)) = 0.0
    7.  
    8.         _ClippingCentre ("Clipping Centre", Vector) = (0,0,0,0)
    9.         _radius ("radius", float) = 1.5
    10.     }
    11.     SubShader {
    12.         Tags { "RenderType"="Opaque" "Queue" = "Transparent"}
    13.         LOD 200
    14.         Cull off
    15.         CGPROGRAM
    16.         // Physically based Standard lighting model, and enable shadows on all light types
    17.         #pragma surface surf Standard fullforwardshadows
    18.  
    19.         // Use shader model 3.0 target, to get nicer looking lighting
    20.         #pragma target 3.0
    21.  
    22.         sampler2D _MainTex;
    23.  
    24.         struct Input {
    25.             float2 uv_MainTex;
    26.             float3 worldPos;
    27.         };
    28.  
    29.         half _Glossiness;
    30.         half _Metallic;
    31.         fixed4 _Color;
    32.         float _radius;
    33.  
    34.         uniform float3 _ClippingCentre;
    35.  
    36.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    37.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    38.         // #pragma instancing_options assumeuniformscaling
    39.         UNITY_INSTANCING_CBUFFER_START(Props)
    40.             // put more per-instance properties here
    41.         UNITY_INSTANCING_CBUFFER_END
    42.  
    43.         void surf (Input IN, inout SurfaceOutputStandard o) {
    44.  
    45.             if(sign(_radius)*(dot((IN.worldPos - _ClippingCentre),(IN.worldPos - _ClippingCentre)) - _radius*_radius)>0) discard; //sign(_radius) : negative to clip the outside of the sphere
    46.  
    47.             // Albedo comes from a texture tinted by color
    48.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    49.             o.Albedo = c.rgb;
    50.             // Metallic and smoothness come from slider variables
    51.             o.Metallic = _Metallic;
    52.             o.Smoothness = _Glossiness;
    53.             o.Alpha = c.a;
    54.         }
    55.         ENDCG
    56.     }
    57.     FallBack "Diffuse"
    58. }
    Then manipulate with Clipping Centre and radius values on the materials.

    This way you can define various geometries for the discard directive.
    Spheres, tubes, boxes and other primitive solids, single and multiple can be achieved.