In unreal engine 4 I want to create an scope, but due to the camera Field of view when you look through the scope the viewing area is very narrow. is it possible to write a custom HLSL shader code using the Custom expression node in the scope material to clip only the narrow area through the scope? for example when the player looking through the scope, the front end is as wide as the rear of the scope which we are looking through.
I have this code in a Custom expression node and connected to the Opacity Mask of the material:
float1 output;
output = CameraPos;
output += float3(OffsetPos, 0,0 );
output = 1 - clamp(output, 0.0, 1.0);
return (output);
This code clips All the scope back to the rear end and only show the rear area of the scope which is close to what I want.
Assistant
Yes, you can write a custom HLSL shader code using the Custom expression node in the scope material to clip only the narrow area through the scope. Here's an example of how you might create the shader:
Copy the updated shader code above and paste it into the Code field of the Custom node.
This shader code calculates the screen position relative to the scope center and then clips only the narrow area through the scope using a circular mask based on the aspect ratio and field of view.
Here's a step-by-step instruction to set your material up in Unreal Engine 4:
Create a new Material.
Add a Custom node to your Material graph.
Copy the shader code above and paste it into the Code field of the Custom node.
Add WorldToAbsoluteScreenPos node and connect it to Custom node Parameter (set input name to WorldToAbsoluteScreenPos).
Add Camera Position node and connect it to Custom node Parameter (set input name to CameraPos).
Add OffsetPos (float3) as a parameter and connect it to Custom node Parameter (set input name to OffsetPos).
Add ScopeRadius (float) as a parameter and connect it to Custom node Parameter (set input name to ScopeRadius).
Connect the output of the Custom node to the Opacity Mask input of the Material node.
Set the Blend Mode to "Masked" and adjust the material properties as needed.
Remember to adjust the OffsetPos and ScopeRadius parameters to match the position and size of your scope geometry.
Add WorldToAbsoluteScreenPos node and connect it to Custom node Parameter (set input name to "WorldToAbsoluteScreenPos").
Add Camera Position node and connect it to Custom node Parameter (set input name to "CameraPos").
Add OffsetPos (float3) as a parameter and connect it to Custom node Parameter (set input name to "OffsetPos").
Add ScopeRadius (float) as a parameter and connect it to Custom node Parameter (set input name to "ScopeRadius").
Connect the output of the Custom node to the Opacity Mask input of the Material node.
Set the Blend Mode to "Masked" and adjust the material properties as needed.
Remember to adjust the OffsetPos and ScopeRadius parameters to match the position and size of your scope geometry.