2024-02-29 13:54:33 -06:00
|
|
|
render.doc = {
|
|
|
|
doc: "Functions for rendering modes.",
|
|
|
|
normal: "Final render with all lighting.",
|
|
|
|
wireframe: "Show only wireframes of models."
|
|
|
|
};
|
|
|
|
|
2024-07-11 14:25:45 -05:00
|
|
|
var cur = {};
|
|
|
|
|
|
|
|
render.use_shader = function(shader)
|
|
|
|
{
|
|
|
|
if (cur.shader === shader) return;
|
|
|
|
cur.shader = shader;
|
|
|
|
cur.globals = {};
|
|
|
|
cur.bind = undefined;
|
|
|
|
cur.mesh = undefined;
|
|
|
|
render.setpipeline(shader.pipe);
|
|
|
|
}
|
|
|
|
|
|
|
|
render.use_mat = function(mat)
|
|
|
|
{
|
|
|
|
if (!cur.shader) return;
|
|
|
|
if (cur.mat === mat) return;
|
|
|
|
|
|
|
|
render.shader_apply_material(cur.shader, mat, cur.mat);
|
|
|
|
|
|
|
|
cur.mat = mat;
|
|
|
|
|
|
|
|
cur.images = [];
|
|
|
|
if (!cur.shader.fs.images) return;
|
|
|
|
for (var img of cur.shader.fs.images)
|
|
|
|
if (mat[img.name])
|
|
|
|
cur.images.push(mat[img.name]);
|
|
|
|
else
|
|
|
|
cur.images.push(game.texture("icons/no_tex.gif"));
|
|
|
|
}
|
|
|
|
|
|
|
|
var models_array = [];
|
|
|
|
|
|
|
|
render.set_model = function(t)
|
|
|
|
{
|
|
|
|
if (cur.shader.vs.unimap.model)
|
|
|
|
render.setunim4(0, cur.shader.vs.unimap.model.slot, t);
|
|
|
|
}
|
|
|
|
|
2024-05-15 14:16:05 -05:00
|
|
|
var shaderlang = {
|
|
|
|
macos: "metal_macos",
|
2024-05-29 20:21:19 -05:00
|
|
|
windows: "hlsl5",
|
2024-05-16 14:50:18 -05:00
|
|
|
linux: "glsl430",
|
2024-05-21 18:50:53 -05:00
|
|
|
web: "wgsl",
|
2024-05-15 14:16:05 -05:00
|
|
|
ios: "metal_ios",
|
|
|
|
}
|
|
|
|
|
|
|
|
var attr_map = {
|
|
|
|
a_pos: 0,
|
|
|
|
a_uv: 1,
|
|
|
|
a_norm: 2,
|
|
|
|
a_bone: 3,
|
|
|
|
a_weight: 4,
|
|
|
|
a_color: 5,
|
|
|
|
a_tan: 6,
|
|
|
|
a_angle: 7,
|
|
|
|
a_wh: 8,
|
|
|
|
a_st: 9,
|
|
|
|
a_ppos: 10,
|
|
|
|
a_scale: 11
|
|
|
|
}
|
|
|
|
|
|
|
|
var blend_map = {
|
|
|
|
mix: true,
|
|
|
|
none: false
|
|
|
|
}
|
|
|
|
|
|
|
|
var primitive_map = {
|
|
|
|
point: 1,
|
|
|
|
line: 2,
|
|
|
|
linestrip: 3,
|
|
|
|
triangle: 4,
|
|
|
|
trianglestrip: 5
|
|
|
|
}
|
|
|
|
|
|
|
|
var cull_map = {
|
|
|
|
none: 1,
|
|
|
|
front: 2,
|
|
|
|
back: 3
|
|
|
|
}
|
|
|
|
|
|
|
|
var depth_map = {
|
|
|
|
off: false,
|
|
|
|
on: true
|
|
|
|
}
|
|
|
|
|
|
|
|
var face_map = {
|
|
|
|
cw: 2,
|
|
|
|
ccw: 1
|
|
|
|
}
|
|
|
|
|
|
|
|
render.poly_prim = function(verts)
|
|
|
|
{
|
|
|
|
var index = [];
|
|
|
|
if (verts.length < 1) return undefined;
|
|
|
|
|
|
|
|
for (var i = 0; i < verts.length; i++)
|
|
|
|
verts[i][2] = 0;
|
|
|
|
|
|
|
|
for (var i = 2; i < verts.length; i++) {
|
|
|
|
index.push(0);
|
|
|
|
index.push(i-1);
|
|
|
|
index.push(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
pos: os.make_buffer(verts.flat()),
|
|
|
|
verts: verts.length,
|
|
|
|
index: os.make_buffer(index, 1),
|
|
|
|
count: index.length
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function shader_directive(shader, name, map)
|
|
|
|
{
|
|
|
|
var reg = new RegExp(`#${name}.*`, 'g');
|
|
|
|
var mat = shader.match(reg);
|
2024-05-16 08:21:13 -05:00
|
|
|
if (!mat) return undefined;
|
2024-05-15 14:16:05 -05:00
|
|
|
|
|
|
|
reg = new RegExp(`#${name}\s*`, 'g');
|
|
|
|
var ff = mat.map(d=>d.replace(reg,''))[0].trim();
|
|
|
|
|
2024-05-16 08:21:13 -05:00
|
|
|
if (map) return map[ff];
|
|
|
|
return ff;
|
2024-05-15 14:16:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function global_uni(uni, stage)
|
|
|
|
{
|
2024-07-11 14:25:45 -05:00
|
|
|
cur.globals[stage] ??= {};
|
|
|
|
if (cur.globals[stage][uni.name]) return true;
|
2024-05-15 14:16:05 -05:00
|
|
|
switch(uni.name) {
|
|
|
|
case "time":
|
2024-07-11 14:25:45 -05:00
|
|
|
cur.globals[stage][uni.name]
|
2024-05-15 14:16:05 -05:00
|
|
|
render.setuniv(stage, uni.slot, profile.secs(profile.now()));
|
2024-07-11 14:25:45 -05:00
|
|
|
cur.globals[stage][uni.name] = true;
|
2024-05-15 14:16:05 -05:00
|
|
|
return true;
|
|
|
|
case "projection":
|
|
|
|
render.setuniproj(stage, uni.slot);
|
2024-07-11 14:25:45 -05:00
|
|
|
cur.globals[stage][uni.name] = true;
|
2024-05-15 14:16:05 -05:00
|
|
|
return true;
|
|
|
|
case "view":
|
|
|
|
render.setuniview(stage, uni.slot);
|
2024-07-11 14:25:45 -05:00
|
|
|
cur.globals[stage][uni.name] = true;
|
2024-05-15 14:16:05 -05:00
|
|
|
return true;
|
|
|
|
case "vp":
|
|
|
|
render.setunivp(stage, uni.slot);
|
2024-07-11 14:25:45 -05:00
|
|
|
cur.globals[stage][uni.name] = true;
|
2024-05-15 14:16:05 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2024-07-14 16:09:50 -05:00
|
|
|
|
|
|
|
var setcam = render.set_camera;
|
|
|
|
render.set_camera = function(cam)
|
|
|
|
{
|
|
|
|
delete cur.shader;
|
|
|
|
setcam(cam);
|
|
|
|
}
|
2024-05-30 17:12:32 -05:00
|
|
|
|
2024-05-15 14:16:05 -05:00
|
|
|
render.make_shader = function(shader)
|
|
|
|
{
|
|
|
|
var file = shader;
|
|
|
|
shader = io.slurp(shader);
|
2024-05-30 17:12:32 -05:00
|
|
|
if (!shader) {
|
|
|
|
console.info(`not found! slurping shaders/${file}`);
|
|
|
|
shader = io.slurp(`shaders/${file}`);
|
|
|
|
}
|
|
|
|
var writejson = `.prosperon/${file.name()}.shader.json`;
|
2024-05-15 14:16:05 -05:00
|
|
|
var st = profile.now();
|
|
|
|
|
|
|
|
breakme: if (io.exists(writejson)) {
|
|
|
|
var data = json.decode(io.slurp(writejson));
|
|
|
|
var filemod = io.mod(writejson);
|
|
|
|
if (!data.files) break breakme;
|
|
|
|
for (var i of data.files)
|
|
|
|
if (io.mod(i) > filemod)
|
|
|
|
break breakme;
|
|
|
|
|
|
|
|
profile.report(st, `CACHE make shader from ${file}`);
|
|
|
|
var shaderobj = json.decode(io.slurp(writejson));
|
2024-05-16 14:50:18 -05:00
|
|
|
var obj = shaderobj[os.sys()];
|
|
|
|
obj.pipe = render.pipeline(obj);
|
|
|
|
return obj;
|
2024-05-15 14:16:05 -05:00
|
|
|
}
|
|
|
|
|
2024-05-30 17:12:32 -05:00
|
|
|
var out = `.prosperon/${file.name()}.shader`;
|
2024-05-15 14:16:05 -05:00
|
|
|
|
|
|
|
var files = [file];
|
2024-05-30 17:12:32 -05:00
|
|
|
|
2024-05-15 14:16:05 -05:00
|
|
|
var incs = shader.match(/#include <.*>/g);
|
|
|
|
if (incs)
|
|
|
|
for (var inc of incs) {
|
|
|
|
var filez = inc.match(/#include <(.*)>/)[1];
|
|
|
|
var macro = io.slurp(filez);
|
2024-06-07 00:43:15 -05:00
|
|
|
if (!macro) {
|
|
|
|
filez = `shaders/${filez}`;
|
|
|
|
macro = io.slurp(filez);
|
|
|
|
}
|
2024-05-15 14:16:05 -05:00
|
|
|
shader = shader.replace(inc, macro);
|
|
|
|
files.push(filez);
|
|
|
|
}
|
|
|
|
|
|
|
|
var blend = shader_directive(shader, 'blend', blend_map);
|
|
|
|
var primitive = shader_directive(shader, 'primitive', primitive_map);
|
|
|
|
var cull = shader_directive(shader, 'cull', cull_map);
|
|
|
|
var depth = shader_directive(shader, 'depth', depth_map);
|
|
|
|
var face = shader_directive(shader, 'face', face_map);
|
2024-05-16 08:21:13 -05:00
|
|
|
var indexed = shader_directive(shader, 'indexed');
|
|
|
|
|
|
|
|
if (typeof indexed == 'undefined') indexed = true;
|
|
|
|
if (indexed === 'false') indexed = false;
|
2024-05-15 14:16:05 -05:00
|
|
|
|
|
|
|
shader = shader.replace(/uniform\s+(\w+)\s+(\w+);/g, "uniform _$2 { $1 $2; };");
|
|
|
|
shader = shader.replace(/(texture2D|sampler) /g, "uniform $1 ");
|
2024-05-21 18:50:53 -05:00
|
|
|
// shader = shader.replace(/uniform texture2D ?(.*);/g, "uniform _$1_size { vec2 $1_size; };\nuniform texture2D $1;");
|
2024-05-15 14:16:05 -05:00
|
|
|
|
|
|
|
io.slurpwrite(out, shader);
|
2024-05-16 14:50:18 -05:00
|
|
|
|
|
|
|
var compiled = {};
|
|
|
|
|
|
|
|
// shader file is created, now cross compile to all targets
|
|
|
|
for (var platform in shaderlang) {
|
|
|
|
var backend = shaderlang[platform];
|
|
|
|
var ret = os.system(`sokol-shdc -f bare_yaml --slang=${backend} -i ${out} -o ${out}`);
|
|
|
|
if (ret) {
|
|
|
|
console.error(`error compiling shader ${file}. No compilation found for ${platform}:${backend}, and no cross compiler available.`);
|
|
|
|
return;
|
|
|
|
}
|
2024-05-15 14:16:05 -05:00
|
|
|
|
|
|
|
/* Take YAML and create the shader object */
|
|
|
|
var yamlfile = `${out}_reflection.yaml`;
|
2024-05-16 08:21:13 -05:00
|
|
|
var jjson = yaml.tojson(io.slurp(yamlfile));
|
2024-05-30 17:12:32 -05:00
|
|
|
var obj = json.decode(jjson);
|
2024-05-15 14:16:05 -05:00
|
|
|
io.rm(yamlfile);
|
|
|
|
|
|
|
|
obj = obj.shaders[0].programs[0];
|
|
|
|
function add_code(stage) {
|
|
|
|
stage.code = io.slurp(stage.path);
|
2024-05-16 14:50:18 -05:00
|
|
|
|
2024-05-15 14:16:05 -05:00
|
|
|
io.rm(stage.path);
|
|
|
|
delete stage.path;
|
|
|
|
}
|
2024-06-03 16:45:08 -05:00
|
|
|
|
2024-05-15 14:16:05 -05:00
|
|
|
add_code(obj.vs);
|
|
|
|
if (!obj.fs)
|
|
|
|
if (obj.vs.fs) {
|
|
|
|
obj.fs = obj.vs.fs;
|
|
|
|
delete obj.vs.fs;
|
|
|
|
}
|
|
|
|
add_code(obj.fs);
|
|
|
|
|
|
|
|
obj.blend = blend;
|
|
|
|
obj.cull = cull;
|
|
|
|
obj.primitive = primitive;
|
|
|
|
obj.depth = depth;
|
|
|
|
obj.face = face;
|
2024-05-16 08:21:13 -05:00
|
|
|
obj.indexed = indexed;
|
2024-05-15 14:16:05 -05:00
|
|
|
|
|
|
|
if (obj.vs.inputs)
|
|
|
|
for (var i of obj.vs.inputs) {
|
|
|
|
if (!(i.name in attr_map))
|
|
|
|
i.mat = -1;
|
|
|
|
else
|
|
|
|
i.mat = attr_map[i.name];
|
|
|
|
}
|
|
|
|
|
|
|
|
function make_unimap(stage) {
|
|
|
|
if (!stage.uniform_blocks) return {};
|
|
|
|
var unimap = {};
|
|
|
|
for (var uni of stage.uniform_blocks) {
|
2024-05-16 14:50:18 -05:00
|
|
|
var uniname = uni.struct_name[0] == "_" ? uni.struct_name.slice(1) : uni.struct_name;
|
2024-05-15 14:16:05 -05:00
|
|
|
|
2024-05-16 14:50:18 -05:00
|
|
|
unimap[uniname] = {
|
|
|
|
name: uniname,
|
2024-05-15 14:16:05 -05:00
|
|
|
slot: Number(uni.slot),
|
|
|
|
size: Number(uni.size)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return unimap;
|
|
|
|
}
|
|
|
|
|
|
|
|
obj.vs.unimap = make_unimap(obj.vs);
|
|
|
|
obj.fs.unimap = make_unimap(obj.fs);
|
|
|
|
|
|
|
|
obj.name = file;
|
2024-05-16 14:50:18 -05:00
|
|
|
|
|
|
|
compiled[platform] = obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
compiled.files = files;
|
2024-05-15 14:16:05 -05:00
|
|
|
|
2024-05-16 14:50:18 -05:00
|
|
|
io.slurpwrite(writejson, json.encode(compiled));
|
2024-05-15 14:16:05 -05:00
|
|
|
profile.report(st, `make shader from ${file}`);
|
|
|
|
|
2024-05-16 14:50:18 -05:00
|
|
|
var obj = compiled[os.sys()];
|
2024-05-15 14:16:05 -05:00
|
|
|
obj.pipe = render.pipeline(obj);
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
var shader_unisize = {
|
|
|
|
4: render.setuniv,
|
|
|
|
8: render.setuniv2,
|
|
|
|
12: render.setuniv3,
|
|
|
|
16: render.setuniv4
|
|
|
|
};
|
|
|
|
|
2024-07-11 14:25:45 -05:00
|
|
|
render.shader_apply_material = function(shader, material = {}, old = {})
|
2024-05-15 14:16:05 -05:00
|
|
|
{
|
|
|
|
for (var p in shader.vs.unimap) {
|
|
|
|
if (global_uni(shader.vs.unimap[p], 0)) continue;
|
2024-07-11 14:25:45 -05:00
|
|
|
if (material[p] === old[p]) continue;
|
|
|
|
assert(p in material, `shader ${shader.name} has no uniform for ${p}`);
|
2024-05-15 14:16:05 -05:00
|
|
|
var s = shader.vs.unimap[p];
|
|
|
|
shader_unisize[s.size](0, s.slot, material[p]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var p in shader.fs.unimap) {
|
|
|
|
if (global_uni(shader.fs.unimap[p], 1)) continue;
|
2024-07-11 14:25:45 -05:00
|
|
|
if (material[p] === old[p]) continue;
|
|
|
|
assert(p in material, `shader ${shader.name} has no uniform for ${p}`);
|
2024-05-15 14:16:05 -05:00
|
|
|
var s = shader.fs.unimap[p];
|
|
|
|
shader_unisize[s.size](1, s.slot, material[p]);
|
|
|
|
}
|
2024-07-11 14:25:45 -05:00
|
|
|
|
2024-06-19 18:52:41 -05:00
|
|
|
if (!material.diffuse) return;
|
2024-07-11 14:25:45 -05:00
|
|
|
if (material.diffuse === old.diffuse) return;
|
2024-05-15 14:16:05 -05:00
|
|
|
|
|
|
|
if ("diffuse_size" in shader.fs.unimap)
|
|
|
|
render.setuniv2(1, shader.fs.unimap.diffuse_size.slot, [material.diffuse.width, material.diffuse.height]);
|
2024-06-19 18:52:41 -05:00
|
|
|
|
2024-05-15 14:16:05 -05:00
|
|
|
if ("diffuse_size" in shader.vs.unimap)
|
2024-06-19 18:52:41 -05:00
|
|
|
render.setuniv2(0, shader.vs.unimap.diffuse_size.slot, [material.diffuse.width, material.diffuse.height]);
|
2024-05-15 14:16:05 -05:00
|
|
|
}
|
|
|
|
|
2024-07-11 14:25:45 -05:00
|
|
|
render.sg_bind = function(mesh, ssbo)
|
2024-05-15 14:16:05 -05:00
|
|
|
{
|
2024-07-11 14:25:45 -05:00
|
|
|
if (cur.mesh === mesh && cur.bind) {
|
|
|
|
cur.bind.inst = 1;
|
|
|
|
cur.bind.images = cur.images;
|
|
|
|
render.setbind(cur.bind);
|
|
|
|
return cur.bind;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur.mesh = mesh;
|
|
|
|
|
2024-05-15 14:16:05 -05:00
|
|
|
var bind = {};
|
|
|
|
bind.attrib = [];
|
2024-07-11 14:25:45 -05:00
|
|
|
if (cur.shader.vs.inputs)
|
|
|
|
for (var a of cur.shader.vs.inputs) {
|
2024-05-15 14:16:05 -05:00
|
|
|
if (!(a.name in mesh)) {
|
|
|
|
if (!(a.name.slice(2) in mesh)) {
|
2024-07-11 14:25:45 -05:00
|
|
|
console.error(`cannot draw shader ${cur.shader.name}; there is no attrib ${a.name} in the given mesh.`);
|
2024-05-15 14:16:05 -05:00
|
|
|
return undefined;
|
|
|
|
} else
|
|
|
|
bind.attrib.push(mesh[a.name.slice(2)]);
|
|
|
|
} else
|
|
|
|
bind.attrib.push(mesh[a.name]);
|
|
|
|
}
|
|
|
|
|
2024-07-11 14:25:45 -05:00
|
|
|
if (cur.shader.indexed) {
|
2024-05-15 14:16:05 -05:00
|
|
|
bind.index = mesh.index;
|
|
|
|
bind.count = mesh.count;
|
|
|
|
} else
|
|
|
|
bind.count = mesh.verts;
|
|
|
|
|
|
|
|
bind.ssbo = [];
|
2024-07-11 14:25:45 -05:00
|
|
|
if (cur.shader.vs.storage_buffers)
|
|
|
|
for (var b of cur.shader.vs.storage_buffers)
|
2024-05-15 14:16:05 -05:00
|
|
|
bind.ssbo.push(ssbo);
|
2024-07-11 14:25:45 -05:00
|
|
|
|
|
|
|
bind.inst = 1;
|
|
|
|
bind.images = cur.images;
|
|
|
|
|
|
|
|
cur.bind = bind;
|
|
|
|
|
|
|
|
render.setbind(cur.bind);
|
2024-05-15 14:16:05 -05:00
|
|
|
|
|
|
|
return bind;
|
|
|
|
}
|
|
|
|
|
2024-05-16 08:21:13 -05:00
|
|
|
render.device = {
|
|
|
|
pc: [1920,1080],
|
|
|
|
macbook_m2: [2560,1664, 13.6],
|
|
|
|
ds_top: [400,240, 3.53],
|
|
|
|
ds_bottom: [320,240, 3.02],
|
|
|
|
playdate: [400,240,2.7],
|
|
|
|
switch: [1280,720, 6.2],
|
|
|
|
switch_lite: [1280,720,5.5],
|
|
|
|
switch_oled: [1280,720,7],
|
|
|
|
dsi: [256,192,3.268],
|
|
|
|
ds: [256,192, 3],
|
|
|
|
dsixl: [256,192,4.2],
|
|
|
|
ipad_air_m2: [2360,1640, 11.97],
|
|
|
|
iphone_se: [1334, 750, 4.7],
|
|
|
|
iphone_12_pro: [2532,1170,6.06],
|
|
|
|
iphone_15: [2556,1179,6.1],
|
|
|
|
gba: [240,160,2.9],
|
|
|
|
gameboy: [160,144,2.48],
|
|
|
|
gbc: [160,144,2.28],
|
|
|
|
steamdeck: [1280,800,7],
|
|
|
|
vita: [960,544,5],
|
|
|
|
psp: [480,272,4.3],
|
|
|
|
imac_m3: [4480,2520,23.5],
|
|
|
|
macbook_pro_m3: [3024,1964, 14.2],
|
|
|
|
ps1: [320,240,5],
|
|
|
|
ps2: [640,480],
|
|
|
|
snes: [256,224],
|
|
|
|
gamecube: [640,480],
|
|
|
|
n64: [320,240],
|
|
|
|
c64: [320,200],
|
|
|
|
macintosh: [512,342,9],
|
|
|
|
gamegear: [160,144,3.2],
|
|
|
|
};
|
|
|
|
|
|
|
|
render.device.doc = `Device resolutions given as [x,y,inches diagonal].`;
|
|
|
|
|
2024-06-07 00:43:15 -05:00
|
|
|
var textshader;
|
|
|
|
var circleshader;
|
|
|
|
var polyshader;
|
2024-07-09 01:03:39 -05:00
|
|
|
var slice9shader;
|
2024-06-07 00:43:15 -05:00
|
|
|
|
|
|
|
render.init = function() {
|
|
|
|
textshader = render.make_shader("shaders/text_base.cg");
|
|
|
|
render.spriteshader = render.make_shader("shaders/sprite.cg");
|
|
|
|
render.postshader = render.make_shader("shaders/simplepost.cg");
|
2024-07-09 01:03:39 -05:00
|
|
|
slice9shader = render.make_shader("shaders/9slice.cg");
|
2024-06-07 00:43:15 -05:00
|
|
|
circleshader = render.make_shader("shaders/circle.cg");
|
|
|
|
polyshader = render.make_shader("shaders/poly.cg");
|
|
|
|
|
|
|
|
render.textshader = textshader;
|
2024-06-07 09:49:13 -05:00
|
|
|
|
|
|
|
os.make_circle2d().draw = function() {
|
|
|
|
render.circle(this.body().transform().pos, this.radius, [1,1,0,1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
var disabled = [148/255,148/255, 148/255, 1];
|
|
|
|
var sleep = [1, 140/255, 228/255, 1];
|
|
|
|
var dynamic = [1, 70/255, 46/255, 1];
|
|
|
|
var kinematic = [1, 194/255, 64/255, 1];
|
|
|
|
var static_color = [73/255, 209/255, 80/255, 1];
|
|
|
|
|
|
|
|
os.make_poly2d().draw = function() {
|
|
|
|
var body = this.body();
|
|
|
|
var color = body.sleeping() ? [0,0.3,0,0.4] : [0,1,0,0.4];
|
|
|
|
var t = body.transform();
|
|
|
|
render.poly(this.points, color, body.transform());
|
|
|
|
color.a = 1;
|
|
|
|
render.line(this.points.wrapped(1), color, 1, body.transform());
|
|
|
|
}
|
|
|
|
|
|
|
|
os.make_seg2d().draw = function() {
|
|
|
|
render.line([this.a(), this.b()], [1,0,1,1], Math.max(this.radius/2, 1), this.body().transform());
|
|
|
|
}
|
|
|
|
|
|
|
|
joint.pin().draw = function() {
|
|
|
|
var a = this.bodyA();
|
|
|
|
var b = this.bodyB();
|
|
|
|
render.line([a.transform().pos.xy, b.transform().pos.xy], [0,1,1,1], 1);
|
|
|
|
}
|
2024-06-07 00:43:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
render.circle = function(pos, radius, color) {
|
|
|
|
var mat = {
|
|
|
|
radius: radius,
|
|
|
|
coord: pos,
|
|
|
|
shade: color
|
|
|
|
};
|
2024-07-11 14:25:45 -05:00
|
|
|
render.use_shader(circleshader);
|
|
|
|
render.use_mat(mat);
|
|
|
|
render.draw(shape.quad);
|
2024-06-07 00:43:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
render.poly = function(points, color, transform) {
|
|
|
|
var buffer = render.poly_prim(points);
|
|
|
|
var mat = { shade: color};
|
2024-07-11 14:25:45 -05:00
|
|
|
render.use_shader(polyshader);
|
|
|
|
render.set_model(transform);
|
|
|
|
render.use_mat(mat);
|
|
|
|
render.draw(buffer);
|
2024-06-07 00:43:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
render.line = function(points, color = Color.white, thickness = 1, transform) {
|
|
|
|
var buffer = os.make_line_prim(points, thickness, 0, false);
|
2024-07-11 14:25:45 -05:00
|
|
|
render.use_shader(polyshader);
|
2024-06-07 00:43:15 -05:00
|
|
|
var mat = {
|
|
|
|
shade: color
|
|
|
|
};
|
2024-07-11 14:25:45 -05:00
|
|
|
render.use_mat(mat);
|
|
|
|
render.set_model(transform);
|
|
|
|
render.draw(buffer);
|
2024-06-07 00:43:15 -05:00
|
|
|
}
|
|
|
|
|
2024-05-16 08:21:13 -05:00
|
|
|
/* All draw in screen space */
|
|
|
|
render.point = function(pos,size,color = Color.blue) {
|
2024-05-30 12:05:51 -05:00
|
|
|
render.circle(pos,size,size,color);
|
2024-05-16 08:21:13 -05:00
|
|
|
};
|
|
|
|
|
2024-07-03 16:38:29 -05:00
|
|
|
render.cross = function(pos, size, color = Color.red, thickness = 1) {
|
2024-05-21 09:33:17 -05:00
|
|
|
var a = [
|
|
|
|
pos.add([0,size]),
|
|
|
|
pos.add([0,-size])
|
|
|
|
];
|
|
|
|
var b = [
|
|
|
|
pos.add([size,0]),
|
|
|
|
pos.add([-size,0])
|
|
|
|
];
|
2024-07-03 16:38:29 -05:00
|
|
|
render.line(a,color,thickness);
|
|
|
|
render.line(b,color,thickness);
|
2024-05-21 09:33:17 -05:00
|
|
|
};
|
2024-05-16 08:21:13 -05:00
|
|
|
|
|
|
|
render.arrow = function(start, end, color = Color.red, wingspan = 4, wingangle = 10) {
|
|
|
|
var dir = end.sub(start).normalized();
|
|
|
|
var wing1 = [
|
|
|
|
Vector.rotate(dir, wingangle).scale(wingspan).add(end),
|
|
|
|
end
|
|
|
|
];
|
|
|
|
var wing2 = [
|
|
|
|
Vector.rotate(dir,-wingangle).scale(wingspan).add(end),
|
|
|
|
end
|
|
|
|
];
|
|
|
|
render.line([start,end],color);
|
|
|
|
render.line(wing1,color);
|
|
|
|
render.line(wing2,color);
|
|
|
|
};
|
|
|
|
|
|
|
|
render.coordinate = function(pos, size, color) {
|
|
|
|
render.text(JSON.stringify(pos.map(p=>Math.round(p))), pos, size, color);
|
|
|
|
render.point(pos, 2, color);
|
|
|
|
}
|
|
|
|
|
|
|
|
render.boundingbox = function(bb, color = Color.white) {
|
2024-07-10 09:39:28 -05:00
|
|
|
render.line(bbox.topoints(bb).wrapped(1), color);
|
2024-05-16 08:21:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
render.rectangle = function(lowerleft, upperright, color) {
|
|
|
|
var points = [lowerleft, lowerleft.add([upperright.x-lowerleft.x,0]), upperright, lowerleft.add([0,upperright.y-lowerleft.y])];
|
|
|
|
render.poly(points, color);
|
|
|
|
};
|
|
|
|
|
|
|
|
render.box = function(pos, wh, color = Color.white) {
|
|
|
|
var lower = pos.sub(wh.scale(0.5));
|
|
|
|
var upper = pos.add(wh.scale(0.5));
|
2024-07-11 16:37:24 -05:00
|
|
|
render.rectangle(lower,upper,color);
|
2024-05-16 08:21:13 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
render.window = function(pos, wh, color) {
|
|
|
|
var p = pos.slice();
|
|
|
|
p = p.add(wh.scale(0.5));
|
|
|
|
render.box(p,wh,color);
|
|
|
|
};
|
|
|
|
|
2024-07-09 13:48:15 -05:00
|
|
|
render.text_bb = function(str, size = 1, wrap = -1, pos = [0,0])
|
|
|
|
{
|
|
|
|
var bb = render.text_size(str,size,wrap);
|
2024-07-09 01:03:39 -05:00
|
|
|
var w = (bb.r - bb.l);
|
|
|
|
var h = (bb.t - bb.b);
|
2024-07-09 13:48:15 -05:00
|
|
|
|
2024-07-09 01:03:39 -05:00
|
|
|
bb.r += pos.x;
|
|
|
|
bb.l += pos.x;
|
|
|
|
bb.t += pos.y;
|
|
|
|
bb.b += pos.y;
|
2024-07-09 13:48:15 -05:00
|
|
|
return bb;
|
|
|
|
}
|
|
|
|
|
|
|
|
render.text = function(str, pos, size = 1, color = Color.white, wrap = -1, anchor = [0,1], cursor = -1) {
|
2024-07-10 09:39:28 -05:00
|
|
|
var bb = render.text_bb(str, size, wrap, pos);
|
2024-07-09 13:48:15 -05:00
|
|
|
gui.text(str, pos, size, color, wrap, cursor);
|
2024-07-09 01:03:39 -05:00
|
|
|
return bb;
|
|
|
|
|
2024-05-16 08:21:13 -05:00
|
|
|
p.x -= w * anchor.x;
|
|
|
|
bb.r += (w*anchor.x);
|
|
|
|
bb.l += (w*anchor.x);
|
|
|
|
p.y += h * (1 - anchor.y);
|
|
|
|
bb.t += h*(1-anchor.y);
|
|
|
|
bb.b += h*(1-anchor.y);
|
2024-07-09 01:03:39 -05:00
|
|
|
|
2024-05-16 08:21:13 -05:00
|
|
|
return bb;
|
|
|
|
};
|
|
|
|
|
2024-07-09 16:43:09 -05:00
|
|
|
render.image = function(tex, pos, scale = [tex.width, tex.height], rotation = 0, color = Color.white) {
|
2024-06-26 16:57:17 -05:00
|
|
|
var t = os.make_transform();
|
|
|
|
t.pos = pos;
|
2024-07-09 16:43:09 -05:00
|
|
|
t.scale = [scale.x/tex.width,scale.y/tex.height,1];
|
2024-07-11 14:25:45 -05:00
|
|
|
render.use_shader(render.spriteshader);
|
|
|
|
render.set_model(t);
|
|
|
|
render.use_mat({
|
2024-06-26 16:57:17 -05:00
|
|
|
shade: color,
|
2024-07-14 16:09:50 -05:00
|
|
|
diffuse: tex,
|
|
|
|
rect:[0,0,1,1]
|
2024-06-26 16:57:17 -05:00
|
|
|
});
|
2024-07-11 14:25:45 -05:00
|
|
|
|
|
|
|
render.draw(shape.quad);
|
2024-07-09 01:03:39 -05:00
|
|
|
|
|
|
|
var bb = {};
|
|
|
|
bb.b = pos.y;
|
|
|
|
bb.l = pos.x;
|
|
|
|
bb.t = pos.y + tex.height*scale;
|
|
|
|
bb.r = pos.x + tex.width*scale;
|
|
|
|
return bb;
|
|
|
|
}
|
|
|
|
|
2024-07-15 15:54:18 -05:00
|
|
|
|
|
|
|
// pos is the lower left corner, scale is the width and height
|
2024-07-09 16:43:09 -05:00
|
|
|
render.slice9 = function(tex, pos, bb, scale = [tex.width,tex.height], color = Color.white)
|
2024-07-09 01:03:39 -05:00
|
|
|
{
|
|
|
|
var t = os.make_transform();
|
|
|
|
t.pos = pos;
|
2024-07-09 16:43:09 -05:00
|
|
|
t.scale = [scale.x/tex.width,scale.y/tex.height,1];
|
|
|
|
var border;
|
|
|
|
if (typeof bb === 'number')
|
|
|
|
border = [bb/tex.width,bb/tex.height,bb/tex.width,bb/tex.height];
|
|
|
|
else
|
|
|
|
border = [bb.l/tex.width, bb.b/tex.height, bb.r/tex.width, bb.t/tex.height];
|
2024-07-11 14:25:45 -05:00
|
|
|
|
|
|
|
render.use_shader(slice9shader);
|
|
|
|
render.set_model(t);
|
|
|
|
render.use_mat({
|
2024-07-09 13:48:15 -05:00
|
|
|
shade: color,
|
|
|
|
diffuse:tex,
|
|
|
|
rect:[0,0,1,1],
|
2024-07-09 16:43:09 -05:00
|
|
|
border: border,
|
|
|
|
scale: [scale.x/tex.width,scale.y/tex.height]
|
2024-07-09 01:03:39 -05:00
|
|
|
});
|
2024-07-11 14:25:45 -05:00
|
|
|
|
|
|
|
render.draw(shape.quad);
|
2024-07-09 01:03:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var textssbo = render.text_ssbo();
|
|
|
|
|
|
|
|
render.flush_text = function()
|
|
|
|
{
|
|
|
|
if (!render.textshader) return;
|
2024-07-11 14:25:45 -05:00
|
|
|
render.use_shader(render.textshader);
|
|
|
|
render.use_mat({text:render.font.texture});
|
|
|
|
|
|
|
|
render.draw(shape.quad, textssbo, render.flushtext());
|
2024-05-16 08:21:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
render.fontcache = {};
|
|
|
|
render.set_font = function(path, size) {
|
|
|
|
var fontstr = `${path}-${size}`;
|
2024-07-09 01:03:39 -05:00
|
|
|
if (render.font && render.fontcache[fontstr] === render.font) return;
|
2024-05-16 08:21:13 -05:00
|
|
|
if (!render.fontcache[fontstr]) render.fontcache[fontstr] = os.make_font(path, size);
|
2024-07-09 01:03:39 -05:00
|
|
|
|
|
|
|
render.flush_text();
|
2024-05-16 08:21:13 -05:00
|
|
|
|
|
|
|
gui.font_set(render.fontcache[fontstr]);
|
|
|
|
render.font = render.fontcache[fontstr];
|
|
|
|
}
|
|
|
|
|
|
|
|
render.doc = "Draw shapes in screen space.";
|
|
|
|
//render.circle.doc = "Draw a circle at pos, with a given radius and color.";
|
|
|
|
render.cross.doc = "Draw a cross centered at pos, with arm length size.";
|
|
|
|
render.arrow.doc = "Draw an arrow from start to end, with wings of length wingspan at angle wingangle.";
|
|
|
|
render.rectangle.doc = "Draw a rectangle, with its corners at lowerleft and upperright.";
|
|
|
|
|
2024-07-11 14:25:45 -05:00
|
|
|
render.draw = function(mesh, ssbo, inst = 1)
|
|
|
|
{
|
|
|
|
render.sg_bind(mesh, ssbo);
|
|
|
|
render.spdraw(cur.bind.count, inst);
|
|
|
|
}
|
2024-05-16 08:21:13 -05:00
|
|
|
|
2024-02-29 13:54:33 -06:00
|
|
|
return {render};
|