Skip to main content

Game engine API · Lesson 10

3D Scenes

Start a 3D scene with local three.js. Learn the DPR cap of 1.6, the single frame under reduced motion, and the 8 3D scenes.

The 3D mode imports three.js dynamically from /assets/vendor/three.module.min.js. There is no CDN and no external network request.

What you will learn

  • Starting 3D mode and readiness events
  • The eight registered scene ids
  • DPR ≤ 1.6 and reduced-motion behaviour

API signatures

CallResult
basla(mod, config){ok, hata?} · validates mode and config
olay(ad, fn)function that cancels the subscription
sahneYukle(ad, veri){ok, hata?} · needs a running session
durum()state copy · mutation has no effect
dogrulapure helper defined in the source module
oyunlarpure helper defined in the source module

Local library and cost limits

motif-3d.js uses a fixed default path and yukle() imports the module through a Promise. Failure is never silent; a hata event is published.

The WebGL renderer is created with antialias:false, and renderer.setPixelRatio(Math.min(1.6, devicePixelRatio)) limits the pixel load on high-DPI devices. The camera is PerspectiveCamera(45, genislik/yukseklik, 0.1, 100).

Start the 3D mode

var M = window.OyunMotoru;
var alan = document.getElementById('oyunAlani3D');
M.olay('3d-hazir', function (e) {
  console.log('Scene ready:', e.oyun);
});
M.olay('hata', function (e) {
  console.log('3D error:', e.mesaj);
});
var r = M.basla('3d', {
  kok: alan,
  oyun: 'geometri',
  genislik: 480,
  yukseklik: 320
});
if (!r.ok) console.log(r.hata);

What happens in this code? The state opens immediately; the 3d-hazir event arrives when the asynchronous load completes. Update the UI from that event.

Scenes and validation

The eight registered scenes are geometri, renkler, gunes-sistemi, sekiller, molecule (molekul), colour cube (renk-kupu), spectrum (tayf) and cell (hucre). Oyun3D.dogrula(config) checks width 240-1600 and height 200-1000 and rejects unknown scenes.

With prefers-reduced-motion: reduce, no continuous loop is created; only one frame is drawn. This respects the reduced-motion preference.

List and switch scenes

var M = window.OyunMotoru;
console.log(window.Oyun3D.oyunlar().map(function (s) {
  return s.id;
}));
// ['geometri','renkler','gunes-sistemi','sekiller','molekul','renk-kupu','tayf','hucre']
console.log(window.Oyun3D.dogrula({ oyun: 'sekiller' })); // ''
console.log(window.Oyun3D.dogrula({ oyun: 'yok' }));     // error text

M.sahneYukle('gunes-sistemi', {});
console.log(M.durum().sahne); // 'gunes-sistemi'

What happens in this code? The solar system scene uses deterministic starting angles for its orbits, so it looks the same on every opening.

Common mistakes

Wrong approachResultCorrect approach
Loading three.js from a CDNThe scene fails offlineUse the local vendor file
No DPR capPixel load rises on high-DPI screensMath.min(1.6, devicePixelRatio)
Ignoring reduced motionThe accessibility preference is violatedDraw a single frame
Unknown scene namedogrula() rejects itUse one of the eight ids

Try it yourself

Open the eight scenes in turn with sahneYukle(). Confirm that each sahne event carries the right name and that no hata event arrives.