- Home /
Spread material over multiple objects or cubes
Hello there,
I am making a game with cubes that react to physics and can be destroyed. I need to spread a material over these cubes so when the game starts they form an image. Like bricks in a wall with graffiti over the top.
I have made some progress in finding the SetTextureOffset and Scale but I can't work out how to offset each texture by the right amount to make up my seamless image.
renderer.material.SetTextureScale("_MainTex", Vector2 (scaleX,scaleY));
renderer.material.SetTextureOffset("_MainTex", Vector2(transform.position.x/5, transform.position.y));
I have this which sets the material offset based on it's position in the world. I will now go in and manually try to work out each offset value but there must be an easier way.
Thanks for your help
Will
isnt the size of a texture from 0 to 1? like 0% to 100% ? if so, I would just divide the number of "tiles" per row and column with width and height and use this tilesize for offset.
That is pretty genius. I think that will work.
Unity requires SQUARE textures.. to look good right?? $$anonymous$$y wall is rectangular so I need to do some more calculations to allow for the borders top and bottom but that should be fine. I've set up a nice grid texture to make testing easier.
Thanks
Will
Answer by BerggreenDK · Jul 20, 2011 at 10:35 AM
Ups! forgot to post my idea as an answer:
"Isnt the size of a texture from 0 to 1? like 0% to 100% ? if so, I would just divide the number of "tiles" per row and column with width and height and use this tilesize for offset."
Please use COMMENT on this answer @Wiplash, if you need further help.
Answers are meant to be answers to the question only. One answer can have many comments.
Yes, that was the solution! Thanks! It looks like this: int numberOfSegments = 15; $$anonymous$$eshRenderer mr = roadTile.GetComponent<$$anonymous$$eshRenderer>(); mr.material.SetTextureScale("$$anonymous$$ainTex", new Vector2(1f / numberOfSegments, 1f / numberOfSegments)); mr.material.SetTextureOffset("$$anonymous$$ainTex", new Vector2(x 1f / numberOfSegments, y 1f / numberOfSegments)); mr.material.mainTexture = texture;