Share a Shader!

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Ratchet
Citizen
Posts: 67
Joined: Mon Apr 08, 2013 10:32 am

Bloom for 0.9.1?

Post by Ratchet » Mon Jun 02, 2014 5:03 am

Somebody got a working bloom shader for 0.9.1? I can't get the stuff to work that's around here :o:

Especially this does just nothing :?
macOS 10.14 Mojave | LÖVE 11.2

User avatar
CaptainMaelstrom
Party member
Posts: 157
Joined: Sat Jan 05, 2013 10:38 pm

Re: Share a Shader!

Post by CaptainMaelstrom » Tue Jun 03, 2014 6:16 pm

This should work. Credit to BlackBulletIV.

Code: Select all

shaders.bloom = lg.newShader[[
   //BlackBulletIV
   extern vec2 size = vec2(20,20);
   extern int samples = 2; // pixels per axis; higher = bigger glow, worse performance
   extern float quality = .5; // lower = smaller glow, better quality

   vec4 effect(vec4 color, Image tex, vec2 tc, vec2 sc)
   {
      vec4 src = Texel(tex, tc);
      vec4 sum = vec4(0);
      int diff = (samples - 1) / 2;
      vec2 sizeFactor = vec2(1) / size * quality;

      for (int x = -diff; x <= diff; x++)
      {
         for (int y = -diff; y <= diff; y++)
         {
            vec2 offset = vec2(x, y) * sizeFactor;
            sum += Texel(tex, tc + offset);
         }
      }

   return ((sum / (samples * samples)) + src) * color;
   }
]]

User avatar
Ratchet
Citizen
Posts: 67
Joined: Mon Apr 08, 2013 10:32 am

Re: Share a Shader!

Post by Ratchet » Wed Jun 04, 2014 1:08 pm

My whole computer freezes when I use it :huh:
Yes, my graphic card does support shaders.
macOS 10.14 Mojave | LÖVE 11.2

User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Re: Share a Shader!

Post by Zilarrezko » Sat Jun 07, 2014 9:16 am

I'm having trouble finding good rich in-depth tutorials for shaders. You could say they're... a Shady Business.

Germanunkol
Party member
Posts: 710
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Re: Share a Shader!

Post by Germanunkol » Sat Jun 07, 2014 1:50 pm

It is difficult, getting started. But download some of the examples in this thread and learn from them (I think there was a few simpler image-manipulation demos towars the beginning).
I assume you've read the introduction to shaders?

Apart from that, go read some glsl (fragment) shader tutorials. Apart from the main function call's name and the "uniform" keyword, they're pretty much exactly what Löve shaders do.
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de

User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: Share a Shader!

Post by substitute541 » Tue Jun 10, 2014 4:45 am

Created a simple terrain generator along with lighting.
terrain.love
(1.97 KiB) Downloaded 405 times
FPS 990.png
FPS 990.png (587.83 KiB) Viewed 3090 times
And here's the code for the normal map generator and shader.

Code: Select all

function drawNormals(data)
	local normals = love.image.newImageData(800, 600)
	local sqrt = math.sqrt
	local floor = math.floor
	local a = 50
	for y=1, 600-2 do
		for x=1, 800-2 do
			local s1, s2 = data:getPixel(x-1, y)
			local s3, s4 = data:getPixel(x+1, y)
			local v1 = s1*s2/65025 -- these multiplications are basically calculating
			local v2 = s3*s4/65025 -- the final height value of my heightmap.
			s1, s2 = data:getPixel(x, y-1)
			s3, s4 = data:getPixel(x, y+1)
			local v3 = s1*s2/65025 -- the height values are stored in two channels
			local v4 = s3*s4/65025 -- first is the (red) details of the terrain,
			local s5, s6 = data:getPixel(x, y) -- next is the (green) overall shape, including mountains.

			local xgrad = a*(v1-v2) -- calculate the gradients by finding the difference between
			local ygrad = a*(v3-v4) -- the two height valaues

			local d = sqrt(xgrad*xgrad+ygrad*ygrad+1) -- normalize it
			xgrad = xgrad/d
			ygrad = ygrad/d
			local z = 1/d

			normals:setPixel(x, y, floor(xgrad*128+128), floor(ygrad*128+128), z*255, 255) -- store the normals
		end
	end
	return normals
end

Code: Select all

extern vec2 screen = vec2(800.0f, 600.0f);
extern vec2 sundir = vec2(0.0f, 0.0f);
extern Image colormap;

const float sunheight = 1.0f;
const vec3 terrain = vec3(0.0f, 0.7f, 0.0f);
const vec3 ambient = vec3(0.01f, 0.0f, 0.015f);

// assume color is green for now

vec4 effect(vec4 color, Image texture, vec2 texcoords, vec2 pixcoords) {
	// source color
	vec4 source = Texel(texture, texcoords);


	vec3 sunvector = normalize(vec3(sundir, -sunheight));
	vec3 normal = normalize(source.rgb-vec3(0.5f, 0.5f, 0.5f));
	vec4 data = Texel(colormap, texcoords);
	float height = (data.r*data.g);

	float l = dot(normal, -sunvector)*0.5f +0.5f;
	l *= 1/((sunheight-height)*0.7f+1);


	vec4 texcolor = vec4(terrain*l+ambient, 1.0f);
	return texcolor * color;
}
Currently designing themes for WordPress.

Sometimes lurks around the forum.

User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Re: Share a Shader!

Post by Zilarrezko » Thu Jun 19, 2014 10:10 am

Alright... So I'm having problems with transferring openGL's GLSL globals like gl_NormalMatrix and gl_Normal.

It's really confusing having things renamed to... "Be made easier for coding" when it's making it more of a pain to learn the stuff. Although if I ever get GLSL Love enchanced version down, I'm sure I would appreciate having the globals not me huge names with a bunch of underscores and the like.

It would be a lot nicer if Love had on it's wiki page with global variables the variables' equivalent to GLSL variables. Or if someone would make a video tutorial on Love's version of GLSL. I'd be willing to do it, although of course I'm have variable problems. Which is one of the last things I want.

Sorry if I'm being blunt. Just frustrated when I can't figure things out. :?

User avatar
Positive07
Party member
Posts: 1006
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Share a Shader!

Post by Positive07 » Fri Jun 20, 2014 7:34 am

[wiki]Shader Variables[/wiki] and Shader Language... this?
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(Github.com/Positive07)

User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Re: Share a Shader!

Post by Zilarrezko » Fri Jun 20, 2014 9:36 am

Positive07 wrote:[wiki]Shader Variables[/wiki] and Shader Language... this?
Nah I've already looked through those. I mean something that shows something like what Shader Language does with all the "sampler2D is image", except for all of love's shader variables. I would make a better example but unfortunatly if I knew which GLSL variables corresponded to love's variables then I wouldn't be asking this question.

User avatar
Tanner
Party member
Posts: 166
Joined: Tue Apr 10, 2012 1:51 am

Re: Share a Shader!

Post by Tanner » Fri Jun 20, 2014 2:19 pm

Love's default shaders are actually open-source! You can just look at the whole shader program yourself here: https://bitbucket.org/rude/love/src/585 ... ua#cl-1294 :)

Post Reply

Who is online

Users browsing this forum: slime and 36 guests