How to make dynamic connecting textures

This looks like a job for Superman Connected Textures Mod (CTM), which is included in OptiFine. If you haven't already, go ahead and download the latest version of OptiFine before you continue reading this.

In order for this mod to work, you'll have to create three .properties files; one for the side texture, another one for the top texture and one for the bottom texture. The names of the files doesn't really matter, but it's good to know what the different files are used for.

Edited for 1.15.2:

smooth_stone_slab_side.properties

matchBlocks=smooth_stone_slab smooth_stone
method=horizontal
tiles=100-103
connect=block
faces=sides

smooth_stone_slab_top.properties

matchBlocks=smooth_stone_slab smooth_stone
method=ctm_compact
tiles=104-108
faces=top

smooth_stone_slab_bottom.properties

matchBlocks=smooth_stone_slab smooth_stone
method=ctm_compact
tiles=104-108
faces=bottom

After that we need to add textures for the connecting slabs but since I don't know how many images I can add to this answer, I've decided to merge all textures as one image so you'll have to split them up in nine 16×16px PNG images yourself. Make sure to name them according to the table below in order to make everything work out.

100.png | 101.png | 102.png

103.png | 104.png | 105.png

106.png | 107.png | 108.png

Sprite of all stone slab textures

Now we have to create a resource pack and add some folders where all these files and images should be. In the resource pack, open the assets and minecraft folders, then you have to create a new folder called optifine. Inside this new folder, create another folder named ctm. And inside this folder, you must create yet another folder which you name default.

Add the .properties files and images to the default folder. If done correctly, you should have this folder structure:

| assets/
| - minecraft/
| -- optifine/
| --- ctm/
| ---- default/

When you start Minecraft, go to Options -> Video Settings -> Quality and set Connected Textures to either Fast or Fancy and apply your newly created resource pack. When you are done, it should look like this:

Image depicting the wonders of CTM

Have fun!

Edit: Added a .properties file for the bottom texture of the slabs.
Edit 2: Added the metadata parameter for the files so the texture is only applied to top and bottom stone slabs.


matchblocks was changed somewhat recently in optifine -- instead of matchBlocks=44 it should be matchBlocks=stone_slab or alternatively minecraft:stone_slab see this demo file for details https://github.com/sp614x/optifine/blob/master/OptiFineDoc/doc/ctm.properties