Search Unity

Make Object less visible the closer it is

Discussion in 'Shaders' started by lauchsuppe, Mar 27, 2017.

  1. lauchsuppe

    lauchsuppe

    Joined:
    Dec 6, 2014
    Posts:
    39
    I've been reading about shaders for the whole day but I'm still super lost.
    What I want to achieve is a shader that renders an object less visible the closer it is to the camera ( = less alpha I guess). I know about _WorldSpaceCameraPosition. But is there a way to get the position of an object in world space?
    In general, is there a reference somewhere for these kind of things?
    Also, is there a switch I can turn on in Monodevelop so my code will autocomplete similar to writing in c#?
    In general, where can I find an overview of what kind of cool things I can use in my shaders?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,336
    Yes & No. It's easy to get the world space position of each vertex, but getting the object position isn't always as straightforward due to Unity doing batching. Specifically you don't get the object position as much as you get the mesh's pivot, and if your mesh is being batched the individual pivot for each mesh in the batch is lost as it's now just one big mesh on the GPU. You can turn off batching on your shader to avoid this, or use instancing.

    However here's the code for getting world positions.
    float3 vertexWorldPosition = mul(unity_ObjectToWorld, v.vertex).xyz;
    float3 objectWorldPosition = mul(unity_ObjectToWorld, float4(0.0, 0.0, 0.0, 1.0)).xyz;


    There's also a "better" way, if you don't need the world position for something else, you can just get the camera relative, or view position rather than getting the world position and subtracting the camera position.

    float3 vertexViewPosition = mul(UNITY_MATRIX_MV, v.vertex).xyz;

    Then the distance is just length(vertexViewPosition), or you can use vertexViewPosition.z for the depth which is even cheaper.

    https://docs.unity3d.com/Manual/SL-Reference.html
    http://www.alanzucconi.com/2015/06/10/a-gentle-introduction-to-shaders-in-unity3d/
    https://msdn.microsoft.com/en-us/library/windows/desktop/bb509638(v=vs.85).aspx
    There are more links on this forum if you search for it, but the above are the ones I reference or point people towards the most.

    None that I know of for Mono, no. I've heard of ones for Visual Studio and SublimeText, but I've never gotten the one for VS to work and I found the one I tried for SublimeText to cause more harm than help. I also don't like auto-complete for c# though, so I might just be weird.

    There's not really any one place, though I think the Amplify Shader guys are trying to make a community for that (for shaders using their paid shader editor asset). Trolling this forum, or the work in progress assets forum, or just the asset store can give you some ideas, but not everyone is open about sharing how they do things. That's not a knock against those people btw, buy their assets if you want to see how they do things.

    Kind of the only major repository for shaders with viewable source code is ShaderToy, but that's a lot of WebGL based raytracing which isn't necessarily applicable to something usable for Unity or a shipping product (most are way too slow to be useful).
    http://shadertoy.com
     
    lauchsuppe and hippocoder like this.
  3. lauchsuppe

    lauchsuppe

    Joined:
    Dec 6, 2014
    Posts:
    39
    Wow, thanks for your reply -
    That was a lot of very helpful information!

    Sadly,
    http://www.alanzucconi.com/2015/06/10/a-gentle-introduction-to-shaders-in-unity3d/
    doesn't work - it's been down this whole week (since when I started getting involved with shaders).
    Edit: Woah it seems to be available again! wohoo
    I liked his tutorial on the unity page, so it's a shame I'm not able to find the successive tutorials.

    Other than that, I think I still struggle with the Cg syntax in general. I'm not a programmer by trade and as such I'm a bit confused about the usage of structs, #pragma and these semantic declarations like "position : POSITION".
    I started reading the Cg documentation on the nvidia developer side but that thing is massive (and probably not made for reading from start to finish anyway). Is there any guide of some sorts that teaches the very basics of how a language like Cg works and how it's structured?

    For the time being, my "programming shaders" was more like "copying and pasting different code snippets into each other and see what happens".
     
    Last edited: Mar 30, 2017