SHADER模块整理,初始化代码移至main

This commit is contained in:
MrZ626
2020-11-16 09:21:22 +08:00
parent ba486b5de7
commit f1f2eb583f
19 changed files with 55 additions and 59 deletions

90
Zframework/light/init.lua Normal file
View File

@@ -0,0 +1,90 @@
--LIGHT MODULE (Optimized by MrZ, Original on github/love2d community/simple-love-lights)
--Heavily based on mattdesl's libGDX implementation:
--https://github.com/mattdesl/lwjgl-basics/wiki/2D-Pixel-Perfect-Shadows
local gc=love.graphics
local clear,translate=gc.clear,gc.translate
local setCanvas,setShader=gc.setCanvas,gc.setShader
local render=gc.draw
local shadowMapShader=gc.newShader("Zframework/light/shadowMap.glsl")--Shader for caculating the 1D shadow map.
local lightRenderShader=gc.newShader("Zframework/light/lightRender.glsl")--Shader for rendering blurred lights and shadows.
local Lights={}--Lightsource objects
local function move(L,x,y)
L.x,L.y=x,y
end
local function setPow(L,pow)
L.size=pow
end
local function destroy(L)
L.blackCanvas:release()
L.shadowCanvas:release()
L.renderCanvas:release()
end
local function draw(L)
--Initialization
local r,g,b,a=love.graphics.getColor()
setCanvas(L.blackCanvas)clear()
setCanvas(L.shadowCanvas)clear()
setCanvas(L.renderCanvas)clear()
lightRenderShader:send("xresolution",L.size)
shadowMapShader:send("yresolution",L.size)
--Get up-left of light
local X=L.x-L.size*.5
local Y=L.y-L.size*.5
--Render solid
translate(-X,-Y)
L.blackCanvas:renderTo(L.blackFn)
translate(X,Y)
--Render shade canvas by solid
setShader(shadowMapShader)
setCanvas(L.shadowCanvas)
render(L.blackCanvas)
--Render light canvas by shade
setShader(lightRenderShader)
setCanvas(L.renderCanvas)
render(L.shadowCanvas,0,0,0,1,L.size)
--Ready to final render
setShader()setCanvas()gc.setBlendMode("add")
--Render to screes
gc.setColor(r,g,b,a)
render(L.renderCanvas,X,Y+L.size,0,1,-1)
--Reset
gc.setBlendMode("alpha")
end
local LIGHT={}
function LIGHT.draw()
for i=1,#Lights do
Lights[i]:draw()
end
end
function LIGHT.clear()
for i=#Lights,1,-1 do
Lights[i]:destroy()
Lights[i]=nil
end
end
function LIGHT.add(x,y,R,F)
local id=#Lights+1
Lights[id]={
id=id,
x=x,y=y,size=R,
blackCanvas=gc.newCanvas(R,R),--Solid canvas
shadowCanvas=gc.newCanvas(R,1),--1D vis-depth canvas
renderCanvas=gc.newCanvas(R,R),--Light canvas
blackFn=F,--Solid draw funcion
move=move,
setPow=setPow,
draw=draw,
destroy=destroy,
}
end
return LIGHT

View File

@@ -0,0 +1,29 @@
#define PI 3.14159
extern float xresolution;
//sample from 1D vis-depth map
float samp(vec2 coord,float r,Image u_texture){
return step(r,Texel(u_texture,coord).r);
}
vec4 effect(vec4 color,Image texture,vec2 texture_coords,vec2 screen_coords){
//cartesian to polar, y of 1D sample is always 0
vec2 norm=texture_coords.st*2.-1.;
vec2 tc=vec2((atan(norm.y,norm.x)+PI)/(2.*PI),0.);
float r=length(norm);
//enlarge blur parameter by distance, light scattering simulation
float blur=(1./xresolution)*smoothstep(0.3,1.,r);
//Simple Gaussian blur
float sum=//brightness(0~1)
samp(vec2(tc.x-3.*blur,tc.y),r,texture)*0.1
+samp(vec2(tc.x-2.*blur,tc.y),r,texture)*0.13
+samp(vec2(tc.x-1.*blur,tc.y),r,texture)*0.17
+samp(tc,r,texture)*0.2//The center tex coord,which gives us hard shadows.
+samp(vec2(tc.x+1.*blur,tc.y),r,texture)*0.17
+samp(vec2(tc.x+2.*blur,tc.y),r,texture)*0.13
+samp(vec2(tc.x+3.*blur,tc.y),r,texture)*0.1;
//Multiply the distance to get a soft fading
return vec4(vec3(1.),sum*smoothstep(1.,0.,r));
}

View File

@@ -0,0 +1,21 @@
#define PI 3.14
extern float yresolution;
vec4 effect(vec4 color,Image texture,vec2 texture_coords,vec2 screen_coords){
//Iterate through the occluder map's y-axis.
for(float y=0.;y<yresolution;y++){
//cartesian to polar
// y/yresolution=distance to light source(0~1)
vec2 norm=vec2(texture_coords.s,y/yresolution)*2.-1.;
float theta=PI*1.5+norm.x*PI;
float r=(1.+norm.y)*0.5;
//sample from solid
if(
Texel(texture,(
vec2(-r*sin(theta),-r*cos(theta))*0.5+0.5//coord of solid sampling
))
.a>0.1
)return vec4(vec3(y/yresolution),1.);//collision check, alpha>0.1 means transparent
}
return vec4(1.,1.,1.,1.);//return max distance 1
}