Monday, 4 August 2014

Map a texture to world coordinates. Part 1.


Today we'll start looking at mapping a texture UVs to the world coordinates.
It's taking quite long to write the whole thing up so I'll keep the examples for later posts.

Mapping to world.

We've looked before at remapping a texture with another texture. The coordinates of a texture have vertical and horizontal data, which you can remap to whatever you want.
‘Whatever you want’ can be the world coordinates.
We're going to use the world position, but we need to divide it to start with. The values not quite usable as they are. The more you divide the world position, the larger the texture will be. It's sort of like zooming in your world: the texture appears larger.

The result is just like setting your texture tiling in a texture coordinates node.

Dividing the world by 500:
 
Dividing the world by 100:

Unlike your world, a texture has no depth, it only has two axis.
Next thing we need to know is the way your material will be oriented in the world so as to map the texture to the correct two axis. Imagine that you're facing a plane with your material on it. Which axis do you see pointing away ?
 
In our example we want to map it to the ground. If we look down from above, we see the R and G arrows pointing away. The z channel will not be visible.
 
So we use a component mask to keep only the R and G channel (x and y).


 Bear in mind that the texture will stretch along the third axis, the z axis in our case.

<teaser>
Would we be to use lerps and map the texture several times along different axis we could start to get a 3D feel. (at a cost obviously)
</teaser>

That's it for today, several examples will come soon.

Thursday, 10 July 2014

Keep it local.

I'm preparing a future post which is growing by the minute. I'll probably have to break it down a bit to keep it clear. Here's a bit I'll refer to in the (near) future.

I mentioned the World Position minus Object World position to make things local before, but I didn't explain it properly. 


 The WorldPosition gives you the world position value per pixel, whereas the ObjectWorldPosition gives you the world position of your object, just the value at which its center is.
When you subtract the ObjectWorldPosition to the WorldPosition it means that every pixel will have a fixed value wherever you place it in the map. The value becomes relative to the center.
 
Imagine that this quad is in the world. We check the value of the same pixel at different arbitrary locations. WorldPosition - ObjectWorldPosition always returns the same value.
(Remember, if you're thinking of a formula don't just keep it abstract. Give it some numbers to verify that the result makes sense.)

This set up is useful if you want to be able to move your object around and you want the material to stick to it, it is also good for you to ‘tame your value.
Sometimes you can be testing a feature in your test map, but as you implement it in the game, some things will go crazy. Depending on where you are in a map, the output of the WorldPosition could range from 1 to tens of thousands. Would you be to use the WorldPosition as, say a multiplier, you can see where the troubles start.

We had that in DmC 5 with the summoner. She's a flying enemy so the character team put some vertex offset in her hair to give it a floating motion. To do so they used the world position to drive some sine waves. The first tests were looking good in the test map but her hair first went all over the place in gameplay. The fix was just a matter of subtracting the ObjectWorldPosition to the WorldPosition.

Monday, 16 June 2014

Stretched Mask.


When you pan a warped texture, it can be annoying to see the patterns repeated too often.

If you're going to pan it only or mostly in one axis, what you can do is create a long rectangular texture and use a small tiling value on the long axis in your material (0.1 in the example below). As you create the texture, you need to keep in mind the ratio it will be used at.

The top texture is the original with unmodified UVs.
 


You could for instance use this method to create a wobbly mask that will bring an illusion of flowing liquid.




Wednesday, 21 May 2014

Masking a volume.

Today we'll look at defining a 3d mask.

I originally made this set up for a quick and dirty rain test. The rain was one big gpu particles emitter, no collisions, no nothing, so it was raining indoors.
The dirty approach to solve this issue was to make the rain transparent in the indoors volume.

The idea is to define the range which will remain white in each of the x, y and z axis and then multiply everything together to define the volume.

Defining the range per axis.

We're using a set up I've already described so I'll be quick there. (Told you I liked to use it all over the place.)
 
 For each axis, we're remapping a range of values to a gradient from 0 to 1, then we will also ceil it and clamp the result.


We do it for the min, and we end up with 0 below the minimum and 1 until the infinite.
We do the same thing again but we invert the values (min becomes max and vice versa) so we end up with 0 above the max and an infinite 1 in the negative direction.
Finally, we multiply the min and the max.

