Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by amiroo54f · Aug 27, 2021 at 02:48 PM · meshmesh-deformation

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();
 
 
     }
 
 
 }
 
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Robotinker · Aug 27, 2021 at 09:04 PM 0
Share

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;

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image amiroo54f · Aug 27, 2021 at 07:19 PM 0
Share

the code woking, mesh have triangles(in the inspector) and there is no error. but ther is no actual mesh.

avatar image
0

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image amiroo54f · Aug 28, 2021 at 07:39 AM 0
Share

alt text

at least there is somthing :D. thank you. but how do i fix this?

screenshot-76.png (447.1 kB)
avatar image amiroo54f · Aug 28, 2021 at 10:08 AM 0
Share

i think i found out. what exactly you mean by size? is it lenght of vertex array?

avatar image bubzy amiroo54f · Aug 28, 2021 at 03:54 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

142 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges