Skip to content

Commit

Permalink
fix: change wgsl shader to raw js string
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoiver committed Jan 9, 2024
1 parent 467759e commit 36ddeaa
Show file tree
Hide file tree
Showing 112 changed files with 1,259 additions and 204 deletions.
48 changes: 48 additions & 0 deletions __tests__/ecs/hierarchy.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Entity, System } from '@lastolivegames/becsy';
import {
App,
Children,
Commands,
HierarchyPlugin,
Parent,
StartUp,
} from '../../src';

describe('Hierarchy', () => {
it('test parent children relationship', async () => {
const app = new App({});
let parent_entity: Entity | undefined;
let child_entity: Entity | undefined;

class StartUpSystem extends System {
commands = new Commands(this);

q = this.query((q) => q.using(Parent, Children).write);
q2 = this.query((q) => q.current.with(Parent).read);

initialize(): void {
const parent = this.commands.spawn();
const child = this.commands.spawn();
parent.add_child(child.id());

parent_entity = parent.id().hold();
child_entity = child.id().hold();

this.commands.execute();
}

execute(): void {
expect(parent_entity?.read(Parent).children).toBe([child_entity]);
}
}

app.add_plugins(HierarchyPlugin);
app.add_systems(StartUp, StartUpSystem);
await app.run();

expect(parent_entity?.has(Parent)).toBeTruthy();
expect(child_entity?.has(Children)).toBeTruthy();

await app.exit();
});
});
24 changes: 24 additions & 0 deletions __tests__/math/mat4.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { to_radians } from '../../src/components/render/utils';
import { Vec3 } from '../../src/math/Vec3';
import { Mat4 } from '../../src/math/Mat4';
import { toBeDeepCloseTo, toMatchCloseTo } from 'jest-matcher-deep-close-to';
expect.extend({ toBeDeepCloseTo, toMatchCloseTo });

describe('Mat4', () => {
it('inverse', () => {
const inv = Mat4.IDENTITY.inverse();
expect(inv).toMatchCloseTo(Mat4.IDENTITY);

const rotz = Mat4.from_rotation_z(to_radians(90.0));
let rotz_inv = rotz.inverse();
expect(Mat4.IDENTITY).toMatchCloseTo(rotz.mul(rotz_inv));
expect(Mat4.IDENTITY).toMatchCloseTo(rotz_inv.mul(rotz));

let trans = Mat4.from_translation(new Vec3(1.0, 2.0, 3.0));
let trans_inv = trans.inverse();
// assert_ne!(None, trans_inv);
// let trans_inv = trans_inv.unwrap();
expect(Mat4.IDENTITY).toMatchCloseTo(trans.mul(trans_inv));
expect(Mat4.IDENTITY).toMatchCloseTo(trans_inv.mul(trans));
});
});
4 changes: 2 additions & 2 deletions __tests__/math/vec3.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Vec3 } from '../../src/math/Vec3';

