sm64coopdx/docs/lua/examples/lighting-engine-demo/shading.lua
Agent X 7009e7da86
Some checks are pending
Build coop / build-linux (push) Waiting to run
Build coop / build-steamos (push) Waiting to run
Build coop / build-windows-opengl (push) Waiting to run
Build coop / build-windows-directx (push) Waiting to run
Build coop / build-macos-arm (push) Waiting to run
Build coop / build-macos-intel (push) Waiting to run
Lighting Engine: Awesome Edition
Just making a brief commit and thats all to add onto dj's lighting engine improvements because I heard about the PR, in mine I
* allowed all Vtx objects to be manipulated/retrieved
* updated lighting engine demo to be an SM64 night mode with a flashlight
* Fixed longterm bug where lighting bugs out unless you spawn a light
* Added new default LE mode LE_MODE_AFFECT_ALL_SHADED_AND_COLORED which also affects vertex colored surfaces
2025-06-29 12:08:04 -04:00

51 lines
No EOL
1.3 KiB
Lua

-- special thanks to Coolio for figuring out worldspace shading
--- @param dest Vec3f
--- @param rotate Vec3f
--- Rotates `dest` around the Z, Y, and X axes
local function vec3f_rotate_zyx(dest, rotate)
local v = { x = dest.x, y = dest.y, z = dest.z }
local sx = sins(rotate.x)
local cx = coss(rotate.x)
local sy = sins(rotate.y)
local cy = coss(rotate.y)
local sz = sins(rotate.z)
local cz = coss(rotate.z)
-- Rotation around Z axis
local xz = v.x * cz - v.y * sz
local yz = v.x * sz + v.y * cz
local zz = v.z
-- Rotation around Y axis
local xy = xz * cy + zz * sy
local yy = yz
local zy = -xz * sy + zz * cy
-- Rotation around X axis
dest.x = xy
dest.y = yy * cx - zy * sx
dest.z = yy * sx + zy * cx
return dest
end
local LIGHTING_OFFSET = { x = 0x28 / 0xFF, y = 0x28 / 0xFF, z = 0x28 / 0xFF }
function shading_update()
local l = gLakituState
local pitch = calculate_pitch(l.pos, l.focus)
local yaw = calculate_yaw(l.pos, l.focus)
local roll = l.roll
local lightingDir = gVec3fOne()
vec3f_rotate_zyx(lightingDir, { x = -pitch, y = -yaw, z = roll })
vec3f_sub(lightingDir, LIGHTING_OFFSET)
set_lighting_dir(0, lightingDir.x)
set_lighting_dir(1, lightingDir.y)
set_lighting_dir(2, lightingDir.z)
end