Skip to content

Commit 04c0334

Browse files
committed
Bevy 0.7 running, but colors in 3d are strange. (#17).
1 parent 1c08212 commit 04c0334

File tree

6 files changed

+132
-90
lines changed

6 files changed

+132
-90
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ version = "0.6.1"
1313
exclude = ["demo.gif", "demo_2.png", "demo_2.webm"]
1414

1515
[dependencies]
16-
bevy = { version = "0.6", default-features = false, features = [ "render" ] }
16+
bevy = { version = "0.7", default-features = false, features = [ "bevy_core_pipeline", "bevy_render", "bevy_pbr", "bevy_sprite" ] }
1717

1818
[features]
1919
example_deps_2d = [

examples/3d.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ fn main() {
66
App::new()
77
.insert_resource(Msaa { samples: 4 })
88
.add_plugins(DefaultPlugins)
9-
//.add_plugin(DebugLinesPlugin::default())
10-
//.add_startup_system(setup)
11-
//.add_system(demo)
12-
//.run();
9+
.add_plugin(DebugLinesPlugin::default())
10+
.add_startup_system(setup)
11+
.add_system(demo)
12+
.run();
1313
}
1414

1515
fn setup(mut commands: Commands, mut lines: ResMut<DebugLines>) {

examples/bench.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use bevy::diagnostic::FrameTimeDiagnosticsPlugin;
22
use bevy::diagnostic::LogDiagnosticsPlugin;
33
use bevy::prelude::*;
4+
use bevy::window::PresentMode;
45

56
use bevy_prototype_debug_lines::{DebugLines, DebugLinesPlugin};
67

78
fn main() {
89
App::new()
910
.insert_resource(WindowDescriptor {
10-
vsync: false,
11+
present_mode: PresentMode::Immediate,
1112
..Default::default()
1213
})
1314
.add_plugins(DefaultPlugins)

src/debuglines.wgsl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
#endif
88

99
struct Vertex {
10-
[[location(0)]] color: vec4<f32>;
11-
[[location(1)]] pos: vec3<f32>;
10+
[[location(0)]] pos: vec3<f32>;
11+
[[location(1)]] color: u32;
1212
};
1313

1414
struct VertexOutput {
@@ -25,7 +25,9 @@ struct FragmentOutput {
2525
fn vertex(vertex: Vertex) -> VertexOutput {
2626
var out: VertexOutput;
2727
out.clip_position = view.view_proj * vec4<f32>(vertex.pos, 1.0);
28-
out.color = vertex.color;
28+
//out.color = vertex.color;
29+
// https://github.com/bevyengine/bevy/blob/328c26d02c50de0bc77f0d24a376f43ba89517b1/examples/2d/mesh2d_manual.rs#L234
30+
out.color = vec4<f32>((vec4<u32>(vertex.color) >> vec4<u32>(8u, 8u, 16u, 24u)) & vec4<u32>(255u)) / 255.0;
2931

3032
return out;
3133
}

src/debuglines2d.wgsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ fn vertex(vertex: Vertex) -> VertexOutput {
1818
var out: VertexOutput;
1919
out.clip_position = view.view_proj * vec4<f32>(vertex.place, 1.0);
2020
// What is this craziness?
21-
//out.color = vec4<f32>((vec4<u32>(vertex.color) >> vec4<u32>(0u, 8u, 16u, 24u)) & vec4<u32>(255u)) / 255.0;
21+
out.color = vec4<f32>((vec4<u32>(vertex.color) >> vec4<u32>(0u, 8u, 16u, 24u)) & vec4<u32>(255u)) / 255.0;
2222
//out.color = vertex.color;
23-
out.color = vec4<f32>(1.0, 0.0, 0.0, 1.0);
23+
//out.color = vec4<f32>(1.0, 0.0, 0.0, 1.0);
2424

2525
return out;
2626
}

src/render_dim.rs

Lines changed: 118 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,7 @@ pub mod r3d {
22
use bevy::{core_pipeline::Opaque3d, pbr::{
33
DrawMesh, MeshPipeline, MeshPipelineKey, MeshUniform, SetMeshBindGroup,
44
SetMeshViewBindGroup,
5-
}, prelude::*, render::{
6-
mesh::MeshVertexBufferLayout,
7-
render_asset::RenderAssets,
8-
render_phase::{DrawFunctions, RenderPhase, SetItemPipeline},
9-
render_resource::{
10-
PipelineCache, PrimitiveTopology, RenderPipelineDescriptor,
11-
SpecializedMeshPipeline, SpecializedMeshPipelineError, SpecializedMeshPipelines,
12-
VertexAttribute, VertexBufferLayout, VertexFormat, VertexStepMode,
13-
},
14-
view::{ExtractedView, Msaa},
15-
}, utils::Hashed};
5+
}, prelude::*, render::{mesh::MeshVertexBufferLayout, render_asset::RenderAssets, render_phase::{DrawFunctions, RenderPhase, SetItemPipeline}, render_resource::{BlendState, ColorTargetState, ColorWrites, CompareFunction, DepthBiasState, DepthStencilState, FragmentState, FrontFace, MultisampleState, PipelineCache, PolygonMode, PrimitiveState, PrimitiveTopology, RenderPipelineDescriptor, SpecializedMeshPipeline, SpecializedMeshPipelineError, SpecializedMeshPipelines, StencilFaceState, StencilState, TextureFormat, VertexAttribute, VertexBufferLayout, VertexFormat, VertexState, VertexStepMode}, texture::BevyDefault, view::{ExtractedView, Msaa}}, utils::Hashed};
166

177
use crate::{DebugLinesConfig, RenderDebugLinesMesh, DEBUG_LINES_SHADER_HANDLE};
188

@@ -48,23 +38,76 @@ pub mod r3d {
4838
shader_defs.push("DEPTH_TEST_ENABLED".to_string());
4939
}
5040

51-
let mut descriptor = self.mesh_pipeline.specialize(key, &layout)?;
52-
descriptor.vertex.shader = self.shader.clone_weak();
53-
descriptor.vertex.shader_defs = shader_defs.clone();
54-
let formats = vec![
55-
VertexFormat::Uint32,
56-
VertexFormat::Float32x3,
57-
];
58-
descriptor.vertex.buffers[0] = VertexBufferLayout::from_vertex_formats(VertexStepMode::Vertex, formats);
59-
let fragment = descriptor.fragment.as_mut().unwrap();
60-
fragment.shader = self.shader.clone_weak();
61-
fragment.shader_defs = shader_defs.clone();
62-
descriptor.primitive.topology = PrimitiveTopology::LineList;
63-
descriptor.primitive.cull_mode = None;
64-
//if self.always_in_front {
65-
//descriptor.depth_stencil.as_mut().unwrap().bias.constant = i32::MAX;
66-
//}
67-
Ok(descriptor)
41+
let vertex_buffer_layout = layout.get_layout(&[
42+
Mesh::ATTRIBUTE_POSITION.at_shader_location(0),
43+
Mesh::ATTRIBUTE_COLOR.at_shader_location(1),
44+
])?;
45+
let (label, blend, depth_write_enabled);
46+
if key.contains(MeshPipelineKey::TRANSPARENT_MAIN_PASS) {
47+
label = "transparent_mesh_pipeline".into();
48+
blend = Some(BlendState::ALPHA_BLENDING);
49+
// For the transparent pass, fragments that are closer will be alpha
50+
// blended but their depth is not written to the depth buffer.
51+
depth_write_enabled = false;
52+
} else {
53+
label = "opaque_mesh_pipeline".into();
54+
blend = Some(BlendState::REPLACE);
55+
// For the opaque and alpha mask passes, fragments that are closer
56+
// will replace the current fragment value in the output and the depth is
57+
// written to the depth buffer.
58+
depth_write_enabled = true;
59+
}
60+
61+
Ok(RenderPipelineDescriptor {
62+
vertex: VertexState {
63+
shader: self.shader.clone_weak(),
64+
entry_point: "vertex".into(),
65+
shader_defs: shader_defs.clone(),
66+
buffers: vec![vertex_buffer_layout],
67+
},
68+
fragment: Some(FragmentState {
69+
shader: self.shader.clone_weak(),
70+
shader_defs,
71+
entry_point: "fragment".into(),
72+
targets: vec![ColorTargetState {
73+
format: TextureFormat::bevy_default(),
74+
blend,
75+
write_mask: ColorWrites::ALL,
76+
}],
77+
}),
78+
layout: Some(vec![self.mesh_pipeline.view_layout.clone()]),
79+
primitive: PrimitiveState {
80+
front_face: FrontFace::Ccw,
81+
cull_mode: None,
82+
unclipped_depth: false,
83+
polygon_mode: PolygonMode::Fill,
84+
conservative: false,
85+
topology: PrimitiveTopology::LineList,
86+
strip_index_format: None,
87+
},
88+
depth_stencil: Some(DepthStencilState {
89+
format: TextureFormat::Depth32Float,
90+
depth_write_enabled,
91+
depth_compare: CompareFunction::Greater,
92+
stencil: StencilState {
93+
front: StencilFaceState::IGNORE,
94+
back: StencilFaceState::IGNORE,
95+
read_mask: 0,
96+
write_mask: 0,
97+
},
98+
bias: DepthBiasState {
99+
constant: 0,
100+
slope_scale: 0.0,
101+
clamp: 0.0,
102+
},
103+
}),
104+
multisample: MultisampleState {
105+
count: key.msaa_samples(),
106+
mask: !0,
107+
alpha_to_coverage_enabled: false,
108+
},
109+
label: Some(label),
110+
})
68111
}
69112
}
70113

@@ -79,7 +122,6 @@ pub mod r3d {
79122
config: Res<DebugLinesConfig>,
80123
mut views: Query<(&ExtractedView, &mut RenderPhase<Opaque3d>)>,
81124
) {
82-
dbg!("start queue");
83125
let draw_custom = opaque_3d_draw_functions
84126
.read()
85127
.get_id::<DrawDebugLines>()
@@ -107,7 +149,6 @@ pub mod r3d {
107149
}
108150
}
109151
}
110-
dbg!("end queue");
111152
}
112153

113154
pub(crate) type DrawDebugLines = (
@@ -119,17 +160,7 @@ pub mod r3d {
119160
}
120161

121162
pub mod r2d {
122-
use bevy::{asset::Handle, core::FloatOrd, core_pipeline::Transparent2d, prelude::*, render::{
123-
mesh::MeshVertexBufferLayout,
124-
render_asset::RenderAssets,
125-
render_phase::{DrawFunctions, RenderPhase, SetItemPipeline},
126-
render_resource::{
127-
PipelineCache, PrimitiveTopology, RenderPipelineDescriptor, Shader,
128-
SpecializedMeshPipeline, SpecializedMeshPipelineError, SpecializedMeshPipelines,
129-
VertexAttribute, VertexBufferLayout, VertexFormat, VertexStepMode,
130-
},
131-
view::{Msaa, VisibleEntities},
132-
}, sprite::{DrawMesh2d, Mesh2dHandle, Mesh2dPipeline, Mesh2dPipelineKey, Mesh2dUniform, SetMesh2dBindGroup, SetMesh2dViewBindGroup}};
163+
use bevy::{asset::Handle, core::FloatOrd, core_pipeline::Transparent2d, prelude::*, render::{mesh::MeshVertexBufferLayout, render_asset::RenderAssets, render_phase::{DrawFunctions, RenderPhase, SetItemPipeline}, render_resource::{BlendState, ColorTargetState, ColorWrites, CompareFunction, DepthBiasState, DepthStencilState, FragmentState, FrontFace, MultisampleState, PipelineCache, PolygonMode, PrimitiveState, PrimitiveTopology, RenderPipelineDescriptor, Shader, SpecializedMeshPipeline, SpecializedMeshPipelineError, SpecializedMeshPipelines, StencilFaceState, StencilState, TextureFormat, VertexAttribute, VertexBufferLayout, VertexFormat, VertexState, VertexStepMode}, texture::BevyDefault, view::{Msaa, VisibleEntities}}, sprite::{DrawMesh2d, Mesh2dHandle, Mesh2dPipeline, Mesh2dPipelineKey, Mesh2dUniform, SetMesh2dBindGroup, SetMesh2dViewBindGroup}};
133164

134165
use crate::{RenderDebugLinesMesh, DEBUG_LINES_SHADER_HANDLE};
135166

@@ -157,47 +188,55 @@ pub mod r2d {
157188
key: Self::Key,
158189
layout: &MeshVertexBufferLayout,
159190
) -> Result<RenderPipelineDescriptor, SpecializedMeshPipelineError> {
160-
//use VertexFormat::{Float32x3, Float32x4};
161191

162-
//let mut shader_defs = Vec::new();
163-
//shader_defs.push("2D".to_string());
164-
165-
let mut descriptor = self.mesh_pipeline.specialize(key, layout)?;
166-
descriptor.vertex.shader = self.shader.clone_weak();
167-
let formats = vec![
168-
VertexFormat::Float32x3,
169-
VertexFormat::Uint32,
170-
];
171-
descriptor.vertex.buffers[0] = VertexBufferLayout::from_vertex_formats(VertexStepMode::Vertex, formats);
172-
//descriptor.vertex.shader_defs = shader_defs.clone();
173192
/*
174-
descriptor.vertex.buffers[0] = VertexBufferLayout {
175-
// NOTE: I've no idea why, but `color` is at offset zero and
176-
// `position` at 4*4. Swapping breaks everything
177-
array_stride: 4 * 4 + 4 * 3, // sizeof(Float32x4) + sizeof(Float32x3)
178-
step_mode: VertexStepMode::Vertex,
179-
attributes: vec![
180-
VertexAttribute {
181-
// Vertex.color
182-
format: Float32x4,
183-
offset: 0,
184-
shader_location: 0,
185-
},
186-
VertexAttribute {
187-
// Vertex.place (position)
188-
format: Float32x3,
189-
offset: 4 * 4, // sizeof(Float32x4)
190-
shader_location: 1,
191-
},
192-
],
193-
};
193+
let mut shader_defs = Vec::new();
194+
shader_defs.push("LINES_3D".to_string());
195+
if depth_test {
196+
shader_defs.push("DEPTH_TEST_ENABLED".to_string());
197+
}
194198
*/
195-
let fragment = descriptor.fragment.as_mut().unwrap();
196-
fragment.shader = self.shader.clone_weak();
197-
//fragment.shader_defs = shader_defs.clone();
198-
descriptor.primitive.topology = PrimitiveTopology::LineList;
199-
descriptor.primitive.cull_mode = None;
200-
Ok(descriptor)
199+
200+
let vertex_buffer_layout = layout.get_layout(&[
201+
Mesh::ATTRIBUTE_POSITION.at_shader_location(0),
202+
Mesh::ATTRIBUTE_COLOR.at_shader_location(1),
203+
])?;
204+
205+
Ok(RenderPipelineDescriptor {
206+
vertex: VertexState {
207+
shader: self.shader.clone_weak(),
208+
entry_point: "vertex".into(),
209+
shader_defs: vec![],
210+
buffers: vec![vertex_buffer_layout],
211+
},
212+
fragment: Some(FragmentState {
213+
shader: self.shader.clone_weak(),
214+
shader_defs: vec![],
215+
entry_point: "fragment".into(),
216+
targets: vec![ColorTargetState {
217+
format: TextureFormat::bevy_default(),
218+
blend: Some(BlendState::ALPHA_BLENDING),
219+
write_mask: ColorWrites::ALL,
220+
}],
221+
}),
222+
layout: Some(vec![self.mesh_pipeline.view_layout.clone()]),
223+
primitive: PrimitiveState {
224+
front_face: FrontFace::Ccw,
225+
cull_mode: None,
226+
unclipped_depth: false,
227+
polygon_mode: PolygonMode::Fill,
228+
conservative: false,
229+
topology: PrimitiveTopology::LineList,
230+
strip_index_format: None,
231+
},
232+
depth_stencil: None,
233+
multisample: MultisampleState {
234+
count: key.msaa_samples(),
235+
mask: !0,
236+
alpha_to_coverage_enabled: false,
237+
},
238+
label: None,
239+
})
201240
}
202241
}
203242

0 commit comments

Comments
 (0)