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 Chimera3D · Feb 18, 2013 at 02:16 AM · texturemeshsharing

Share Texture Across Multiple Meshes

I have a plane that has a normal diffuse material and a texture to go with it. I want to break the plane up into smaller planes with pieces that are random sizes but all fit within the area of the plane (like breaking up an assembled puzzle with rectangular pieces). However the pieces are not supposed to just be separated all at the same time, the plane will break up into mini-planes from a point. The mini planes should still retain the texture occupying the space that they used to be in, when the plane wasn't broken. I'm not asking for code or even psuedo-code (although that would be nice), but what is some of the logic that would go into this as I haven't the slightest idea about how to do this? Or even a way to somewhat fake the effect would be very helpful.

Comment
Add comment · Show 1
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 robertbu · Feb 18, 2013 at 02:32 AM 0
Share

You are looking for the uv coordinates of the mesh. The individual pieces can be authored in a 3D program, or you can assign the values in code.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by numberkruncher · Feb 18, 2013 at 03:43 AM

Here is a rather crude example which I have put together for you from one of my past experiments :-)

How to use it:

  1. Create an empty game object.

  2. Attach the following "Puzzle" script.

  3. Select a texture using the inspector.

  4. Hit "Play" and then hold space key to animate tiles.

How does it work:?

Initially the script generates the triangles and UV coordinates for each tile. The vertices are updated each frame using the Update message. Tiles can be moved individually by adjusting their respective tilePosition. Use SetTilePosition to adjust the position of a specific puzzle tile.

Source Code:

 using UnityEngine;
 
 public class Puzzle : MonoBehaviour {
 
     // Texture for puzzle
     public Texture2D puzzleTexture;
     
     // Number of rows and columns in puzzle
     public int rows = 5;
     public int columns = 5;
 
     // Size of a single tile
     public Vector2 tileSize = new Vector2(1, 1);
 
     // Speed of random movements
     private Vector3[] randomDirections;
     public float randomSpeed = 1f;
 
     // Tile positions in local space
     [System.NonSerialized]
     public Vector3[] tilePositions;
 
     // Generated objects
     private Material _mat;
     private Mesh _mesh;
 
     private Vector3[] _vertices;
 
     private void Awake() {
         _mat = new Material(Shader.Find("Unlit/Texture"));
         _mat.mainTexture = puzzleTexture;
         
         PrepareMesh();
         
         MeshFilter filter = gameObject.AddComponent<MeshFilter>();
         filter.sharedMesh = _mesh;
         
         MeshRenderer renderer = gameObject.AddComponent<MeshRenderer>();
         renderer.sharedMaterial = _mat;
 
         // Prepare random directions
         randomDirections = new Vector3[ tilePositions.Length ];
         for (int i = 0; i < randomDirections.Length; ++i)
             randomDirections[i] = new Vector3(Random.value - 0.5f, Random.value - 0.5f, 0);
     }
 
     private void Update() {
         // Only perform animation when space key is pressed
         if (Input.GetKey(KeyCode.Space)) {
             float deltaSpeed = randomSpeed * Time.deltaTime;
 
             // Do something with tiles!
             for (int ti = 0; ti < tilePositions.Length; ++ti) {
                 tilePositions[ti] += randomDirections[ti] * deltaSpeed;
             }
         }
 
         UpdateVertices();
     }

     public void SetTilePosition(int row, int column, Vector3 position) {
         tilePositions[ row * columns + column ] = position;
     }

     public Vector3 GetTilePosition(int row, int column) {
         return tilePositions[ row * columns + column ];
     }
 
     private void PrepareMesh() {
         _mesh = new Mesh();
 
         int tileCount = rows * columns;
         int vertexCount = tileCount * 4;
         int triangleCount = tileCount * 2;
 
         tilePositions = new Vector3[ tileCount ];
 
         Vector2[] uvs = new Vector2[ vertexCount ];
         int[] tris = new int[triangleCount * 3];
         _vertices = new Vector3[ vertexCount ];
 
         // Size of single tile in UV space
         Vector2 uvSize = new Vector2(1f / (float)columns, 1f / (float)rows);
         // Current position of cursor
         Vector3 cursor = new Vector3(0f, rows * tileSize.y, 0f);
         Vector2 uvCursor = new Vector2(0, 1 - uvSize.y);
         
         int ti = 0;
         int vi = 0;
         int tri = 0;
 
         // Generate vertices
         for (int row = 0; row < rows; ++row) {
             for (int column = 0; column < columns; ++column) {
                 uvs[vi + 0] = uvCursor;
                 uvs[vi + 1] = new Vector2(uvCursor.x, uvCursor.y + uvSize.y);
                 uvs[vi + 2] = new Vector2(uvCursor.x + uvSize.x, uvCursor.y + uvSize.y);
                 uvs[vi + 3] = new Vector2(uvCursor.x + uvSize.x, uvCursor.y);
 
                 tilePositions[ti++] = cursor;
 
                 tris[ tri++ ] = vi + 0;
                 tris[ tri++ ] = vi + 1;
                 tris[ tri++ ] = vi + 2;
                 tris[ tri++ ] = vi + 2;
                 tris[ tri++ ] = vi + 3;
                 tris[ tri++ ] = vi + 0;
 
                 // Proceed to next vertex!
                 cursor.x += tileSize.x;
                 uvCursor.x += uvSize.x;
                 vi += 4;
             }
 
             // Move to start of next row
             cursor.x = 0;
             cursor.y -= tileSize.y;
             uvCursor.x = 0;
             uvCursor.y -= uvSize.y;
         }
 
 #if UNITY_4_0
         _mesh.MarkDynamic(); // Unity4 Only!
 #endif
         _mesh.vertices = _vertices;
         _mesh.uv = uvs;
         _mesh.triangles = tris;
 
         //_mesh.RecalculateNormals(); // Add if you need them
     }
 
     private void UpdateVertices() {
         int tileCount = rows * columns;
         int vi = 0;
 
         // Update vertices for each frame!
         for (int ti = 0; ti < tileCount; ++ti) {
             Vector3 position = tilePositions[ti];
 
             _vertices[vi + 0] = new Vector3(position.x, position.y - tileSize.y, position.z);
             _vertices[vi + 1] = position;
             _vertices[vi + 2] = new Vector3(position.x + tileSize.x, position.y, position.z);
             _vertices[vi + 3] = new Vector3(position.x + tileSize.x, position.y - tileSize.y, position.z);
 
             vi += 4;
         }
 
         _mesh.vertices = _vertices;
     }
 
 }
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

10 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

Related Questions

Layered Textures/Materials 0 Answers

3d text mesh hides plane object 1 Answer

How to confirm the vertex of a mesh that mouse in and change its texture 0 Answers

rendering hollow mesh ? 0 Answers

select part of mesh and re texture it. 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