• 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 Lava444 · 4 days ago · texture2duv mappingperlin noise

Unity Perlin noise function generates straight lines on the texture.

I followed a tutorial for Perlin noise. I tried mapping it onto a plane I generated with code, but something about the texture caused the noise to generate like a barcode (straight lines in the z direction) (I think it might be a UV mapping issue, but I can't figure out where it went wrong). I tried attaching the same two scripts to a normal unity plane and it worked, but when I tried my code-generated one it didn't work.

Code for the noise function:

 public int width = 256;
 public int height = 256;

 public float scale = 20f;
 public float offsetX = 100f;
 public float offsetY = 100f;

 void OnValidate()
 {
     GetComponent<Renderer>().sharedMaterial = new Material(Shader.Find("Standard"));
     GetComponent<Renderer>().sharedMaterial.mainTexture = null;
     mapNoise();
 }

 Texture2D GenerateTexture()
 {
     Texture2D noiseTexture = new Texture2D(width, height);
     Color[] colors = new Color[width * height];
     for (int y = 0; y < height; y++)
     {
         for (int x = 0; x < width; x++)
         {
             colors[y * width + x] = CalculateColor(x, y);
         }
     }
     noiseTexture.SetPixels(colors);
     noiseTexture.Apply();
     return noiseTexture;
 }

 Color CalculateColor(int x, int y)
 {
     float xCoord = (float)x / width * scale + offsetX;
     float yCoord = (float)y / height * scale + offsetY;
     
     float sample = Mathf.PerlinNoise(xCoord, yCoord);
     return new Color(sample, sample, sample);
 }

 public void mapNoise()
 {
     Renderer renderer = GetComponent<Renderer>();
     renderer.sharedMaterial.mainTexture = GenerateTexture();
 }


Code I used to generate a plane mesh w/ uv mapping:

 public class dynamicPlane : MonoBehaviour {
     
     Mesh mesh;
     Vector3[] vertices;
     int[] triangles;
 
     [Range(1f, 100f)]
     public float xScale = 1f;
 
     [Range(1f, 100f)]
     public float zScale = 1f;
 
     [Range(2, 256)]
     public int resolution = 2;
 
     public bool getTerrainNoise = false;
     void OnValidate() {
         mesh = new Mesh();
         GetComponent<MeshFilter>().sharedMesh = mesh;
         GeneratePlane();
     }
 
     void GeneratePlane() {
         mesh.Clear();
         Vector3[] vertices = new Vector3[resolution * resolution];
         int[] triangles = new int[(resolution) * (resolution) * 6];
         int triIndex = 0;
         for (int z = 0; z < resolution; z++) {
             for (int x = 0; x < resolution; x++) {
                 int i = x + z * resolution;
                 float y = transform.position.y;
                 Vector3 planePoint = new Vector3(x * xScale / (resolution - 1), y, z * zScale / (resolution - 1));
                 vertices[i] = planePoint;
                 if (x != resolution - 1 && z != resolution - 1) {
                     triangles[triIndex] = i;
                     triangles[triIndex + 1] = resolution + i;
                     triangles[triIndex + 2] = i + 1;
                     triangles[triIndex + 3] = resolution + i + 1;
                     triangles[triIndex + 4] = i + 1;
                     triangles[triIndex + 5] = resolution + i;
                     triIndex += 6;
                 }
             }
         }
         mesh.Clear();
         mesh.vertices = vertices;
         mesh.triangles = triangles;
         Vector3[] verts = vertices;
         Vector2[] uvs = new Vector2[verts.Length];
         for (int i = 0; i < verts.Length; i++)
         {
             uvs[i] = new Vector2(verts[i].x, verts[i].y);
         }
         mesh.uv = uvs;
         mesh.RecalculateNormals();
     }   
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Lava444 · 4 days ago

ok i figured it out. It's line 52-53. I used the y component instead of the z component and I'm stupid.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

145 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

Related Questions

Strange texture2d behaviour 0 Answers

Texture breaking along center of mesh 1 Answer

Cloud texture generation not working 0 Answers

How to smooth out the values? 1 Answer

How can I convert the texture coordinates of the multi-spite to 0-1? 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