Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
0
Question by FuturePilot1701 · Aug 05, 2019 at 12:42 AM · texturemeshmaterialprocedural

How to add texture to a cube sphere?

I have been trying for a few weeks now to figure out how to generate procedural planets. I currently have a script that generates a cube sphere of any size / resolution. But I dont really understand materials/textures/UV mapping, etc. I have watched a tutorial on youtube that goes through the entire process but I want to create it myself, and the guy in the tutorial doesnt really explain what hes doing most of the time, and I dont understand some of the code. I guess I have 2 main questions.

1) what is the best way to add a material / color to a cube sphere? 2) how do I make the different faces match up when adding a material / color / height nosie?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CubeSphere : MonoBehaviour
 {
 
     public float radius;
     public float resolution;
     public List<Vector3> points;
     public Vector3[] vertices;
     public Color[] colors;
 
     public GameObject point;
 
     void Start()
     {
 
 
         int i = 0;
         //bottom
         for (float x = -radius; x <= radius; x += radius / resolution)
         {
             for (float z = -radius; z <= radius; z += radius / resolution)
             {
                 points.Add(new Vector3(x, -radius, z));
                 i++;
             }
         }
         //0XY
         for (float y = -radius; y <= radius; y += radius / resolution)
         {
             for (float x = -radius; x <= radius; x += radius / resolution)
             {
                 points.Add(new Vector3(x, y, -radius));
                 i++;
             }
         }
         //0ZY
         for (float y = -radius; y <= radius; y += radius / resolution)
         {
             for (float z = radius; z >= -radius; z -= radius / resolution)
             {
                 points.Add(new Vector3(-radius, y, z));
                 i++;
             }
         }
         //1XY
         for (float y = -radius; y <= radius; y += radius / resolution)
         {
             for (float x = radius; x >= -radius; x -= radius / resolution)
             {
                 points.Add(new Vector3(x, y, radius));
                 i++;
             }
         }
         //1ZY
         for (float y = -radius; y <= radius; y += radius / resolution)
         {
             for (float z = -radius; z <= radius; z += radius / resolution)
             {
                 points.Add(new Vector3(radius, y, z));
                 i++;
             }
         }
         //top
         for (float x = -radius; x <= radius; x += radius / resolution)
         {
             for (float z = radius; z >= -radius; z -= radius / resolution)
             {
                 points.Add(new Vector3(x, radius, z));
                 i++;
             }
         }
 
 
 
 
 
         vertices = points.ToArray();
 
         for (int n = 0; n < vertices.Length; n++)
         {
             Vector3 temp = vertices[n];
             vertices[n] = temp.normalized * radius;
         }
 
         int[] triangles = new int[vertices.Length * 6];
 
         //foreach (Vector3 vertex in vertices)
         //{
         //    Instantiate(point, vertex, transform.rotation);   
         //}
 
         int v = 0;
         int index = 0;
 
         int xSize = 3;
         int zSize = 3;
 
         if (resolution == 1)
         {
             xSize = 3;
             zSize = 3;
         }
 
         if (resolution == 2)
         {
             xSize = 3 + 2;
             zSize = 3 + 2;
         }
 
         if (resolution > 2)
         {
             xSize = 3 + (((int)resolution - 1) * 2);
             zSize = 3 + (((int)resolution - 1) * 2);
         }
 
         for (int s = 0; s < 6; s++)
         {
             for (int x = 0; x < xSize - 1; x++)
             {
                 for (int z = 0; z < zSize - 1; z++)
                 {
                     triangles[index] = v;
                     triangles[index + 1] = v + xSize;
                     triangles[index + 2] = v + 1;
                     triangles[index + 3] = v + xSize;
                     triangles[index + 4] = v + xSize + 1;
                     triangles[index + 5] = v + 1;
 
                     v++;
                     index += 6;
                 }
 
                 v++;
                 
             }
 
             v += xSize;
         }
 
         //Vector2[] uv = new Vector2[vertices.Length];
 
         //for (int u = 0; u < uv.Length; u++)
         //{
         //    uv[u] = new Vector2(vertices[u].x, vertices[u].z);
         //}
 
         Mesh mesh = GetComponent<MeshFilter>().mesh;
         mesh.Clear();
         mesh.vertices = vertices;
         mesh.triangles = triangles;
         //mesh.uv = uv;
         mesh.Optimize();
         mesh.RecalculateNormals();
 
         //GetComponent<MeshCollider>().sharedMesh = mesh;
 
     }
 }

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

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.

Follow this Question

Answers Answers and Comments

236 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Texture UV in custom mesh 0 Answers

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

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

Why does my model not display proper material? 2 Answers

Automatically assign hundred of textures to .obj file 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