Procedural Mesh - Lighting

Hi again,

Another problem has arisen.

Whats happened is I have created a small voxel land which runs pretty smoothly, the only problem is that underground is dark, as it should be. I have attached a spotlight onto my Main Camera (3rd person Controller), and it lights up the terrain pretty badly, Some image examples are shown below.

Here’s my code that I’m currently using to generate the terrain.

    function GenerateMesh()
    {
        var mesh = voxelMesh.GetComponent(MeshFilter).mesh;
        mesh.Clear();
       
        var newVertices = new Vector3[0];
        var newTriangles = new int[0];
        var newUVs = new Vector2[0];
        var newColors = new Color[0];
        var newNormals = new Vector3[0];
       
        _triangles = [ 0, 1, 2, 2, 1, 3 ];
        var _topNormals =       [Vector3( 0, 1, 0), Vector3( 0, 1, 0), Vector3( 0, 1, 0), Vector3( 0, 1, 0)];
        var _bottomNormals =    [Vector3( 0,-1, 0), Vector3( 0,-1, 0), Vector3( 0,-1, 0), Vector3( 0,-1, 0)];
        var _frontNormals =     [Vector3( 0, 0,-1), Vector3( 0, 0,-1), Vector3( 0, 0,-1), Vector3( 0, 0,-1)];
        var _backNormals =      [Vector3( 0, 0, 1), Vector3( 0, 0, 1), Vector3( 0, 0, 1), Vector3( 0, 0, 1)];
        var _leftNormals =      [Vector3(-1, 0, 0), Vector3(-1, 0, 0), Vector3(-1, 0, 0), Vector3(-1, 0, 0)];
        var _rightNormals =     [Vector3( 1, 0, 0), Vector3( 1, 0, 0), Vector3( 1, 0, 0), Vector3( 1, 0, 0)];
       
        for(x=0; x<dim.x; x++)
        {
            for(y=0; y<dim.y; y++)
            {
                for(z=0; z<dim.z; z++)
                {
                    if(gridArray[x,y,z] != 0)
                    {
                        //////////////
                        // Vertices //
                        //////////////
                       
                        var y1 = y * -1;
                       
                        frontVertices =     [Vector3(x,y1,z), Vector3(x,y1+1,z), Vector3(x+1,y1,z), Vector3(x+1,y1+1,z)];
                        topVertices =       [Vector3(x,y1+1,z), Vector3(x,y1+1,z+1), Vector3(x+1,y1+1,z), Vector3(x+1,y1+1,z+1)];
                        leftVertices =      [Vector3(x,y1,z+1), Vector3(x,y1+1,z+1), Vector3(x,y1,z), Vector3(x,y1+1,z)];
                        rightVertices =     [Vector3(x+1,y1,z), Vector3(x+1,y1+1,z), Vector3(x+1,y1,z+1), Vector3(x+1,y1+1,z+1)];
                        backVertices =      [Vector3(x+1,y1,z+1), Vector3(x+1,y1+1,z+1), Vector3(x,y1,z+1), Vector3(x,y1+1,z+1)];
                        bottomVertices =    [Vector3(x,y1,z+1), Vector3(x,y1,z), Vector3(x+1,y1,z+1),Vector3(x+1,y1,z)];
                       
                       
                        ////////////
                        // Coding //
                        ////////////
                       
                        if(CheckGrid(Vector3(x,y-1,z)) == 0)
                        {
                            // Draw Top Face
                            newVertices += topVertices;
                            newTriangles += _triangles;
                            newColors += [colorArray[x,y,z],colorArray[x,y,z],colorArray[x,y,z],colorArray[x,y,z]];
                            _triangles = IncreaseIntArray(_triangles, 4);
                            newNormals += _topNormals;
                        }
                        if(CheckGrid(Vector3(x,y+1,z)) == 0)
                        {
                            // Draw Bottom Face
                            newVertices += bottomVertices;
                            newTriangles += _triangles;
                            newColors += [colorArray[x,y,z],colorArray[x,y,z],colorArray[x,y,z],colorArray[x,y,z]];
                            _triangles = IncreaseIntArray(_triangles, 4);
                            newNormals += _bottomNormals;
                        }
                        if(CheckGrid(Vector3(x-1,y,z)) == 0)
                        {
                            // Draw Left Face
                            newVertices += leftVertices;
                            newTriangles += _triangles;
                            newColors += [colorArray[x,y,z],colorArray[x,y,z],colorArray[x,y,z],colorArray[x,y,z]];
                            _triangles = IncreaseIntArray(_triangles, 4);
                            newNormals += _leftNormals;
                        }
                        if(CheckGrid(Vector3(x+1,y,z)) == 0)
                        {
                            // Draw Right Face
                            newVertices += rightVertices;
                            newTriangles += _triangles;
                            newColors += [colorArray[x,y,z],colorArray[x,y,z],colorArray[x,y,z],colorArray[x,y,z]];
                            _triangles = IncreaseIntArray(_triangles, 4);
                            newNormals += _rightNormals;
                        }
                        if(CheckGrid(Vector3(x,y,z-1)) == 0)
                        {
                            // Draw Front Face
                            newVertices += frontVertices;
                            newTriangles += _triangles;
                            newColors += [colorArray[x,y,z],colorArray[x,y,z],colorArray[x,y,z],colorArray[x,y,z]];
                            _triangles = IncreaseIntArray(_triangles, 4);
                            newNormals += _frontNormals;
                        }
                        if(CheckGrid(Vector3(x,y,z+1)) == 0)
                        {
                            // Draw Back Face
                            newVertices += backVertices;
                            newTriangles += _triangles;
                            newColors += [colorArray[x,y,z],colorArray[x,y,z],colorArray[x,y,z],colorArray[x,y,z]];
                            _triangles = IncreaseIntArray(_triangles, 4);
                            newNormals += _backNormals;
                        }
                    }
                }
            }
        }
       
        mesh.Clear();
        mesh.vertices = newVertices;
        mesh.triangles = newTriangles;
        mesh.colors = newColors;
        mesh.normals = newNormals;
        mesh.Optimize();
       
        voxelMesh.GetComponent(MeshCollider).sharedMesh = null;
        voxelMesh.GetComponent(MeshCollider).sharedMesh = mesh;
        voxelMesh.renderer.material = voxelMaterial;
    }

