Manual 2.9

From CrossSection
Jump to navigation Jump to search

Instalation

The package supports a range of Unity versions. The ever growing feature list advertised on the AssetStore relates to the latest version of the package which was uploaded using a specific version of Unity.

If you are going to use a specific feature with an earlier version of Unity, please check the Changelog, whether this feature will be present in your version.
Import21.png

Import CrossSection to your project from Asset Store.

Unity will show a list of files to import. The main package has no dependencies and should work straightaway after importing into the Unity project.

However it contains a few optional components which are dependend on Unity optional packages. These are stored inside the asset in form of unitypackages:

edgeEffect(Post Processing)
is dependent on Post-Processing Stack v2, provides support for it for the crossSection shaders and adds selective edge effect for the outline and cut edges. If you are going to use the Post-Processing Stack v2 or selective edge effect then you will need it
crossSection (URP)
provides support for Universal Render Pipeline (URP) and is dependent on it
crossSection (URP&HDRP)
provides support for Shader Graph. If you are going to use Universal Render Pipeline (URP) or High Definition Render Pipeline (HDRP) you will need it.
crossSection (HDRP)
provides support for High Definition Render Pipeline (HDRP) and is dependent on it

You can untick any of these components at this stage if you are not going to use them. Then just click on Import.

Files will get imported to Assets/WorldSpaceTransitions. These folders can be moved later.

Unity will then import crossSection and compile the scripts.

Import2.png

Some time it may show a window like this:

In which case you should click "Go Ahead".

UnityPackageImport.png

If you are going to use any of the above components (unitypackages) - first import the dependency package using Unity Package Manager.

Then double click on the unitypackage file icon and a window with files to import will unfold.

The package content

The package is based on a number of C# scripts and set of special crosssection shaders. The package contains eleven demonstration scenes.

The crosssection shaders currently include the clipping versions of the following builtin Unity shaders:

  • Legacy/Bumped Diffuse
  • Legacy/Bumped Specular
  • Legacy/Diffuse
  • Legacy/Decal
  • Legacy/Reflective/Diffuse
  • Legacy/Reflective/Specular
  • Legacy/Reflective/VertexLit
  • Legacy/Transparent/Diffuse
  • Legacy/Transparent/Specular
  • Legacy/Transparent/Bumped Specular
  • Legacy/Transparent/Cutout/Bumped Specular
  • Legacy/Lightmapped/Diffuse
  • Legacy/Lightmapped/Bumped Diffuse
  • Unlit/Texture
  • Unlit/Color
  • Standard
  • Standard (Specular setup)
  • Autodesk Interactive
  • Standard Surface Shader
  • GUI/Text Shader
  • all the TextMeshPro shaders (packed inside the zip file – require Text Mesh Pro Asset)
  • and more ...


These crossection shaders are named after them by adding the “CrossSection/” prefix at the beginning of the name.

The example scenes

Most of the example scenes for legacy renderpipeline can be viewed in this demo:

- Single plane demo scene contains the TestGameObject with the SectionSetup component attached. The TestGameObject renderers can use any materials using any shaders. The SectionSetup.model variable is bound to TestGameObject so the component can check these shaders. In case of crossection shaders, the component leaves the material as it is, otherwise it tries to find a shader substitute among these listed above shaders. Then it generates a material duplicate with properties inherited from original. If it can not find the exact matching shaders it puts the CrossSection/Legacy/Diffuse shader by default. Note that in that case the visual appearance of the material will probably change. To get the best control over the object appearance use the crossSection shaders from the asset. The SinglePlaneSection script sets the cross section shaders keywords on/off on entering and on leaving the play mode. The cross-section plane follows the position and orientation of the Gizmo object which is also included in the scene. Generally the cross-section plane can follow any moving GameObject with the GizmoFollow script attached. The GizmoFollow script updates the cross section shaders global variables, following the orientation of GizmoFollow transform, during the play mode.

- Planar xyz setup contains a GUI to setup and move the cross-section plane along the coordinate system's xyz axis, in a way constrained within the GameObject's bounds. These bounds are calculated within the SectionSetup script in editor on model variable assignment. If you move, rotate or change the model hierarchy content, please reassign this variable to get these bounds recalculated.

- Various materials is just the same as above, but using a bigger number of materials, representing shaders covered by the asset

- Intersecing geometry is just a single plane cross section with a special support for intersecting geometry.

- Material transition - the setup is the same as in single plane scene. The submesh count on the mesh renderers on the test GameObject equals to one, but the materials are two. In cases like that both materials get rendered. The inverse material variable lets us inverse the the clipping plane on one of these materials.

- Pie section - this is the a two angled planes section.

