top of page

Snow Shader Part 2

This second section of my snow shader shows some of the code i used to determine the level of snow at individual points on the object.

I achieved the solution i wanted by creating a 2D texture at run time which I use as a height map with values ranging from 0.0 to 1.0. Whenever there is a collision at a certain point I convert the collisions into the 2D texture and then add a small amount to this value and that can then be used to represent the colour (how far the object has lerped from the brick texture to the snow texture) and the snow height (how much displacement is required).

The code below shows the workings of the OnParticleCollision function, which triggers whenever a particle collides with the object it is on. We can then access the variable "intersection" to check the exact transform that the particle collision happened at in world coordinates. After we have done this and through the change snow value function i go through a process of converting the intersection transform from world coordinates to a point on the snow texture that I created at run time. The logic behind this is as follows

The intersection point can be converted to local space using a function called "InverseTransformPoint.

This gives us a local transform variable from -1 to 1.

i then add the full value of the axis to the variable e.g. if the width is 1 and i got a local transform of -1 it would now be 0.

I then divide this value by the total size of the object to find where it is as a % of the objects total size.

I finally use this percentage to change the value at the equivalent position on the texture that i created at run time.

Whilst this requires a few steps it allows me to find the correct position and then simply iterate over a region of pixels to change them to a slightly snowier colour fairly easily.


Recent Posts
bottom of page