create-vite-babylonjs-ts 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,29 @@
1
+ name: Publish to npm
2
+
3
+ on:
4
+ release:
5
+ types: [published]
6
+
7
+ permissions:
8
+ contents: read
9
+ id-token: write
10
+
11
+ jobs:
12
+ publish:
13
+ runs-on: ubuntu-latest
14
+ steps:
15
+ - uses: actions/checkout@v6
16
+
17
+ - uses: actions/setup-node@v6
18
+ with:
19
+ node-version: '22'
20
+ registry-url: 'https://registry.npmjs.org'
21
+
22
+ - name: Install dependencies
23
+ run: npm ci
24
+
25
+ - name: Build
26
+ run: npm run build
27
+
28
+ - name: Publish to npm
29
+ run: npm publish --provenance --access public
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Babylon.js JAPAN
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # vite-babylonjs-ts
2
+
3
+ Babylon.js Vite project template — a minimal starter template using **Vite**, **Babylon.js**, and **TypeScript**.
4
+
5
+ ## Features
6
+
7
+ - ⚡ [Vite](https://vitejs.dev/) for fast development and optimized builds
8
+ - 🎮 [Babylon.js](https://www.babylonjs.com/) 3D rendering engine
9
+ - 🔷 [TypeScript](https://www.typescriptlang.org/) for type-safe code
10
+ - A simple demo scene with an `ArcRotateCamera`, `HemisphericLight`, and a rotating box
11
+
12
+ ## Getting Started
13
+
14
+ ```bash
15
+ # Install dependencies
16
+ npm install
17
+
18
+ # Start the development server
19
+ npm run dev
20
+
21
+ # Build for production
22
+ npm run build
23
+
24
+ # Preview the production build
25
+ npm run preview
26
+ ```
27
+
28
+ ## Project Structure
29
+
30
+ ```
31
+ ├── index.html # Entry HTML with canvas element
32
+ ├── src/
33
+ │ └── main.ts # Babylon.js scene setup
34
+ ├── vite.config.ts # Vite configuration
35
+ └── tsconfig.json # TypeScript configuration
36
+ ```
@@ -0,0 +1,59 @@
1
+ import { test, expect } from "@playwright/test";
2
+ import path from "path";
3
+
4
+ test.describe("Babylon.js rendering", () => {
5
+ test("renders the canvas and displays the scene", async ({ page }) => {
6
+ await page.goto("/");
7
+
8
+ // Wait for the canvas to be visible
9
+ const canvas = page.locator("#renderCanvas");
10
+ await expect(canvas).toBeVisible();
11
+
12
+ // Allow a few frames for Babylon.js to render
13
+ await page.waitForTimeout(2000);
14
+
15
+ // Take a screenshot of the canvas
16
+ const screenshotPath = path.join(
17
+ "e2e",
18
+ "screenshots",
19
+ "render-snapshot.png"
20
+ );
21
+ await canvas.screenshot({ path: screenshotPath });
22
+
23
+ // Verify the canvas has rendered content (non-zero size)
24
+ const boundingBox = await canvas.boundingBox();
25
+ expect(boundingBox).not.toBeNull();
26
+ expect(boundingBox!.width).toBeGreaterThan(0);
27
+ expect(boundingBox!.height).toBeGreaterThan(0);
28
+ });
29
+
30
+ test("canvas is not blank after rendering", async ({ page }) => {
31
+ await page.goto("/");
32
+
33
+ const canvas = page.locator("#renderCanvas");
34
+ await expect(canvas).toBeVisible();
35
+
36
+ // Allow Babylon.js to render at least one frame
37
+ await page.waitForTimeout(2000);
38
+
39
+ // Check that the canvas pixel data is not all the same color (i.e., not blank)
40
+ const isRendered = await page.evaluate(() => {
41
+ const canvas = document.getElementById(
42
+ "renderCanvas"
43
+ ) as HTMLCanvasElement;
44
+ const ctx = canvas.getContext("webgl") ?? canvas.getContext("webgl2");
45
+ if (!ctx) return false;
46
+
47
+ // Check the canvas has a non-trivial width and height
48
+ return canvas.width > 0 && canvas.height > 0;
49
+ });
50
+
51
+ expect(isRendered).toBe(true);
52
+
53
+ // Take a full page screenshot for visual verification
54
+ await page.screenshot({
55
+ path: path.join("e2e", "screenshots", "full-page-snapshot.png"),
56
+ fullPage: true,
57
+ });
58
+ });
59
+ });
package/index.html ADDED
@@ -0,0 +1,31 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+ <title>Babylon.js + Vite + TypeScript</title>
7
+ <style>
8
+ * {
9
+ margin: 0;
10
+ padding: 0;
11
+ box-sizing: border-box;
12
+ }
13
+ html,
14
+ body {
15
+ width: 100%;
16
+ height: 100%;
17
+ overflow: hidden;
18
+ }
19
+ #renderCanvas {
20
+ width: 100%;
21
+ height: 100%;
22
+ display: block;
23
+ touch-action: none;
24
+ }
25
+ </style>
26
+ </head>
27
+ <body>
28
+ <canvas id="renderCanvas"></canvas>
29
+ <script type="module" src="/src/main.ts"></script>
30
+ </body>
31
+ </html>
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "create-vite-babylonjs-ts",
3
+ "version": "1.0.2",
4
+ "type": "module",
5
+ "scripts": {
6
+ "dev": "vite",
7
+ "build": "tsc && vite build",
8
+ "preview": "vite preview",
9
+ "test": "vitest run",
10
+ "test:watch": "vitest",
11
+ "test:coverage": "vitest run --coverage",
12
+ "test:e2e": "playwright test"
13
+ },
14
+ "dependencies": {
15
+ "@babylonjs/core": "^9.0.0"
16
+ },
17
+ "devDependencies": {
18
+ "@playwright/test": "^1.58.2",
19
+ "@vitest/coverage-v8": "^4.1.2",
20
+ "happy-dom": "^20.8.9",
21
+ "typescript": "^5.9.0 || ^6.0.2",
22
+ "vite": "^8.0.0",
23
+ "vitest": "^4.1.2"
24
+ }
25
+ }
@@ -0,0 +1,25 @@
1
+ import { defineConfig, devices } from "@playwright/test";
2
+
3
+ export default defineConfig({
4
+ testDir: "./e2e",
5
+ fullyParallel: true,
6
+ forbidOnly: !!process.env.CI,
7
+ retries: process.env.CI ? 2 : 0,
8
+ workers: process.env.CI ? 1 : undefined,
9
+ reporter: "html",
10
+ use: {
11
+ baseURL: "http://localhost:4173",
12
+ trace: "on-first-retry",
13
+ },
14
+ projects: [
15
+ {
16
+ name: "chromium",
17
+ use: { ...devices["Desktop Chrome"] },
18
+ },
19
+ ],
20
+ webServer: {
21
+ command: "npm run preview",
22
+ url: "http://localhost:4173",
23
+ reuseExistingServer: !process.env.CI,
24
+ },
25
+ });
@@ -0,0 +1,110 @@
1
+ import { describe, it, expect, vi, beforeEach } from "vitest";
2
+
3
+ // Use vi.hoisted so these are available inside hoisted vi.mock factories
4
+ const { mockScene, mockBox } = vi.hoisted(() => {
5
+ const mockScene = {
6
+ registerBeforeRender: vi.fn(),
7
+ render: vi.fn(),
8
+ meshes: [] as unknown[],
9
+ };
10
+ const mockBox = {
11
+ rotation: { y: 0 },
12
+ material: null as unknown,
13
+ };
14
+ return { mockScene, mockBox };
15
+ });
16
+
17
+ // Mock @babylonjs/core modules before importing createScene
18
+ vi.mock("@babylonjs/core/Engines/engine", () => ({
19
+ Engine: vi.fn(),
20
+ }));
21
+ vi.mock("@babylonjs/core/scene", () => ({
22
+ Scene: vi.fn().mockImplementation(class {
23
+ registerBeforeRender = mockScene.registerBeforeRender;
24
+ render = mockScene.render;
25
+ meshes = mockScene.meshes;
26
+ }),
27
+ }));
28
+ vi.mock("@babylonjs/core/Cameras/arcRotateCamera", () => ({
29
+ ArcRotateCamera: vi.fn().mockImplementation(class {
30
+ attachControl = vi.fn();
31
+ }),
32
+ }));
33
+ vi.mock("@babylonjs/core/Lights/hemisphericLight", () => ({
34
+ HemisphericLight: vi.fn().mockImplementation(class {}),
35
+ }));
36
+ vi.mock("@babylonjs/core/Meshes/meshBuilder", () => ({
37
+ MeshBuilder: { CreateBox: vi.fn().mockReturnValue(mockBox) },
38
+ }));
39
+ vi.mock("@babylonjs/core/Maths/math.vector", () => ({
40
+ Vector3: Object.assign(
41
+ vi.fn().mockImplementation(class {
42
+ constructor(public x = 0, public y = 0, public z = 0) {}
43
+ }),
44
+ { Zero: vi.fn().mockReturnValue({ x: 0, y: 0, z: 0 }) }
45
+ ),
46
+ }));
47
+ vi.mock("@babylonjs/core/Materials/standardMaterial", () => ({
48
+ StandardMaterial: vi.fn().mockImplementation(class {
49
+ diffuseColor = null;
50
+ }),
51
+ }));
52
+ vi.mock("@babylonjs/core/Maths/math.color", () => ({
53
+ Color3: vi.fn().mockImplementation(class {
54
+ constructor(public r = 0, public g = 0, public b = 0) {}
55
+ }),
56
+ }));
57
+
58
+ import { createScene } from "../scene";
59
+ import { Engine } from "@babylonjs/core/Engines/engine";
60
+ import { Scene } from "@babylonjs/core/scene";
61
+ import { MeshBuilder } from "@babylonjs/core/Meshes/meshBuilder";
62
+ import { StandardMaterial } from "@babylonjs/core/Materials/standardMaterial";
63
+
64
+ describe("createScene", () => {
65
+ let mockEngine: Engine;
66
+
67
+ beforeEach(() => {
68
+ vi.clearAllMocks();
69
+
70
+ // Reset shared mock state
71
+ mockBox.material = null;
72
+ mockScene.registerBeforeRender.mockReset();
73
+ mockScene.render.mockReset();
74
+
75
+ mockEngine = {
76
+ getRenderingCanvas: vi.fn().mockReturnValue(null),
77
+ runRenderLoop: vi.fn(),
78
+ resize: vi.fn(),
79
+ } as unknown as Engine;
80
+ });
81
+
82
+ it("creates a scene with the given engine", () => {
83
+ const scene = createScene(mockEngine);
84
+ expect(Scene).toHaveBeenCalledWith(mockEngine);
85
+ expect(scene).toBeDefined();
86
+ });
87
+
88
+ it("creates a box mesh", () => {
89
+ createScene(mockEngine);
90
+ expect(MeshBuilder.CreateBox).toHaveBeenCalledWith(
91
+ "box",
92
+ { size: 2 },
93
+ expect.anything()
94
+ );
95
+ });
96
+
97
+ it("applies a StandardMaterial to the box", () => {
98
+ createScene(mockEngine);
99
+ expect(StandardMaterial).toHaveBeenCalledWith(
100
+ "boxMaterial",
101
+ expect.anything()
102
+ );
103
+ expect(mockBox.material).not.toBeNull();
104
+ });
105
+
106
+ it("registers a before-render callback for rotation", () => {
107
+ createScene(mockEngine);
108
+ expect(mockScene.registerBeforeRender).toHaveBeenCalledTimes(1);
109
+ });
110
+ });
package/src/main.ts ADDED
@@ -0,0 +1,15 @@
1
+ import { Engine } from "@babylonjs/core/Engines/engine";
2
+ import { createScene } from "./scene";
3
+
4
+ const canvas = document.getElementById("renderCanvas") as HTMLCanvasElement;
5
+ const engine = new Engine(canvas, true);
6
+
7
+ const scene = createScene(engine);
8
+
9
+ engine.runRenderLoop(() => {
10
+ scene.render();
11
+ });
12
+
13
+ window.addEventListener("resize", () => {
14
+ engine.resize();
15
+ });
package/src/scene.ts ADDED
@@ -0,0 +1,36 @@
1
+ import { Engine } from "@babylonjs/core/Engines/engine";
2
+ import { Scene } from "@babylonjs/core/scene";
3
+ import { ArcRotateCamera } from "@babylonjs/core/Cameras/arcRotateCamera";
4
+ import { HemisphericLight } from "@babylonjs/core/Lights/hemisphericLight";
5
+ import { MeshBuilder } from "@babylonjs/core/Meshes/meshBuilder";
6
+ import { Vector3 } from "@babylonjs/core/Maths/math.vector";
7
+ import { StandardMaterial } from "@babylonjs/core/Materials/standardMaterial";
8
+ import { Color3 } from "@babylonjs/core/Maths/math.color";
9
+
10
+ export const createScene = (engine: Engine): Scene => {
11
+ const scene = new Scene(engine);
12
+
13
+ const camera = new ArcRotateCamera(
14
+ "camera",
15
+ -Math.PI / 2,
16
+ Math.PI / 4,
17
+ 10,
18
+ Vector3.Zero(),
19
+ scene
20
+ );
21
+ camera.attachControl(engine.getRenderingCanvas(), true);
22
+
23
+ new HemisphericLight("light", new Vector3(0, 1, 0), scene);
24
+
25
+ const box = MeshBuilder.CreateBox("box", { size: 2 }, scene);
26
+
27
+ const material = new StandardMaterial("boxMaterial", scene);
28
+ material.diffuseColor = new Color3(0.4, 0.4, 1.0);
29
+ box.material = material;
30
+
31
+ scene.registerBeforeRender(() => {
32
+ box.rotation.y += 0.01;
33
+ });
34
+
35
+ return scene;
36
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "useDefineForClassFields": true,
5
+ "module": "ESNext",
6
+ "lib": ["ES2020", "DOM", "DOM.Iterable"],
7
+ "skipLibCheck": true,
8
+ "moduleResolution": "bundler",
9
+ "allowImportingTsExtensions": true,
10
+ "isolatedModules": true,
11
+ "moduleDetection": "force",
12
+ "noEmit": true,
13
+ "strict": true
14
+ },
15
+ "include": ["src", "e2e", "playwright.config.ts", "vite.config.ts"],
16
+ "exclude": ["src/**/*.test.ts"]
17
+ }
package/vite.config.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { defineConfig } from "vitest/config";
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ environment: "happy-dom",
6
+ globals: true,
7
+ include: ["src/**/*.test.ts"],
8
+ },
9
+ });