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 Freak-ey · Feb 08, 2015 at 01:11 PM · texturemesh

Texture problem when applying to procedural mesh

I am currently writing a tilemap system in which the map is required to generate at runtime and also have a procedural texture applied. The Mesh generates perfectly and the texture almost fits the mesh but is 1 pixel greater than the set tilesize.

I have messed around with the variables to see if it was just a mesh or texture scaling problem but it appears not.

alt text

Notice that the texture squares are not the same size as the grid AND that they go over the edge of the texture due to appearing to be 1 pixel to large.

When I save the texture onto my hard-drive however, it is 8 * 8. (The image is fuzzy (When uploaded onto this site (it isn't in Unity)) so I will not upload an example of it).

The most obvious solution I can think of then is scaling the image down to fit the entire board but A. I don't know how. B. I don't know how efficient that is. C. It seems like a cheap workaround. (Right now I am generating an 8 * 8 grid on the X / Y Axis)

 void BuildTexture(int size_x, int size_y) //Pass In number of tiles
     {
         int texWidth = size_x * Tile.resolution; //Resolution is currently 1 so the math here is simply 8 * 1
         int texHeight = size_y * Tile.resolution;
 
         Texture2D texture = new Texture2D (texWidth, texHeight); //Create Texture
 
         for(int x = 0; x < texWidth; x++) //Loop through texture
         {
             for(int y = 0; y < texHeight; y++)
             {
                 Color c = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f)); //get Random Color
                 texture.SetPixel(x, y, c); //I am aware that this is inefficient, this is a standin system to fix the current tiling issues
             }
         }
 
         texture.wrapMode = TextureWrapMode.Clamp; //I heard this was useful for preventing this problem... It wasn't
         texture.filterMode = FilterMode.Point; //To prevent fuzzy textures
 
         texture.Apply (); //...
 
         meshRenderer.sharedMaterial.mainTexture = texture; //Apply Texture
     }

It is also important to note that I do not receive any error messages.

help.png (23.8 kB)
Comment
Add comment · Show 2
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 Glurth · Feb 08, 2015 at 04:38 PM 0
Share

You will probably want to manually set the UVcoordinates of your mesh manually, to exactly match your texture coordinates.
The UV coordinates, define which point on your texture (x,y) to associate with a given vertex on your mesh(x,y,z).

The most obvious solution I can think of then is scaling the image down to fit the entire board...

but if your texture is only 8x8, how would you scale it DOWN? I would think you would want to scale it UP. (this would be done by changing your loop's x++ and y++ to x+=Tile.resolution and y+=Tile.resolution, just make sure you write the same color to ALL the pixels you are now skipping)

It is also important to note that I do not receive any error messages.

agreed, hehe

avatar image Freak-ey · Feb 08, 2015 at 06:31 PM 0
Share

Thanks for the reply, now trying it.

Also good point with the "Scaling down", I forget that the image is currently 8 * 8 (despite having specifically stated such).

1 Reply

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

Answer by Freak-ey · Feb 08, 2015 at 09:50 PM

I have somehow managed to fix my code by simply making this change:

 int texWidth = size_x * Tile.resolution + 1; //Resolution is currently 1 so the math here is simply 8 * 1
 int texHeight = size_y * Tile.resolution + 1;

from:

 int texWidth = size_x * Tile.resolution; //Resolution is currently 1 so the math here is simply 8 * 1
 int texHeight = size_y * Tile.resolution;

I am unable to see any reason for this but this works... for all resolutions...

Task Accomplished... I guess...

Comment
Add comment · Show 3 · 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 Glurth · Feb 08, 2015 at 10:47 PM 0
Share

This implies to me, that you are somehow setting the WRONG values in your size_x,size_y or Tile.resolution variables. $$anonymous$$ight want to address that or a similar symptom might crop up again elsewhere. (no bashing your solution! I'd just never be satisfied with not knowing WHY it fixed it.)

Edit: oh! the answer is: X tiles, means X+1 vertices. (if the first X verticies are on the left of the tiles, we need one more vertex for the right side of the rightmost tile)

avatar image Freak-ey · Feb 09, 2015 at 04:00 PM 0
Share

I was planning to look into the reason why this works today but I think you just solved it. Thanks again for the help.

avatar image Freak-ey · Feb 09, 2015 at 04:10 PM 0
Share

You were correct, I was passing in the number of tiles rather than the number of vertices. $$anonymous$$y original code works just as well (and is more readable) as a result.

Thanks again for all of the assistance.

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

Apply PNG as texture to 3d object 0 Answers

How to switch the texture being used by the material of the mesh renderer 1 Answer

Different prefabs with same mesh but different textures(materials). 1 Answer

can anybody help with script that takes webcam texture pixel information to deform mesh vertices? 0 Answers

Realtime mesh deformation or texturing 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