- Sphere section - a point clicked on the demonstration GameObject defines the position of centre of the clipping sphere. Then you can drag on the demonstration GameObject to set the clipping sphere radius.

- Cube section - a point clicked on the demonstration sphere defines the position of centre of the clipping cube. Then you can drag on the demonstration sphere to set the clipping cube size. The cube orientation is set by normal in the clicked point and by transform.up of the demonstration sphere.

- Multi tube section - a point clicked on the demonstration GameObject defines the position of centre of the clipping cylinder. The cylinder height is infinite; i.e. the cylinder has no caps. The cylinder axis is determined by normal in the clicked point. Then you can drag on the demonstration GameObject to set the cylinder radius. The example allows up to 5 clipping cylinders.


The crosssection shaders just render until they meet the section plane and leave the rest of the gameobject not rendered.

The crossSection materials in the above scenes can be manipulated using material and shader global keywords and variables.

The keyword names are self-explanatory:

CLIP_PLANE
CLIP_TWO_PLANES
CLIP_SPHERE
CLIP_CUBE
CLIP_TUBES
and more...

The variables are:

_SectionPoint: - point which has to be included in the section plane (or both if there are two planes) or centre of radius in case of sphere section
_SectionPlane: - normal of the section plane
_SectionPlane2: - normal of the second section plane
_Radius: - radius in case of sphere section.

Tube sections (multiple) use it's own set of array variables. The hardcoded number of tube sections is 64. The corresponding variables are VectorArray: _AxisDirs - for the tube axes, _centerPoints - for the cylinder centres and _Radiuses - for the cylinder radiuses. The integer _centerCount variable is to control the actual number of active cylindres for the shader code to loop. The _SectionColor variable is a local level shader variable to control the color of the backfaces individually on the material level. I have also used the alpha value of that to control the visibility of backfaces on some od the shaders depending on if a>0.5, (CrossSection/Standard, Standard Specular Setup) - because someone has requested the backfaces on/off functionality.

The basic CrossSection scenario consist of a single effect with single plane or sphere imposed on a number of materials with use of shader global variables. It is also possible to expose the variables on the shaders and assign them on the material level or to override the global variables in scripts so we could have a number of section planes, circles or boxes in a single scene. An example of this is the single_plane_individual scene. You can slighty edit the shader code to change the property scope from global to local. In case of most properties – excluding arrays (like FloatArray or VectorArray) it is just a mater or putting the property into the shader properties block. Most of the cross-section shader HSLS files inside the asset already contain the relevant property lines commented and it is just a matter of uncommenting them. In case of shader graphs it is a matter of editing the graph blackboard, marking the properties as exposed.

Capped setup example scenes:

The capped setups are the right choice when you like to use shading on the clipped surface or to apply some hatching texture to it. Or when using multiple clipping planes when their normals form an obtuse angle – to prevent seeing holes on clipped backfaces. Then the main shader writes the backfaces as well as clipped backfaces areas to the stenclil buffer so they could later be rendered by shaders used on the clipping planes.

- Box section - the test GameObject can use any of the following shaders:

 - Clipping/Box/SurfaceShader (a remake of SurfaceShader)
 - Clipping/Box/Standard (a remake of Standard shader)
 - Clipping/BoxS/tandard Specular setup (a remake of Standard Specular setup shader)

Drag the semitransparent planes of CrossSectionBox object to adjust the box section.

- Box section advanced - the same as box section, but each model gameObject material has assigned a specific material for clipping planes. GameObject materials have separate stencil masks. A Gameobject material and the corresponding clipping plane material share the same stencil mask value and could also share the same diffuse texture. The six CrossSectionBox quad elements represent clipping planes of the box: X_hatch, Xneg_hatch, Y_hatch, Yneg_hatch, Z_hatch, Zneg_hatch and have assigned not a single material, but an array of clipping plane materials.


- Corner section - the test GameObject can use any of the following shaders:

 - Clipping/Corner/ SurfaceShader (a remake of SurfaceShader)
 - Clipping/Corner/Standard (a remake of Standard shader)
 - Clipping/Corner/Standard Specular setup (a remake of Standard Specular setup shader)

Drag the semitransparent planes of CrossSectionCorner object to adjust the corner section.

- Corner section advanced - the same as corner section, but each model gameObject material has assigned a specific material for clipping planes. GameObject materials have separate stencil masks. A Gameobject material and the corresponding section plane material share the same stencil mask value and could also share the same diffuse texture. The three CrossSectionCorner quad elements, which represent three corner clipping planes: X_hatch, Y_hatch, Z_hatch have assigned not a single material, but an array of clipping plane materials with separate stencil masks.

