Click video to watch the camo effect in motion
For this mechanic, the player drapes a quilt over their head and the quilt takes visual data from the world around the player to become "invisible." The principal challenge is that quilt is constantly moving and deforming; the data pulled from around the player and transposed onto a material needs to align with the position of the camera, meaning I need to ignore UVs altogether. But first, I needed to capture the data.
Getting dataComing from Unity, I knew that you could render camera data to a texture target. In Unreal, they achieve this through a Scene Capture Component 2D, a camera whose only function is to capture visual data and write to a Render Target. I created an Actor BP and attached a Scene Capture Component to it.
It would be odd to see the character or the quilt as part of the texture, so you can filter them out by changing the Primitive Render Mode to "Use ShowOnly List." We can access the ShowOnly list through BluePrint.
The ShowOnly list accepts an array of Actors, which in my BluePrint was populated through a Get All Actors Of Class function, getting all static meshes and any other Actors that I needed that didn't fit that type. If an Actor was excluded from this command, you can manually add it to the array. The BluePrint ran this code only once during the Event BeginPlay.
The Scene Capture component needed to follow the player, so under an Event Tick, I had the script find the player through a tag, getting the world location of the player and the transform data of the camera to have the BP Actor both follow the player and rotate with the camera.
Using Scene Data
Now that the Render Target texture was getting all the environment data I needed to attach to a material, I could move onto the next step; however, the problem with a constantly rotating and deforming actor made it impossible for me to use. To solve this, I used a decal which projects a material onto a surface, conforming to the normals of a mesh. Now, the issue with decals is that it will project onto anything within its range. We can get around that by having it only project on the quilt by masking out the rest of the scene using a BitMask through a Custom Stencil. A Custom Stencil allows you to mask out specific objects assigned to a particular bit. Here, I chose a bit value of 1. In the decal material, I got the Custom Stencil from the Scene Texture, getting the r color channel and dividing by 255 to get the BitMask value, and choosing a bit value of 1. The quilt will be the only actor with that stencil bit value. That mask then goes into the opacity channel so that the mask can essentially crop the material.
On the quilt actor, I enabled custom depth, set the depth stencil to the first bit, and set the stencil value to 1. Now the material will work exclusively with the quilt.
Finally, I set the transform location of the decal to be positioned onto the quilt. By default, we can set the quilt to never receive decals until the player activates the camo, and now everything works! The last issue to resolve is shadows. Shadows cast from other actors and itself will make it difficult to see the camo effect, so I added a slight emissive to the material.
|Before emissive was added to camo material
|After emissive was added to camo material