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 Zynx87 · Feb 07, 2014 at 09:23 AM · texturenoiseperlin

Applying noise to a texture

I have been following Unity's Perlin reference and this reference attempting to just easily view my noise function by making a texture of it.

Here is the code:

 internal void init()
         {
 
             Texture2D noiseTex = new Texture2D(20,20);
             Color[] pix = new Color[noiseTex.width * noiseTex.height];
             
 
 
             SimplexNoiseGenerator3 noiseGen3 = new SimplexNoiseGenerator3();
 
 
             int ytex = 0;

//loops through texture coords applying a noise color at each coord.

             while(ytex < noiseTex.height){
                 int xtex = 0;
                 while(xtex < noiseTex.width){
  
                     float sample = noiseGen3.GetNoise(xtex,ytex,0);
                     float value = (255 * sample);
                     
                     pix[ytex * noiseTex.width + xtex] = new Color(value, value, value);
                     Color thisColor = new Color(value,value,value);
                     //noiseTex.SetPixel(xtex,ytex, thisColor);
                     //noiseTex.SetPixels (pix);
                     //noiseTex.Apply();
                     Debug.Log ("at coords " + xtex + " " + ytex + "is color " + pix[ytex*noiseTex.width + xtex]);
                     Debug.Log ("get pixel color " + xtex + " " + ytex + " " + noiseTex.GetPixel(xtex, ytex));
                     xtex++;
 
                 }
                 ytex++;
             }
             noiseTex.SetPixels (pix);
             noiseTex.Apply();
 
 
             Mesh meshTex = new Mesh();
             GameObject texGO = new GameObject();
             texGO.name = "texGO";
 
             texGO.AddComponent<MeshFilter>().mesh = meshTex;
             texGO.AddComponent<MeshRenderer>();
 
             Vector3[] meshTexVerts = new Vector3[4];
             meshTexVerts[0] = new Vector3(-20,0,0);
             meshTexVerts[1] = new Vector3(-20,20,0);
             meshTexVerts[2] = new Vector3(0,20,0);
             meshTexVerts[3] = new Vector3(0,0,0);
 
             int[] meshTexTris = new int[6];
             meshTexTris[0] = 0;
             meshTexTris[1] = 1;
             meshTexTris[2] = 2;
             meshTexTris[3] = 0;
             meshTexTris[4] = 2;
             meshTexTris[5] = 3;
 
             texGO.renderer.material.mainTexture = noiseTex;
             Vector2[] meshUV = new Vector2[meshTexVerts.Length];
 
             /*meshUV[0] = new Vector2(0,0);
             meshUV[1] = new Vector2(0,20);
             meshUV[2] = new Vector2(20,20);
             meshUV[3] = new Vector2(20,0);
             */
             int i = 0;
             while(i< meshUV.Length){
                 meshUV[i] = new Vector2(meshTexVerts[i].x, meshTexVerts[i].z);
                 i++;
             }
             meshTex.vertices = meshTexVerts;
             meshTex.triangles = meshTexTris;
             meshTex.uv = meshUV;
             meshTex.Optimize();

My debugging shows that the noise value is correct between 0-255 for each (xtex,ytex). The color array pix[] also inserts the value appropriately for each coord. Unfortunately, noiseTex.GetPixel returns the same RBG value for each coord, and I'm unable to figure out why. As such my whole mesh displays as a single shade of grey.

Any ideas to the issue or other options of implementation are appreciated.

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 ArkaneX · Feb 07, 2014 at 10:24 AM

Valid RGBA values range for Color class is 0-1, while you're using values in range 0-255. If your GetNoise method returns value from 0 to 1, then just do:

 float value = noiseGen3.GetNoise(xtex,ytex,0);

and skip multiplying by 255.

If you want to use values in 0-255 range, then use Color32 class and Texture2D.SetPixels32 method.

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 Zynx87 · Feb 07, 2014 at 11:42 AM 0
Share

This fixes it, thank you very much for the help!

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

19 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

Related Questions

Using noise as texture 2 Answers

Why does Perlin-generated Texture lose gradient when with colour? 1 Answer

creating a perlin noise generates all pixels the with the same shade of gray 2 Answers

Always same result from Mathf.PerlinNoise() 2 Answers

Perlin Noise and Terrain Generation 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