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 Cyber_Defect · Mar 22, 2013 at 12:23 AM · texturecubesphereprocedural mesh

Applying texture to quads on procedural cube

Hey guys, I am getting some weird texture stretching, I have spherized a cube with a random number of faces (which means twice as many triangles) per side and randomized their height from the origin to make my planetoid.

However, I want to apply my texture to each side of the cube before I spherize it.

I will attach my code as I have procedurally generated my cube (basically iterate through x,y,z and if x||y||z = units/2 (units is my number of quads per side) then I place a vector3 at (x,y,z).

I then assigned references to those verts in subcollections based on their position (so my edge/corner vertices are in multiple collections).

I now want to use those collections to assign my texture to that face...

WHAT AM I MISSING?!

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System;
 
 
 // manipulate indexOf of cubeSides instead!
 
 
 public class Spherizer : MonoBehaviour {
     public Transform World, cube; 
     MeshFilter worldFilter;
     public float radius = 5f; //IDE
     public float units = 10, limit = 5; //units defined in IDE
     public int smooth = 50; // 0-100 bigger = smoother
     Vector3 cubeOrigin;
     List<Vector2> uvs;
     Mesh mesh;
     // Use this for initialization
     void Start () {        
         MakeSphere();
     }
     
     // Update is called once per frame
     void Update () {
         
         /*for(int i = 0; i < worldFilter.mesh.vertices.Length; i++) {
 
             Debug.DrawRay(worldFilter.mesh.vertices[i], worldFilter.mesh.normals[i] * 2, Color.green);
 
         }*/
     }
     public void MakeSphere(){
         //instantiate global variables
         World = GameObject.FindGameObjectWithTag("World").transform; 
         cubeOrigin = transform.position;
         limit = units/2;
         worldFilter = World.gameObject.GetComponent<MeshFilter>();
         
         //instantiate local variables
         List<Vector3> worldVertices = new List<Vector3>();
         List<Vector3> worldNormals = new List<Vector3>();
         List<int> worldTriangles = new List<int>();
         List<Vector2> uvs = new List<Vector2>();
         //temp lists
         List<List<Vector3>> cubeSides = new List<List<Vector3>>();
         for (int i = 0; i < 6; i++) {
             cubeSides.Add(new List<Vector3>());
         }
         
         //Build world mesh
         for (float x=-limit;x<=limit;x++){
             for (float y=-limit;y<=limit;y++) {
                 for (float z=-limit;z<=limit;z++){
                     if (Math.Abs(x)==limit||Math.Abs(y)==limit||Math.Abs(z)==limit&&!worldVertices.Contains(new Vector3(x,y,z))) {
                         worldVertices.Add(new Vector3(x,y,z));                    
                     }
                 }
             }
         }
         foreach (Vector3 vert in worldVertices) {
             worldNormals.Add(vert.normalized);
             if (vert.x==limit) {
                 cubeSides[0].Add(vert);
             }
             if (vert.y==limit) {                
                 cubeSides[1].Add(vert);
             }
             if (vert.z==limit) {
                 cubeSides[2].Add(vert);
             }
             if (vert.x==-limit) {                
                 cubeSides[3].Add(vert);
             }
             if (vert.y==-limit) {
                 cubeSides[4].Add(vert);
             }
             if (vert.z==-limit) {                
                 cubeSides[5].Add(vert);
             }
         }
         for (int side = 0;side<cubeSides.Count;side++) {
             switch (side) {
             case 0: //x=5
                 for (int i = 0;i<(cubeSides[side].Count);i++){
                     if (cubeSides[side][i].y<limit&&cubeSides[side][i].z<limit) {
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i]));
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i+(int)units+2]));
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i+1]));
                         
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i]));
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i+(int)units+1]));
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i+(int)units+2]));
                     }
                 }
                 break;
             case 1: //y=5
                 for (int i = 0;i<(cubeSides[side].Count);i++){
                     if (cubeSides[side][i].x<limit&&cubeSides[side][i].z<limit) {
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i]));
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i+1]));
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i+(int)units+2]));
                         
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i]));
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i+(int)units+2]));
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i+(int)units+1]));
                     }
                 }
                 break;
             case 2: //z=5
                 for (int i = 0;i<(cubeSides[side].Count);i++){
                     if (cubeSides[side][i].x<limit&&cubeSides[side][i].y<limit) {
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i]));
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i+(int)units+2]));
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i+1]));
                         
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i]));
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i+(int)units+1]));
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i+(int)units+2]));
                     }
                 }
                 break;
             case 3: //x=-5
                 for (int i = 0;i<(cubeSides[side].Count);i++){
                     if (cubeSides[side][i].y<limit&&cubeSides[side][i].z<limit) {
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i]));
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i+1]));
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i+(int)units+2]));
                         
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i]));
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i+(int)units+2]));
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i+(int)units+1]));
                     }
                 }
                 break;
             case 4: //y=-5
                 for (int i = 0;i<(cubeSides[side].Count);i++){
                     if (cubeSides[side][i].x<limit&&cubeSides[side][i].z<limit) {
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i]));
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i+(int)units+2]));
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i+1]));
                         
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i]));
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i+(int)units+1]));
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i+(int)units+2]));
                     }
                 }
                 break;
             case 5: //z=-5
                 for (int i = 0;i<(cubeSides[side].Count);i++){
                     if (cubeSides[side][i].y<limit&&cubeSides[side][i].x<limit) {
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i]));
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i+1]));
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i+(int)units+2]));
                         
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i]));
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i+(int)units+2]));
                         worldTriangles.Add(worldVertices.IndexOf(cubeSides[side][i+(int)units+1]));
                     }
                 }
                 break;
             default:
                 Debug.Log("Out of Bounds");
             break;
             }
         }
         for(int i = 0;i<worldVertices.Count;i++){
             Vector3 vert = worldVertices[i];
             float distance = (cubeOrigin-vert).magnitude;
             Vector3 vect = cubeOrigin - vert;
             vect = vect.normalized;
             vect *= ((distance - radius) + UnityEngine.Random.Range(-radius/smooth, radius/smooth));
             vert += vect;
             worldVertices[i] = vert;
         }
         //worldFilter.mesh.RecalculateNormals();
         //worldFilter.mesh.RecalculateBounds();
         worldFilter.mesh.vertices = worldVertices.ToArray();
         worldFilter.mesh.triangles = worldTriangles.ToArray();
         worldFilter.mesh.normals = worldNormals.ToArray();
         foreach (Vector3 vert in worldVertices) {
             //vert.Normalize();
             uvs.Add (new Vector2(    vert.normalized.x,// / Mathf.PI + 0.5f,
                                     vert.normalized.y));// / Mathf.PI + 0.5f));
         }
         worldFilter.mesh.uv = uvs.ToArray();
         worldFilter.mesh.uv1 = worldFilter.mesh.uv;
         worldFilter.mesh.uv2 = worldFilter.mesh.uv; 
         //World.gameObject.renderer.material.mainTextureScale = new Vector2(units,units);
         //Debug.Log(worldFilter.gameObject.GetComponent<Shader>().name);
     }
 }
 


