<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by jesseruder on Medium]]></title>
        <description><![CDATA[Stories by jesseruder on Medium]]></description>
        <link>https://medium.com/@jesseruder?source=rss-178ceccb7d10------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*TlTiyWH9AUnheaJrXzZW8w.jpeg</url>
            <title>Stories by jesseruder on Medium</title>
            <link>https://medium.com/@jesseruder?source=rss-178ceccb7d10------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 11:37:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jesseruder/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Tutorial Stream: Intro to Shaders]]></title>
            <link>https://medium.com/castle-archives/tutorial-stream-intro-to-shaders-e042d5c34d92?source=rss-178ceccb7d10------2</link>
            <guid isPermaLink="false">https://medium.com/p/e042d5c34d92</guid>
            <category><![CDATA[lua]]></category>
            <category><![CDATA[love2d]]></category>
            <category><![CDATA[shaders]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[jesseruder]]></dc:creator>
            <pubDate>Wed, 18 Dec 2019 17:34:32 GMT</pubDate>
            <atom:updated>2019-12-18T17:35:35.542Z</atom:updated>
            <content:encoded><![CDATA[<p>Today at 4:30pm PST, I’ll be <a href="https://www.twitch.tv/castleteam/">streaming</a> a tutorial about how to use shaders in Castle.</p><h4>What is a shader?</h4><p>A shader is a small program that runs on your GPU. It’s more limited than a normal computer program but the advantage is that the GPU can run it extremely quickly. When you draw an image on the screen the shader program has two jobs. First, the <strong>vertex shader</strong> decides where on the screen the image should be drawn. Second, the <strong>pixel shader</strong> decides the color and opacity for each pixel in the region that the vertex shader selected. We’ll focus on the pixel shader for this tutorial since it can produce more interesting effects.</p><h4>Trying shaders in Castle</h4><p>I built a small game in Castle for learning about shaders. Open this game in Castle to try: <a href="https://castle.games/+mol8o0/@jesse/shader-tutorial">https://castle.games/+mol8o0/@jesse/shader-tutorial</a></p><p>The source is at <a href="https://github.com/jesseruder/castle-shader-tutorial">https://github.com/jesseruder/castle-shader-tutorial</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BWTmUjZEQUQ0VQ-0fMmGkQ.png" /><figcaption>Here’s an example of using a shader to distort an image</figcaption></figure><p>Once you’re in the game you’ll see a text input. This is the source code for the shader and will get recompiled when you click “Update”. The default shader simply renders the image as you’d normally expect.</p><p>As you can see the source code contains one function name effect. This is the pixel shader that we talked about above, and the return value is used for the color and opacity of a single pixel. Let’s try making the image completely red. Copy this into the text field and click “Update”:</p><pre>vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords)<br>{<br>    return vec4(1.0, 0.0, 0.0, 1.0);<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Uztlka9wYOuu0H2rPPUD_A.png" /></figure><p>This new shader ignores the image texture and just returns a constant color for every pixel. The first three numbers the vec4 represent the RGB color and then last number represents the opacity. If we change the last number to 0 we’ll see that the square disappears and we just see the black background instead.</p><p>Here’s an example of rotating the image using just the pixel shader:</p><pre>vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords)<br>{<br>    vec2 newTexCoords = vec2(texture_coords.y, texture_coords.x);<br>    vec4 texturecolor = Texel(tex, newTexCoords);<br>    return texturecolor * color;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ezDj35hUrNpi0mFGAgOPfw.png" /></figure><p><strong>Sending variables to the shader</strong></p><p>There are a few different ways of getting data into shaders. In the basic shader we get the image texture as an argument to the effect function. But what if we want to send in more data?</p><p>Shaders have a type of variable called a “uniform”. Uniform in this case means that the variable has a uniform value over every invocation of the shader function during a single frame. I added a uniform variable named time to the example that can be enabled by toggling the checkbox. Try selecting the checkbox and then using this shader:</p><pre>uniform highp float time;</pre><pre>vec4 effect(vec4 color, Image tex, vec2 texture_coords, vec2 screen_coords)<br>{<br>    vec2 newTexCoords = vec2(texture_coords.x + sin(time), texture_coords.y);<br>    vec4 texturecolor = Texel(tex, newTexCoords);<br>    return texturecolor * color;<br>}</pre><p>The time variable contains the seconds elapsed since clicking the “Update” button. In this shader we pass that into the sin function, which means that the x coordinate of the texture selected for each pixel changes from [-1 + original x coordinate, 1 + original x coordinate] over time.</p><h4>Next steps</h4><p>There are a lot of different effects you can build with shaders. You can add blur, add CRT scan lines, distort the shape of the screen, or anything else you can imagine! I’ll work through some more examples of different effects on the stream.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e042d5c34d92" width="1" height="1" alt=""><hr><p><a href="https://medium.com/castle-archives/tutorial-stream-intro-to-shaders-e042d5c34d92">Tutorial Stream: Intro to Shaders</a> was originally published in <a href="https://medium.com/castle-archives">Castle Games Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Making a 3D Game in Castle]]></title>
            <link>https://medium.com/castle-archives/making-a-3d-game-in-castle-da628cb7e5e5?source=rss-178ceccb7d10------2</link>
            <guid isPermaLink="false">https://medium.com/p/da628cb7e5e5</guid>
            <category><![CDATA[gamedev]]></category>
            <category><![CDATA[lua]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[jesseruder]]></dc:creator>
            <pubDate>Mon, 28 Oct 2019 16:47:20 GMT</pubDate>
            <atom:updated>2019-10-29T18:11:03.249Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Hello Castle community! This tutorial was accompanied by a live stream. </em><a href="https://www.twitch.tv/videos/501051121"><em>Watch the video here</em></a><em>.</em></p><p><em>We’re doing a whole set of tutorial streams as part of </em><a href="https://castle.games/party"><em>Castle Halloween Party</em></a><em>. See the </em><a href="https://medium.com/castle-archives/tutorial-schedule-for-the-castle-halloween-party-51577f84e555"><em>full stream schedule here</em></a><em>.</em></p><p>This tutorial will cover the basics of making a 3D game in <a href="https://castle.games/">Castle</a>. When we’re done we’ll have a maze game that looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z3TMH52D5t0cTd2NAztggg.png" /><figcaption>A spooky maze game</figcaption></figure><p>You can try the game out <a href="https://castle.games/+fmbvx6/@jesse/basic-3d-tutorial">here</a>.</p><p>Castle games use a library called <a href="https://love2d.org/">LÖVE 2D</a>. As the name implies, LÖVE is focused on 2D games so there aren’t many built in tools for 3D games. We can still build 3D games but we have to do a little more work to get started! This tutorial will give you the tools to get started on your own 3D game in Castle.</p><p>The <a href="https://github.com/castle-games/basic-3d-tutorial">source code</a> has branches corresponding to the end of each step in the tutorial. This project is too large to cover every line of code in the tutorial so I recommend looking at the diffs between branches on Github. I’ll add a link to a relevant code at the end of each section.</p><h3>Rendering in 3D</h3><p>Making a 3D game requires much more boilerplate code than a 2D game. In addition to everything needed for 2D games you also need to store meshes, set up shaders, and do matrix math. Castle and LÖVE don’t provide any of this out of the box so I recommend using an additional library and modifying it as necessary. For this tutorial I started with <a href="https://github.com/groverburger/ss3d">this library</a> and tweaked it a bit to fit my use case.</p><p>You can see the initial commit where I imported this library <a href="https://github.com/castle-games/basic-3d-tutorial/commit/ccdeb196f78bc152b83dec4a7ccb787cf74634d3">here</a>. This step is mainly importing the library and making sure we can draw something simple to the screen. I added a helper function to draw a rectangle given 4 vertices:</p><pre>function rectColor(coords, color, scale)<br>    local model = Engine.newModel({ coords[1], coords[2], coords[4], coords[2], coords[3], coords[4] }, nil, nil, color, { <br>        {&quot;VertexPosition&quot;, &quot;float&quot;, 3}, <br>    }, scale)<br>    table.insert(Scene.modelList, model)<br>    return model<br>end</pre><p>and then call that in love.load():</p><pre>rectColor({<br>  {-1, -1, 1},<br>  {-1, 1, 1},<br>  {1, 1, 1},<br>  {1, -1, 1}<br>}, {1,0,0}, 1.0)</pre><p>which gives us this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l7NPREBx4PT_NIw1zMFtcw.png" /><figcaption>Not very exciting yet but at least we can see something!</figcaption></figure><h3>Adding Lighting</h3><p>One of the big differences between 2D and 3D rendering is lighting. A lot of 2D games don’t need to consider lighting at all but most 3D games would feel flat and lifeless without lighting. For this tutorial we’re going to implement the Phong lighting model. You can take a look at <a href="https://learnopengl.com/Lighting/Basic-Lighting">this guide</a> if you want more details about the math.</p><p>The first thing we need to do is add normal vectors to our rectangles. If a rectangle is facing towards our light source it will be more lit up than if it is facing away from our light source, and we need normals to figure that out.</p><p>For this tutorial I manually added normal vector for each vertex. Here’s how I changed my call to rectColor.</p><pre>-- the 4th and 5th number in each row represent the texture UV coordinates, which are unused for now</pre><pre>rectColor({<br>  {-1, -1, 1,  0,0,  0,0,1},<br>  {-1, 1, 1,   0,0,  0,0,1},<br>  {1, 1, 1,    0,0,  0,0,1},<br>  {1, -1, 1,   0,0,  0,0,1}<br>}, {1,0,0}, 1.0)</pre><p>In engine.newModel you can see that the last 3 numbers represent the normal:</p><pre>format = {<br>  {&quot;VertexPosition&quot;, &quot;float&quot;, 3},<br>  {&quot;VertexTexCoord&quot;, &quot;float&quot;, 2},<br>  {&quot;VertexNormal&quot;, &quot;float&quot;, 3},<br>}</pre><p>Now that we have normals, we just need to implement the Phong lighting model in our shaders. Take a look at the <a href="https://github.com/castle-games/basic-3d-tutorial/compare/step_1_wasd_square...step_2_lighting">diff for this step</a> to see how the shaders changed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VOXnQRhj1iVvNNR9hgIHJg.png" /><figcaption>The cube looks better now!</figcaption></figure><h3>Adding Textures</h3><p>Now we actually get to start designing the game! I decided to make a spooky maze game so I found a wall texture, a ground texture, and a skybox texture. The engine I imported when I started the project already did a good job supporting textures, so I just had to modify my rectColor function to take a texture instead of a color and then pass UV coordinates along with my vertex positions and normal.</p><pre>wallImage = love.graphics.newImage(&quot;assets/wall.png&quot;)</pre><pre>-- 1-3 are position, 4-5 are UV coordinates, 6-8 are the normal<br>rect({<br>  {-1, -1, 1,  0,1,  0,0,1},<br>  {-1, 1, 1,   0,0,  0,0,1},<br>  {1, 1, 1,    1,0,  0,0,1},<br>  {1, -1, 1,   1,1,  0,0,1}<br>}, wallImage, 1.0)</pre><p>After I added the skybox I decided that the skybox looked wrong with Phong lighting so I modified my fragment shader so that the skybox would only be lit with ambient light. I did this by setting the skybox normals to {0, 0, 0} and then only using Phong lighting in the fragment shader if the length of the normal is greater than 0.</p><p>You can see all the changes for this step <a href="https://github.com/castle-games/basic-3d-tutorial/compare/step_2_lighting...step_3_textures">here</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZGNoumXG-js2B77U44ZCIQ.png" /><figcaption>Getting there…</figcaption></figure><h3>Creating a Map</h3><p>Now that we can render blocks and make them look good, it’s time to actually make this a game! There are many different ways you could handle map data. For this example I decided to use a string where each character represents a tile. Here’s an example of that:</p><pre>-- x is a wall, s is start, e is end</pre><pre>Map =  [[<br>        e<br>xxxxxxxx xxx<br>xx x       x<br>xx xx  xxx x<br>xxxxxx   xxx<br>x    xxx   x<br>x xxxx   x x<br>x      x   x<br>xx xxxxxxxxx<br>  s<br>]]</pre><p>Once I decided on that format I added this code to parse the string:</p><pre>local z = 0</pre><pre>for line in Map:gmatch(&quot;[^\r\n]+&quot;) do<br>  for x = 0, string.len(line) do<br>    local char = string.sub(line, x, x)</pre><pre>    if char == &#39;x&#39; then<br>      box(x, z)<br>    elseif char == &#39;s&#39; then<br>      Engine.camera.pos.x = x + 0.5<br>      Engine.camera.pos.z = z + 0.5<br>    end</pre><pre>    -- no end yet<br>  end<br>  z = z + 1<br>end</pre><p>I also changed my box function to take x and z coordinates. I used a setTransform function provided by the 3D engine to move the boxes around.</p><pre>local m1 = rect({<br>  {0, 0, 1,  0,1,  0,0,1},<br>  {0, 1, 1,  0,0,  0,0,1},<br>  {1, 1, 1,  1,0,  0,0,1},<br>  {1, 0, 1,  1,1,  0,0,1}<br>}, wallImage, 1.0)</pre><pre>... similar code for the other sides</pre><pre>local models = {m1, m2, m3, m4}</pre><pre>for k,v in pairs(models) do<br>  v:setTransform({x, 0, z})<br>end</pre><p>Now we have a full map! See <a href="https://github.com/castle-games/basic-3d-tutorial/compare/step_3_textures...step_4_map">the code for this step</a>.</p><h3>Making it a Real Game</h3><p>At this point you can move around the map and explore the maze, but you can also go straight through walls and there’s no way to actually win. We’ll start off by preventing you from walking through walls.</p><p>Since everything is laid out in a grid we don’t have to do any complicated collision detection. Here’s the code for checking if pos is in the (x, z) cell:</p><pre>function isInSquare(pos, x, z)<br>  -- this gives us some buffer so that the camera doesn&#39;t go partially into a wall<br>  local d = 0.1</pre><pre>  local camX = pos.x<br>  local camZ = pos.z</pre><pre>  return camX &gt;= x - d and camX &lt;= x + 1 + d and camZ &gt;= z - d and camZ &lt;= z + 1 + d<br>end</pre><p>Now that we have that function, we just need to keep track of which cells have walls and then check all of those cells before we move. I made a new table and added table.insert(Blocks, {x, z}) to the box function, then added some an extra check before moving the camera:</p><pre>local canMove = true</pre><pre>for k,v in pairs(Blocks) do<br>  if isInSquare(newPos, v[1], v[2]) then<br>    canMove = false<br>  end<br>end</pre><p>Now that you can’t move through walls, all that’s left is to make the game winnable. I added another isInSquare call at the end of love.update() which tests if you are in the ending square:</p><pre>if isInSquare(Engine.camera.pos, EndPosition[1], EndPosition[2]) then<br>  WonGame = true<br>end</pre><p>If WonGame is true then we show a text overlay and let you restart the game by pressing space.</p><p>Here is the <a href="https://github.com/castle-games/basic-3d-tutorial/compare/step_4_map...step_5_gameplay">diff for this step</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5hrTFEIL5qJ9HCgKCtt2mA.png" /><figcaption>It was a pretty easy maze</figcaption></figure><h3>Future Work</h3><p>You could extend this game by adding visual effects (fog? ghosts? rain?) or by adding multiple levels. You could even add a level editor using the <a href="https://castle.games/documentation/reference/post-api-reference">Post API</a>!</p><p>I hope you enjoyed this tutorial and are able to make something fun. Reach out to me on Castle chat or <a href="https://discord.gg/4C7yEEC">Discord</a> if you have any questions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=da628cb7e5e5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/castle-archives/making-a-3d-game-in-castle-da628cb7e5e5">Making a 3D Game in Castle</a> was originally published in <a href="https://medium.com/castle-archives">Castle Games Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>