0

I am tinkering around with cubes trying to build variations of 'block types' (in an effort to get more familiar with Unity's abilities, shaders, editor tools etc).

I have a generic cube:

enter image description here

That I want to add a material/shader.. which I have done (no problem there):

enter image description here

Which looks well enough (for my purposes) when it's just one block, but when I stick them altogether, I don't like the effect; you can see the individual boxes and the shader (which you can't see in the still image) is actually animated water, so when it's animating it looks ... pretty ugly.

(Bad/undesired) enter image description here

I am trying to STRETCH or share the shader/material across all the selected blocks. See the below example (in this case, I have taken a SINGLE block and stretched it, but that's not keeping with the spirit of having individual blocks, so also not what I want).

(better/more desired)

enter image description here

I have thought the following may help, but they all seem overly complicated (aka I think I'm going about it incorrectly)

  1. Have the individual blocks, but stretch a single plane across them and then apply the material.
  2. I have found examples of programmatically joining meshes, and then apply the material/shader to the single object.
  3. Take a single block and stretch it to the dimensions needed.
  4. Maybe (not sure if I can), but have a plane with the water material applied to it and use the blocks as masks to only display water for those blocks? Not sure how that works...

In the end I am hoping to have the following:

  1. Individual blocks (so I can interact with them.
  2. Shader animations/colors are shared across the shared/connected blocks.
  3. It won't always be a 2x3 grid... it could be diagonal, or contain odd shapes of connected blocks...

(this is all in EDITOR mode).

Any thoughts on how I might approach this?

Community
  • 1
  • 1
Justin Carroll
  • 1,362
  • 1
  • 13
  • 37
  • You could re-code the fragment shader or surface shader to be able to treat its surface as being only a fraction of its size, but exactly how to do this depends on the specific shader. – Ruzihm Mar 20 '20 at 20:44
  • Interesting... so each "cube" would get a 'fraction' of the overall shader, but then other cubes would take their (separate) portion of the shader making it LOOK like it's one material/shader but really it's X*Y unique materials/shaders across all blocks? I am using the new universal shaders (LWRP) with PBR. Still VERY new to shaders... what should I google/lookup given your suggestion + what I am using? – Justin Carroll Mar 20 '20 at 21:16
  • Awesome. Thank you. I can upvote your comments, and I will def research it, but if you want to post an answer I am +1 for giving you credit for putting me on the right path. – Justin Carroll Mar 20 '20 at 21:33
  • I rewrote my comments below, fixing some mistakes and including better format. Happy to help. – Ruzihm Mar 20 '20 at 21:41

1 Answers1

1

Phrases you could try searching are "converting from world space to uv space", "transforming uv coordinates", "uv math". UV is the name for coordinates in textures that a shader samples from, and if you take already existing shader code, you can do interesting things by changing the UV(s) it uses. One of those things is letting you "stretch" it.

In your 2x3 cube example you could tell each cube to treat its U value as going from 0 to 0.5 or 0.5 to 1 and the V as going from 0 to 0.33 or 0.33 to 0.67 or 0.67 to 1 depending on where it is instead of each one going from 0 to 1. You could do this by having a property on the shader to tell it where to start the uv (a) and where to end its uv (b), and you lerp from (0,0) - (1,1) to a - b.

My answer to a different question uses some similar logic to that by comparing the world position of the pixel vs a range of world positions to get a UV. The relevant shader code is:

fixed4 colorizedMapUV = (IN.worldPos.xz-_WorldSpaceRange.xy) 
        / (_WorldSpaceRange.zw-_WorldSpaceRange.xy); 

Another option is to only look at the world position, and completely disregard a notion of where the "corners" of the uv should be. A method called "triplanar mapping" might guide you to a solution that does this

Ruzihm
  • 19,749
  • 5
  • 36
  • 48