- Home /
i have this entire code but nothing showes up. what is problem?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Noise : MonoBehaviour
{
public static int xlenght = 250;
public static int ylenght = 250;
[SerializeField] float[] Frequencies;
[SerializeField] float[] Amplitudes;
float[,] generatednoise = new float[xlenght + 1, ylenght + 1];
Texture2D texture;
[SerializeField] Material mat;
[SerializeField] float scale;
public float xOffset;
public float yOffset;
[SerializeField] terrainrype[] terrainrypearray;
Mesh mesh;
Vector3[] vertecies = new Vector3[(xlenght + 1) * (ylenght + 1)];
[SerializeField] MeshCollider terraincollider;
private void Start()
{
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
calculatenoise();
updatemesh();
mat.mainTexture = generatetexture(texture);
}
private void Update()
{
}
Texture2D generatetexture(Texture2D texture)
{
texture = new Texture2D(xlenght, ylenght);
for (int x = 0; x <= xlenght; x++)
{
for (int y = 0; y <= ylenght; y++)
{
for (int i = 0; i < Amplitudes.Length; i++)
{
texture.SetPixel(x, y, calculatecolor(x, y));
}
}
}
texture.Apply();
return texture;
}
void calculatenoise()
{
for (int x = 0, e = 0; x < xlenght + 1; x++)
{
for (int y = 0; y < ylenght + 1; y++)
{
for (int i = 0; i < Amplitudes.Length; i++)
{
generatednoise[x, y] += Amplitudes[i] * Mathf.PerlinNoise(
Frequencies[i] * x / xlenght * scale + xOffset, Frequencies[i] * y / ylenght * scale + yOffset);
}
vertecies[e] = new Vector3(x, generatednoise[x,y], y);
e++;
}
}
}
int[] trianglesgenerate()
{
int[] triangles = new int[xlenght * ylenght * 6];
int tris = 0;
int verts = 0;
for (int y = 0; y < ylenght; y++)
{
for (int x = 0; x < xlenght; x++)
{
triangles[tris] = verts + 0;
triangles[tris + 1] = verts + xlenght + 1;
triangles[tris + 2] = verts + xlenght + 1;
triangles[tris + 3] = verts + 1;
triangles[tris + 4] = verts + 1;
triangles[tris + 5] = verts + xlenght + 2;
tris += 6;
verts++;
}
}
return triangles;
}
Color calculatecolor(int x, int y)
{
float sample = 0;
Color color = Color.white;
sample = generatednoise[x, y];
for (int a = 0; a < terrainrypearray.Length; a++)
{
if (sample <= terrainrypearray[a].depth)
{
color = terrainrypearray[a].color;
break;
}
}
return color;
}
[System.Serializable] public struct terrainrype
{
public string name;
public float depth;
public Color color;
}
void updatemesh()
{
mesh.Clear();
mesh.vertices = vertecies;
mesh.triangles = trianglesgenerate();
mesh.RecalculateNormals();
mesh.RecalculateBounds();
}
}
Try adjusting your trianglesgenerate method. This code block is generating 2 triangles:
triangles[tris] = verts + 0;
triangles[tris + 1] = verts + xlenght + 1;
triangles[tris + 2] = verts + xlenght + 1;
triangles[tris + 3] = verts + 1;
triangles[tris + 4] = verts + 1;
triangles[tris + 5] = verts + xlenght + 2;
The ints for tris + 1 and tris + 2 must be different for a triangle to generate. As is, the triangles will all be invisible since 2 points of each triangle match and therefore collapse to being invisible. Probably not what you're going for, but this will at least make something visible:
triangles[tris] = verts + 0;
triangles[tris + 1] = verts + xlenght + 1;
triangles[tris + 2] = verts + xlenght + 2;
triangles[tris + 3] = verts + 1;
triangles[tris + 4] = verts + xlenght + 1;
triangles[tris + 5] = verts + xlenght + 2;
Answer by Robotinker · Aug 27, 2021 at 07:08 PM
Attach your Visual Studio instance to Unity, choose some lines of the code to inspect, and press F9 while on them to add a breakpoint. When you run Unity, your Visual Studio debugger should halt wherever you put your breakpoints if your code is getting executed. From there, you can inspect the different variables you've calculated to see what's wrong.
If your breakpoint isn't getting hit, you need to attach your component to a game object in the scene that's executing.
the code woking, mesh have triangles(in the inspector) and there is no error. but ther is no actual mesh.
Answer by bubzy · Aug 28, 2021 at 06:55 AM
your triangle code generation is iffy
triangles[triIndex] = y * size + x;
triangles[triIndex + 1] = (y + 1) * size + x;
triangles[triIndex + 2] = y * size + (x + 1);
triangles[triIndex + 3] = y * size + (x + 1);
triangles[triIndex + 4] = (y + 1) * size + x;
triangles[triIndex + 5] = (y + 1) * size + (x + 1);
triIndex += 6;
this is generally how it is done, there is no multiplication in your code, only addition, i think your triangles are not properly being generated.
Also, it would be very useful for you to use proper naming technique too for your variables and function names. i have to go to work now so i cant look any more, if it hasn't been answered by the time i get back, ill look deeper for ya, good luck! :D
i think i found out. what exactly you mean by size? is it lenght of vertex array?
width, your yLength
would be the parameter, i usually build square terrain meshes so i dont bother with x and y, lazy but easier :D
Your answer
Follow this Question
Related Questions
UnifyWiki BasicQuad 1 Answer
How to manipulate(flatten) the Mesh of an object 0 Answers
new Mesh() works outside of array, but not in array. Huh? 3 Answers
mesh deformation 1 Answer
Accessing mesh vertices is extremely inefficient; any workarounds? 0 Answers