ColinLeung-NiloCat/UnityURPUnlitScreenSpaceDecalShader

Question about rotation

tw2021 opened this issue · 16 comments

Dear Colin,

thank you so much for your URP Decal Shader!

I want to create a laser dot like in RE4.

Here is a video of my current approach.

I have created cube and assigned a material with a dot and your shader.
The cube is placed via a raycast under the mouse position.

As you can see in the video, it works great if the projection angle is correct.
However, when the cube / geometry is not in the correct angle, the circle becomes a rectangle.
I guess I need to rotate the cube correctly towards the geometry?

Can you recommend how to keep the laser dot in the "correct" angle?
Is there a preferred solution for this?

Thank you very much!

laser1

You can use raycast's hit information's norrmal vector to "LookAt()" the cube's transform in the opposite direction

Thank you, I have tried that with the following script on my gun:

void Update()
{
    var mousePos = Input.mousePosition;
    mousePos.z = Camera.main.farClipPlane;

    Ray ray = Camera.main.ScreenPointToRay(mousePos);

    RaycastHit hit;
    if (Physics.Raycast(ray, out hit))
    {
        Vector3 target = hit.point;

        transform.LookAt(hit.point);//aim the gun at the point in worldspace

        DecalCube.position = hit.point;//position the decal where the ray hits

        // Find the line from the gun to the point that was clicked.
        Vector3 incomingVec = hit.point - transform.position;

        // Use the point's normal to calculate the reflection vector.
        Vector3 reflectVec = Vector3.Reflect(incomingVec, hit.normal);

        DecalCube.transform.LookAt(reflectVec);
    }

The cube does rotate, but the results don't look right: I still get the rectangle material.

To debug what is happening, I have then put a standard material onto the cube instead. This way I can see what the rotation looks like:

1

Do you think the cube rotation is correct?

I have used exactely this example.

try
DecalCube.position = hit.point;
DecalCube.forward = -hit.normal;

This looks good for me. The cube is rotated nicely towards the surface, but the decal doesn't look right to me.
I have recorded a 1-minute-video here.

First, I use a standard material on the cube to visualize the rotation nicely.
Then I put the decal material on it.
The decal doesn't look right to me.
Is there any setting in the Shader that could solve my issue?

the video is quite blurry, I can't see the result.
Will toggle _ProjectionAngleDiscardEnable ON solve the issue?

I don't think so.
I have recorded a better video here.
Could you please have a look again?

Thanks for the better video, It is HD and I can understand the problem now.
(1) toggle _ProjectionAngleDiscardEnable ON
(2) edit _ProjectionAngleDiscardThreshold until the problem is solved

Thank you, it's better now, but on the edges, the decal disappears in certain situations.
Here are 2 videos that show this:

Video 1
Video 2

try make the cube very thin,

This improves it, but it isn't a solution. It keeps disappearing sometimes, and I don't see why.
Would you like me to provide you my sample project?

This is what it currenty looks like for me: https://www.youtube.com/watch?v=TTRcVaeEjnI

maybe too thin

It was a collider issue.

My last question, I hope:

edge

Currently, on the edges, the decal goes either on the top or onto the bottom of the cube.
Is there a way to show it on both top and bottom somehow?
I needed to make it thicker, that was the first step, but I still have a problem with the rotation.

Can you suggest how I have to change

        DecalCube.forward = -hit.normal;

so that it rotates not so abruptly?

Yipppieeeeeeeeeee, I got it:

        DecalCube.position = hit.point;//position the decal where the ray hits
        DecalCube.LookAt(new Vector3(hit.point.x, hit.point.y, 0));

Thank you so much!!