[modified] optimised vec3 swizzling

This commit is contained in:
Max Cahill 2020-04-01 20:56:36 +11:00
parent c7d96461cf
commit 870d291369

View File

@ -343,39 +343,48 @@ end
--swizzle extraction --swizzle extraction
--not as nice as property accessors so might be worth doing that later :) --not as nice as property accessors so might be worth doing that later :)
--also dog slow, so there's that
local _swizzle_x_byte = ("x"):byte()
local _swizzle_y_byte = ("y"):byte()
local _swizzle_z_byte = ("z"):byte()
local _allowed_swizzle = { local _allowed_swizzle = {
x = true, [_swizzle_x_byte] = "x",
y = true, [_swizzle_y_byte] = "y",
z = true, [_swizzle_z_byte] = "z",
} }
function vec3:extract_single(swizzle)
if _allowed_swizzle[swizzle] then function vec3:encode_swizzle_field(swizzle)
return self[swizzle] if type(swizzle) == "string" then
swizzle = swizzle:byte()
end end
return 0 return _allowed_swizzle[swizzle] or "x"
end
function vec3:extract_single(swizzle)
return self[self:encode_swizzle_field(swizzle)]
end end
function vec3:infuse_single(swizzle, v) function vec3:infuse_single(swizzle, v)
if _allowed_swizzle[swizzle] then self[self:encode_swizzle_field(swizzle)] = v
self[swizzle] = v
end
return self return self
end end
function vec3:extract_vec2(swizzle, into) function vec3:extract_vec2(swizzle, into)
if not into then into = vec2:zero() end if not into then into = vec2:zero() end
local x = self:extract_single(swizzle:sub(1, 1)) local x = self:extract_single(swizzle:byte(1))
local y = self:extract_single(swizzle:sub(2, 2)) local y = self:extract_single(swizzle:byte(2))
return into:sset(x, y) return into:sset(x, y)
end end
function vec3:infuse_vec2(swizzle, v) function vec3:infuse_vec2(swizzle, v)
self:infuse_single(swizzle:sub(1, 1), v.x) self:infuse_single(swizzle:byte(1), v.x)
self:infuse_single(swizzle:sub(2, 2), v.y) self:infuse_single(swizzle:byte(2), v.y)
return self return self
end end
--rotate around a swizzle --rotate around a swizzle
--todo: angle-axis version
function vec3:rotatei(swizzle, angle) function vec3:rotatei(swizzle, angle)
local v = vec2:pooled() local v = vec2:pooled()
self:extract_vec2(swizzle, v) self:extract_vec2(swizzle, v)
@ -428,16 +437,12 @@ function vec3:inverse()
return self:copy():inversei() return self:copy():inversei()
end end
function vec3:rotate(angle) function vec3:rotate(swizzle, angle)
return self:copy():rotatei(angle) return self:copy():rotatei(swizzle, angle)
end end
function vec3:rot90r() function vec3:rotate_euler(angle_x_axis, angle_y_axis, angle_z_axis)
return self:copy():rot90ri() return self:copy():rotate_euleri(angle_x_axis, angle_y_axis, angle_z_axis)
end
function vec3:rot90l()
return self:copy():rot90li()
end end
vec3.rot180 = vec3.inverse --alias vec3.rot180 = vec3.inverse --alias