Any ideas how to fix it?

Use a shader with per pixel lighting. Your screenshot clearly shows that you’re using per vertex lighting.

Thanks for the reply Krobill :),

I’m using a shader with vertex colors.
Is there any shaders that use vertex colors wtih per pixel lighting.

Sorry for the noob question but I’m not very good at creating shaders :frowning:

This is my current shader if it helps

// Upgrade NOTE: replaced 'SeperateSpecular' with 'SeparateSpecular'

Shader " Vertex Colored" {
Properties {
    _Color ("Main Color", Color) = (1,1,1,1)
    _SpecColor ("Spec Color", Color) = (1,1,1,1)
    _Emission ("Emmisive Color", Color) = (0,0,0,0)
    _Shininess ("Shininess", Range (0.01, 1)) = 0.7
    _MainTex ("Base (RGB)", 2D) = "white" {}
}

SubShader {
    Pass {
        Material {
            Shininess [_Shininess]
            Specular [_SpecColor]
            Emission [_Emission]    
        }
        ColorMaterial AmbientAndDiffuse
        Lighting On
        SeparateSpecular On
        SetTexture [_MainTex] {
            Combine texture * primary, texture * primary
        }
        SetTexture [_MainTex] {
            constantColor [_Color]
            Combine previous * constant DOUBLE, previous * constant
        } 
    }
}

Fallback " VertexLit", 1
}

Hmmm no I don’t think you can get a per pixel lighting shader using the fixed functions pipeline (the kind of syntax you’re using in your example). You would have to write a surface shader to do that.

If you haven’t got an other answer by tomorrow, I may be able to patch something for you at work…