Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ end
- @Buckle2000
- Benoit Giannangeli @giann
- Ivory @tiwe0
- Conner @connershoop
30 changes: 24 additions & 6 deletions examples/normalMap.lua
Original file line number Diff line number Diff line change
@@ -1,34 +1,52 @@
-- Example: Normal map Example
local LightWorld = require "lib"
local image, image_normal
local lightWorld, lightMouse
local lightWorld, lightMouse, imageBody, font
local rotation = 0

local function load()
-- load images
image = love.graphics.newImage("examples/img/normalMap/rock.png")
image_normal = love.graphics.newImage("examples/img/normalMap/normal.png")
-- load font
font = love.graphics.newImageFont("examples/img/complex/font.png", " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/():;%&`'*#=[]\"")
love.graphics.setFont(font)
-- create light world
lightWorld = LightWorld({ambient = {0.21,0.21,0.21}})
-- create light
lightMouse = lightWorld:newLight(0, 0, 160, 160, 160, 300)
lightMouse.normalInvert = true
lightMouse.normalInvert = true
-- create shadow bodys
local w, h = love.graphics.getWidth(), love.graphics.getHeight()
lightWorld:newImage(image, w/2, h/2):setNormalMap(image_normal)
local w, h = love.graphics.getWidth(), love.graphics.getHeight()
imageBody = lightWorld:newImage(image, w/2, h/2)
imageBody:setNormalMap(image_normal)
end

local function update(dt)
love.window.setTitle("Light vs. Shadow Engine (FPS:" .. love.timer.getFPS() .. ")")
lightWorld:update(dt)
lightWorld:update(dt)
lightMouse:setPosition(love.mouse.getX(), love.mouse.getY())

-- Handle arrow key rotation
if love.keyboard.isDown("left") then
rotation = rotation - 2 * dt
end
if love.keyboard.isDown("right") then
rotation = rotation + 2 * dt
end
imageBody:setRotation(rotation)
end

local function draw()
love.graphics.clear(0.21,0.21,0.21)
lightWorld:draw(function(l, t, w, h, s)
love.graphics.setColor(1, 1, 1)
love.graphics.draw(image, w/2-(image:getWidth()/2), h/2-(image:getHeight()/2))
love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight())
love.graphics.draw(image, w/2, h/2, rotation, 1, 1, image:getWidth()/2, image:getHeight()/2)
end)
love.graphics.setBlendMode("alpha")
love.graphics.setColor(0, 0.5, 1)
love.graphics.print("Arrow Keys: Rotate Object", 4, love.graphics.getHeight() - 20 * 1)
end

return {
Expand Down
7 changes: 7 additions & 0 deletions lib/body.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ body.__index = body

body.glowShader = love.graphics.newShader(_PACKAGE.."/shaders/glow.glsl")
body.materialShader = love.graphics.newShader(_PACKAGE.."/shaders/material.glsl")
body.normalRotateShader = love.graphics.newShader(_PACKAGE .. "/shaders/normal_rotate.glsl")

local function new(id, type, ...)
local args = {...}
Expand Down Expand Up @@ -575,11 +576,17 @@ end
function body:drawNormal()
if not self.refraction and not self.reflection and self.normalMesh then
love.graphics.setColor(1, 1, 1)

love.graphics.setShader(body.normalRotateShader)
body.normalRotateShader:send("rotation", self.rotation)

if self.type == 'animation' then
self.animation:draw(self.normal, self.x, self.y, self.rotation, self.scalex, self.scaley, self.nx, self.ny)
else
love.graphics.draw(self.normalMesh, self.x, self.y, self.rotation, self.scalex, self.scaley, self.nx, self.ny)
end

love.graphics.setShader()
end
end

Expand Down
22 changes: 22 additions & 0 deletions lib/shaders/normal_rotate.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
extern float rotation;

vec4 effect(vec4 color, Image normalTex, vec2 uv, vec2 pixel_coords) {
vec4 tex = Texel(normalTex, uv);

vec3 normal = tex.rgb * 2.0 - 1.0;

// Z-axis rotation (rotates in XY plane)
float c = cos(rotation);
float s = sin(rotation);
vec3 rotated = vec3(
c * normal.x - s * normal.y, // X'
s * normal.x + c * normal.y, // Y'
normal.z // Z'
);

rotated = normalize(rotated);

vec3 encoded = rotated * 0.5 + 0.5;

return vec4(encoded, tex.a);
}