Skip to content

Commit

Permalink
Add building props (windows etc.)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeplay committed Jan 8, 2019
1 parent c50ab00 commit 8fdc375
Show file tree
Hide file tree
Showing 6 changed files with 406 additions and 58 deletions.
2 changes: 1 addition & 1 deletion cb_browser_ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ stdweb = "0.4.9"
serde = "1.0"
serde_derive = "1.0"
compact_macros = "0.1.0"
kay = {version = "0.4.0", default-features = false, features = ["browser"]}
kay = {version = "0.4.1", default-features = false, features = ["browser"]}

[dependencies.cb_simulation]
path = "../cb_simulation"
Expand Down
37 changes: 32 additions & 5 deletions cb_browser_ui/src/land_use_browser/LandUse.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
import colors from '../colors';
import renderOrder from '../renderOrder';
import { RenderLayer } from "../browser_utils/Utils";
import * as propMeshes from './propMeshes';

const MATERIALS = ["WhiteWall", "TiledRoof", "FlatRoof", "FieldWheat", "FieldRows", "FieldPlant", "FieldMeadow", "WoodenFence", "MetalFence", "LotAsphalt"];
const initialRenderingState = {};
const PROP_TYPES = ["SmallWindow", "ShopWindowGlass", "ShopWindowBanner", "NarrowDoor", "WideDoor"];

const initialRenderingState = {
buildingMeshes: {},
buildingProps: {},
propMeshes: {
SmallWindow: propMeshes.smallWindow,
NarrowDoor: propMeshes.narrowDoor,
WideDoor: propMeshes.wideDoor,
ShopWindowBanner: propMeshes.shopWindowBanner,
ShopWindowGlass: propMeshes.shopWindowGlass,
}
};
const materialInstances = {};

for (let material of MATERIALS) {
initialRenderingState[material] = {}
initialRenderingState.buildingMeshes[material] = {};
materialInstances[material] = new Float32Array([0.0, 0.0, 0.0, 1.0, 0.0, ...colors[material]]);
}

for (let propType of PROP_TYPES) {
initialRenderingState.buildingProps[propType] = {};
}

