Skip to content

Commit

Permalink
Update to engine v2 (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
slimbuck authored Oct 29, 2024
1 parent c243ca0 commit e076268
Show file tree
Hide file tree
Showing 46 changed files with 618 additions and 1,075 deletions.
3 changes: 0 additions & 3 deletions .eslintignore

This file was deleted.

19 changes: 0 additions & 19 deletions .eslintrc.json

This file was deleted.

39 changes: 39 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import typescriptEslint from "@typescript-eslint/eslint-plugin";
import tsParser from "@typescript-eslint/parser";
import path from "node:path";
import { fileURLToPath } from "node:url";
import js from "@eslint/js";
import { FlatCompat } from "@eslint/eslintrc";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
allConfig: js.configs.all
});

export default [{
ignores: ["**/node_modules", "**/dist", "**/build"],
}, ...compat.extends(
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
), {
plugins: {
"@typescript-eslint": typescriptEslint,
},

languageOptions: {
parser: tsParser,
},

rules: {
"no-empty": "off",
"no-prototype-builtins": "off",
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/no-unsafe-function-type": "off",
},
}];
799 changes: 338 additions & 461 deletions package-lock.json

Large diffs are not rendered by default.

26 changes: 14 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,40 @@
"develop:local": "cross-env ENGINE_PATH=../engine npm run develop",
"build:local": "cross-env ENGINE_PATH=../engine npm run build",
"watch:local": "cross-env ENGINE_PATH=../engine npm run watch",
"lint": "eslint --ext .ts src",
"lint": "eslint src eslint.config.mjs",
"test": "jest"
},
"devDependencies": {
"@playcanvas/eslint-config": "^1.7.4",
"@eslint/eslintrc": "^3.1.0",
"@eslint/js": "^9.13.0",
"@playcanvas/eslint-config": "^2.0.6",
"@playcanvas/pcui": "^4.5.1",
"@rollup/plugin-alias": "^5.1.1",
"@rollup/plugin-image": "^3.0.3",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.3.0",
"@rollup/plugin-strip": "^3.0.4",
"@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-typescript": "^12.1.0",
"@rollup/plugin-typescript": "^12.1.1",
"@types/wicg-file-system-access": "^2023.10.5",
"@typescript-eslint/eslint-plugin": "^7.15.0",
"@typescript-eslint/parser": "^7.15.0",
"@typescript-eslint/eslint-plugin": "^8.12.2",
"@typescript-eslint/parser": "^8.12.2",
"autoprefixer": "^10.4.20",
"concurrently": "^9.0.1",
"cors": "^2.8.5",
"cross-env": "^7.0.3",
"eslint": "^8.56.0",
"i18next": "^23.15.1",
"eslint": "^9.13.0",
"i18next": "^23.16.4",
"i18next-browser-languagedetector": "^8.0.0",
"jest": "^29.7.0",
"playcanvas": "^2.2.0",
"jszip": "^3.10.1",
"playcanvas": "^1.74.0",
"postcss": "^8.4.47",
"rollup": "^4.23.0",
"rollup": "^4.24.2",
"rollup-plugin-postcss": "^4.0.2",
"rollup-plugin-visualizer": "^5.12.0",
"sass": "^1.79.4",
"serve": "^14.2.3",
"tslib": "^2.7.0"
"sass": "^1.80.4",
"serve": "^14.2.4",
"tslib": "^2.8.0"
}
}
1 change: 0 additions & 1 deletion src/asset-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ class AssetLoader {
contents: loadRequest.contents
},
{
elementFilter: this.loadAllData ? (() => true) : null,
// decompress data on load
decompress: true
}
Expand Down
60 changes: 37 additions & 23 deletions src/camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
PIXELFORMAT_DEPTH,
BoundingBox,
Entity,
EventHandler,
Layer,
Mat4,
Picker,
Plane,
Expand Down Expand Up @@ -74,12 +74,11 @@ class Camera extends Element {
minElev = -90;
maxElev = 90;

events = new EventHandler();

sceneRadius = 5;

picker: Picker;
pickModeRenderTarget: RenderTarget;

workRenderTarget: RenderTarget;

updateCameraUniforms: () => void;

Expand Down Expand Up @@ -231,7 +230,7 @@ class Camera extends Element {
this.maxElev = (controls.maxPolarAngle * 180) / Math.PI - 90;

// tonemapping
this.scene.app.scene.toneMapping = {
this.scene.app.scene.rendering.toneMapping = {
linear: TONEMAP_LINEAR,
filmic: TONEMAP_FILMIC,
hejl: TONEMAP_HEJL,
Expand All @@ -252,37 +251,51 @@ class Camera extends Element {
const { width, height } = this.scene.targetSize;
this.picker = new Picker(this.scene.app, width, height);

// override buffer allocation to use our render target
this.picker.allocateRenderTarget = () => { };
this.picker.releaseRenderTarget = () => { };

this.scene.events.on('scene.boundChanged', this.onBoundChanged, this);

// multiple elements in the scene require this callback
this.entity.camera.onPreRenderLayer = (layer: Layer, transparent: boolean) => {
this.scene.events.fire('camera.preRenderLayer', layer, transparent);
};

// prepare camera-specific uniforms
this.updateCameraUniforms = () => {
const device = this.scene.graphicsDevice;
const entity = this.entity;
const camera = entity.camera;

const inv = new Mat4().mul2(camera.projectionMatrix, camera.viewMatrix).invert();

const set = (name: string, vec: Vec3) => {
device.scope.resolve(name).setValue([vec.x, vec.y, vec.z]);
};

// get frustum corners in world space
const points = camera.camera.getFrustumCorners(-100);
const worldTransform = entity.getWorldTransform();
for (let i = 0; i < points.length; i++) {
worldTransform.transformPoint(points[i], points[i]);
}

// near
if (camera.projection === PROJECTION_PERSPECTIVE) {
// perspective
set('near_origin', entity.getPosition());
set('near_origin', worldTransform.getTranslation());
set('near_x', Vec3.ZERO);
set('near_y', Vec3.ZERO);
} else {
// orthographic
set('near_origin', hmul(va.set(0, 0, -10), inv));
set('near_x', hmul(vb.set(1, 0, -10), inv).sub(va));
set('near_y', hmul(vc.set(0, 1, -10), inv).sub(va));
set('near_origin', points[3]);
set('near_x', va.sub2(points[0], points[3]));
set('near_y', va.sub2(points[2], points[3]));
}

// far
set('far_origin', hmul(va.set(0, 0, 1), inv));
set('far_x', hmul(vb.set(1, 0, 1), inv).sub(va));
set('far_y', hmul(vc.set(0, 1, 1), inv).sub(va));
set('far_origin', points[7]);
set('far_x', va.sub2(points[4], points[7]));
set('far_y', va.sub2(points[6], points[7]));
};
}

Expand Down Expand Up @@ -310,15 +323,14 @@ class Camera extends Element {
}

serialize(serializer: Serializer) {
const camera = this.entity.camera.camera;
serializer.pack(this.fov);
serializer.packa(this.entity.getWorldTransform().data);
serializer.pack(this.entity.camera.renderTarget?.width, this.entity.camera.renderTarget?.height);
}

// handle the viewer canvas resizing
rebuildRenderTargets() {
const device = this.scene.graphicsDevice as WebglGraphicsDevice;
const device = this.scene.graphicsDevice;
const { width, height } = this.scene.targetSize;

const rt = this.entity.camera.renderTarget;
Expand All @@ -331,8 +343,8 @@ class Camera extends Element {
rt.destroyTextureBuffers();
rt.destroy();

this.pickModeRenderTarget.destroy();
this.pickModeRenderTarget = null;
this.workRenderTarget.destroy();
this.workRenderTarget = null;
}

const createTexture = (name: string, width: number, height: number, format: number) => {
Expand All @@ -356,15 +368,18 @@ class Camera extends Element {
autoResolve: false
});
this.entity.camera.renderTarget = renderTarget;
this.entity.camera.camera.horizontalFov = width > height;
this.entity.camera.horizontalFov = width > height;

// create pick mode render target (reuse color buffer)
this.pickModeRenderTarget = new RenderTarget({
this.workRenderTarget = new RenderTarget({
colorBuffer,
depth: false,
autoResolve: false
});

// set picker render target
this.picker.renderTarget = this.workRenderTarget;

this.scene.events.fire('camera.resize', { width, height });
}

Expand All @@ -391,7 +406,7 @@ class Camera extends Element {
this.fitClippingPlanes(this.entity.getLocalPosition(), this.entity.forward);

const { camera } = this.entity;
camera.orthoHeight = 0.5 * this.distanceTween.value.distance * this.sceneRadius / this.fovFactor * (camera.camera.horizontalFov ? this.scene.targetSize.height / this.scene.targetSize.width : 1);
camera.orthoHeight = this.distanceTween.value.distance * this.sceneRadius / this.fovFactor * (this.fov / 90) * (camera.horizontalFov ? this.scene.targetSize.height / this.scene.targetSize.width : 1);
camera.camera._updateViewProjMat();
}

Expand Down Expand Up @@ -455,7 +470,6 @@ class Camera extends Element {
const scene = this.scene;
const cameraPos = this.entity.getPosition();

// @ts-ignore
const target = scene.canvas;
const sx = screenX / target.clientWidth * scene.targetSize.width;
const sy = screenY / target.clientHeight * scene.targetSize.height;
Expand Down Expand Up @@ -520,7 +534,7 @@ class Camera extends Element {
const { width, height } = this.scene.targetSize;
const worldLayer = this.scene.app.scene.layers.getLayerByName('World');

const device = this.scene.graphicsDevice as WebglGraphicsDevice;
const device = this.scene.graphicsDevice;
const events = this.scene.events;
const alpha = events.invoke('camera.mode') === 'rings' ? 0.0 : 0.2;

Expand Down
Loading

0 comments on commit e076268

Please sign in to comment.