CabinGame/Luxe/math/util.wren
2020-08-29 21:25:16 +02:00

53 lines
No EOL
1.4 KiB
Text

import "luxe: assets" for Assets
import "luxe: render" for Material
import "math/math" for M
import "math/Vector" for Vector
class Util{
static material_from_image_path(path){
var image = Assets.image(path)
return material_from_image(image)
}
static material_from_image(image){
var material = Material.create("luxe: material_basis/sprite_pixelated")
Material.set_image(material, 0, image)
return material
}
static copy_list(from, to){
for(i in 0...from.count){
to[i] = from[i]
}
}
static for_all(sequences, fn){
var count = sequences.count
var counter = (0...count)
var iterators = List.filled(count, null)
counter.each{|i| iterators[i] = sequences[i].iterate(iterators[i])}
while(iterators.all{|iter| iter}){
var values = (0...count).map{|i| sequences[i].iteratorValue(iterators[i])}
fn.call(values)
counter.each{|i| iterators[i] = sequences[i].iterate(iterators[i])}
}
}
//hue value saturation to rgb colors. all values in 0-1 range
static hsv(h, s, v){
//hue to rgb
h = h % 1
var col = [
(h * 6 - 3).abs - 1,
2 - (h * 6 - 2).abs,
2 - (h * 6 - 4).abs
]
col = M.clamp(col, 0, 1)
//then add saturation
col = M.lerp(1, col, s)
//and value
col = col.map{|comp| comp * v}
return col.toList + [1] //poor womans add
}
}