export const initialState = {
rendering: initialRenderingState
}
Expand All @@ -25,9 +42,19 @@ export function Layers(props) {
key={material}
decal={false}
renderOrder={material.startsWith("Field") ? renderOrder.buildingGround : renderOrder.building3D}
batches={Object.values(state.landUse.rendering[material]).map(housePart => ({
mesh: housePart,
batches={Object.values(state.landUse.rendering.buildingMeshes[material]).map(buildingPart => ({
mesh: buildingPart,
instances: materialInstances[material]
}))} />
);
).concat(PROP_TYPES.map(propType =>
<RenderLayer
key={propType}
decal={false}
renderOrder={renderOrder.building3D}
batches={[{
mesh: state.landUse.rendering.propMeshes[propType],
instances: new Float32Array(Object.values(state.landUse.rendering.buildingProps[propType])
.reduce((allPropInstances, buildingPropInstances) => allPropInstances.concat(buildingPropInstances), []))
}]} />
));
}
39 changes: 32 additions & 7 deletions cb_browser_ui/src/land_use_browser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use compact::CVec;
use stdweb::serde::Serde;
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
use stdweb::js_export;
use browser_utils::to_js_mesh;
use browser_utils::{to_js_mesh, flatten_instances};
use SYSTEM;
use ::std::collections::HashMap;

Expand Down Expand Up @@ -43,11 +43,11 @@ impl LandUseUI for BrowserLandUseUI {
style: ::land_use::buildings::BuildingStyle,
world: &mut World,
) {
let meshes =
let building_mesh =
::land_use::buildings::architecture::build_building(lot, style, households, world);

let material_updates: ::stdweb::Object = meshes
.0
let material_updates: ::stdweb::Object = building_mesh
.meshes
.into_iter()
.map(|(material, mesh)| {
let update_op: ::stdweb::Object = Some(("$set", to_js_mesh(&mesh)))
Expand All @@ -63,9 +63,26 @@ impl LandUseUI for BrowserLandUseUI {
.collect::<HashMap<_, _>>()
.into();

let prop_updates: ::stdweb::Object = building_mesh.props.into_iter().map(|(prop_type, instances)| {
let update_op: ::stdweb::Object = Some(("$set", flatten_instances(&instances)))
.into_iter()
.collect::<HashMap<_, _>>()
.into();
let material_update: ::stdweb::Object = Some((id.as_raw_string(), update_op))
.into_iter()
.collect::<HashMap<_, _>>()
.into();
(prop_type.to_string(), material_update)
})
.collect::<HashMap<_, _>>()
.into();;

js! {
window.cbReactApp.boundSetState(oldState => update(oldState, {
landUse: {rendering: @{material_updates}},
landUse: {rendering: {
buildingMeshes: @{material_updates},
buildingProps: @{prop_updates}
}},
households: {
buildingPositions: {[@{Serde(id)}]: {
"$set": @{Serde(lot.center_point())}
Expand All @@ -87,14 +104,22 @@ impl LandUseUI for BrowserLandUseUI {
.into_iter()
.collect::<HashMap<_, _>>()
.into();
let unsets: ::stdweb::Object = ::land_use::buildings::architecture::ALL_MATERIALS
let material_unsets: ::stdweb::Object = ::land_use::buildings::architecture::ALL_MATERIALS
.iter()
.map(|material| (material.to_string(), unset_op.clone()))
.collect::<HashMap<_, _>>()
.into();
let prop_unsets: ::stdweb::Object = ::land_use::buildings::architecture::ALL_PROP_TYPES
.iter()
.map(|prop_type| (prop_type.to_string(), unset_op.clone()))
.collect::<HashMap<_, _>>()
.into();
js! {
window.cbReactApp.boundSetState(oldState => update(oldState, {
landUse: {rendering: @{unsets}},
landUse: {rendering: {
buildingMeshes: @{material_unsets},
buildingProps: @{prop_unsets}
}},
households: {buildingPositions: {"$unset": [@{Serde(id)}]}}
}));
}
Expand Down
64 changes: 64 additions & 0 deletions cb_browser_ui/src/land_use_browser/propMeshes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
export const smallWindow = {
vertices: new Float32Array([
-0.7, 0.1, 1.2,
0.7, 0.1, 1.2,
0.7, 0.1, 2.5,
-0.7, 0.1, 2.5,
]),
indices: new Uint16Array([
0, 1, 2,
0, 2, 3
])
}

export const narrowDoor = {
vertices: new Float32Array([
-0.6, 0.1, 0.0,
0.6, 0.1, 0.0,
0.6, 0.1, 2.4,
-0.6, 0.1, 2.4,
]),
indices: new Uint16Array([
0, 1, 2,
0, 2, 3
])
}

export const wideDoor = {
vertices: new Float32Array([
-1.0, 0.1, 0.0,
1.0, 0.1, 0.0,
1.0, 0.1, 2.4,
-1.0, 0.1, 2.4,
]),
indices: new Uint16Array([
0, 1, 2,
0, 2, 3
])
}

export const shopWindowBanner = {
vertices: new Float32Array([
-1.5, 0.1, 0.5,
1.5, 0.1, 0.5,
1.5, 0.1, 1.5,
-1.5, 0.1, 1.5,
]),
indices: new Uint16Array([
0, 1, 2,
0, 2, 3
])
}

export const shopWindowGlass = {
vertices: new Float32Array([
-1.5, 0.1, 1.5,
1.5, 0.1, 1.5,
1.5, 0.1, 2.6,
-1.5, 0.1, 2.6,
]),
indices: new Uint16Array([
0, 1, 2,
0, 2, 3
])
}
17 changes: 8 additions & 9 deletions cb_browser_ui/src/vegetation_browser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use kay::{World, ActorSystem, TypedID, RawID, External};
use environment::vegetation::{PlantID, PlantPrototype, VegetationType};
use environment::vegetation::ui::{VegetationUI, VegetationUIID};
use browser_utils::to_js_mesh;
use stdweb::serde::Serde;
use descartes::{P2, LinePath, ClosedLinePath, PrimitiveArea};
use michelangelo::{SculptLine, Sculpture, FlatSurface, Instance};
use michelangelo::{Sculpture, FlatSurface, Instance};
use std::collections::HashMap;
use browser_utils::{FrameListener, FrameListenerID, flatten_instances};

Expand Down Expand Up @@ -55,16 +54,16 @@ impl BrowserVegetationUI {
),
0.0,
);
let (_, trunk_roots_surface) = trunk_base.extrude(0.0, 1.0);
let (_, trunk_roots_surface) = trunk_base.extrude(0.0, 0.8);
let (side_surface, top_surface) = trunk_base.extrude(2.0, -0.1);
let trunk_mesh =
Sculpture::new(vec![trunk_roots_surface.into(), side_surface.into()]).to_mesh();

let medium_canopy_base = top_surface;
let (medium_canopy_wall_1, medium_canopy_middle_1) = medium_canopy_base.extrude(1.0, 4.0);
let (medium_canopy_wall_1, medium_canopy_middle_1) = medium_canopy_base.extrude(0.8, 3.2);
let (medium_canopy_wall_2, medium_canopy_middle_2) =
medium_canopy_middle_1.extrude(3.0, 0.0);
let (medium_canopy_wall_3, medium_canopy_top) = medium_canopy_middle_2.extrude(3.0, -2.0);
medium_canopy_middle_1.extrude(2.4, 0.0);
let (medium_canopy_wall_3, medium_canopy_top) = medium_canopy_middle_2.extrude(2.4, -1.6);

let medium_canopy_mesh = Sculpture::new(vec![
medium_canopy_wall_1.into(),
Expand All @@ -75,9 +74,9 @@ impl BrowserVegetationUI {
.to_mesh();

let (_, small_canopy_base) = medium_canopy_base.extrude(-1.0, 0.0);
let (small_canopy_wall_1, small_canopy_middle_1) = small_canopy_base.extrude(1.0, 3.0);
let (small_canopy_wall_2, small_canopy_middle_2) = small_canopy_middle_1.extrude(2.0, 0.0);
let (small_canopy_wall_3, small_canopy_top) = small_canopy_middle_2.extrude(2.0, -2.0);
let (small_canopy_wall_1, small_canopy_middle_1) = small_canopy_base.extrude(0.8, 2.4);
let (small_canopy_wall_2, small_canopy_middle_2) = small_canopy_middle_1.extrude(1.6, 0.0);
let (small_canopy_wall_3, small_canopy_top) = small_canopy_middle_2.extrude(1.6, -1.6);

let small_canopy_mesh = Sculpture::new(vec![
small_canopy_wall_1.into(),
Expand Down
Loading

0 comments on commit 8fdc375

Please sign in to comment.