Browse Source

Update Grid examples;

bjorn 9 months ago
parent
commit
e8b12b3e9b
2 changed files with 16 additions and 6 deletions
  1. 2 2
      examples/Environment/Grid/main.lua
  2. 14 4
      examples/Environment/Grid_(Smooth)/main.lua

+ 2 - 2
examples/Environment/Grid/main.lua

@@ -4,7 +4,7 @@ end
 
 
 function lovr.draw(pass)
 function lovr.draw(pass)
   pass:setColor(.1, .1, .12)
   pass:setColor(.1, .1, .12)
-  pass:plane(0, 0, 0, 25, 25, -math.pi / 2, 1, 0, 0)
+  pass:plane(0, 0, 0, 100, 100, -math.pi / 2, 1, 0, 0)
   pass:setColor(.2, .2, .2)
   pass:setColor(.2, .2, .2)
-  pass:plane(0, 0, 0, 25, 25, -math.pi / 2, 1, 0, 0, 'line', 50, 50)
+  pass:plane(0, 1e-5, 0, 100, 100, -math.pi / 2, 1, 0, 0, 'line', 100, 100)
 end
 end

+ 14 - 4
examples/Environment/Grid_(Smooth)/main.lua

@@ -1,3 +1,9 @@
+-- Grid shader from https://bgolus.medium.com/the-best-darn-grid-shader-yet-727f9278b9d8
+-- Compared to the simpler Grid example, this example:
+-- * Supports custom line widths
+-- * Has world-space lines instead of lines that are always the same thickness
+-- * Has better antialiasing, especially at the horizon
+
 function lovr.load()
 function lovr.load()
   shader = lovr.graphics.newShader([[
   shader = lovr.graphics.newShader([[
     out vec2 scale;
     out vec2 scale;
@@ -8,10 +14,11 @@ function lovr.load()
     }
     }
   ]], [[
   ]], [[
     uniform float lineWidth;
     uniform float lineWidth;
+    uniform vec3 background;
+    uniform vec3 foreground;
 
 
     in vec2 scale;
     in vec2 scale;
 
 
-    // https://bgolus.medium.com/the-best-darn-grid-shader-yet-727f9278b9d8
     vec4 lovrmain() {
     vec4 lovrmain() {
       vec2 uv = (UV - 1.) * scale;
       vec2 uv = (UV - 1.) * scale;
       vec4 uvDDXY = vec4(dFdx(uv), dFdy(uv));
       vec4 uvDDXY = vec4(dFdx(uv), dFdy(uv));
@@ -23,13 +30,16 @@ function lovr.load()
       grid2 *= clamp(lineWidth / drawWidth, 0., 1.);
       grid2 *= clamp(lineWidth / drawWidth, 0., 1.);
       grid2 = mix(grid2, vec2(lineWidth), clamp(uvDeriv * 2. - 1., 0., 1.));
       grid2 = mix(grid2, vec2(lineWidth), clamp(uvDeriv * 2. - 1., 0., 1.));
       float grid = mix(grid2.x, 1., grid2.y);
       float grid = mix(grid2.x, 1., grid2.y);
-      return vec4(Color.rgb * grid, Color.a);
+      vec3 rgb = mix(gammaToLinear(background), gammaToLinear(foreground), grid);
+      return vec4(rgb, 1.);
     }
     }
   ]])
   ]])
 end
 end
 
 
 function lovr.draw(pass)
 function lovr.draw(pass)
   pass:setShader(shader)
   pass:setShader(shader)
-  pass:send('lineWidth', .05)
-  pass:plane(0, 0, 0, 50, 50, -math.pi / 2, 1, 0, 0)
+  pass:send('lineWidth', .005)
+  pass:send('background', { .05, .05, .05 })
+  pass:send('foreground', { .5, .5, .5 })
+  pass:plane(0, 0, 0, 200, 200, -math.pi / 2, 1, 0, 0)
 end
 end