• 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
1
Question by batch · Feb 20, 2014 at 04:13 PM · texture2dproceduraltexturinguvsplanets

How should I be procedurally texturing an object that has uvs?

Hello!

I'm trying to procedurally generate a planet. I can generate the planet, it's just texturing I'm having a hard time with. The object is a cube that has it's points normalised and then ridged multifractal noise applied to each point. That works fine.

What I'm trying to do now is texture it. The desired outcome being a planet of solid colour, with this colour value changing depending on the height of peaks (distance from its center). The object has the UVs of a cube. I create a texture2D and I've started by going through the loop for each of the vertices and drawing a pixel on the texture. This is what I get:

alt text

So, obviously I shouldn't be drawing a pixel on each vertex, but I want to have different pixel values for different vertex heights, so this way seemed to make sense to me.

Here's the function where I generate the mesh and texture. Most of this you don't have to worry about, it's just near the bottom after the 'Procedural Texturing:' comment. I left the rest in, just in case it's helpful. I understand why this isn't working, I just don't understand how I should be doing it instead. Does anyone have any advice?

 void GenerateMesh()
 {
     perlin = new Perlin(seed);
     fractal = new FractalNoise(h, lacunarity, octaves, perlin);
 
     PlanetTexture = new Texture2D(imageWidth, imageHeight, TextureFormat.RGB24, false);
     renderer.material.mainTexture = PlanetTexture;
     
     Mesh mesh = GetComponent<MeshFilter>().mesh;
     baseVertices = mesh.vertices;
     Vector3[] vertices = new Vector3[baseVertices.Length];
 
     for(int i=0;i<vertices.Length;i++)
     {
         Vector3 vertex = baseVertices[i];
 
         // Normalise the cube to make a quadsphere
         vertex = vertex.normalized;
 
         // Initialise it as something, could be anything.
         float noise = 1f;
 
         // Create noise on vertex depending on which noise is selected
         switch (noiseType)
         {
         case NoiseType.Perlin:
             noise = perlin.Noise(vertex.x, vertex.y, vertex.z);
             break;
         case NoiseType.RidgedMultifractal:
             noise = fractal.RidgedMultifractal3 (vertex.x, vertex.y, vertex.z, offset, gain);
             break;
         case NoiseType.HybridMultifractal:
             noise = fractal.HybridMultifractal3 (vertex.x, vertex.y, vertex.z, offset);
             break;
         case NoiseType.fBm:
             noise = fractal.BrownianMotion3 (vertex.x, vertex.y, vertex.z);
             break;
         }
 
         noise = noise * scale;
 
         vertex.x *= (1.0f + noise);
         vertex.y *= (1.0f + noise);
         vertex.z *= (1.0f + noise);
 
         vertices[i] = vertex;
 
         // Procedural texturing:
         pixelUV = new Vector2(mesh.uv[i].x, mesh.uv[i].y);
 
         pixelUV.x *= PlanetTexture.width;
         pixelUV.y *= PlanetTexture.height;
 
         // convert to int because setPixel doesn't accept floats
         pixelUVx = Mathf.RoundToInt(pixelUV.x);
         pixelUVy = Mathf.RoundToInt(pixelUV.y);
 
         PlanetTexture.SetPixel(pixelUVx, pixelUVy, Color.black);
 
     }
 
     mesh.vertices = vertices;
 
     mesh.RecalculateNormals();    
     mesh.RecalculateBounds();
 
     // Apply the texture changes
     PlanetTexture.Apply();
 }


Thank you!

screen shot 2014-02-20 at 15.43.42.png (434.3 kB)
Comment
Add comment · Show 5
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 ArkaneX · Feb 21, 2014 at 07:09 PM 0
Share

Could you write what result do you want to achieve? Currently your output is a dotted texture, probably with size of 256x256. Pixels are blurred because of texture size - if you increase the size, they will become smaller, if you descrease size, they will be larger.

avatar image batch · Feb 22, 2014 at 04:51 PM 0
Share

I'm trying to have a fully textured object, not just dotted, with the colour value changing depending on the height of the vertex (distance from the center).

avatar image robertbu · Feb 22, 2014 at 05:26 PM 0
Share

One way of getting a solid color with the value changing depending on height is to use Vertex colors. You need a shader that supports Vertex colors (most shaders don't). Simply set the color you want at each vertex in the $$anonymous$$esh.colors array.

avatar image batch · Feb 22, 2014 at 10:55 PM 0
Share

What I've done now is written a surface shader which changes vertex colours based on their distance from the object center. It's pretty much what I wanted to do in the first place and runs real time. I just hadn't used shaders before!

avatar image ArkaneX · Feb 22, 2014 at 11:52 PM 0
Share

I think that setting vertex colors in the script is a better idea in your case. Just prepare an array of colors like you do for vertices, set them inside for loop, and then assign to mesh.colors (or mesh.colors32).

0 Replies

· Add your reply
  • Sort: 

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

20 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

Related Questions

Importing animated texture to unity from blender. 0 Answers

Create planar UVs on procedurally generated mesh 1 Answer

How to UV a texture over the mesh with no tiling 2 Answers

Coin shine 1 Answer

Setting mipmaps 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