1
\$\begingroup\$

I want to change the vertex colors of my mesh. The light sources must not affect the objects with this shader, its lighting must be determined only by its vertices colors, so I turned the Lighting Off. I use this simple shader:

Shader "VertexColored/Simple" {
Properties {
    _MainTex ("Base (RGB)", 2D) = "white" {}
}

SubShader {
    Pass {
        ColorMaterial AmbientAndDiffuse
        Lighting Off

        SetTexture [_MainTex] {
            Combine texture * primary
        }
    }
}
Fallback "VertexLit"
}

And this script:

using UnityEngine;
using System.Collections;

public class BehaviourScript : MonoBehaviour {
    void Start() {
        Color[] exclrs = new Color[11]
        {
            new Color32(25, 25, 25, 255),
            new Color32(50, 50, 50, 255),
            new Color32(75, 75, 75, 255),
            new Color32(150, 150, 150, 255),
            new Color32(200, 200, 200, 255),

            new Color32(255, 255, 255, 255),

            new Color32(200, 200, 200, 255),
            new Color32(150, 150, 150, 255),
            new Color32(75, 75, 75, 75),
            new Color32(50, 50, 50, 255),
            new Color32(25, 25, 25, 255),
        };

        Mesh mesh = GetComponent<MeshFilter>().mesh;
        Vector3[] vertices = mesh.vertices;
        Vector3[] normals = mesh.normals;

        Color[] colors = mesh.colors;

        for(int i = 0; i < vertices.Length; i++)
        {
            for(int j = 0; j < 11; j++, i++)
            {
                colors[i] = exclrs[j];
            }
            i--;
        }

        mesh.colors = colors;
    }
}

When the game is launched, in editor I can see this: enter image description here

This is EXACTLY what I wanted to achieve!

But in built for PC(x86) version I see this: enter image description here

Why the results from the editor and from the built .exe differs? What should I do to see my effect in built version? Maybe I need to use an another shader?

\$\endgroup\$
1
\$\begingroup\$

Fallback "VertexLit" might be hiding an issue with the primary shader, although it looks fine to me. You should probably still remove that line until you know your main shader is working correctly.

My other guesses would be that the mesh colors are not being set for some reason. Are you using the .sharedMesh instead of .mesh accessor?

There's a lot of trial and error that you could do to simplify the problem, I think.

\$\endgroup\$
  • \$\begingroup\$ This is the likely culprit. Check the quality settings in Edit -> Project -> Quality. The highlighted row is what the editor is using, the green checkmark is the default for the target platform (columns). It could be that the PC default is a "fast / poor" quality setting which causes shaders to use simpler fallbacks. \$\endgroup\$ – Draco18s Dec 21 '15 at 22:33

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.