Search Unity

Spherical clipping

Discussion in 'Shaders' started by Ed F, Jan 15, 2015.

  1. Ed F

    Ed F

    Joined:
    Apr 28, 2013
    Posts:
    4
    Hi,

    I'm looking for a shader that excludes everything that is not inside a sphere. Something like this:

    http://rottingspace.com/unity-3d-ar/unity-world-clip-shader-package/

    Ideally the sphere would take its size and position from a sphere gameobject which I can readily control from a script (I can do this bit)

    Can anyone help me get hold of the author of the shader linked above or know where I can get such a shader from ?

    Many thanks
     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    From the top of my head:
    Code (csharp):
    1.  
    2. float3 spherePos; // External, can be linked to transform position
    3. float3 sphereSize; // External, can be linked to transform scale
    4. float sphereFade; // External, should be between 0 and 1
    5.  
    6. float3 posWorld; // Pixel world position
    7. float3 posWorldSphere = (posWorld - spherePos) / sphereSize; // Position relative to unit sphere
    8. float sphereAlpha = saturate((1.0 - length(posWorldSphere)) / max(sphereFade, 0.0001));
    9. Color.a *= sphereAlpha;
    10.  
     
  3. Ed F

    Ed F

    Joined:
    Apr 28, 2013
    Posts:
    4
    Hi jvo3dc,

    many thanks for taking the time to reply to my question.

    I think that you are fading out from the centre of the sphere getting to fully transparent when you reach the surface of the sphere. Is that correct ? What I'm trying to do is more boolean, either fully opaque inside the bounds of the sphere or fully transparent outside of the bounds of the sphere so I think probably I want to test if the posworld is within the sphere and if so the Color.a will be 1 and otherwise it would be zero.

    I haven't really understood all of the structure of shaders yet so don't understand how to implement your kind solution or fully get it. I'm reading the documentation so hopefully will get there soon. Looking forward to understanding more ... and thanks again for taking the time to give me your reply
     
  4. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Place the code inside a pixel (frag or surf) shader block, and use it to modify final alpha. Shader will need to be transparent ie have a blend mode.

    This approach can work nicely but also be subject to sorting issues. Overall this is the approach to take. Another approach is to render the object to a texture of your choice instead, and map to a sphere, generating screen space coordinates. This should work but also has niggling issues with alpha so will need clearing etc and will need a shader for the screenspace issues and has no shadowing from other objects.

    The final approach I would imagine is via the stencil buffer.
     
  5. Ed+

    Ed+

    Joined:
    May 6, 2013
    Posts:
    2
     
  6. Ed+

    Ed+

    Joined:
    May 6, 2013
    Posts:
    2
    Thanks for the replies, I managed to write a spherical clipping as copied below:

    Shader "spherical_clipping" {
    Properties {
    _MainTex ("Texture", 2D) = "white" {}
    _BumpMap ("Bumpmap", 2D) = "bump" {}
    _radius("size",Range(1,10)) = 6
    _centralspot("centre", Vector ) = (1,1,1,1)
    }
    SubShader {
    Tags { "RenderType" = "Opaque" }
    Cull Off
    CGPROGRAM
    #pragma surface surf Lambert
    struct Input {
    float2 uv_MainTex;
    float2 uv_BumpMap;
    float3 worldPos;
    };
    sampler2D _MainTex;
    sampler2D _BumpMap;
    float _radius;
    float4 _centralspot;
    void surf (Input IN, inout SurfaceOutput o) {
    clip (_radius*_radius-((_centralspot.x-IN.worldPos.x)*(_centralspot.x-IN.worldPos.x)+(_centralspot.y-IN.worldPos.y)*(_centralspot.y-IN.worldPos.y)+(_centralspot.z-IN.worldPos.z)*(_centralspot.z-IN.worldPos.z)));
    o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
    o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
    o.Alpha = 0.005;

    }
    ENDCG
    }
    Fallback "Diffuse"
    }

    Now need to get the back faces to be really dark and then my cut objects might look sort of capped ...