How can I make only things in specific regions visible?

Hi everyone,

I’m creating a game where only things in specific regions like spheres or cylinders are visible, but I’m not sure how to implement it.

Here are some of the approaches I tried.

  1. Apply transparent material to the spheres and cylinders and use custom depth and post-process the scene.(As in Sphere of visibility - #5 by etudenc)
    This doesn’t work for me as everything beyond those sphere and cylinders are also visible, as in this image.

  2. Use sphere mask to make everything outside the spheres and cylinder insivible
    This doesn’t work too as if two spheres are on the same line from the camera origin, the further one won’t be visible.

I’m not sure if I should stick to the material approach, or try using lights.
Could anyone give me advices?
Thanks.

Doing it with spheres and cuboids is really easy, but there’s no way to make it infinitely scalable as far as I know.

You can easily create a manager that decides which masks are active at any given time which allows you to have an infinite number of masks, but you can’t have infinite active at once.

This is the result:

How I’ve decided to do it is via a material parameter collection. You’ll have to make a number of vector parameters equal to the number of locations you want to be able to be on screen at once. At least that’s the case for sphere locations, you’ll need two vector parameters for every cuboid location.

For the spheres, the XYZ are the position, and the alpha is the radius. I’ve named the parameter to reflect such:


For testing purposes, I’ve filled out these values, but you’ll leave them blank for an actual implementation. In implementation, you’ll set the values via SetVectorParameterValue


As mentioned earlier, the cuboids need two vector parameters. For one of the parameters, we store the position in the XYZ and falloff (edge sharpness) in the alpha. For the other, we store the bounds (the size) in the XYZ and Z rotation in the alpha. Once again, I’ve named them appropriately:


The rotation is for the Z-axis by default since that’s likely the only axis you’ll want to rotate it around, but you can easily add a third vector for every instance for a custom rotation axis.

This is the final collection once I’ve put in placeholder values:




Before creating the actual material, we’ll create the material functions for the two types of mask.

The first we’ll create is the sphere mask. Very simply, we get a vector 4 function input, plug it into a sphere mask with a world position node, and output the results.

The second we’ll create is the cuboid mask. This one’s a bit more complicated since we want rotation, but it’s not too bad. We have two input parameter vector 4s correlating to the material parameter collection values.


This is where you can have a custom rotation axis. Simply make the 0,0,1,1 into an input and pass in your custom parameter.



Now in the actual material itself, it’s very simple.
Make sure the blend mode is Before Tonemapping, else you’ll have a lot of location issues:

Here’s the actual material part:


The background color (at the moment pitch black) is determined by the A value of the lerp node. At the moment it’s 0 (hence, black) but you can plug a color or anything you want in there.
The /2 is just because before tonemapping makes it very bright.

Off the max node is where we actually do the masking.
Very simply, we get the values from the material parameter collection, plug them into max nodes to effectively combine them, and plug that into the lerp:

To find the collection value, you have to search collection parameter rather than material collection parameter






The final material:

1 Like

Thanks! I’ll try your approach!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.