0:02 for years game developers have created
0:04 3D models in a program like blender
0:06 before loading them into their games but
0:09 we can skip this step completely in this
0:11 video I'll show a new approach that I
0:13 use to render plants and terrain without
0:16 importing any models first let's look at
0:19 the default blender Cube It's composed
0:21 of a bunch of triangles which are saved
0:24 to a file this data is loaded onto the
0:26 GPU and render to the screen using a
0:28 Shader every vertex that the Shader
0:31 processes has a unique ID called GL
0:34 vertex ID this triangle has three
0:39 vertices with ID 0 1 and 2 we can use
0:41 this ID to modify the position of each
0:44 vertex the Shader on the right has two
0:47 lines commented out when uncommented the
0:48 top vertex in this triangle will move
0:52 from left to right this ID can also be
0:55 used to create geometry from scratch
0:57 first we'll set the position of each
1:00 vertex to zero then we'll position each
1:07 ID we are no longer using model data
1:09 from blender and we have complete
1:11 control over the position of each
1:13 vertex I use the same approach to render
1:16 terrain and I'll cover this in another
1:19 video now let's create a fern by adding
1:21 more vertices our Shader code is pretty
1:24 messy so let's replace it with some
1:26 maths vertices now mooving groups of two
1:29 along the x- AIS an odd and even
1:31 numbered vertic SE move up and down
1:33 along the Zed
1:35 AIS now let's use this geometry
1:38 generating technique to create some
1:40 plants we want our phone to start in an
1:42 upright position so rather than
1:44 increasing its length it should increase
1:46 vertically we also want our Fern to be
1:49 curved I'll demonstrate how I achieve
1:52 this using pitch and your maths think of
1:54 a firstperson game you've played moving
1:56 your mouse up and down controls your
1:58 character's pitch and moving your mouse
2:00 left and right controls its your
2:02 pitch and your values can then be
2:04 converted to a 3D direction using S and cos
2:05 cos
2:08 functions let's do this in our Shader
2:10 first we need to change the way we use
2:13 GL vertex ID it's currently controlling
2:15 the height of each vertex but we want it
2:17 to control distance which is how far
2:19 away the vertex is from the bottom of
2:22 the mesh the pitch value controls which
2:24 direction to move and the distance value
2:27 controls how far to move to bend the
2:29 mesh we will increase the pitch value as
2:31 the distance increases this causes the
2:33 top of the mesh to bend further than the
2:35 base of the mesh these two pitch and
2:37 bend strength values can now be
2:39 customized to bend this mesh in different
2:40 different
2:42 ways this looks very blocky but when we
2:45 put a texture over the top it disguises
2:47 the low poly geometry we now have a leaf
2:49 that we can use to construct larger
2:52 vegetation like a fern but we're missing
2:53 the ability to rotate it
2:55 horizontally with the leaf standing
2:57 upright let's forget about pitch for now
3:00 and just look at your similar to before
3:03 Geo vertex ID will no longer modify the
3:05 x value directly instead it will control
3:08 the radius of a small
3:11 circle when we Bend this Leaf its tip
3:12 moves outwards and has to move in a
3:15 larger Circle the orange code controls
3:17 the width of the leaf and the blue code
3:20 moves vertices perpendicular to the WID
3:22 we now have a leaf that we can bend and
3:25 rotate it's time to add more leaves but
3:27 when I add more vertices the leaf just
3:30 keeps getting longer we need a way to
3:31 start a new
3:34 Leaf in the Shader I'll increase the Y
3:38 of the leaf by 90° every 10 vertices
3:40 this worked but an Uninvited third Leaf
3:42 snuck its way in this is because the top
3:44 of the first Leaf is now connected to
3:46 the bottom of the second Leaf therefore
3:48 creating a new Leaf let's look at a
3:51 clearer example we need to get rid of
3:52 the two red triangles that are
3:54 connecting the mesh on the left with the
3:56 mesh on the right one way to do this is
3:58 to render each mesh separately but for a
4:01 fern containing 100 leaves that involves
4:03 sending 100 separate draw calls to the
4:06 GPU which is not optimal instead we can
4:09 make the triangles invisible I'll move
4:11 the bottom right vertex towards the one
4:13 on the
4:16 left the triangle completely
4:18 disappears to use this in our Leaf mesh
4:20 we need to add one more triangle at the
4:22 start and one more triangle at the end
4:24 then the trick is to make these two triangles
4:25 triangles
4:27 invisible if we repeat this with our
4:29 second mesh we can see the connecting
4:31 Tri triangles
4:33 disappear we've solved our connecting
4:35 triangle problem and can now render more
4:38 leaves finally we need to animate them I
4:40 want each Leaf to move up and down in a
4:43 wind-like pattern we can achieve this by
4:45 modifying the pitch of the leaf based on
4:48 a sine wave this looks weird for three
4:51 reasons wind doesn't work like this
4:53 every leaf is moving in unison and every
4:55 leaf is
4:58 rigid let's go back to one leaf and make
4:59 this wind animation look more natur
5:02 natural a single sine wave is
5:04 predictable so let's add and subtract
5:10 intervals the leaf is still rigid so
5:12 I'll subtract the current time by the
5:14 distance variable this makes the tip of
5:16 the leaf lag slightly behind the base of the
5:18 the
5:21 leaf let's add the other leaves
5:23 back they are still moving in unison so
5:26 we'll offset Time Again by The Leaf ID
5:29 the leaf ID is unique per Leaf similar
5:33 to to how GL vertex ID is unique per
5:36 vertex the very last step is to enable
5:38 Shadows I hope you've enjoyed watching
5:40 in my next Dev Vlog I'll show you how I
5:44 use GL vertex ID to render terrain I'll
5:46 leave you with a fancy Fone montage and