- Single plane capped demonstrates the cross-section surface covered with a hatched quad. The TestGameObject materials use stencil buffers and write specific masks to it. Then these masks are revealed by corresponding materials assigned to the quad. The SectionPlaneCapped script is attached to the quad. On assigning the toBeSectioned variable in editor - the quad gets sized to fit. It also adjusts itself to the position and rotation of Gizmo GameObject.

Limitations

The asset will work well only on closed and not intersecting meshes. Transparent, fading and cutout crossSection shaders do not display backfaces and support clipping only.

Basic cross section setups

Just put the SectionSetup.cs component into the scene and set SectionSetup.model variable to the GameObjects which you want to section. Attach the GizmoFollow.cs script to the gizmo GameObject, whose position and orientation is supposed to determine the cross-section plane position and orientation.

In case of planar_xyz setup just put the Planar_xyzSection prefab into the scene. Don't forget to place the EventSystem, because GUI inside the prefab will not work without it. Find the SectionSetup.cs Planar_xyzClippingSection.cs components on the prefab and bind the SectionSetup.model variables to the GameObject which you want to section. Then you will be able to move the cross-section planes either with GUI slider or by dragging on the GameObject.. The slider range will adjust itself automatically to the GameObject's bounds. Most of the scenarios, including the planar_xyz scene require calculating the bound box of the sectioned object. In the planar_xyz scene the boundbox is needed for setting the range of the GUI sliders. The bounds get calculated in edit mode within OnValidate function in editor. The zero point on the sliders corresponds to the position of ZeroAlignment transform object - if attached - or to the GameObject's bounds minimum. There are two options available for bound box calculations. The first one, when the accurateBounds toggle is deselected, is based on encapsulation of the renderer bounds of contained meshes. This results in bloating the size, especially when the child objects are rotated relative to their parents. The second one, when the accurateBounds toggle is selected, is based on calculating the vertices from all the contained meshes. It uses multithreading and the special library, contained inside MathGeoLib.Exports.v0.1 folder.

Capped cross section setups

Just put one of prefabs: CrossSectionBox prefab or CrossSectionCorner prefab into the­ scene. In order to prevent undesired interaction between the gameObject and maxCamera orbit script when dragging the section planes - disable colliders on the GameObject or move it to the IgnoreRaycast layer or layer>13, Bind the model variables on two of the prefab's scripts to your GameObject. To get the best control over the object appearance use the crossSection shaders: - Clipping/(Box or Corner)/SurfaceShader/Standard - Clipping/(Box or Corner)/Standard - Clipping/(Box or Corner)/Standard (Specular Setup)

Retract backfaces

If elements touch each other, i.e. front faces of preceding object are coplanar with back faces of the following object, z-fighting and flickering occurs. Here comes a new shader feature to move the backfaces slighty. It can be controlled on the material by the _retrackBackfaces boolean switch. The extrusion amount can be controlled on the global shader value _BackfaceExtrusion.

Edge (Outline) effect

- Standard pipeline

This feature shows edges on cross-section materials. There are separate settings for colors and visibility on object and section surfaces. It is integrated with Unity Post-processing stack (v2) from Unity Essentials. The feature assets are packed in EdgeEffect(Post Processing).unitypackage file. In order to activate download the Post-processing package using the Package manager and unpack the unitypackage. Scene setup: Put the EdgeObject component on each of the objects, where you need the edges to be drawn, and also a single EdgeRenderer component in the scene. The EdgeRenderer Object draws a custom _EdgeMap texture to mask the section and original areas of cross-section objects using command buffer. You will also need to setup a post processing profile, using CrossSecion/PerObjectEdgeEffect and attach all the Post Processing components to the camera. The edge effect also requires a special camera component CameraDepthNormalSetting to setup the special depth normal texture.

- URP (Universal) pipeline

In URP this feature is based on Renderer Features.

Example scene setup

Lets consider the single_plane_cap_outl_urp.scene from crossSection (URP)/Scenes folder.

Camera settings.png

1. Check if the proper RenderPipelineAsset is loaded. A way to ensure that - is to attach the AutoLoadPipelineAsset script to the camera. The script should have the right pipeline asset assigned.

2. Go to the main camera, the rendering section and check the renderer assigned.


game view of the scene

Renderer settings.png

3. Examine the renderer's settings. To get the outlines to work - the renderer should contain three renderer features:

MaskFeature
- the purpose of this is to generate a selective masks for front faces and backfaces of the sliced objects. This is to get the outlines on the sliced objects only, not on the background objects. This is also to get edges on the boundaries between the front and back faces. The selection filter is layer mask defined in the mask feature setting and the sticed objects need to stay within the mask defined here.
CrossSectionDepthNormalsFeature
- this is to get edges where normals change direction on the sliced object outside the cutting plane. Again the selection filter is layer mask defined in the depth normals feature setting and the sliced objects need to stay within the mask defined here.
EdgeFeature
- this is the camera effect postprocessing the texture input from these two features above and combining it with camera color texture.


