A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
"""The engine's side of a GI lab scene: a project whose scene is the boxes of gi_specs as built-in cubes, the camera and the sun.
python gi_project.py <projectDir> <lab> (env BACKEND, EFFECTS (json list), Q (json quality), BAKE=1 adds a BakedLighting entity)
"""
import json
import os
import sys
import uuid
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import gi_specs as S # noqa: E402
import lab_specs as L # noqa: E402
proj, lab = sys.argv[1:3]
spec = S.GI[lab]
os.makedirs(proj + '/assets/scenes', exist_ok=True)
os.makedirs(proj + '/assets/materials', exist_ok=True)
json.dump({'formatVersion': '1', 'name': 'GiLab', 'version': '0.1.0', 'tag': '3D', 'defaultScene': 'assets/scenes/lab.esscene',
'designResolution': {'width': S.SIZE[0], 'height': S.SIZE[1]}, 'spineVersion': 'none',
'features': {'rendering': {'backend': os.environ.get('BACKEND', 'webgl2'), 'colorSpace': 'linear', 'outputTransform': 'none',
'quality': dict({'mode': 'high'}, **json.loads(os.environ.get('Q', '{}')))}}},
open(proj + '/project.esproject', 'w'), indent=2)
pos, target, fov = spec['cam']
qw, qx, qy, qz = L.cam_quaternion(spec['cam'])
entities = [{'id': 0, 'name': 'Camera', 'parent': None, 'children': [], 'visible': True, 'components': [
{'type': 'Transform', 'data': {'position': {'x': pos[0], 'y': pos[1], 'z': pos[2]}, 'rotation': {'w': qw, 'x': qx, 'y': qy, 'z': qz}}},
{'type': 'Camera', 'data': {'projectionType': 0, 'fov': fov, 'nearPlane': 5.0, 'farPlane': 5000.0, 'isActive': True, 'priority': 0, 'clearFlags': 3}}]}]
panel_material = None
if 'emission' in spec:
e = spec['emission']
mid = str(uuid.uuid4())
json.dump({'version': '1.0', 'type': 'material', 'shader': 'builtin:model', 'blendMode': 0, 'depthTest': True, 'depthWrite': True,
'properties': {'u_emissive': {'r': e, 'g': e, 'b': e, 'a': 1}, 'u_roughness': 1}},
open(proj + '/assets/materials/panel.esmaterial', 'w'), indent=2)
json.dump({'uuid': mid, 'version': '2.0', 'type': 'material', 'importer': {}}, open(proj + '/assets/materials/panel.esmaterial.meta', 'w'), indent=2)
panel_material = '@uuid:' + mid
MESH_BOXES = os.environ.get('MESH_BOXES') == '1'
if MESH_BOXES:
# Each box a mesh with the files the dynamic GI reads beside it, as an import with `dynamicGI` on would write them.
import subprocess
boxes = [{'size': list(b[2]), 'colour': list(b[3]), 'emissive': [spec.get('emission', 0)] * 3 if b[0] == 'panel' else [0, 0, 0]} for b in spec['boxes']]
json.dump(boxes, open(proj + '/boxes.json', 'w'))
subprocess.check_call(['node', os.path.join(os.path.dirname(os.path.abspath(__file__)), 'make_mesh_boxes.mjs'), proj, proj + '/boxes.json'])
for i, (name, centre, size, colour) in enumerate(spec['boxes']):
mr = {'mesh': 'assets/meshes/box_%d.esmesh' % i if MESH_BOXES else 'builtin:cube', 'lit': True, 'opaque': True, 'cullBackfaces': True,
'color': {'r': colour[0], 'g': colour[1], 'b': colour[2], 'a': 1}}
if name == 'panel' and panel_material:
mr['material'] = panel_material
entities.append({'id': 10 + i, 'name': name, 'parent': None, 'children': [], 'visible': True, 'components': [
{'type': 'Transform', 'data': {'position': {'x': centre[0], 'y': centre[1], 'z': centre[2]},
'scale': {'x': 1, 'y': 1, 'z': 1} if MESH_BOXES else {'x': size[0] / 100.0, 'y': size[1] / 100.0, 'z': size[2] / 100.0}}},
{'type': 'MeshRenderer', 'data': mr}]})
if spec.get('point'):
pt = spec['point']
entities.append({'id': 7, 'name': 'Lamp', 'parent': None, 'children': [], 'visible': True, 'components': [
{'type': 'Transform', 'data': {'position': {'x': pt['pos'][0], 'y': pt['pos'][1], 'z': pt['pos'][2]}}},
{'type': 'Light', 'data': {'type': 0, 'color': {'r': 1, 'g': 1, 'b': 1, 'a': 1}, 'intensity': pt['intensity'], 'radius': pt['radius'],
'innerRadius': 0.0, 'falloff': 1.0, 'inverseSquare': False, 'enabled': True, 'meshShadows': True, 'shadowStrength': 1.0}}]})
if spec.get('spot'):
sp = spec['spot']
# Aimed down: the rotation of -45 degrees about X takes the light's forward (-Z) to -Y.
entities.append({'id': 8, 'name': 'Spot', 'parent': None, 'children': [], 'visible': True, 'components': [
{'type': 'Transform', 'data': {'position': {'x': sp['pos'][0], 'y': sp['pos'][1], 'z': sp['pos'][2]}, 'rotation': {'w': 0.70710678, 'x': -0.70710678, 'y': 0.0, 'z': 0.0}}},
{'type': 'Light', 'data': {'type': 3, 'color': {'r': 1, 'g': 1, 'b': 1, 'a': 1}, 'intensity': sp['intensity'], 'radius': sp['radius'],
'innerRadius': 0.0, 'falloff': 1.0, 'inverseSquare': False, 'innerAngle': sp['inner'], 'outerAngle': sp['outer'],
'enabled': True, 'meshShadows': True, 'shadowStrength': 1.0}}]})
if spec['sun']:
sw, sx, sy, sz = L.sun_quaternion(spec['sun']['dir'])
entities.append({'id': 5, 'name': 'Sun', 'parent': None, 'children': [], 'visible': True, 'components': [
{'type': 'Transform', 'data': {'rotation': {'w': sw, 'x': sx, 'y': sy, 'z': sz}}},
{'type': 'Light', 'data': {'type': 1, 'color': {'r': 1, 'g': 1, 'b': 1, 'a': 1}, 'intensity': spec['sun']['intensity'], 'enabled': True,
'meshShadows': True, 'shadowExtent': 0.0, 'shadowStrength': 1.0, 'sourceAngle': 0.0}}]})
if os.environ.get('BAKE') == '1':
entities.append({'id': 6, 'name': 'Lighting', 'parent': None, 'children': [], 'visible': True, 'components': [
{'type': 'BakedLighting', 'data': {'atlasSize': 1024, 'texelsPerUnit': float(os.environ.get('TPU', '0.2')), 'bounces': int(os.environ.get('BOUNCES', '4')),
'samples': int(os.environ.get('BAKE_SAMPLES', '256')), 'probeSamples': 128}}]})
if os.environ.get('CAM_MOVE') == '1':
# A script that walks the camera back and forth along z, 200 units over a few seconds: a camera moving through the room, for the cost of moving it.
os.makedirs(proj + '/src', exist_ok=True)
open(proj + '/src/main.ts', 'w').write("""import { addSystemToSchedule, defineSystem, Query, Mut, Res, Time, Transform, Camera, Schedule } from 'esengine';
let t = 0;
addSystemToSchedule(Schedule.PostUpdate, defineSystem([Query(Mut(Transform), Camera), Res(Time)], (cameras, time) => {
t += time.delta;
for (const [, transform] of cameras) transform.position.z = -235 + 100 * (1 - Math.cos(t * 0.6));
}, { name: 'CameraWalk' }));
""")
effects = json.loads(os.environ.get('EFFECTS', '[]'))
if effects:
entities[0]['components'].append({'type': 'PostProcessVolume', 'data': {'isGlobal': True, 'weight': 1, 'priority': 0, 'effects': effects}})
json.dump({'version': 4, 'name': 'Lab', 'entities': entities}, open(proj + '/assets/scenes/lab.esscene', 'w'), indent=1)
print('project ready')