pixi.js + three.js
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="importmap">
{
"imports": {
"@mulsense/xnew": "https://unpkg.com/@mulsense/xnew@0.7.x/dist/xnew.mjs",
"@mulsense/xnew/addons/xpixi": "https://unpkg.com/@mulsense/xnew@0.7.x/dist/addons/xpixi.mjs",
"@mulsense/xnew/addons/xthree": "https://unpkg.com/@mulsense/xnew@0.7.x/dist/addons/xthree.mjs",
"pixi.js": "https://cdnjs.cloudflare.com/ajax/libs/pixi.js/8.6.6/pixi.min.mjs",
"three": "https://cdn.jsdelivr.net/npm/three@0.176.0/+esm"
}
}
</script>
<script type="module" src="./script.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
</head>
<body class="m-0 p-0 w-full h-screen overflow-hidden">
<div id="main" class="relative w-full h-full"></div>
</body>
</html>
script.js
import xnew from '@mulsense/xnew';
import xpixi from '@mulsense/xnew/addons/xpixi';
import xthree from '@mulsense/xnew/addons/xthree';
import * as PIXI from 'pixi.js';
import * as THREE from 'three';
xnew(document.querySelector('#main'), Main);
function Main(unit) {
const [width, height] = [800, 600];
xnew.extend(xnew.basics.Screen, { width, height });
// three setup
xthree.initialize({ canvas: new OffscreenCanvas(width, height) });
xthree.camera.position.set(0, 0, +100);
unit.on('render', () => {
xthree.renderer.render(xthree.scene, xthree.camera);
});
// pixi setup
xpixi.initialize({ canvas: unit.canvas });
const texture = PIXI.Texture.from(xthree.canvas);
unit.on('render', () => {
texture.source.update();
xpixi.renderer.render(xpixi.scene);
});
xnew(Contents);
}
function Contents(unit) {
// three.js (offscreen canvas)
xnew(Cubes);
// pixi.js (screen canvas)
xnew(ThreeTexture); // render three.js canvas as pixi texture
xnew(Boxes);
}
function ThreeTexture(unit) {
const texture = PIXI.Texture.from(xthree.canvas)
const object = xpixi.nest(new PIXI.Sprite(texture));
}
function Boxes(unit) {
const object = xpixi.nest(new PIXI.Container());
object.position.set(xpixi.canvas.width / 2, xpixi.canvas.height / 2); // center
for (let y = -1; y <= 1; y++) {
for (let x = -1; x <= 1; x++) {
xnew(Box, { x: 120 * x, y: 120 * y, size: 60, color: 0xEA1E63 });
}
}
unit.on('update', () => object.rotation += 0.01);
}
function Box(unit, { x, y, size, color }) {
const object = xpixi.nest(new PIXI.Container());
object.position.set(x, y);
object.addChild(new PIXI.Graphics().rect(-size / 2, -size / 2, size, size).fill(color));
unit.on('update', () => object.rotation += 0.01);
}
function Cubes(unit) {
const object = xthree.nest(new THREE.Object3D());
for (let z = -1; z <= 1; z++) {
for (let y = -1; y <= 1; y++) {
for (let x = -1; x <= 1; x++) {
xnew(Cube, { x: 15 * x, y: 15 * y, z: 15 * z, size: 6 });
}
}
}
unit.on('update', () => {
object.rotation.y += 0.01;
object.rotation.z += 0.01;
});
}
function Cube(unit, { x, y, z, size }) {
const geometry = new THREE.BoxGeometry(size, size, size);
const material = new THREE.MeshNormalMaterial();
const object = xthree.nest(new THREE.Mesh(geometry, material));
object.position.set(x, y, z);
unit.on('update', () => {
object.rotation.x += 0.01;
object.rotation.y += 0.01;
});
}