Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

The Open-Cross-Section-Project (PBR+Shaderforge)

Discussion in 'Shaders' started by Cascho01, Apr 22, 2016.

  1. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,218
    Hi,

    I am producing ArchViz projects and tinkered a little CrossSection shader in Shaderforge:




    See it in WebGL.

    Being optimized for ArchViz it has two section planes, a horizontal and a vertical one.
    The shader recieves position and y-rotation using global values from a runtime-helper-transform-gizmo which is included in the attached Unitypackage.

    It is far away from being perfect but may be a good starting point for your improvements:
    You are invited to use the package for free and share your improvements here.

    Maybe someone knows how to access the backfaces and make them red?

    Looking forward,
    Carsten

    PS: Please make your improvements in Shaderforge, so that users can simply customize it to their needs without writing any shadercode.
     

    Attached Files:

    Last edited: Apr 22, 2016
    cvansas and mgear like this.
  2. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    682
    I have accessed the backfaces.
    To get that I turned your shader backfaces off.
    Then made a simple backfaces shader and applied it to GameObject's duplicates.
    See the result here:
    Zrzut ekranu (21).png
    http://virtualplayground.d2.pl/SFcrossSection/

    I think it would be more logical to set/enable a shader keyword instead of a float like "_OpacityOverride" to manage the clipping on/off.

    I made this backfaces shader outside SF, but I guess it should be possible to make a similar one in SF or even add the backface in the crossSection shader.

    I am just wondering if there are any practical reasons for setting another clipping plane just perpendicular to the first one instead of paralell or inclined at arbitrary angle.
    Is there a need for more planes, like a third one perpendicular to the first two?

    I also like the gizmo, athough there are errors with moving it along axis. It actually moves on clicking the axis, especially when clicking from directions close to paralell. I guess the raycast hit.point should be cast on the axis in order to eliminate that.
     

    Attached Files:

    Last edited: Apr 24, 2016
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    10,101
    You can use the Face Sign node to differentiate between front and back faces, then lerp the color based on that value.
     
    Cascho01 likes this.
  4. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,218
    Thanks, that´s a way to do it, but doublicating geometry is not my favorite way.

    In architecture presentations we usually have only two sections, horizontal and vertical.
    But defining a sectionplane by a direction (a faces normal, Vector3) would also be a great.
    But unfortunately I wasn´t able to make it in SF.

    Feel free to improve and upload the Gizmo.cs script ;)
     
    Last edited: Apr 25, 2016
  5. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,218
    Ahhhh, I didn´t know about that node! :eek:
    Implemented it in the Shaderforge-Shader and updated the Unitypackage:

    Great - now we have customized colored backfaces!
     
    Last edited: Apr 25, 2016
  6. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    682
    Do you mean two sections active at a time or only one at a time; horizontal or vertical ?
    I guess you can define a second angle so the normal could be converted and passed as angles.
    So now you will be able to use the cross section to show variety of materials using separate colors for structure, insulation, etc.
     
    Last edited: Apr 25, 2016
  7. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,218
    Common 2d cad-drawings are based on a single section, vertical OR horizotal..
    Well, having a cross section tool for realtime architecture presentations offers new possibilities and there are no rules.
    So I thought having the two sectionplanes active at the same time is cool and reasonable.

    This would be the way to go, but my nodetree in SF is a bit hacky and until now I don´t know how it could be done.
    Maybe the whole node structure should be reconsidered.
     
    Last edited: Apr 25, 2016
  8. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    682
    Agree, the project now looks cool with 2 perpendicular planes and y rotation, don't you fear it would get a bit weird if we added the second rotation axis? Not too much and too complicated for an average user?
    I guess that could be a key issue for the further development of this open source SF-cross-section project. Possibly someone will know and will help us.
     
    Last edited: Apr 25, 2016
    Cascho01 likes this.
  9. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,218
    Yes, it would get too complicated for the average user, but I would allow to enable the second axis in a little menue under advanced settings....

    Definately!
     
    Last edited: Apr 25, 2016
  10. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    682
    pie.jpg

    It might be cool to apply a pie section like this:
    http://virtualplayground.d2.pl/SFcrossSection/pie/

    - if someone finds a way to put the plane normals in the SF nodetree. I have modified the shaders manually.
     
  11. Drowning-Monkeys

    Drowning-Monkeys

    Joined:
    Mar 6, 2013
    Posts:
    328
    is it possible, or has anyone made a shader where the polygons are clipped against a bounding volume, like a cube or sphere?
     
  12. konarada

    konarada

    Joined:
    May 4, 2016
    Posts:
    2
    I would like to make two parallel cross-sections, just to get a slice of 3d object. Is it possible with hat shader?
     
  13. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,218
    Yes, definately, but I´m afraid you´ll have to try it on your own (with Shaderforge).
     
  14. konarada

    konarada

    Joined:
    May 4, 2016
    Posts:
    2
    I understand. However, unfortunately I know nothing about shaders, nor shadowforge, so I won't be able to do this on my own. I just had an idea, how I could use it and hoped that maybe it will be ok as it is. Thanks for reply.
     
  15. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    682
  16. Spartan-M23

    Spartan-M23

    Joined:
    Jun 2, 2016
    Posts:
    2
    Hello. I've make a system like yours for an app of Embryo Visualization in 3D, but i want to make that hidden part of the gameObject not clickable. Do you think this possible?
     
  17. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,218
    Well, the vertices still exist and the collider also remains unaffected, - so I don´t know how, sorry.
     
  18. Spartan-M23

    Spartan-M23

    Joined:
    Jun 2, 2016
    Posts:
    2
    Or maybe, when the object is totally hidden, disabling it. But i don't know what condition i can use to make it happen.
     

    Attached Files:

  19. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    682
    That's relatively easy to achieve, but with the section area not clickable, either. I could post a simple script for that, if still interested.
    To make clipped volume not clickable - just cast a ray and check the hit.point the same way like is is being done in shader and that's all.
     
    Last edited: Jul 14, 2016
  20. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,446
    I would use a more generic plane notation that can handle any plane position and rotation. Pretty much the same as unity uses internally in the Plane class. (Normal and distance from the origin.) It might look a bit more complicated at first, but it can be implemented very elegantly (and fast) in a shader. And it still needs just 4 floating point values.

    There are various ways to initialize the Plane object base on a transform, but this is one:
    Code (csharp):
    1.  
    2. new Plane (transform.rotation * Vector3.up, transform.position); // No rotation has the plane facing up
    3.  
    Then you transfer the information to the shader like this:
    Code (csharp):
    1.  
    2. new Vector4 (plane.normal, plane.distance); // This probably only works with my own Vector4 version...
    3. new Vector4 (plane.normal.x, plane.normal.y, plane.normal.z, plane.distance); // This should work by default
    4.  
    Then to detect whether you are clipped by the plane in the shader:
    Code (csharp):
    1.  
    2. if (dot (pos_world, plane.xyz) > plane.w) // Elegant
    3. {
    4. }
    5.  
    An extra feature is that you can easily apply shading to the clipped part by just reusing the plane normal:
    Code (csharp):
    1.  
    2. // Inside the clip detection if
    3. normal_world = plane.xyz;
    4. // Apply shading or just output the new normal
    5.  
     
  21. WHKS

    WHKS

    Joined:
    Dec 13, 2011
    Posts:
    61
    Man this is amazing, ive been talkin to a buddy to see how we can do it. this is nuts
     
  22. bachirk

    bachirk

    Joined:
    Mar 9, 2014
    Posts:
    15
    That's super cool thanks for sharing.

    @Cascho01 how can we have 2 or more planes that aren't perpendicular.
    I'm still new to SF and can't figure out how you've created two perpendicular planes.

    thanks
     
  23. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,218
    Creating two or more section planes that are not perpendicular is a little beyond my horizon, sorry
     
  24. berasamas

    berasamas

    Joined:
    Jan 10, 2017
    Posts:
    1
    Hi @Cascho01, I'm really interested in your shader, but I couldn't managed to find it in Shader Forge's package, would it be the "SFN_Cross"?
     
  25. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,218
    It´s not a part of shaderforge, you can download it at the very first post here on top.
     
  26. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    682
    You can also use stenclil buffer to handle the cross section materials.
    Possibly it could be practical in some cases.
    I have found this option in the ShaderLab examples here:
    https://docs.unity3d.com/Manual/SL-Stencil.html
    A way is to define the masking materials with Colormask 0 and to add stencil mask directives on the top of shader code of target materials. Then to add the masking and target materials to renderers in a given sequence.

    This is a very basic example of masking shader code:

    Code (CSharp):
    1. Shader "SectionPrepareFront" {
    2.     SubShader {
    3.         Tags { "RenderType"="Opaque" "Queue"="Geometry+1"}
    4.         ColorMask 0
    5.         Stencil {
    6.             Ref 1
    7.             Comp Always
    8.             Pass Replace
    9.         }
    10.  
    11.         CGINCLUDE
    12.  
    13.             struct appdata {
    14.                 float4 vertex : POSITION;
    15.             };
    16.             struct v2f {
    17.                 float4 pos : SV_POSITION;
    18.                 float3 wpos : TEXCOORD1;
    19.             };
    20.  
    21.             v2f vert(appdata v) {
    22.                 v2f o;
    23.                 o.pos = UnityObjectToClipPos(v.vertex);
    24.                 float3 worldPos = mul (unity_ObjectToWorld, v.vertex).xyz;
    25.                 o.wpos = worldPos;
    26.                 return o;
    27.             }
    28.             half4 frag(v2f i) : SV_Target {
    29.                 float4 col = half4(0.6,0,0,1);
    30.                 if(i.wpos.y<-0.3) discard;
    31.                 return col;
    32.             }
    33.  
    34.         ENDCG
    35.  
    36.         Pass {
    37.             CGPROGRAM
    38.             #pragma vertex vert
    39.             #pragma fragment frag
    40.             ENDCG
    41.         }
    42.     }
    43. }
    and then you have just to add a stencil test directives at the top of the target shader code:

    Code (CSharp):
    1.         ........
    2. Tags { "RenderType"="Opaque" "Queue"="Geometry+2"}
    3.         Stencil {
    4.              Ref 1
    5.              Comp equal
    6.         }
    7.         CGPROGRAM ........
    8.  
     
unityunity