- Home /
Question by
MaG3Stican · May 23, 2014 at 02:52 AM ·
c#mesh
Shader from auto generated mesh looks black on screen
Hi, I am making an auto generated world and I am trying to create the meshes from scratch in code. After being able to draw my shapes, it seems that no matter what shader I apply to my object it always looks black.
This is how it looks like (the spot you see is a light I used to check if it looked dark because of no light) :
This is my code :
using UnityEngine;
using System.Collections;
using Helpers;
using System.Linq;
using MapGenerator.Models;
using System.Collections.Generic;
namespace Managers
{
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(MeshCollider))]
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class Tetrahedron : MonoBehaviour
{
List<Vector3> vertices = new List<Vector3>();
List<int> triangles = new List<int>();
List<Vector2> uv = new List<Vector2>();
List<Vector3> normals = new List<Vector3>();
public bool sharedVertices = false;
public void Build(Center c){
MeshFilter meshFilter = GetComponent<MeshFilter>();
MeshRenderer renderer = (MeshRenderer) meshFilter.renderer;
renderer.material = Resources.Load("Grass",typeof(Material)) as Material;
if (meshFilter==null){
Debug.LogError("MeshFilter not found!");
return;
}
Mesh mesh = meshFilter.sharedMesh;
if (mesh == null){
meshFilter.mesh = new Mesh();
mesh = meshFilter.sharedMesh;
}
mesh.Clear();
foreach (Triangle t in c.Polygon.tris) {
t.vertices.Select(b => b.Position).ToList().ForEach(x => vertices.Add(x));
}
mesh.vertices = vertices.ToArray();
System.Random rand = new System.Random();
triangles = new List<int>();
int i = 0;
foreach( var v in vertices)
{
triangles.Add(i);
i++;
}
mesh.triangles = triangles.ToArray();
uv = new List<Vector2>();
vertices.ForEach(b => uv.Add( new Vector2( rand.Next(2),rand.Next(2))));
// basically just assigns a corner of the texture to each vertex
mesh.uv = new Vector2[vertices.Count];// uv.ToArray();
c.Normal.Normalize();
var nomalized =c.Normal;
vertices.ForEach(b => normals.Add( nomalized));
mesh.normals = normals.ToArray();
mesh.RecalculateNormals();
mesh.RecalculateBounds();
mesh.Optimize();
meshFilter.mesh = mesh;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
}
capture.png
(30.9 kB)
capture.png
(30.9 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613145701im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to active mesh renderer with a script C# 2 Answers
How do I sync a mesh over network? (Photon) 1 Answer
Subdivide mesh C#... help 0 Answers