Box-Section as an extended Cross-Section Shader?!

Hey everybody,

I am looking for month for a way to limit my terrain-view as I am looking down on it and has found some more or less useful ways. The stencil buffer was okay, but still eating my Galaxy S7 cpu/gpu and not really what I am looking for. However. I have found an asset from tomekkie2 (Unity Asset Store - The Best Assets for Game Making) and a related topic here (The Open-Cross-Section-Project (PBR+Shaderforge) - Unity Engine - Unity Discussions) which is quite what I am looking for.

I am not into shaders btw and was just looking for an asset I can buy and use. I have downloaded the package from the topic and has done some experiments with the shader. I used two passes (passes? pass-functions?!) for example and used to build some kind of a box around my objects. But it did not work, unfortunately (I wasnt able to use the clip() function in two different ways so i could mask the center of my object)

As you can see I was just able to mask a part with try-and-error until I finaly gave up due to time pressure (finals!).

So I am asking if you know any asset or is anyone here who can help me out with this issue. All I want is just to see a part of my map and use the arrow-keys/d-pad to scroll the map like the huge maps in godus:

I am also willing to pay about 50€ for an asset or even a developer ( I know, its not the proper price for a developer).

Thanks in advance.
Baris

Any purely shader solution isn’t going to be enough for mobile. Using clip() and stencil based shader methods can actually be more expensive than just rendering the entire mesh normally. If you want to only show a small portion of your world you’ll want to remove portions of the mesh that aren’t seen either by dynamically slicing and generating a new mesh on the CPU (which is what Godus does) or pre-chunking your world into a grid and only show the tiles you need. You can augment that with a clip() shader to make things smoother, or there are assets on the asset store for dynamically cutting or doing Boolean operations on meshes.

Thank you, but I guess I have to try it due to time pressure. I can not risk to try new things and lose myself again in bigger issues. I have tried some codes on mobile (galaxy s7) and it worked fine until now. With static baked objects…

I have a code here using the discard fuction. Can I invert this function?

if (dot((posWorld - _SectionPoint),(posWorld - _SectionPoint))<_Radius*_Radius) discard;

It is the cross section script by tomek and I guess this line creates the sphere. I would be fine with a sphere, but I need to invert it so show only the part inside it instead of cutting it out.

And can anyone explain the dot-function more precisely. I could not find any good explanation in the unity documentation. i guess it is just compares the two float3 values and gives true/false back?!

thanks

The dot function is a dot product, a common mathematics operation. Doing dot(myVector, myVector) like what is in that code gives the square of the vector length, so it compares the dot product to the square of the radius (as an optimization over doing a relatively expensive square root) and discards anything less than the radius. If you want the inverse just change the < to >.

Thanks a lot. I have tried that before. Until I got that unity is not always working proper when changing scripts - I have learned that I better reimport always the shaders… It works fine for me now. Do you have any keywords to run this with a box? Like working with my own gameobject / the box and using its dimensions instead of the dot-function? I mean I know that I can get the coordinates of each gameobject in shader, but I need some kind of a direction where to look at.

thanks

This might work:
if (all(abs(posWorld - _SectionPoint) > _Radius)) discard;

Otherwise:
float3 range = abs(posWorld - _SectionPoint);
if (range.x > _Radius && range.y > _Radius && range.z > _Radius) discard;

Those should be the same as far as the compiled shader is concerned, but the first one is less written code. It’s getting the distance on each axis, then doing the same > test against each. If you replace _Radius with a float3 value the fist one should work with out change (assuming it works to begin with), but the second would need _Radius.x _Radius.y etc

hmm… curious… both codes works only the opposite way - so, when i use the other arrow brackets. so i got a hole in my map… otherwise it seems not working?!

but when i de- or increase the z axis to about +/-30 i got a crossover:

since there is no way for proper debug in unity itself i will try to do it manually now:

In _SectionPoint I got my float3 with the seen three values. _Radius is fixed to 20 in the code!

And posWorld is the real position of the objects in the world coordination and I just get the Screen coordination with posWorld minus _SecctionPoint, right?

Dont get why it is working the opposite way?!

Thank you very much!

Try this one:

if (!all(1.0-saturate(abs(powWorld - _SectionPoint) - abs(_Radius)))) discard;

Dude, you rock! Thanks a lot! BTW you were right, partially… My S7 is fine with the discarding but sometimes the frames drop despite occlusion culling, baking and the discard function. I guess the discard does not help to make the unseen objects disappear but just unseen. I guess I have to work with different scenes and trigger-areas to prevent the frame-drops.

Thank you very much!