0

The last few days I have been working on programming the effect I saw in a game called the Catacombs of Solaris. To test the effect I created this demo:

  1. This image is overlayed on the screen: (Scaled to match resolution) Rainbow image overlay.
  2. At the press of the space bar, the on-screen coordinates of the in-game mesh's vertices are converted into UVs and applied to the mesh with the same texture.

When the camera facing at the quad at a perpendicular angle the effect looks like this: Perpendicular camera example.

To explain in a different way, if the top left coordinate of the quad is 30% through the screen horizontally, and 40% through the screen vertically, I hope that making the UV for the vertex (0.3, 0.4) that the texture would match the overlay perfectly (as seen in the picture).

Now, the effect only works if the camera can be looking from other angles as well, so here is a picture of the camera at a slight rotation (about the Y axis), after updating the texture: Not-perpendicular camera example.

As you can see the texture almost lines up, but is a bit skewed, especially around the triangle borders of the mesh, shown here in white: Triangles of the mesh.

Here is the code I am using to update the texture

void UpdateTexture() {
    // test is the rainbow Texture2D
    // r is the Renderer
    r.material.mainTexture = test;

    // m is the Quad mesh
    Vector3[] vert = m.vertices;
    Vector2[] uv = m.uv;

    // We will go through each vertex of the mesh
    for(int i = 0; i < uv.Length; i++) {
        // Find where the world position of that vertex is
        Vector3 worldPosition = transform.TransformPoint(vert[i]);
        // Convert that into a screen position based on our camera
        Vector2 screenPosition = CameraToTexture.cam.WorldToScreenPoint(worldPosition);
        // Put that within the range 0...1 as a UV
        Vector2 uvPosition = new Vector2(screenPosition.x / CameraToTexture.cam.pixelWidth, screenPosition.y / CameraToTexture.cam.pixelHeight);
        // Save that UV into our temp array
        uv[i] = uvPosition;
    }

    // Apply to the UV array of the mesh
    m.uv = uv;
} 

My questions:

  1. Why is the texture warped around the triangle boundaries?
  2. Why does the effect work differently when the camera is not at a right angle?
  3. How can I fix this? / What can I look into to better understand what is happening?

If any more information is needed I will likely be able to provide it.

Alistair Carscadden
  • 1,198
  • 5
  • 18

0 Answers0