The selection filter is layer mask defined in the mask feature setting and the sticed objects need to stay within the mask defined here.

Single plane cap outl urp.png


- HDRP (High Definition) pipeline

In HDRP this feature is based on Render Objects feature

Scriptable Render Pipeline Support - location of sub-assets

The asset supports standard (built in) as well as scriptable render pipelines, universal (URP) and high definition (HDRP). The sub-assets for specific scriptable render pipelines are packed inside unitypackages. What sub-assets are needed depending on used render pipeline? When using URP or HDRP you will not need anything from the crossSection (Built In) folder. The crossSection (HDRP).unitypackage contains asset specific for HDRP and crossSection (URP).unitypackage contains asset specific for URP. The crossSection (URP&HDRP).unitypackage contains shadergraphs needed in both URP and HDRP materials and demo scenes.

URP Support

Since Unity 2019.2.16 - all the features are also working in Universal Render Pipeline (URP). Most of the scenes, prefabs and materials are duplicated and have also a version for URP. The example scenes for legacy renderpipeline can be viewed in this demo.

The asset contains a set of the main URP shaders with cross-section features added. These are hlsl shaders: - CrossSectionURP/BakedLit - CrossSectionURP/Lit - CrossSectionURP/SimpleLit - CrossSectionURP/Unlit and shader graphs: - Autodesk Interactive - Autodesk Interactive Masked - Autodesk Interactive Transparent and others. In case of baked lighting and shader graphs - there is no way to switch the lightmaps off for the backfaces in graphs, so they display in black and _SectionColor has no effect.

The Universal Render Pipeline does not support the multiple render passes in a single shader. An additional render pass, not visible, to write into the stencil buffers only is especially needed in the capped box scerario. In standard render pipeline it is included into the main shader, while in case of URP it constitutes a separate shader, that is - CrossSectionURP/CapPrepare To fix this the objects that require two passes can have two materials; one for each of the passes. But shader graph materials do not support stencil buffers at all and here is where the new scriptable render pipeline features like custom renderer and custom render pass come very useful. These new features make possible to define custom renderers and select them at the camera level. The custom renderers can contain custom render passes which enable rendering objects selected by layer masks in specific way or multiple times with multiple materials. The example of this is the single_plane_cap_urpGraph scene.

The selective cross-section effect is added using Depth Texture and ScriptableRendererFeature's.

HDRP Support

Since Unity 2020.1.1 - the features are also working in High Definition Render Pipeline (HDRP),but in HDRP - only ShaderGraph shaders are supported.

The example scenes for High Definition Render Pipeline can be viewed in this demo.

The asset contains a number of shadergraphs, that are working both for URP and HDRP. They are located in the crossSection (URP&HDRP) folder. The capped setups use stencil buffers. The cros-section needs to write to stencil buffers and they do that using Draw Renderers Custom Passes. The stencil write materials use the dedicated CrossSectionHDRP/Renderers/CapWrite shader. Cap materials use the decicated CrossSectionHDRP/Renderers/StencilShow shader, which is a modified version of the HDRP/Unlit shader, with stencils and triplanar mapping options added . Unfortunately HDRP pipeline uses stencil buffers extensively, leaving only two bit for custom uses. So I guess if I we tried to use more bits - in case of more cap materials - it might lead to some colisions with other HDRP shaders.

Switching between scenes with different pipelines

If you have downloaded assets for scriptable pipelines URP or HDRP you might like to switch between scenes using different render pipelines.

RenderPipelineToBuiltin.png

The demo scenes for UPR and HDRP contain the AutoLoadPipelineAsset.cs script, attached to camera, which does that automatically. When no scriptable pipelines are installed, (in the scenes intended for the builtin render pipeline) this script would cause errors, so I wrote a simple script adding menu item "Edit/Render Pipeline/To Built in" to switch to built in render pipeline in the scenes for built in render pipeline. You can use it to switch to the built in render pipeline quickly.

I would greatly appreciate any suggestions for improvement of the package. I would be willing to expand the list of supported shaders list due to demand from the users.

WebPlayer demo: http://virtualplayground.d2.pl/crossSection/cs.html (for the old version of package) WebGL demo: http://virtualplayground.d2.pl/crossSection/ Android demo: http://virtualplayground.d2.pl/crossSection/cs.apk (Tested on Samsung Galaxy S8 and A7) Standalone HDRP demo: http://virtualplayground.d2.pl/crossSection/cs_hdrp_win.zip

Help and improvement suggestions:

forum thread: https://forum.unity3d.com/threads/released-crosssection-tool.223790/

e-mail: virtual@virtualplayground.d2.pl