I am basically looping through a texture and moving each "sand" pixel down if the pixel underneath is empty. Then for the sand to pile up you need to additionally also check the diagonals and move the pixel accordingly.