How to import and render vertex color data from MeshLab-exported OBJ?

I have created a .obj file with colors using MeshLab. The following is a part of that file:

OBJ File Generated by Meshlab

Object possion.obj

Vertices: 662658
Faces: 1324253

vn 0.610852 0.114539 0.783416
v -20.273296 6.488000 -0.961103 0.549020 0.584314 0.603922
vn 0.828214 0.058605 0.557339
v -20.284149 6.557515 -0.943281 0.541176 0.576471 0.600000
vn 0.846151 0.145073 0.512819
v -20.305111 6.603981 -0.927347 0.541176 0.576471 0.600000

I understand that indices 4, 5, and 6 of v represent colors.

However, when I put this file in the Assets and then into the scene, it only appears in white color. Is there a way to display the colors correctly?

MeshLab view

Unity view

Can I download a basic mesh sample to test this? I can't find any online (meshlab's obj with vertex color data that is). I have a importer script but can't test if that works without that.

–

When I imported the .obj file after adding ObjFileFormatVertexColorImporter.cs , I got the following error. What's the problem? "Mesh.colors is out of bounds. The supplied array needs to be the same size as the Mesh.vertices array" here is my data, can you help me? https://drive.google.com/file/d/1hU41Z4m_Q5zgAriEsgj-YEhjpUB6JKJr/view?usp=sharing Thank you for reading it.

–

Thanks for sharing that mesh @scn03170 ! I think I know what is wrong and will fix it soon. Will let you know.

–

2 Answers

2

1. OBJ file format specification does not support vertex color

This is why it’s not supported by default in most applications, Unity included. People need to write their own extensions for that to change and here is one of those:

→ OBJ file format vertex color importer for Unity · GitHub

Add this ObjFileFormatVertexColorImporter.cs to your project and it will start importing vertex color data for every .obj file on next reimport (Right click>Reimport).

2. Shader

Importing vertex color data is one thing but seeing it is another. For that to happen you need a material that uses a shader created to visualize this vertex color data in some way (final color, surface color tint, etc.).


If your project supports Shader-Graph then creating such shader is very straight forward:

If not then it is one of the few easy shaders to write yourself
Shader "MyUnlitVertexColor"
{
    SubShader
    {
        Tags { "Queue"="Geometry" "IgnoreProjector"="True" "RenderType"="Opaque" }
        Lighting Off
        ZTest LEqual
        ZWrite On
        Cull Back

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float4 color : COLOR;
            };

            struct v2f
            {
                float4 pos : SV_POSITION;
                float4 color : COLOR;
            };

            v2f vert (appdata v)
            {
                v2f o;

                o.pos = UnityObjectToClipPos(v.vertex);
                o.color = v.color;

                return o;
            }

            half4 frag (v2f i) : COLOR
            {
                return i.color;
            }

            ENDCG
        }
    }
}

Thanks for the reply. I'll read it and try it.

–

Importer worked for Blender-exported obj but was not tested with MeshLab ones yet. (EDIT: mesh exported from MeshLab works now)

–

Thank you for your answer. Let me check.

–

Hi @scn03170

Thanks for sharing that test mesh. I looked into it and made sure it imports fine now.

Screenshot 2023-08-29 214426

I finally achieved the exact results I wanted by trying the code you provided. I am immensely grateful for your assistance. Thank you for your time i really appreciate it!!!

–