Now our range is 1 and any value above or below is 0.

Doing it the proper way.

That was the simple way of explaining it and the way I actually created the original material, but while I recreated it for this post I realised the set up could be simplified a lot with a decent saving on instructions count (34 instead of 43). The idea is the same but we're dealing with all 3 channels at the same time for the mins on the one hand and on the other hand for the maxs. After we've been through the value (world position in this case) minus min / divided by max minus min / ceil / constant clamp, we separate the 3 channels (with a component mask) and multiply them by one another.

We end up for the mins with 0 below the min and an infinite 1 along the positive of each axis, and for the maxs with 0 above the max and an infinite 1 along the negative of each axis. 
Multiply mins and maxs together and you've got your volume.


In the following video, I'm moving the emitter around so you can see that the coordinates are in world space. (I applied that material to a bunch of simple sprites.)




We could also make them local space if we'd fancy that, by simply subtracting the object position to the world position.



Tuesday, 29 April 2014

Scale a texture from the centre.


Nice and easy today.
We're starting off with this texture, which is clamped. 


When you scale it down (= tile it at a value greater than 1) it moves to the upper left corner. That's because 0,0 is in this corner.


With the following setup, you'll scale the texture from the visual centre of the material:


What is happening exactly?

First we subtract 0.5,0.5 to our texture coordinates. We're shifting the texture and bringing 0,0 to the centre.

Then we scale it.

Finally we add 0.5, 0.5 to bring the texture back to the original position. Simple :)

This was used in Enslaved in the dragonfly scan post process. The values subtracted/added back were exposed and so was the scaling factor, so that the glowy reticle could be animated in matinee.

A screenshot won't tell you much about it but you can see in it in my enslaved showreel from 2:00. It's super short but I couldn't find any specific youtube video. Well, gotta play Enslaved to see it at its best ;)


Now, that was a bit short so let's talk some more.
When reducing the size of a clamped texture a lot, you can come across mipmap artifacts.

One fix is the Preserve Border option.
The tooltip says: ‘If true, the color border pixels are preserved by mipmap generation. One flag per color channel.’

Now the result is much better, but we can still see some artifacts.

Here is the texture I created. The last two pixels are plain black but pixel 3 and 4 start to be white. The dxt compression is done with blocks of 4 pixels so since I've got both black and white in a 4 pixel group, my mipmaps get messed up.

I reimported the texture and made sure I've got a clean 4 pixels border. Even with preserve border off, I don't get any artifacts.

They will appear at a lower mipmap though, but won't ever appear with a correct border and the preserve border option.

Maya tip: clear particles initial state.

Today I wanted to clear an initial state on particles in maya but didn't know how to. I found my answer here:
http://patrickvfx.blogspot.sg/2011/12/maya-particles-initial-state-method-3.html#.U1_UhVecx8E

Here's the MEL command you need to enter:
clearParticleStartState <particleObjectName>

which typically will be
clearParticleStartState particle1

(and… I've got my copy/paste ready to use.)

Monday, 14 April 2014

Super simple manual flipbook.

Say you want to manually animate a flipbook in matinee.
Here's a very simplified setup for it. The downside is: is has to be one single row but you can modify the amount of columns as you wish.

There could be less instructions but the idea is to make it easy to instance. In the material instance, you just have to modify the FramesAmount parameter and you can animate the FrameIndex parameter in matinee with everything working fine.


What's happening there?
You simple tile and shift your texture.

Tiling.

In this case, one tile is a forth of the texture size. So you divide 1 by the amount of horizontal frames in your texture and multiply only the U of your texture coordinates.
You've got the correct tile size.

Shifting.

To display the correct frame at the right moment, you'll just need to move the texture horizontally.
The amount by which you need to shift the texture to reach the next frame is one nth of your texture, n being th amount of horizontal frames. (4 in this case)
The FrameIndex value (animated from matinee) multiplies this to find how many times you need to shift it.

The floor node is there to ensure you only display full frames.
Since the frame index is floored, your index will be starting at 0, remember this when you control the value in matinee. To animate a four-frames texture, the value will have to interpolate between 0 and 3. (If your texture is set to wrap, a FrameIndex of four will get you back to displaying frame 0.)

Simple.
Not super clean or flexible but definitely super simple.