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 /
  • Help Room /
avatar image
1
Question by Phoenix2233 · Jun 11, 2021 at 07:07 PM · texturemeshmaterialperlin noise

Having trouble with a texture not showing on a generated mesh.

I am having a lot of trouble with perlin noise right now. This is the code I am using to generate the noise:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEditor.PackageManager.UI;
 using UnityEngine;
 
 public class NoiseTextureGenerator : MonoBehaviour
 {
     Texture2D noiseTexture;
     public Texture2D GenerateNoise(int pixelWidth, int pixelHeight, float xOrigin, float yOrigin, float scale)
     {
         Color[] colors = new Color[pixelWidth * pixelHeight];
         noiseTexture = new Texture2D(pixelWidth, pixelHeight);
         for (float x = 0.0f; x < noiseTexture.width; x++ )
         {
             for(float y = 0.0f; y < noiseTexture.height; y++)
             {
                 float xCoord = xOrigin + x / noiseTexture.width * scale;
                 float yCoord = yOrigin + y / noiseTexture.height * scale;
                 float sample = Mathf.PerlinNoise(xCoord, yCoord);
                 colors[Mathf.RoundToInt(y) * noiseTexture.width + Mathf.RoundToInt(x)] = new Color(sample, sample, sample);
             }
         }
         noiseTexture.SetPixels(colors);
         noiseTexture.Apply();
         return noiseTexture;
     }
 }

This is the script I am using the generate the mesh:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MeshGenerator : MonoBehaviour
 {
     Mesh mesh;
     Vector3[] vertices;
     int[] triangles;
 
     public Mesh GenerateMesh(int mapWidth, int mapHeight)
     {
         mesh = new Mesh();
         vertices = new Vector3[(mapHeight + 1) * (mapWidth + 1)];
         triangles = new int[6 * (mapHeight * mapWidth)];
         int index = 0;
         for (int x = 0; x <= mapHeight; x++)
         {
             for (int y = 0; y <= mapWidth; y++)
             {
                 vertices[index] = new Vector3(x, 0, y);
                 index += 1;
             }
         }
         int z = 0;
         int a = 0;
         for (int i = 0; i < triangles.Length; i += 6)
         {
             if (z == mapWidth)
             {
                 triangles[i] = a + 1;
                 triangles[i + 1] = a + 2;
                 triangles[i + 2] = a + mapWidth + 3;
                 triangles[i + 3] = a + 1;
                 triangles[i + 4] = a + mapWidth + 3;
                 triangles[i + 5] = a + mapWidth + 2;
                 z = 1;
                 a += 2;
             }
             else
             {
                 triangles[i] = a;
                 triangles[i + 1] = a + 1;
                 triangles[i + 2] = a + mapWidth + 2;
                 triangles[i + 3] = a;
                 triangles[i + 4] = a + mapWidth + 2;
                 triangles[i + 5] = a + mapWidth + 1;
                 z++;
                 a++;
             }
         }
         mesh.vertices = vertices;
         mesh.triangles = triangles;
         mesh.RecalculateNormals();
         return mesh;
     }
 }
 

   

Both of these scripts are called by this one:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CreateMesh : MonoBehaviour
 {
     Texture2D texture2D;
     public int mapWidth;
     public int mapHeight;
     public int textureResX;
     public int textureResY;
     public Vector2 textureOrigins;
     public float textureScale;
     public Material material;
     void Start()
     {
         GameObject terrainMesh = new GameObject();
         terrainMesh.transform.position = new Vector3(0, 0, 0);
         terrainMesh.AddComponent<MeshRenderer>();
         terrainMesh.GetComponent<MeshRenderer>().sharedMaterial = material;
         terrainMesh.AddComponent<MeshFilter>();
         MeshFilter meshFilter = terrainMesh.GetComponent<MeshFilter>();
         MeshGenerator meshGenerator = FindObjectOfType<MeshGenerator>();
         Mesh mesh = meshGenerator.GenerateMesh(mapWidth, mapHeight);
         meshFilter.mesh = mesh;
         NoiseTextureGenerator noiseTextureGenerator = FindObjectOfType<NoiseTextureGenerator>();
         texture2D = noiseTextureGenerator.GenerateNoise(textureResX, textureResY, textureOrigins.x, textureOrigins.y, textureScale);
         terrainMesh.GetComponent<MeshRenderer>().sharedMaterial.mainTexture = texture2D;
     }
 }
 

For my material variable on the previous script, I am using a simple unlit texture. When I generate the mesh, it just appears grey, but if I open the material instance on the mesh renderer, the texture that it shows looks like perlin noise. So I guess my question is: Why isn't the texture being applied to the mesh when it is applied to the material instance?

If you look at the picture below, you'll see what I mean about the texture on the mesh not matching the texture on the material. alt text

unity-screenshot.png (249.1 kB)
Comment
Add comment · Show 2
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 Phoenix2233 · Jun 12, 2021 at 02:33 AM 0
Share

I've tested applying the texture to a cube, after it is generated, and the cube renders the texture fine. There must be something that I am doing wrong when I generate the mesh, but I can't think of what it could be. It's not the normals, because I am recalculating them after generating the mesh, and the mesh looks fine when lit. Maybe I'm creating overlapping triangles, but I don't think so, as that would cause visible graphical glitches on the mesh in general. If someone could look over my mesh generation for flaws, that would be a welcome gesture, because as far as I know, the mesh generation should work fine.

avatar image Phoenix2233 · Jun 12, 2021 at 02:46 AM 0
Share

I also just tested changing the offset of the texture at runtime, and the value of the color changes, almost as if it is rendering only a single pixel of the texture. I tried changing the texture scale in code, but it made no change at all.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Phoenix2233 · Jun 12, 2021 at 06:29 PM

Turns out I just needed to generate the uvs for my mesh.

Comment
Add comment · 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

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

208 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 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

Imported meshes colors changed to blue. How do I fix them? 2 Answers

2 Models 1 Cracked How to make both look the same? (Material Problems) 1 Answer

Why does my model not display proper material? 2 Answers

Automatically assign hundred of textures to .obj file 0 Answers

How do you draw a sprite from an atlas on to a mesh? 1 Answer


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