Skip to content

Commit adb762c

Browse files
committed
feat: babylon extension
1 parent c2ac191 commit adb762c

File tree

9 files changed

+161
-117
lines changed

9 files changed

+161
-117
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ callback ([DWM](https://github.com/deno-windowing/dwm) handles both).
2323

2424
```ts
2525
// GLES2 3.2 API
26-
import * as gl from "https://deno.land/x/[email protected].0/api/gles23.2.ts";
26+
import * as gl from "https://deno.land/x/[email protected].1/api/gles23.2.ts";
2727

2828
// Initialize function pointers
2929
gl.load(yourGetProcAddress);
@@ -45,6 +45,11 @@ along with `--allow-ffi`.
4545
deno run --unstable --allow-ffi <file>
4646
```
4747

48+
Extensions:
49+
50+
- [Babylon](https://www.babylonjs.com/) (Use
51+
`ext/babylon.ts` for an easy to use wrapper)
52+
4853
## Maintainers
4954

5055
- Dj ([@DjDeveloperr](https://github.com/DjDeveloperr))

examples/babylon.js

-45
This file was deleted.

examples/babylon.ts

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { init, World } from "../ext/babylon.ts";
2+
3+
class Game extends World {
4+
constructor() {
5+
super({
6+
title: "Babylon.js",
7+
width: 800,
8+
height: 600,
9+
}, {
10+
preserveDrawingBuffer: true,
11+
stencil: true,
12+
// deno-lint-ignore no-explicit-any
13+
} as any, true);
14+
}
15+
16+
createScene() {
17+
const scene = new BABYLON.Scene(this.engine);
18+
19+
// This creates and positions a free camera (non-mesh)
20+
const camera = new BABYLON.FreeCamera(
21+
"camera1",
22+
new BABYLON.Vector3(0, 5, -10),
23+
scene,
24+
);
25+
26+
// This targets the camera to scene origin
27+
camera.setTarget(BABYLON.Vector3.Zero());
28+
29+
// This attaches the camera to the canvas
30+
camera.attachControl(window, true);
31+
32+
// This creates a light, aiming 0,1,0 - to the sky (non-mesh)
33+
const light = new BABYLON.HemisphericLight(
34+
"light",
35+
new BABYLON.Vector3(0, 1, 0),
36+
scene,
37+
);
38+
39+
// Default intensity is 1. Let's dim the light a small amount
40+
light.intensity = 0.7;
41+
42+
// Our built-in 'sphere' shape.
43+
const sphere = BABYLON.MeshBuilder.CreateSphere("sphere", {
44+
diameter: 2,
45+
segments: 32,
46+
}, scene);
47+
48+
// Move the sphere upward 1/2 its height
49+
sphere.position.y = 1;
50+
51+
// Our built-in 'ground' shape.
52+
const _ground = BABYLON.MeshBuilder.CreateGround("ground", {
53+
width: 6,
54+
height: 6,
55+
}, scene);
56+
57+
return scene;
58+
}
59+
}
60+
61+
init(new Game());

examples/babylon2.js

-26
This file was deleted.

examples/babylon2.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { init, World } from "../ext/babylon.ts";
2+
3+
class Game extends World {
4+
constructor() {
5+
super({
6+
title: "Babylon.js",
7+
width: 800,
8+
height: 600,
9+
}, {
10+
preserveDrawingBuffer: true,
11+
stencil: true,
12+
// deno-lint-ignore no-explicit-any
13+
} as any, true);
14+
}
15+
16+
createScene() {
17+
const scene = new BABYLON.Scene(this.engine);
18+
19+
const camera = new BABYLON.ArcRotateCamera(
20+
"camera",
21+
-Math.PI / 2,
22+
Math.PI / 2.5,
23+
15,
24+
new BABYLON.Vector3(0, 0, 0),
25+
);
26+
camera.attachControl(this.canvas, true);
27+
const _light = new BABYLON.HemisphericLight(
28+
"light",
29+
new BABYLON.Vector3(1, 1, 0),
30+
scene,
31+
);
32+
33+
BABYLON.SceneLoader.ImportMeshAsync(
34+
["ground", "semi_house"],
35+
"https://assets.babylonjs.com/meshes/",
36+
"both_houses_scene.babylon",
37+
);
38+
39+
return scene;
40+
}
41+
}
42+
43+
init(new Game());

examples/three.js renamed to examples/three.ts

+24-17
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
// deno-lint-ignore-file no-explicit-any
12
// Copied from https://github.com/mrdoob/three.js/blob/master/examples/webgl_interactive_cubes_gpu.html
23
// Copyright © 2010-2021 three.js authors
34

45
import { WebGLCanvas } from "../src/webgl/mod.ts";
5-
import * as THREE from "https://raw.githubusercontent.com/mrdoob/three.js/master/build/three.module.js";
6-
import * as BufferGeometryUtils from "https://raw.githubusercontent.com/mrdoob/three.js/master/examples/jsm/utils/BufferGeometryUtils.js";
7-
import { TrackballControls } from "https://raw.githubusercontent.com/mrdoob/three.js/master/examples/jsm/controls/TrackballControls.js";
6+
import * as THREE from "npm:three";
7+
import * as BufferGeometryUtils from "npm:three/examples/jsm/utils/BufferGeometryUtils.js";
8+
import { TrackballControls } from "npm:three/examples/jsm/controls/TrackballControls.js";
89

910
const canvas = new WebGLCanvas({
1011
title: "THREE.js Interactive Example",
@@ -29,19 +30,19 @@ Object.defineProperties(window, {
2930
},
3031
});
3132

32-
let camera, controls, scene, renderer;
33-
let pickingTexture, pickingScene;
34-
let highlightBox;
33+
let camera: any, controls: any, scene: any, renderer: any;
34+
let pickingTexture: any, pickingScene: any;
35+
let highlightBox: any;
3536

36-
const pickingData = [];
37+
const pickingData: any[] = [];
3738

3839
const pointer = new THREE.Vector2();
3940
const offset = new THREE.Vector3(10, 10, 10);
4041

4142
init();
4243
animate();
4344

44-
function applyVertexColors(geometry, color) {
45+
function applyVertexColors(geometry: any, color: any) {
4546
const position = geometry.attributes.position;
4647
const colors = [];
4748

@@ -59,7 +60,8 @@ function init() {
5960
try {
6061
camera = new THREE.PerspectiveCamera(
6162
70,
62-
window.innerWidth / window.innerHeight,
63+
canvas.window.framebufferSize.width /
64+
canvas.window.framebufferSize.height,
6365
1,
6466
10000,
6567
);
@@ -156,8 +158,10 @@ function init() {
156158
scene.add(highlightBox);
157159

158160
renderer = new THREE.WebGLRenderer({ antialias: true, canvas });
159-
renderer.setPixelRatio(window.devicePixelRatio);
160-
renderer.setSize(window.innerWidth, window.innerHeight);
161+
renderer.setSize(
162+
canvas.window.framebufferSize.width,
163+
canvas.window.framebufferSize.height,
164+
);
161165

162166
controls = new TrackballControls(camera, renderer.domElement);
163167
controls.rotateSpeed = 1.0;
@@ -176,7 +180,7 @@ function init() {
176180
}
177181
}
178182

179-
function onMouseMove(e) {
183+
function onMouseMove(e: any) {
180184
pointer.x = e.clientX;
181185
pointer.y = e.clientY;
182186
}
@@ -190,12 +194,11 @@ function pick() {
190194
// render the picking scene off-screen
191195

192196
// set the view offset to represent just a single pixel under the mouse
193-
194197
camera.setViewOffset(
195198
renderer.domElement.width,
196199
renderer.domElement.height,
197-
pointer.x * window.devicePixelRatio | 0,
198-
pointer.y * window.devicePixelRatio | 0,
200+
pointer.x * (window as any).devicePixelRatio | 0,
201+
pointer.y * (window as any).devicePixelRatio | 0,
199202
1,
200203
1,
201204
);
@@ -236,8 +239,12 @@ function pick() {
236239
}
237240

238241
addEventListener("resize", () => {
239-
renderer.setSize(window.innerWidth, window.innerHeight);
240-
camera.aspect = window.innerWidth / window.innerHeight;
242+
renderer.setSize(
243+
canvas.window.framebufferSize.width,
244+
canvas.window.framebufferSize.height,
245+
);
246+
camera.aspect = canvas.window.framebufferSize.width /
247+
canvas.window.framebufferSize.height;
241248
camera.updateProjectionMatrix();
242249
});
243250

examples/three_map.json

-5
This file was deleted.

examples/triangle.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
createWindow,
33
getProcAddress,
44
mainloop,
5-
} from "https://deno.land/x/dwm@0.1.0/mod.ts";
5+
} from "https://deno.land/x/dwm@0.2.1/mod.ts";
66
import * as gl from "../api/gles23.2.ts";
77

88
const window = createWindow({

examples/babylon_runner.js renamed to ext/babylon.ts

+26-22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//@deno-types="npm:babylonjs"
12
import "https://deno.land/x/[email protected]/mod.ts";
23
import { WebGLCanvas } from "../src/webgl/mod.ts";
34
import "https://preview.babylonjs.com/ammo.js";
@@ -10,42 +11,45 @@ import "https://preview.babylonjs.com/proceduralTexturesLibrary/babylonjs.proced
1011
import "https://preview.babylonjs.com/postProcessesLibrary/babylonjs.postProcess.min.js";
1112
import "https://preview.babylonjs.com/loaders/babylonjs.loaders.js";
1213
import "https://preview.babylonjs.com/serializers/babylonjs.serializers.min.js";
14+
import { CreateWindowOptions } from "../src/webgl/deps.ts";
1315

1416
BABYLON.SceneLoader.ShowLoadingScreen = false;
1517

16-
export const canvas = new WebGLCanvas({
17-
title: "Babylon.js",
18-
width: 800,
19-
height: 600,
20-
// maximized: true,
21-
});
22-
23-
export const engine = new BABYLON.Engine(canvas, true, {
24-
preserveDrawingBuffer: true,
25-
stencil: true,
26-
});
18+
export abstract class World {
19+
canvas: WebGLCanvas;
20+
engine: BABYLON.Engine;
21+
constructor(
22+
settings: CreateWindowOptions = {},
23+
engineSettings: BABYLON.EngineOptions = {},
24+
antialias = true,
25+
) {
26+
this.canvas = new WebGLCanvas(settings);
27+
this.engine = new BABYLON.Engine(this.canvas, antialias, engineSettings);
28+
addEventListener("resize", () => {
29+
this.engine.resize();
30+
});
2731

28-
addEventListener("resize", () => {
29-
engine.resize();
30-
});
32+
addEventListener("close", () => {
33+
this.engine.stopRenderLoop();
34+
});
35+
}
3136

32-
addEventListener("close", () => {
33-
engine.stopRenderLoop();
34-
});
37+
abstract createScene(): BABYLON.Scene;
38+
}
3539

36-
export function runScene(createScene) {
40+
export function init<T extends World>(app: T) {
41+
const world = app;
3742
try {
38-
const scene = createScene();
39-
40-
engine.runRenderLoop(() => {
43+
const scene = world.createScene();
44+
world.engine.runRenderLoop(() => {
4145
try {
4246
scene.render();
4347
} catch (e) {
4448
console.log(e);
4549
}
4650
});
4751

48-
return canvas.run();
52+
return world.canvas.run();
4953
} catch (e) {
5054
console.log(e);
5155
}

0 commit comments

Comments
 (0)