Image effect on specific part of objects

Image effect on specific part of objects

In this article I'm going to show you how to apply an Image effect only to specific area

this is a screenshot from CyberPunk 2077 that censored some parts of the bodies

No alt text provided for this image

Solution 1

Using GrabPass

GrabPass is a special pass type - it grabs the contents of the screen where the object is about to be drawn into a texture. This texture can be used in subsequent passes to do advanced image based effects.

  • Just GrabPass { } grabs the current screen contents into a texture. The texture can be accessed in further passes by _GrabTexture name. Note: this form of grab pass will do the time-consuming screen grabbing operation for each object that uses it.
  • GrabPass { "TextureName" } grabs the current screen contents into a texture, but will only do that once per frame for the first object that uses the given texture name. The texture can be accessed in further passes by the given texture name. This is a more performant method when you have multiple objects using GrabPass in the scene
No alt text provided for this image

Initializing position and grab position in shader:

No alt text provided for this image

pixelating results:

No alt text provided for this image

now attach this shader to a material and use it

No alt text provided for this image

Solution 2

Using two cameras that I explained before

1.Make secondary camera

2.On second camera, in Clear Flags set it to Don't clear

No alt text provided for this image
No alt text provided for this image

Solution 3

Using Command Buffer

we know, that the GPU can work on stuff in parallel. But what’s about the communication between CPU and GPU? Does the CPU has to wait until the GPU finished the job before it can receive new commands?

No alt text provided for this image

No! because this communication would create bottlenecks (e.g. when the CPU can’t deliver commands fast enough) and would make parallel working impossible.bottleneck, as for how it is named, occurs when there is a limit on how much data is being sent for processing or how much data can be processed at the same time.

The solution is a list where commands can be added by the CPU and read by the GPU – independent from each other! This list is called: Command Buffer.


Command buffers hold list of rendering commands ("set render target, draw mesh, ..."). They can be set to execute at various points during camera rendering (see Camera.AddCommandBuffer), light rendering (see Light.AddCommandBuffer) or be executed immediately (see Graphics.ExecuteCommandBuffer).

The command buffer makes it possible that CPU and GPU can work independent from each other. When the CPU wants something to be rendered, it can push that command into the queue and when the GPU has free resources, it can take the command out of the list and execute it (but the list works as a FIFO – so the GPU can only take the oldest item in the list (which was first/earlier added than all others) and work on that).

By the way: there are different commands possible. One example is a draw call, another would be to change the render state.

No alt text provided for this image

this is a vulkan command buffer as you can see there are bunch of commands in command buffer:

https://vulkan-tutorial.com/Drawing_a_triangle/Drawing/Command_buffers

https://developer.nvidia.com/engaging-voyage-vulkan

No alt text provided for this image

Let's Start to use the command buffer

No alt text provided for this image

Masking part of the character with the sphere

No alt text provided for this image
No alt text provided for this image

create a command

No alt text provided for this image

Temporarily copy the rendering result to the Render Texture

No alt text provided for this image
No alt text provided for this image



Apply the rendering result to the material that you want to apply image effects

No alt text provided for this image
No alt text provided for this image


Release temporary render texture

No alt text provided for this image
No alt text provided for this image


Register where you want to add the command buffer

Below is a high-level overview of how Cameras use the Forward or Deferred pipeline to render a Scene in Unity.

  • Black boxes represent an internal Unity process.
  • Blue boxes represent a CameraEvent where you can add Command Buffers.
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image


add Image effects to where Ref is equal to 1

No alt text provided for this image





No alt text provided for this image

adding color to pixelated area

No alt text provided for this image

More Examples!

pixelating specific objects

No alt text provided for this image

grayscale where that is not equal to Ref 1

No alt text provided for this image





No alt text provided for this image

References:

https://simonschreibt.de/gat/renderhell-book1/

https://gurutaka-log.com/commandbuffer-stencilbuffer

Sajad Beigjani

Co-Founder, Technical Lead at Funtory

3y

بسیار عالی. استفاده کردیم 

Hadi AliGol

Product Manager, Game Designer at True Dream

3y

Ahsantom !

To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics