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 Reselence · Jun 29, 2014 at 05:07 PM · textureterrainmeshproceduraluv

How do I go about texturing a flat-shaded generated mesh?

So far I've come up with code that generates the mesh without sharing vertices for different triangles, in order to achieve a flat-shaded style. The problem comes when I try to texture it using height map values as guides.

At the moment the outcome looks like this: 3D view

and as seen from above: Top-down view

I want to remove the possibility of only some vertices in a triangle to be coloured. I want either all three, or none, but I have no idea of how to achieve this.

The other problem I'm having is that some blue appears on the top and right of the mesh which I suppose is an issue with overlapping.

Any help would be greatly appreciated!

My code is as follows:

 private void GenerateChunk(int width, int height, float vSpacing)
     {
         Mesh mesh = new Mesh();
         mesh.name = "newMesh";
 
         List<List<Vector3>> vertices = new List<List<Vector3>>();
         List<int> triangles = new List<int>();
         List<Vector2> uvs = new List<Vector2>();
 
         float vertexSpacing = vSpacing;
         float[,] heightMap = new float[width * width, height * height];
 
         int count = 0;
         for (float z = 0; z < height; z++)
         {
             vertices.Add(new List<Vector3>());
             for (float x = 0; x < width; x++)
             {
                 float intensity = 50.0f;
 
                 for (int f = 0; f < 6; f++)
                 {
                     float xData = 0;
                     float zData = 0;
                     float yData = 0;
 
                     switch (f)
                     {
                         case 0: { xData = x * vertexSpacing; zData = z * vertexSpacing; break; }
                         case 1: { xData = (x * vertexSpacing) + vertexSpacing; zData = z * vertexSpacing; break; }
                         case 2: { xData = x * vertexSpacing; zData = (z * vertexSpacing) - vertexSpacing; break; }
 
                         case 3: { xData = x * vertexSpacing; zData = (z * vertexSpacing) - vertexSpacing; break; }
                         case 4: { xData = (x * vertexSpacing) + vertexSpacing; zData = (z * vertexSpacing) - vertexSpacing; break; }
                         case 5: { xData = (x * vertexSpacing) + vertexSpacing; zData = z * vertexSpacing; break; }
                     }
 
                     float normalGround = TerrainGen.GroundNoise.FractalNoise2D(xData, zData, 4, TerrainGen.GroundFrequency, 1.2f) + 0.1f;
                     float highGround = Mathf.Max(0.0f, TerrainGen.MountainNoise.FractalNoise2D(xData, zData, 6, TerrainGen.MountainFrequency, 5.4f));
                     heightMap[(int)z, (int)x] = (normalGround + highGround);
                     yData = (normalGround + highGround) * intensity;
 
                     Vector3 point = new Vector3(xData, yData, zData);
 
                     vertices[(int)z].Add(point);
                 }
 
                 // uv test
 
                 Vector2 uvVect = new Vector2(x / width, z / height);
                 Vector2 uvVect2 = new Vector2((x + 1) / width, z / height);
                 Vector2 uvVect3 = new Vector2(x / width, (z + 1) / height);
 
                 uvs.Add(uvVect);
                 uvs.Add(uvVect2);
                 uvs.Add(uvVect3);
 
                 Vector2 uvVect4 = new Vector2((x + 1) / width, z / height);
                 Vector2 uvVect5 = new Vector2((x + 1) / width, (z + 1) / height);
                 Vector2 uvVect6 = new Vector2(x / width, (z + 1) / height);
 
                 uvs.Add(uvVect4);
                 uvs.Add(uvVect5);
                 uvs.Add(uvVect6);
 
                 // uv test
 
                 triangles.Add(count);
                 triangles.Add(count + 1);
                 triangles.Add(count + 2);
     
                 triangles.Add(count + 5);
                 triangles.Add(count + 4);
                 triangles.Add(count + 3);
 
                 count += 6;
             }
         }
 
         Vector3[] finalVertices = new Vector3[width * height * 6];
 
         int k = 0;
         foreach (List<Vector3> vList in vertices)
         {
             foreach (Vector3 v3 in vList)
             {
                 finalVertices[k] = v3;
                 k++;
             }
         }

         mesh.vertices = finalVertices;
         mesh.uv = uvs.ToArray();
         mesh.triangles = triangles.ToArray();
         mesh.RecalculateNormals();
 
         GameObject gameObject = new GameObject();
         MeshFilter meshFilter = (MeshFilter)gameObject.AddComponent(typeof(MeshFilter));
         meshFilter.mesh = mesh;
         MeshRenderer meshRenderer = (MeshRenderer)gameObject.AddComponent(typeof(MeshRenderer));
         meshRenderer.material.shader = Shader.Find("Diffuse");
 
         // Test painting
 
         Texture2D texture = new Texture2D(width, height);
 
         for (int tx = 0; tx < width; tx++)
         {
             for (int ty = 0; ty < height; ty++)
             {
                 Debug.Log(heightMap[ty, tx]);
                 if (heightMap[ty, tx] < 0.6f)
                 {
                     texture.SetPixel(tx, ty, Color.blue);
                 }
                 else if (heightMap[ty, tx] < 1.5f && heightMap[ty, tx] > 0.6f)
                 {
                     texture.SetPixel(tx, ty, Color.green);
                 }
 
             }
         }
 
         texture.Apply();
 
         // Test painting
 
         meshRenderer.material.mainTexture = texture;
         meshRenderer.material.color = Color.white;
 
         Debug.Log("Tris: " + mesh.triangles.Length / 3);
         Debug.Log("Verts: " + mesh.vertices.Length);
     }
 }


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
Best Answer

Answer by Reselence · Jun 30, 2014 at 07:19 PM

Got it working by using mesh.colors32 instead of creating a texture. It's much simpler this way and does what I need it to do.

Outcome: alt text

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 Eashaan Kumar · Nov 17, 2015 at 01:04 PM 0
Share

@Reselence How do you mesh.color32 work? I tried it but I see no change. Its pure white. I think it might be because of the "Diffuse" shader. How is ur terrain showing color?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

"Baking" to 2d UV Map? 1 Answer

Discrete divisions on terrain? 0 Answers

How to texture walls in procedurally generated mesh? 1 Answer

Assigning UV Map to model at runtime 0 Answers

Applying UV to plane mesh is pixel imperfect. 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