-
-
Save spissvinkel/16663e6e8d13d187ddf1743d820254b1 to your computer and use it in GitHub Desktop.
Lua script to rotate a complete model in AssetForge by Kenney
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Left-handed coordinates | |
truckModel = { | |
orientation = { 0.0, 0.0, 0.0 }, -- angles in degrees | |
parts = {{ | |
name = "bodyFront", | |
block = "Vehicles/body_frontSUV", | |
position = { 0.0, -0.1, 1.0 }, | |
orientation = { 0.0, 0.0, 0.0 }, -- angles in degrees | |
scale = { 1.0, 1.0, 1.0 } | |
}, { | |
name = "bodyMidBtm", | |
block = "Vehicles/body_standard", | |
position = { 0.0, -0.1, 0.0 }, | |
orientation = { 0.0, 0.0, 0.0 }, -- angles in degrees | |
scale = { 1.0, 1.0, 1.0 } | |
}, { | |
name = "bodyMidTop", | |
block = "Vehicles/body_topStandard", | |
position = { 0.0, 0.9, 0.0 }, | |
orientation = { 0.0, 0.0, 0.0 }, -- angles in degrees | |
scale = { 1.0, 1.0, 1.0 } | |
}, { | |
name = "bodyBack", | |
block = "Vehicles/body_backBedClosed", | |
position = { 0.0, -0.1, -1.0 }, | |
orientation = { 0.0, 0.0, 0.0 }, -- angles in degrees | |
scale = { 1.0, 1.0, 1.0 } | |
}, { | |
name = "wheelFR", | |
block = "Vehicles/wheel_spokes", | |
position = { -0.35, 0.3, 1.0 }, | |
orientation = { 0.0, 180.0, 0.0 }, -- angles in degrees | |
scale = { 1.0, 1.0, 1.0 } | |
}, { | |
name = "wheelRR", | |
block = "Vehicles/wheel_spokes", | |
position = { -0.35, 0.3, -1.0 }, | |
orientation = { 0.0, 180.0, 0.0 }, -- angles in degrees | |
scale = { 1.0, 1.0, 1.0 } | |
}, { | |
name = "wheelFL", | |
block = "Vehicles/wheel_spokes", | |
position = { 0.35, 0.3, 1.0 }, | |
orientation = { 0.0, 0.0, 0.0 }, -- angles in degrees | |
scale = { 1.0, 1.0, 1.0 } | |
}, { | |
name = "wheelRL", | |
block = "Vehicles/wheel_spokes", | |
position = { 0.35, 0.3, -1.0 }, | |
orientation = { 0.0, 0.0, 0.0 }, -- angles in degrees | |
scale = { 1.0, 1.0, 1.0 } | |
}} | |
} | |
-- finds the part named 'partName' | |
findPart = function (model, partName) | |
p = nil | |
ps = model.parts | |
for i = 1, #ps do | |
if ps[i].name == partName then p = ps[i] end | |
end | |
return p | |
end | |
-- converts degrees to radians | |
deg2rad = function (degrees) | |
return degrees * math.pi / 180.0 | |
end | |
-- adds the 3-element vector 'b' to the 3-element vector 'a' and returns the result as a new 3-element vector | |
addVec3 = function (a, b) | |
return { a[1] + b[1], a[2] + b[2], a[3] + b[3] } | |
end | |
-- returns a 3x3 rotation matrix based on the orientation vector 'a' with angles in degrees | |
rotMat3 = function (a) | |
rx = deg2rad(a[1]); ry = deg2rad(a[2]); rz = deg2rad(a[3]) | |
cx = math.cos(rx); cy = math.cos(ry); cz = math.cos(rz) | |
sx = math.sin(rx); sy = math.sin(ry); sz = math.sin(rz) | |
return { | |
r0c0 = cy*cz, r0c1 = sy*sx - cy*sz*cx, r0c2 = cy*sz*sx + sy*cx, | |
r1c0 = sz, r1c1 = cz*cx, r1c2 = -cz*sx, | |
r2c0 = -sy*cz, r2c1 = sy*sz*cx + cy*sx, r2c2 = cy*cx - sy*sz*sx | |
} | |
end | |
-- multiplies the 3x3 matrix 'm' with the 3-element column vector 'a' and returns the result as a new 3-element vector | |
mulMat3Vec3 = function (m, a) | |
return { | |
m.r0c0 * a[1] + m.r0c1 * a[2] + m.r0c2 * a[3], | |
m.r1c0 * a[1] + m.r1c1 * a[2] + m.r1c2 * a[3], | |
m.r2c0 * a[1] + m.r2c1 * a[2] + m.r2c2 * a[3] | |
} | |
end | |
-- builds a model | |
buildModel = function (model) | |
forge.clear() | |
m = rotMat3(model.orientation) | |
for i = 1, #model.parts do | |
part = model.parts[i] | |
p = mulMat3Vec3(m, part.position) | |
o = addVec3(part.orientation, model.orientation) | |
forge.build(part.block, p, o, part.scale) | |
end | |
end | |
-- draws and exports several rotations of model | |
doExport = function (model) | |
adjustY = -180 -- adjust for export settings as appropriate | |
yMax = 18 | |
xMax = 9 | |
for y = 0, yMax do | |
for x = 0, xMax do | |
model.orientation[2] = y * 5 - 45 + adjustY | |
findPart(model, "wheelFR").orientation[1] = x * -9 | |
findPart(model, "wheelRR").orientation[1] = x * -9 | |
findPart(model, "wheelFL").orientation[1] = x * 9 | |
findPart(model, "wheelRL").orientation[1] = x * 9 | |
buildModel(model) | |
imgcode = "CA" .. string.format("%02d", y) .. "WR" .. string.format("%02d", x) | |
filename = "C:\\Users\\XXXXX\\Pictures\\AssetForge\\test\\truck_" .. imgcode | |
forge.exportSprite(512, filename) | |
end | |
end | |
end | |
-- do the export | |
doExport(truckModel) | |
-- just draw model once | |
buildModel(truckModel) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment