[oden] Colors

But they don't work
This commit is contained in:
John Doty 2023-08-26 08:58:00 -07:00
parent db0f22b1db
commit 106db89e9b
5 changed files with 77 additions and 24 deletions

View file

@ -191,7 +191,7 @@ export class Robo extends Actor {
const frame = (anim.start + ((clock / anim.speed) % anim.length)) >> 0; const frame = (anim.start + ((clock / anim.speed) % anim.length)) >> 0;
spr(x, y, w, h, frame * w, 0, 32, 32); spr(x, y, w, h, frame * w, 0, 32, 32);
// circle(this.props.position.x, this.props.position.y, 16, 1); circle(this.props.position.x, this.props.position.y, 16, 1);
} }
} }
} }

View file

@ -8,11 +8,11 @@ struct VertexInput {
}; };
struct InstanceInput { struct InstanceInput {
@location(5) center: vec2<f32>, @location(4) center: vec2<f32>,
@location(6) radius: f32, @location(5) radius: f32,
@location(7) stroke_width: f32, @location(6) stroke_width: f32,
@location(8) stroke_color: vec4<f32>, @location(7) stroke_color: vec4<f32>,
@location(9) fill_color: vec4<f32>, @location(8) fill_color: vec4<f32>,
}; };
struct VertexOutput { struct VertexOutput {

View file

@ -103,18 +103,18 @@ impl CircleInstance {
attributes: &[ attributes: &[
wgpu::VertexAttribute { wgpu::VertexAttribute {
offset: 0, offset: 0,
shader_location: 5, shader_location: 4,
format: wgpu::VertexFormat::Float32x2, format: wgpu::VertexFormat::Float32x2,
}, },
wgpu::VertexAttribute { wgpu::VertexAttribute {
offset: std::mem::size_of::<[f32; 2]>() as wgpu::BufferAddress, offset: std::mem::size_of::<[f32; 2]>() as wgpu::BufferAddress,
shader_location: 6, shader_location: 5,
format: wgpu::VertexFormat::Float32, format: wgpu::VertexFormat::Float32,
}, },
wgpu::VertexAttribute { wgpu::VertexAttribute {
offset: (std::mem::size_of::<[f32; 2]>() + std::mem::size_of::<f32>()) offset: (std::mem::size_of::<[f32; 2]>() + std::mem::size_of::<f32>())
as wgpu::BufferAddress, as wgpu::BufferAddress,
shader_location: 7, shader_location: 6,
format: wgpu::VertexFormat::Float32, format: wgpu::VertexFormat::Float32,
}, },
wgpu::VertexAttribute { wgpu::VertexAttribute {
@ -122,7 +122,7 @@ impl CircleInstance {
+ std::mem::size_of::<f32>() + std::mem::size_of::<f32>()
+ std::mem::size_of::<f32>()) + std::mem::size_of::<f32>())
as wgpu::BufferAddress, as wgpu::BufferAddress,
shader_location: 8, shader_location: 7,
format: wgpu::VertexFormat::Float32x4, format: wgpu::VertexFormat::Float32x4,
}, },
wgpu::VertexAttribute { wgpu::VertexAttribute {
@ -131,7 +131,7 @@ impl CircleInstance {
+ std::mem::size_of::<f32>() + std::mem::size_of::<f32>()
+ std::mem::size_of::<[f32; 4]>()) + std::mem::size_of::<[f32; 4]>())
as wgpu::BufferAddress, as wgpu::BufferAddress,
shader_location: 9, shader_location: 8,
format: wgpu::VertexFormat::Float32x4, format: wgpu::VertexFormat::Float32x4,
}, },
], ],
@ -776,6 +776,8 @@ struct FrameBuilder<'a> {
output: wgpu::SurfaceTexture, output: wgpu::SurfaceTexture,
mode: DrawMode, mode: DrawMode,
fill_color: [f32; 4],
stroke_color: [f32; 4],
target: Rc<wgpu::TextureView>, target: Rc<wgpu::TextureView>,
color: Option<[f64; 4]>, color: Option<[f64; 4]>,
draw_calls: Vec<DrawCall>, draw_calls: Vec<DrawCall>,
@ -806,6 +808,8 @@ impl<'a> FrameBuilder<'a> {
output, output,
mode: DrawMode::Sprites, mode: DrawMode::Sprites,
stroke_color: [0.0, 0.0, 0.0, 1.0],
fill_color: [0.0, 0.0, 1.0, 1.0],
target: last_view, target: last_view,
color: None, color: None,
draw_calls: Vec::new(), draw_calls: Vec::new(),
@ -852,9 +856,15 @@ impl<'a> FrameBuilder<'a> {
self.start_pass(None, self.last_view.clone()); self.start_pass(None, self.last_view.clone());
} }
} }
GraphicsCommand::FillColor(c) => {
self.fill_color = c;
}
GraphicsCommand::StrokeColor(c) => {
self.stroke_color = c;
}
GraphicsCommand::Print(pc) => println!("{}", pc.text), GraphicsCommand::Print(pc) => println!("{}", pc.text),
GraphicsCommand::Sprite(si) => self.push_sprite(si), GraphicsCommand::Sprite(si) => self.push_sprite(si),
GraphicsCommand::Circle(ci) => self.push_circle(ci), GraphicsCommand::Circle(cc) => self.push_circle(cc),
GraphicsCommand::UseTexture(id) => self.use_texture(id), GraphicsCommand::UseTexture(id) => self.use_texture(id),
GraphicsCommand::EndFrame => self.flush(), GraphicsCommand::EndFrame => self.flush(),
@ -965,9 +975,17 @@ impl<'a> FrameBuilder<'a> {
self.state.circle_instance_buffers.get_mut(&vb) self.state.circle_instance_buffers.get_mut(&vb)
} }
fn push_circle(&mut self, ci: CircleInstance) { fn push_circle(&mut self, cc: script::graphics::CircleCommand) {
let stroke_color = self.stroke_color.clone();
let fill_color = self.fill_color.clone();
let vertex_buffer = self.get_circle_instance_buffer(); let vertex_buffer = self.get_circle_instance_buffer();
vertex_buffer.vec.push(ci); vertex_buffer.vec.push(CircleInstance {
center: cc.center,
radius: cc.radius,
stroke_width: cc.stroke_width,
stroke_color,
fill_color: [1.0, 1.0, 1.0, 1.0],
});
} }
fn flush(&mut self) { fn flush(&mut self) {

View file

@ -22,11 +22,20 @@ pub struct CreateTextureCommand {
pub label: Option<String>, pub label: Option<String>,
} }
#[derive(Debug)]
pub struct CircleCommand {
pub center: [f32; 2],
pub radius: f32,
pub stroke_width: f32,
}
#[derive(Debug)] #[derive(Debug)]
pub enum GraphicsCommand { pub enum GraphicsCommand {
Clear(ClearCommand), Clear(ClearCommand),
FillColor([f32; 4]),
StrokeColor([f32; 4]),
Print(PrintCommand), Print(PrintCommand),
Circle(crate::CircleInstance), Circle(CircleCommand),
Sprite(crate::SpriteInstance), Sprite(crate::SpriteInstance),
CreateTexture(CreateTextureCommand), CreateTexture(CreateTextureCommand),
CreateWritableTexture { CreateWritableTexture {
@ -66,6 +75,14 @@ impl GraphicsImpl {
})); }));
} }
fn color(&self, r: f32, g: f32, b: f32, a: f32) -> () {
let _ = self.sender.send(GraphicsCommand::FillColor([r, g, b, a]));
}
fn stroke(&self, r: f32, g: f32, b: f32, a: f32) -> () {
let _ = self.sender.send(GraphicsCommand::StrokeColor([r, g, b, a]));
}
fn spr(&self, x: f32, y: f32, w: f32, h: f32, u: f32, v: f32, sw: f32, sh: f32) { fn spr(&self, x: f32, y: f32, w: f32, h: f32, u: f32, v: f32, sw: f32, sh: f32) {
let _ = self let _ = self
.sender .sender
@ -78,14 +95,10 @@ impl GraphicsImpl {
} }
fn circle(&self, x: f32, y: f32, r: f32, s: f32) { fn circle(&self, x: f32, y: f32, r: f32, s: f32) {
let _ = self let _ = self.sender.send(GraphicsCommand::Circle(CircleCommand {
.sender
.send(GraphicsCommand::Circle(crate::CircleInstance {
center: [x, y], center: [x, y],
radius: r, radius: r,
stroke_width: s, stroke_width: s,
stroke_color: [1.0, 0.0, 0.0, 1.0],
fill_color: [1.0, 1.0, 1.0, 1.0],
})); }));
} }
@ -165,6 +178,24 @@ impl GraphicsAPI {
ctx.new_fn(move |_: &ContextRef, r: f64, g: f64, b: f64| gfx.cls(r, g, b))?, ctx.new_fn(move |_: &ContextRef, r: f64, g: f64, b: f64| gfx.cls(r, g, b))?,
)?; )?;
} }
{
let gfx = gfx.clone();
builder.export(
"color",
ctx.new_fn(move |_: &ContextRef, r: f32, g: f32, b: f32, a: f32| {
gfx.color(r, g, b, a)
})?,
)?;
}
{
let gfx = gfx.clone();
builder.export(
"stroke",
ctx.new_fn(move |_: &ContextRef, r: f32, g: f32, b: f32, a: f32| {
gfx.stroke(r, g, b, a)
})?,
)?;
}
{ {
let gfx = gfx.clone(); let gfx = gfx.clone();
builder.export( builder.export(

View file

@ -4,6 +4,10 @@ export function cls(r: number, g: number, b: number);
export function print(msg: string); export function print(msg: string);
export function color(r: number, g: number, b: number, a: number);
export function stroke(r: number, g: number, b: number, a: number);
export function spr( export function spr(
x: number, x: number,
y: number, y: number,