2
\$\begingroup\$

I'm sorry if this is an obvious thing to solve but I just can't figure it out...did I miss something...I've generated a mesh and during vertices generation I've also set its colors (and colors32 just in case) by reading those values from a ply file, and afterwards I've added a number of vertex color shaders but none of them will show the vertex point colors in play mode, it's either solid black or just like I added no material to the mesh (which I've tried doing manually in play mode too).

Did I miss something please?

\$\endgroup\$
0
\$\begingroup\$

Welcome, it's hard to tell with you not showing any code or explaining further what you've done :)

Does your shader handles vertex colors ?

If not then here's a quick example:

Shader "Unlit Vertex Color"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                float4 color : COLOR;
            };

            struct v2f
            {
                float4 color : COLOR;
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            fixed _Brightness;
            fixed _Gamma;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                o.color = v.color;
                o.color.w = 1.0;
                //UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv);
                col *= i.color;
                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, col);
                return col;
            }
            ENDCG
        }
    }
}

I just pass COLOR semantic to where necessary and blend the result with the pixel.

You can get started writing your own by templating some of the shaders from the builtin ones:

https://unity3d.com/get-unity/download/archive

If you want Standard shader with vertex colors (unofficial):

https://forum.unity.com/threads/standard-shader-with-vertex-colors.316529/

Also, Unity is apparently going to release an official standard shader with vertex colors.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ You Sir are a savior! I had five different shaders of the net and none gave me the points, I was going crazy enough and started looking at values (I noticed something I'm confused about, value in color.r/g/b is float 0..1 and color32.r/g/b is int 0..255, I'd ask how but that's for another question probably the one where I'll ask if I need color at all when color32 is set since unity says 32's better for performance)...but just adding your shader made it work (honestly don't know why others didn't - I can't read them but one said unlit vertex color too - where do you learn about shaders plz)? \$\endgroup\$ – GI1 Feb 23 '19 at 22:39
  • \$\begingroup\$ For colors I think you should use whichever notation you prefer, e.g. Color is natural since it maps from 0.0 to 1.0, then later, if you notice performance problems by profiling you will want to adjust places in your code to emit Color32 instead. Keep in mind they both have implicit operators to each other so be careful when optimizing. Now for the shaders I learned the hard way: the 'builtin shaders' link I've mentioned. Here's a tip, use Notepad++ folder search on them, it'll be less intimidating and you'll get a better understanding overall on what is used where. \$\endgroup\$ – aybe Feb 23 '19 at 22:47
  • \$\begingroup\$ Just one question when I got you here...since I can't seem to get it to work programmatically...do I need to anything else beside : MeshRenderer mr = g.AddComponent<MeshRenderer>(); Material newMat = Resources.Load("vertex", typeof(Material)) as Material; mr.material = newMat; (vertex is the name of the material I assigned the shader too, and it works when I do it in the property, when in game play mode) Thank you in advance again \$\endgroup\$ – GI1 Feb 23 '19 at 22:49
  • \$\begingroup\$ I do the following by code, get my mesh, add MeshFilter component and set its mesh, add MeshRenderer component and set its material; and obviously if I have a texture I set material.mainTexture. \$\endgroup\$ – aybe Feb 23 '19 at 22:52
  • \$\begingroup\$ You mean it doesn't work during play mode or what ? \$\endgroup\$ – aybe Feb 23 '19 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.