describe('Math', () => {
it('Vec3', () => {
describe('Vec3', () => {
it('Create', () => {
const v1 = Vec3.ZERO;
expect(v1.x).toBe(0);
expect(v1.y).toBe(0);
Expand Down
4 changes: 4 additions & 0 deletions examples/demos/light.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export async function render($canvas: HTMLCanvasElement, gui: lil.GUI) {
);

initialize(): void {
// this.commands.insert_resource(new AmbientLight({
// color: Color.ORANGE_RED,
// brightness: 0.02,
// }));
ambient = this.commands
.spawn(
new AmbientLight({
Expand Down
5 changes: 4 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ module.exports = {
},
],
},
// transformIgnorePatterns: ['<rootDir>/node_modules/(?!lodash-es)'],
// @see https://stackoverflow.com/questions/42260218/jest-setup-syntaxerror-unexpected-token-export
moduleNameMapper: {
'^lodash-es$': 'lodash',
},
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"eslint-plugin-jest": "24.3.6",
"husky": "^7.0.4",
"jest": "^29.7.0",
"jest-matcher-deep-close-to": "3.0.2",
"lil-gui": "^0.16.1",
"lint-staged": "^10.5.4",
"markdownlint-cli": "^0.32.2",
Expand Down
48 changes: 48 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from './systems';
import { EventCtor, Events, EventsReader } from './Events';
import { Resource } from './Resource';
import { caf, raf } from './utils';

/**
* @see https://bevy-cheatbook.github.io/programming/app-builder.html
Expand Down Expand Up @@ -119,11 +120,15 @@ export class App {
/**
* Initialize a [`Resource`] with standard starting values by adding it to the [`World`].
*/
init_resource<K, R extends Resource>(key: K, resouce: R) {
this.resources.set(key, resouce);
init_resource<K, R extends Resource>(key: K, resource: R) {
this.resources.set(key, resource);
return this;
}

get_resource<K, R extends Resource>(key: K): R {
return this.resources.get(key) as R;
}

/**
* Start the app and run all systems.
*/
Expand Down Expand Up @@ -166,7 +171,7 @@ export class App {
// @see https://github.com/LastOliveGames/becsy/blob/main/tests/query.test.ts#L22C3-L22C58
// @ts-ignore
// if (import.meta.env.PROD) {
Object.defineProperty(s, 'name', { value: `_System${i}` });
// Object.defineProperty(s, 'name', { value: `_System${i}` });
// }
system(group)(s);
});
Expand All @@ -178,6 +183,7 @@ export class App {
threads: 1,
});

const requestAnimationFrame = raf;
const tick = async () => {
await this.world.execute();
this.rafId = requestAnimationFrame(tick);
Expand All @@ -192,6 +198,7 @@ export class App {
* @see https://bevy-cheatbook.github.io/programming/app-builder.html#quitting-the-app
*/
async exit() {
const cancelAnimationFrame = caf;
cancelAnimationFrame(this.rafId);
await this.world.terminate();
}
Expand Down
4 changes: 3 additions & 1 deletion src/commands/AddChild.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Entity, System } from '@lastolivegames/becsy';
import { Command } from './Command';
import { Children } from '../components';
import { Children, Parent } from '../components';

export class AddChild implements Command {
constructor(public parent: Entity, public child: Entity) {}

apply(system: System) {
this.parent.add(Parent);
// @see https://lastolivegames.github.io/becsy/guide/architecture/components.html#referencing-entities
this.child.add(Children, {
parent: this.parent,
});
Expand Down
6 changes: 6 additions & 0 deletions src/components/camera/Camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ export class Camera {
*/
@field.boolean declare hdr: boolean;

constructor() {
this.order = 0;
this.is_active = true;
this.hdr = false;
}

/**
* Converts a physical size in this `Camera` to a logical size.
*/
Expand Down
22 changes: 18 additions & 4 deletions src/components/camera/Projection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ export class Perspective {
}

get_frustum_corners(z_near: number, z_far: number) {
let tan_half_fov = Math.tan(this.fov / 2);
let a = Math.abs(z_near) * tan_half_fov;
let b = Math.abs(z_far) * tan_half_fov;
let aspect_ratio = this.aspect_ratio;
const tan_half_fov = Math.tan(this.fov / 2);
const a = Math.abs(z_near) * tan_half_fov;
const b = Math.abs(z_far) * tan_half_fov;
const aspect_ratio = this.aspect_ratio;
return [
new Vec3(a * aspect_ratio, -a, z_near), // bottom right
new Vec3(a * aspect_ratio, a, z_near), // top right
Expand Down Expand Up @@ -162,4 +162,18 @@ export class Orthographic {
this.scale * (projection_height - origin_y),
);
}

get_frustum_corners(z_near: number, z_far: number) {
const area = this.area;
return [
new Vec3(area.max.x, area.min.y, z_near), // bottom right
new Vec3(area.max.x, area.max.y, z_near), // top right
new Vec3(area.min.x, area.max.y, z_near), // top left
new Vec3(area.min.x, area.min.y, z_near), // bottom left
new Vec3(area.max.x, area.min.y, z_far), // bottom right
new Vec3(area.max.x, area.max.y, z_far), // top right
new Vec3(area.min.x, area.max.y, z_far), // top left
new Vec3(area.min.x, area.min.y, z_far), // bottom left
] as const;
}
}
3 changes: 3 additions & 0 deletions src/components/hierarchy/Children.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Entity, field } from '@lastolivegames/becsy';

/**
* @see bevy_hierarchy Children
*/
export class Children {
@field.ref declare parent: Entity;
}
3 changes: 3 additions & 0 deletions src/components/hierarchy/Parent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ import { Entity, field } from '@lastolivegames/becsy';
import { Children } from './Children';

export class Parent {
/**
* The backrefs field type lets you build 1-N relationships where the N is unbounded.
*/
@field.backrefs(Children, 'parent') declare children: Entity[];
}
4 changes: 3 additions & 1 deletion src/components/light/CascadeShadowConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,7 @@ function calculate_cascade_bounds(
shadow_maximum_distance / nearest_bound,
1.0 / (num_cascades - 1),
);
return new Array(num_cascades).map((i) => nearest_bound * Math.pow(base, i));
return new Array(num_cascades)
.fill(undefined)
.map((_, i) => nearest_bound * Math.pow(base, i));
}
8 changes: 8 additions & 0 deletions src/components/light/DirectionalLight.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { field } from '@lastolivegames/becsy';
import { Color } from '../render';
import { Resource } from '../../Resource';

/// A Directional light.
///
Expand Down Expand Up @@ -88,3 +89,10 @@ export class DirectionalLight {
this.shadow_normal_bias = shadow_normal_bias;
}
}

/**
* Controls the resolution of [`DirectionalLight`] shadow maps.
*/
export class DirectionalLightShadowMap implements Resource {
constructor(public size: number = 2048) {}
}
4 changes: 2 additions & 2 deletions src/components/pbr/Material.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { field } from '@lastolivegames/becsy';
import { CullMode } from '@antv/g-device-api';
import mesh_shader from '../../shaders/mesh/mesh.wgsl?raw';
import mesh_pbr_shader from '../../shaders/pbr/pbr.wgsl?raw';
import mesh_shader from '../../shaders/mesh/mesh';
import mesh_pbr_shader from '../../shaders/pbr/pbr';
import { Color } from '../render';
import { AlphaMode } from './Alpha';
import { count_ones } from '../render/utils';
Expand Down
4 changes: 2 additions & 2 deletions src/components/primitive/Aabb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ export class Aabb {
* ```
*/
static enclosing(positions: [number, number, number][]) {
let min = Vec3.MIN;
let max = Vec3.MAX;
let min = Vec3.MAX;
let max = Vec3.MIN;
positions.forEach((p) => {
min = min.min(Vec3.from_array(p));
max = max.max(Vec3.from_array(p));
Expand Down
3 changes: 2 additions & 1 deletion src/components/primitive/CascadesFrusta.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Entity, field } from '@lastolivegames/becsy';
import { Frustum } from './Frustum';

export class CascadesFrusta {
frusta: Record<number, Frustum[]>;
@field.object declare frusta: Map<Entity, Frustum[]>;
}
Loading

0 comments on commit 36ddeaa

Please sign in to comment.