alt text

planetoid.jpg (77.0 kB)
Comment
Add comment · Show 7
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 Cyber_Defect · Mar 22, 2013 at 01:03 AM 0
Share

The coding is a little sloppy for now, I try to get functionality and then edit for eloquence.

I am going to be using AddRange() to get rid of all of the Add() calls in my triangles section for sure ;)

Can I assign a material to a range of normals somehow?

avatar image Cyber_Defect · Mar 22, 2013 at 05:11 PM 0
Share

Seriously? Nobody has an answer to this question? I have searched unity answers thoroughly to no end. I have a few ideas based on this post: http://answers.unity3d.com/questions/30934/texturing-a-cube-different-textures-on-a-face.html

Hopefully this produces an answer... I will post my results for the community.

avatar image insominx · Mar 22, 2013 at 05:33 PM 0
Share

It would seem that your texture coordinates are not well distributed. Try applying a a debug UV texture to visualize it (http://i70.photobucket.com/albums/i113/Huidafa/UVTextureChecker4096.png).

avatar image Cyber_Defect · Mar 22, 2013 at 06:17 PM 0
Share

As you can see, it it is not applying it to anything beyond object level...

alt text

planetoid2.jpg (137.8 kB)
avatar image Cyber_Defect · Mar 22, 2013 at 06:26 PM 0
Share

$$anonymous$$aybe this will help, I unscaled it and left my uvs normalized. I also commented out my spherizing loop...

alt text

planetoid3.jpg (163.6 kB)
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Cyber_Defect · Apr 08, 2013 at 08:54 PM

I ended up rethinking the entire process and scrapping this method.

If anyone finds a solution to this send me a message, otherwise I am retiring this question as a poor implementation of a concept.

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.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Apply texture to a bar (cube) 2 Answers

How to add my texture to a cube? 2 Answers

Create Material with gradient on a sphere? 2 Answers

Recommended Sphere Texture size? 1 Answer

How to make my rotating sphere sticky? 2 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