0
\$\begingroup\$

I rendered some UV unwrapped raw mesh data using a special shader, and Graphics.DrawMesh() to a render texture, but upon inspection the data is wrong (world normal and position).

world normal output

  • Left is a vanilla unlit shader modified to output the world normal from the game objects, normals align to the unity basis show in the upper right corner.
  • Right is the target complex shader that use raw mesh with custom matrix, uv unwrap rendered to texture and read back as texture on the gameobject by another shader (the working is out of scope, context is roughly lightmap as a G buffer)

It seems to make sense since I'm bypassing the gameobject construct of Unity, therefore I have faulty matrix. Therefore, I tried passing my own matrix. But the worldnormal data don't seem to match, and doing the inverse of the transpose of my own matrix doesn't seem to do the trick (the standard stuff to transform normal) ...

To investigate further, as I suspect that raw mesh is the imported data before Unity applies its import settings, I created a little program that I attached to the game object to compare its render to various DrawMesh() commands ... but DrawMesh doesn't work as intended ...

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MeshVsObjectNormalTest : MonoBehaviour
{
    //public GameObject normalObject;
    Mesh[] rawMesh;
    Matrix4x4 objectMatrix;
    Camera cam;
    Material mat;

    // Start is called before the first frame update
    void Start()
    {
        rawMesh = new Mesh[1];
        rawMesh[0] = this.GetComponent<MeshFilter>().mesh;
        objectMatrix = Matrix4x4.identity;
        cam = Camera.main;
        mat = this.GetComponent<Renderer>().material;
        
    }

    // Update is called once per frame
    //void Update()
    void LateUpdate()
    //void OnPostRender()
    {
        //Graphics.DrawMesh(rawMesh[0], objectMatrix, mat,0, cam);
        //Graphics.DrawMesh(rawMesh[0], Vector3.zero, Quaternion.identity, mat,0, cam);
        Graphics.DrawMesh(rawMesh[0], Vector3.zero, Quaternion.identity, mat,0);
        
        //mat.SetPass(0);
        //Graphics.DrawMeshNow(rawMesh[0], objectMatrix);
        
        Debug.Log(cam);
    }
    
}

Nothing shows up, but it's how every example I can find when I search does it, and I'm already using that command successfully to render to texture ...

nothing shows up

Is there something I'm missing?

\$\endgroup\$
4
  • \$\begingroup\$ You say "not as intended", but don't say what's intended, what you observed, or how those two things differ. Adding some screenshots with a sample mesh could help. "Nothing shows up" could be due to a few things, like calling DrawMeshNow() in Update, before the main camera has rendered, so the camera's rendering pass covers up the result. Your OnPostRender version wouldn't work because you've attached this script to the object with the mesh, not to the camera. For the other attempts, it could also be that your object is behind / surrounding the camera, or inside/behind the game object. \$\endgroup\$
    – DMGregory
    Oct 10, 2022 at 14:53
  • \$\begingroup\$ You have not shown us how you determined "data is wrong (world normal and position)". You have not shown us your camera settings so we can verify whether the object should be in its view frustum. You've scaled your cube instance by a factor of 100, so we can't even see the mesh on the original game object. You also have not shown us the shader you're using - do you get the same outcomes when using one of the default materials instead? \$\endgroup\$
    – DMGregory
    Oct 10, 2022 at 16:33
  • \$\begingroup\$ THANKS! Trying to answer, your comment, i found out the issue, it's minuscule (x0.01)! And therefore wasn't visible at the distance the camera was in. I put an empty at the origin, and use that to zoom in place, to show where it should have gone (identity matrix) and found it there! It seems my intuition was right about the raw mesh having no applied import settings, as it seems to be at object scope, but underestimate the magnitude of the mesh scale (it's in blender's). Now I need to figure out that inverse transpose matrix stuff. \$\endgroup\$
    – user29244
    Oct 10, 2022 at 16:34
  • \$\begingroup\$ I'll update the question to reflect your concern and answer it. \$\endgroup\$
    – user29244
    Oct 10, 2022 at 16:36

1 Answer 1

0
\$\begingroup\$

It's a problem of scale

The mesh is too small by two magnitude and isn't visible at the camera distance. enter image description here

We can verify that the normal colors are nearly identical from the miniature mesh to the texture sampled version on the right. That mean the matrix used to transform the normal aren't working when it's rendered to texture, which I need to figure out.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .