2

How to render an image in which pixel stores the index on the corresponding face?

We can render barycentric coordinates image in Parametric (Shader Editor >> Geometry node), but how to access the corresponding face index?

Is it related to ptex_face_id in Attribute?

And there's Object Index, Material Index, but no Face Index in Indexes.

LogWell
  • 139
  • 6
  • Perhaps all you need is menu View > Viewport Render Image? – Markus von Broady Nov 20 '21 at 10:27
  • I tested it and couldn't get the desired result. While rendering the depth map, it's convenient to record the index of the surface, but I don't know how to do that. – LogWell Nov 20 '21 at 13:28
  • What is the desired result? Can you describe the difference between the desired result and Viewport Render Image outcome? – Markus von Broady Nov 20 '21 at 15:06
  • I upload an image here.

    For example, when rendering a depth map, each pixel records the distance in the z-direction to the nearest surface to form a depth map. Can the index of the nearest surface be recorded to form a new image for other purposes?

    – LogWell Nov 20 '21 at 15:30
  • You can probably do it with geometry nodes but I don't know how. You could easily do it by using Python to bake the index to eg. a UVMap you can read in the shader, but you'd have to rebake it whenever you change the mesh... – scurest Nov 20 '21 at 16:21
  • I am not too familiar with geometry nodes either. I don’t know if Raycast Node in Blender 3.0 can do this, or if there are other simpler methods.

    I can generate a UV face index based on texture coordinates and treat it as a texture map to render. I will try to do this later.

    – LogWell Nov 20 '21 at 16:46
  • There are some attributes(matrix) associated with each face of the mesh. When the mesh does not deform, it only needs to be calculated once.

    I need to render the mesh from different angles, and do some calculations according to the attributes of the face corresponding to the pixel.

    So I hope to be able to render the index of the face and perform subsequent calculations along with Parametric in Geometry Node.

    – LogWell Nov 20 '21 at 16:46

2 Answers2

3

I'll expand on my comment. Here's a partial answer that will give you the polygon index in a shader. Maybe you can use that to do what you want.

First run this script to bake the polygon index into the U coordinate of a UVMap named PolyIndex. The active object should be the mesh you want to bake. You'll have to re-run the script whenever you edit the mesh to get the UVMap up to date though.

import bpy

ob = bpy.context.object assert ob and ob.type == 'MESH'

if bpy.context.mode != 'OBJECT': bpy.ops.object.mode_set(mode='OBJECT')

mesh = ob.data uvs = [0.0] * (2 * len(mesh.loops))

for poly in mesh.polygons: for loopi in poly.loop_indices: uvs[2*loopi] = poly.index

if 'PolyIndex' not in mesh.uv_layers: mesh.uv_layers.new(name='PolyIndex')

mesh.uv_layers['PolyIndex'].data.foreach_set('uv', uvs)

Then in the shader, you can get the polygon index with this

UVMap node -> Separate XYZ -> X

scurest
  • 10,349
  • 13
  • 31
  • According to your code, I can connect Emission and Material Output after the X in Separate XYZ.
    1. From the result, there is interpolation at the edge boundary. How to avoid this kind of interpolation?
    2. And there are some outlier values at the position corresponding to edge_index=9, 10(without triangulate) in the rendered image, why is there such a singular value and how to avoid it?
    – LogWell Nov 21 '21 at 10:24
  • 1
    See this Q about rendering solid colors. – scurest Nov 21 '21 at 15:55
  • "When using Eevee set the Filter Size in the Film panel to zero, for Cycles select Box Filter." can solve the problem of interpolation.
  • – LogWell Nov 22 '21 at 03:32
  • I upload a video here. The rendering of some views will produce incorrect pixel values at the edge boundary(it is actually the index of the backface instead of the visible front face). I don't know if the outlier pixel values are caused by the small offset like this. I will study this problem again later.
  • – LogWell Nov 22 '21 at 03:32
  • 2.1. The position of incorrect pixels is affected by: Render Properitied >> File >> Overscan(I don't know why), but the position still changes on the edge boundary. – LogWell Nov 22 '21 at 07:01