// ---------------- 3D ---------------- const view = (() => { const host = $('#stage'); const r = new THREE.WebGLRenderer({ antialias: true }); r.setPixelRatio(Math.min(devicePixelRatio, 2)); r.shadowMap.enabled = true; r.shadowMap.type = THREE.PCFSoftShadowMap; host.prepend(r.domElement); const s = new THREE.Scene(); s.background = new THREE.Color('#eceef1'); const cam = new THREE.PerspectiveCamera(34, 1, 0.5, 900); const target = new THREE.Vector3(); let az = 0, el = 0.6, dist = 30, mode = 'orbit', home = {}; const HOMES = { wheelchair: { x: 0, z: -4.5, az: 0.3, el: 0.6, dist: 40 }, remote: { x: -22, z: -1.5, az: 0.18, el: 0.8, dist: 78 }, crossing: { x: 30, z: -9, az: -0.62, el: 0.42, dist: 62 }, sidebyside: { x: 62, z: 4.5, az: 0.25, el: 0.62, dist: 42 }, crowd: { x: 64, z: 4.5, az: 0.25, el: 0.65, dist: 48 }, cane: { x: 77, z: -4.5, az: 0.3, el: 0.6, dist: 34 }, bicycle: { x: 58, z: -4.5, az: 0.3, el: 0.58, dist: 38 }, kids: { x: -38, z: 4.5, az: 0.25, el: 0.6, dist: 26 }, dog: { x: -40, z: 4.5, az: 0.25, el: 0.6, dist: 32 }, sign: { x: -45, z: -4, az: 0.15, el: 1.05, dist: 24 }, rain: { x: -48, z: -4.2, az: 0.3, el: 0.55, dist: 32 }, flock: { x: 6, z: 6, az: 0.2, el: 0.75, dist: 46 }, av: { x: -14, z: -1.5, az: 0.35, el: 0.6, dist: 40 } }; function placeCam() { if (mode === 'follow') return; cam.position.set(target.x + dist * Math.cos(el) * Math.sin(az), target.y + dist * Math.sin(el), target.z + dist * Math.cos(el) * Math.cos(az)); cam.lookAt(target); } const hemi = new THREE.HemisphereLight('#ffffff', '#b9b4a8', 1.6); s.add(hemi); let lastNight = false; const sun = new THREE.DirectionalLight('#ffffff', 1.5); sun.position.set(-25, 45, 30); sun.castShadow = true; sun.shadow.mapSize.set(2048, 2048); Object.assign(sun.shadow.camera, { left: -45, right: 45, top: 45, bottom: -45, near: 1, far: 160 }); s.add(sun); s.add(sun.target); const M = (c) => new THREE.MeshLambertMaterial({ color: c }); const box = (w, h, d, c, x, y, z, cast = false, parent = s) => { const m = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), typeof c === 'string' ? M(c) : c); m.position.set(x, y + h / 2, z); m.castShadow = cast; m.receiveShadow = true; parent.add(m); return m; }; const cyl = (r0, h, c, x, y, z, parent = s, seg = 16) => { const m = new THREE.Mesh(new THREE.CylinderGeometry(r0, r0, h, seg), M(c)); m.position.set(x, y + h / 2, z); m.castShadow = true; parent.add(m); return m; }; const MAP = E.MAP; // 地面・車道・歩道 box(400, 0.1, 400, '#e3e0d8', 0, -0.12, 0); box(240, 0.14, 7, '#7a7f87', 0, -0.02, 0); box(240, 0.18, 2.0, '#efe7d4', 0, 0, -4.5); box(240, 0.18, 2.0, '#efe7d4', 0, 0, 4.5); box(240, 0.2, 0.14, '#d8d2c4', 0, 0, -3.55); box(240, 0.2, 0.14, '#d8d2c4', 0, 0, 3.55); // 縁石 for (let x = -118; x < 118; x += 4) if (Math.abs(x - MAP.crosswalk.x) > 3 && Math.abs(x - MAP.rail.x) > 4) box(2, 0.03, 0.14, '#f2f2f2', x, 0.12, 0); // 横断歩道(車道を南北に渡る) for (let z = -3.0; z <= 3.01; z += 0.9) box(3, 0.03, 0.45, '#ffffff', MAP.crosswalk.x, 0.12, z); // 北側の建物(歩道の奥)。店の入口のへこみだけ奥へ下げる const bcol = ['#a9aeb5', '#9aa0a8', '#b3b7bd', '#a3a8af', '#bfc2c7']; let bi = 0; for (let x = -96; x < 96;) { const w = 7 + ((bi * 37) % 5) * 1.6, h = 4 + ((bi * 53) % 6) * 1.6; const x0 = x, x1 = Math.min(96, x + w); // 店の入口(へこみ)の所は、建物を分けて奥へ下げた店の正面を置く const r0 = MAP.recess; if (x1 > MAP.rail.x - 7 && x0 < MAP.rail.x + 7) { // 線路のまわりは建物を置かない if (x0 < MAP.rail.x - 7) box(MAP.rail.x - 7 - x0, h, 10, bcol[bi % 5], (x0 + MAP.rail.x - 7) / 2, 0, -10.5, true); bi++; x = Math.max(x1, MAP.rail.x + 7) + 0.3; continue; } if (x1 > r0.x0 && x0 < r0.x1) { if (r0.x0 > x0) box(r0.x0 - x0, h, 10, bcol[bi % 5], (x0 + r0.x0) / 2, 0, -10.5, true); if (x1 > r0.x1) box(x1 - r0.x1, h, 10, bcol[bi % 5], (r0.x1 + x1) / 2, 0, -10.5, true); box(r0.x1 - r0.x0, h, 8.5, bcol[bi % 5], (r0.x0 + r0.x1) / 2, 0, -11.25, true); } else box(x1 - x0, h, 10, bcol[bi % 5], (x0 + x1) / 2, 0, -10.5, true); bi++; x = x1 + 0.3; } // 店の入口のへこみ:床・ガラス・ひさし box(MAP.recess.x1 - MAP.recess.x0, 0.18, 1.5, '#e9e2cf', (MAP.recess.x0 + MAP.recess.x1) / 2, 0, -6.25); box(MAP.recess.x1 - MAP.recess.x0 - 0.4, 2.2, 0.06, '#3c4652', (MAP.recess.x0 + MAP.recess.x1) / 2, 0.2, -6.98); box(MAP.recess.x1 - MAP.recess.x0 + 0.6, 0.12, 1.9, '#5f6b78', (MAP.recess.x0 + MAP.recess.x1) / 2, 2.7, -6.2, true); // 狭い所:建物側に自販機2台、車道側に電柱と植木鉢 const N = MAP.narrow; for (const x of [N.x0 + 0.5, N.x0 + 1.5]) { box(0.95, 1.85, 0.7, '#d9dde2', x, 0.18, -5.15, true); box(0.7, 0.9, 0.04, '#3d5a80', x, 0.9, -4.79); } cyl(0.15, 9, '#9a9a93', (N.x0 + N.x1) / 2, 0.18, -3.66); box(0.9, 0.45, 0.25, '#8a7f6e', N.x0 + 0.5, 0.18, -3.63, true); box(0.7, 0.45, 0.25, '#8a7f6e', N.x1 - 0.45, 0.18, -3.63, true); // 工事:建物側を残して、車道側をコーンと柵でふさぐ const W = MAP.works; box(W.x1 - W.x0, 0.2, -3.5 - W.z, '#5d5a55', (W.x0 + W.x1) / 2, 0.01, (W.z - 3.5) / 2); for (let x = W.x0; x <= W.x1 + 0.01; x += 0.75) { const c = new THREE.Mesh(new THREE.ConeGeometry(0.16, 0.7, 12), M('#f07a1a')); c.position.set(x, 0.53, W.z + 0.05); c.castShadow = true; s.add(c); } for (const x of [W.x0, W.x1]) for (let z = W.z; z <= -3.5; z += 0.55) { const c = new THREE.Mesh(new THREE.ConeGeometry(0.16, 0.7, 12), M('#f07a1a')); c.position.set(x, 0.53, z); s.add(c); } box(W.x1 - W.x0, 0.12, 0.05, '#f2c230', (W.x0 + W.x1) / 2, 0.75, W.z + 0.05); box(0.9, 0.9, 0.05, '#f2c230', W.x0 - 0.2, 0.9, -3.4, true); // 南側:低いもの(駐車場・低い生け垣・平屋)。カメラと街のあいだに高いものを置かない for (let x = -90; x < 90; x += 18) { if (Math.abs(x - 30) < 10) continue; box(14, 0.04, 8, '#8d9199', x + 7, 0.0, 10.5); for (let i = 0; i <= 5; i++) box(0.1, 0.05, 4, '#f2f2f2', x + 1 + i * 2.5, 0.02, 9.5); box(14, 0.8, 0.5, '#6f9a5a', x + 7, 0.0, 6.0, true); } // 点字ブロック(北の歩道の東)。黄色の帯 { const P = MAP.paving; box(P.x1 - P.x0, 0.03, 0.3, '#e9c22a', (P.x0 + P.x1) / 2, 0.18, P.z); } // 店の前に並んだ自転車(北の歩道の建物側) const bikeMesh = (parent, x, z, rot = Math.PI / 2) => { const g = new THREE.Group(); g.position.set(x, 0.18, z); g.rotation.y = rot; parent.add(g); for (const dx of [-0.48, 0.48]) { const w = new THREE.Mesh(new THREE.TorusGeometry(0.3, 0.03, 6, 18), M('#2a2a2a')); w.position.set(dx, 0.32, 0); g.add(w); } box(1.0, 0.04, 0.04, '#7a8a99', 0, 0.5, 0, false, g); box(0.04, 0.4, 0.04, '#7a8a99', -0.15, 0.32, 0, false, g); box(0.22, 0.05, 0.1, '#222', -0.15, 0.72, 0, false, g); box(0.04, 0.4, 0.04, '#7a8a99', 0.4, 0.55, 0, false, g); box(0.05, 0.03, 0.45, '#333', 0.4, 0.78, 0, false, g); return g; }; for (let x = MAP.parkedBikes.x0; x <= MAP.parkedBikes.x1; x += 0.75) bikeMesh(s, x, MAP.parkedBikes.z); // 建物の入口(人が出てくる所) for (const x of MAP.doors) { box(1.3, 2.3, 0.06, '#2b3138', x, 0.18, -5.47); box(1.8, 0.35, 0.8, '#5f6b78', x, 2.6, -5.2, true); } // 配送ロボットの拠点(南側)。門と敷地 { const D = MAP.depot.x; box(4, 0.06, 17, '#cfcac0', D, 0.0, 14.6); box(0.15, 1.2, 0.15, '#888', D - 2, 0, 6.1); box(0.15, 1.2, 0.15, '#888', D + 2, 0, 6.1); box(4.3, 0.15, 0.15, '#888', D, 1.2, 6.1); } // 踏切:線路は南北。線路の砂利・枕木・レール、踏切の板、遮断機と警報機 const RX = MAP.rail.x; box(5, 0.16, 400, '#a59e92', RX, -0.06, 0); for (let z = -198; z < 198; z += 0.6) if (Math.abs(z) > 6) box(2.2, 0.1, 0.2, '#7d6e5d', RX, 0.08, z); box(MAP.rail.w, 0.2, 11, '#6c6f74', RX, 0.0, 0); // 踏切の板(車道と歩道の高さにそろえる) for (const tx of MAP.rail.tracks) { box(0.07, 0.16, 400, '#c9ccd1', tx, 0.12, 0); box(0.09, 0.04, 11, '#2a2c30', tx + (tx < RX ? 0.08 : -0.08), 0.19, 0); } const gates = [], lamps = []; const yellowBlack = (() => { const c = document.createElement('canvas'); c.width = 64; c.height = 8; const g = c.getContext('2d'); for (let i = 0; i < 8; i++) { g.fillStyle = i % 2 ? '#111' : '#f2c230'; g.fillRect(i * 8, 0, 8, 8); } const t = new THREE.CanvasTexture(c); t.wrapS = THREE.RepeatWrapping; t.repeat.set(4, 1); return new THREE.MeshLambertMaterial({ map: t }); })(); for (const [x, z, dir] of [[RX - 2.4, -5.9, 1], [RX + 2.4, -5.9, 1], [RX - 2.4, 5.9, -1], [RX + 2.4, 5.9, -1]]) { box(0.35, 1.1, 0.35, '#e8e8e8', x, 0, z, true); const piv = new THREE.Group(); piv.position.set(x, 1.0, z); s.add(piv); const arm = new THREE.Mesh(new THREE.BoxGeometry(0.08, 0.08, 5.6), yellowBlack); arm.position.set(0, 0, dir * 2.8); arm.castShadow = true; piv.add(arm); piv.userData.dir = dir; gates.push(piv); // 警報機:柱・×の板・赤い灯 const px = x + (x < RX ? -0.5 : 0.5); cyl(0.06, 3.0, '#e8e8e8', px, 0, z); for (const a of [0.6, -0.6]) { const b = box(0.9, 0.12, 0.03, '#f2c230', px, 2.72, z); b.rotation.z = a; } const lm = []; for (const dz of [-0.28, 0.28]) { const l = new THREE.Mesh(new THREE.SphereGeometry(0.11, 12, 8), new THREE.MeshBasicMaterial({ color: '#4a1a1a' })); l.position.set(px + (x < RX ? -0.05 : 0.05), 2.25, z + dz); s.add(l); lm.push(l); } lamps.push(lm); } // 列車:3両。北から南へ走る。前の端が group の原点 const train = new THREE.Group(); for (let i = 0; i < 3; i++) { const zc = -10 - i * 20.4; box(2.9, 3.4, 20, '#d9dcdf', 0, 0.9, zc, true, train); box(2.92, 0.22, 20, '#2f6f9f', 0, 1.5, zc, false, train); box(2.94, 0.9, 18.4, '#2b3138', 0, 2.3, zc, false, train); box(2.4, 0.5, 20, '#3a3d42', 0, 0.35, zc, false, train); } box(2.6, 1.1, 0.08, '#2b3138', 0, 2.2, 0.02, false, train); train.position.set(RX, 0.2, -400); s.add(train); // ---- 配送ロボット(120×70×120cm の中に収まる箱型・6輪・旗) ---- const lightMat = { go: new THREE.MeshBasicMaterial({ color: '#e9f6ff' }), stop: new THREE.MeshBasicMaterial({ color: '#ffb020' }), ask: new THREE.MeshBasicMaterial({ color: '#ffb020' }), dim: new THREE.MeshLambertMaterial({ color: '#7a6a45' }), off: new THREE.MeshLambertMaterial({ color: '#333' }) }; function makeRobot() { const g = new THREE.Group(); const body = new THREE.Group(); g.add(body); const sh = new THREE.Shape(); const L = 0.98, H = 0.78, q = 0.16; // 横から見た角の丸い箱 sh.moveTo(-L / 2 + q, 0); sh.lineTo(L / 2 - q, 0); sh.quadraticCurveTo(L / 2, 0, L / 2, q); sh.lineTo(L / 2, H - q); sh.quadraticCurveTo(L / 2, H, L / 2 - q, H); sh.lineTo(-L / 2 + q, H); sh.quadraticCurveTo(-L / 2, H, -L / 2, H - q); sh.lineTo(-L / 2, q); sh.quadraticCurveTo(-L / 2, 0, -L / 2 + q, 0); const geo = new THREE.ExtrudeGeometry(sh, { depth: 0.62, bevelEnabled: true, bevelSize: 0.03, bevelThickness: 0.03, bevelSegments: 3, curveSegments: 10 }); geo.translate(0, 0, -0.31); const m = new THREE.Mesh(geo, M('#f3f3f1')); m.position.y = 0.2; m.castShadow = true; body.add(m); box(0.84, 0.05, 0.6, '#2b2f36', 0, 1.0, 0, false, body); // ふた const face = box(0.03, 0.34, 0.5, '#20252c', 0.505, 0.5, 0, false, body); // 前の面(カメラと表示) const band = box(0.03, 0.06, 0.52, lightMat.go, 0.51, 0.88, 0, false, body); // 前の光の帯 const bandB = box(0.03, 0.06, 0.52, lightMat.dim, -0.51, 0.88, 0, false, body); const sideL = box(0.7, 0.05, 0.02, lightMat.dim, 0, 0.88, 0.345, false, body), sideR = box(0.7, 0.05, 0.02, lightMat.dim, 0, 0.88, -0.345, false, body); for (const x of [-0.36, 0, 0.36]) for (const z of [0.33, -0.33]) { const w = new THREE.Mesh(new THREE.CylinderGeometry(0.11, 0.11, 0.08, 16), M('#262626')); w.rotation.x = Math.PI / 2; w.position.set(x, 0.11, z); body.add(w); } cyl(0.012, 0.75, '#888', -0.38, 1.02, 0.22, body, 6); const flag = box(0.02, 0.16, 0.24, '#f2600c', -0.38, 1.6, 0.34, false, body); const eyes = new THREE.Group(); for (const z of [-0.12, 0.12]) { const e = new THREE.Mesh(new THREE.CircleGeometry(0.06, 16), new THREE.MeshBasicMaterial({ color: '#ffffff' })); e.position.set(0.525, 0.72, z); e.rotation.y = Math.PI / 2; eyes.add(e); } eyes.visible = false; body.add(eyes); g.userData = { body, band, bandB, sideL, sideR, eyes }; s.add(g); return g; } // ---- 人 ---- const person = (c, seated = false) => { const g = new THREE.Group(); const h = seated ? 0.62 : 1.22; const b = new THREE.Mesh(new THREE.CylinderGeometry(0.24, 0.27, h, 14), M(c)); b.position.y = (seated ? 0.55 : 0) + h / 2; b.castShadow = true; g.add(b); const hd = new THREE.Mesh(new THREE.SphereGeometry(0.19, 14, 10), M(c)); hd.position.y = (seated ? 0.55 : 0) + h + 0.24; hd.castShadow = true; g.add(hd); return g; }; function makeWheelchair() { const g = new THREE.Group(); for (const z of [0.3, -0.3]) { const w = new THREE.Mesh(new THREE.TorusGeometry(0.28, 0.035, 8, 24), M('#2a2a2a')); w.position.set(-0.15, 0.3, z); g.add(w); const cw = new THREE.Mesh(new THREE.CylinderGeometry(0.09, 0.09, 0.05, 12), M('#2a2a2a')); cw.rotation.x = Math.PI / 2; cw.position.set(0.38, 0.09, z * 0.85); g.add(cw); } box(0.5, 0.08, 0.5, '#3b4450', 0, 0.45, 0, false, g); box(0.08, 0.55, 0.5, '#3b4450', -0.27, 0.5, 0, false, g); box(0.3, 0.05, 0.5, '#3b4450', 0.4, 0.15, 0, false, g); box(0.25, 0.35, 0.25, '#56606c', -0.32, 0.18, 0, false, g); // 電池 const p = person('#e8b923', true); p.position.set(-0.05, 0, 0); g.add(p); box(0.4, 0.12, 0.4, '#e8b923', 0.22, 0.5, 0, false, g); // 脚 s.add(g); return g; } function makeStroller() { const g = new THREE.Group(); const p = person('#e8b923'); g.add(p); const st = new THREE.Group(); st.position.set(0.75, 0, 0); g.add(st); box(0.65, 0.35, 0.45, '#4e6a8a', 0, 0.35, 0, true, st); const hood = new THREE.Mesh(new THREE.SphereGeometry(0.28, 12, 8, 0, Math.PI * 2, 0, Math.PI / 2), M('#4e6a8a')); hood.position.set(-0.18, 0.7, 0); st.add(hood); for (const x of [-0.25, 0.25]) for (const z of [0.2, -0.2]) { const w = new THREE.Mesh(new THREE.CylinderGeometry(0.1, 0.1, 0.04, 12), M('#222')); w.rotation.x = Math.PI / 2; w.position.set(x, 0.1, z); st.add(w); } box(0.4, 0.04, 0.04, '#333', -0.45, 0.95, 0, false, st); s.add(g); return g; } function makeWalker(p = {}) { const g = person(p.umbrella ? '#3b3f46' : '#e8b923'); const ph = box(0.06, 0.14, 0.09, '#111', 0.12, 1.52, 0.17, false, g); g.userData.phone = ph; if (p.child) g.scale.set(0.8, 0.68, 0.8); if (p.umbrella) { const u = new THREE.Mesh(new THREE.ConeGeometry(0.62, 0.3, 16), M('#24282e')); u.position.set(0, 2.05, 0); g.add(u); cyl(0.015, 0.6, '#555', 0, 1.5, 0, g, 6); } if (p.kind === 'cane') { const c = new THREE.Mesh(new THREE.CylinderGeometry(0.012, 0.012, 1.25, 6), new THREE.MeshLambertMaterial({ color: '#ffffff' })); c.position.set(0.55, 0.5, 0.15); c.rotation.z = -1.05; g.add(c); } s.add(g); return g; } function makeBike() { const g = new THREE.Group(); const b = bikeMesh(g, 0, 0, 0); b.position.y = 0; const p = person('#e8b923'); p.scale.set(0.9, 0.85, 0.9); p.position.set(-0.1, 0.35, 0); g.add(p); g.userData.body = g.children; s.add(g); return g; } function makeDog() { const g = new THREE.Group(); box(0.6, 0.25, 0.22, '#8a5a32', 0, 0.25, 0, true, g); box(0.2, 0.2, 0.18, '#8a5a32', 0.36, 0.42, 0, true, g); for (const x of [-0.22, 0.22]) for (const z of [-0.08, 0.08]) box(0.06, 0.25, 0.06, '#6e4626', x, 0, z, false, g); s.add(g); const leash = new THREE.Mesh(new THREE.CylinderGeometry(0.012, 0.012, 1, 6), M('#c0392b')); s.add(leash); g.userData.leash = leash; return g; } function makeCar(c) { const g = new THREE.Group(); box(4.4, 0.75, 1.7, c, 0, 0.3, 0, true, g); box(2.4, 0.6, 1.55, c, -0.3, 1.05, 0, true, g); box(0.04, 0.45, 1.4, '#2b3138', 0.92, 1.1, 0, false, g); box(2.2, 0.42, 0.04, '#2b3138', -0.3, 1.12, 0.78, false, g); box(2.2, 0.42, 0.04, '#2b3138', -0.3, 1.12, -0.78, false, g); for (const x of [1.35, -1.35]) for (const z of [0.78, -0.78]) { const w = new THREE.Mesh(new THREE.CylinderGeometry(0.32, 0.32, 0.22, 16), M('#222')); w.rotation.x = Math.PI / 2; w.position.set(x, 0.32, z); g.add(w); } s.add(g); return g; } // 目印:事故は赤、ひやりは黒の輪 const ring = (c) => { const g = new THREE.Group(); const mat = new THREE.MeshBasicMaterial({ color: c }); const m = new THREE.Mesh(new THREE.TorusGeometry(1.2, 0.07, 8, 48), mat); m.rotation.x = -Math.PI / 2; g.add(m); const pin = new THREE.Mesh(new THREE.CylinderGeometry(0.05, 0.05, 2.2, 8), mat); pin.position.y = 2.4; g.add(pin); const hd = new THREE.Mesh(new THREE.SphereGeometry(0.28, 16, 12), mat); hd.position.y = 3.6; g.add(hd); g.visible = false; s.add(g); return g; }; const markRed = ring('#d93a3a'), markBlack = ring('#111111'); // 場面ごとの登場物 const objs = { robots: {}, peds: {}, cars: {}, props: {} }; function clearActors() { for (const k of ['robots', 'peds', 'cars', 'props']) { for (const o of Object.values(objs[k])) { s.remove(o); if (o.userData.leash) s.remove(o.userData.leash); } objs[k] = {}; } } function ensureActors(f) { for (const r0 of f.robots || []) if (!objs.robots[r0.id]) objs.robots[r0.id] = makeRobot(); for (const p of f.peds || []) if (!objs.peds[p.id]) objs.peds[p.id] = p.kind === 'wheelchair' ? makeWheelchair() : p.kind === 'stroller' ? makeStroller() : p.kind === 'bike' ? makeBike() : p.kind === 'dog' ? makeDog() : makeWalker(p); for (const c of f.cars || []) if (!objs.cars[c.id]) { const o = makeCar(c.av ? '#ffffff' : c.id === 'c1' ? '#f0f0ee' : c.id === 'pk' ? '#8c3b3b' : '#4a5563'); if (c.av) { box(0.5, 0.18, 0.5, '#2b2f36', -0.3, 1.65, 0, false, o); } objs.cars[c.id] = o; } for (const pr of f.props || []) if (!objs.props[pr.id]) { const g = new THREE.Group(); if (pr.kind === 'sign') { const a = box(0.6, 0.9, 0.04, '#f2f2f2', 0, 0, 0.16, true, g); a.rotation.x = -0.18; const b2 = box(0.6, 0.9, 0.04, '#f2f2f2', 0, 0, -0.16, true, g); b2.rotation.x = 0.18; box(0.5, 0.3, 0.05, '#c0392b', 0, 0.45, 0.2, false, g); } else bikeMesh(g, 0, 0, 0); s.add(g); objs.props[pr.id] = g; } } const lerp = (a, b, k) => a + (b - a) * k; const lerpA = (a, b, k) => { let d = b - a; d = Math.atan2(Math.sin(d), Math.cos(d)); return a + d * k; }; let cur = null; // 今見せている回 function setFrame(T) { if (!cur) return; const fr = cur.frames, i = Math.min(fr.length - 1, Math.max(0, Math.floor(T / cur.dt))), j = Math.min(fr.length - 1, i + 1), k = Math.min(1, Math.max(0, T / cur.dt - i)); const a = fr[i], b = fr[j]; ensureActors(a); const blink = (performance.now() % 600) < 320; for (const ra of a.robots) { const rb = b.robots.find((q) => q.id === ra.id) || ra, o = objs.robots[ra.id], u = o.userData; o.position.set(lerp(ra.x, rb.x, k), 0.18, lerp(ra.z, rb.z, k)); o.rotation.y = lerpA(ra.h, rb.h, k); u.body.rotation.set(0, 0, 0); u.body.position.set(0, 0, 0); if (ra.tilt) u.body.rotation.z = -ra.tilt * 2; // 前の車輪が線路のすき間に落ちて前に傾く if (ra.hit) { u.body.rotation.x = Math.PI / 2.2; u.body.position.set(0, 0.2, 2.6); } u.eyes.visible = cur.scene === 'kids' && cur.cfg.face === 'face'; if (ra.fallen) { u.body.rotation.x = -Math.PI / 2; u.body.position.set(0, 0.35, -0.4); } const lm = ra.light === 'go' ? lightMat.go : ra.light === 'stop' ? lightMat.stop : ra.light === 'ask' ? (blink ? lightMat.ask : lightMat.dim) : lightMat.off; u.band.material = lm; u.bandB.material = ra.light === 'go' ? lightMat.dim : lm; // どちらへ寄るかの合図:場面1で合図ありのとき、寄る側の横の帯を光らせる const sig = sceneId === 'wheelchair' && cur.cfg.signal && ra.light !== 'off'; u.sideL.material = sig ? (blink ? lightMat.stop : lightMat.dim) : lightMat.dim; u.sideR.material = lightMat.dim; // 建物側(北)の横の帯を点滅 } for (const pa of a.peds) { const pb = b.peds.find((q) => q.id === pa.id) || pa, o = objs.peds[pa.id]; o.position.set(lerp(pa.x, pb.x, k), 0.18, lerp(pa.z, pb.z, k)); o.rotation.y = lerpA(pa.h, pb.h, k); if (o.userData.phone) o.userData.phone.visible = !!pa.phone; if (pa.kind === 'bike') o.rotation.x = pa.fallen ? Math.PI / 2.3 : 0; if (pa.kind === 'dog' && pa.leash) { const own = a.peds.find((q) => q.id === pa.leash), owb = b.peds.find((q) => q.id === pa.leash) || own; const L = o.userData.leash; const hx = lerp(own.x, owb.x, k), hz = lerp(own.z, owb.z, k), p1 = new THREE.Vector3(hx, 1.05, hz), p2 = new THREE.Vector3(o.position.x + 0.4 * Math.cos(o.rotation.y), 0.6, o.position.z - 0.4 * Math.sin(o.rotation.y)); const d = p1.distanceTo(p2); L.scale.set(1, d, 1); L.position.copy(p1).add(p2).multiplyScalar(0.5); L.quaternion.setFromUnitVectors(new THREE.Vector3(0, 1, 0), p2.clone().sub(p1).normalize()); } } for (const pr of a.props || []) { const o = objs.props[pr.id]; o.position.set(pr.x, 0.18, pr.z); o.rotation.set(pr.down ? Math.PI / 2 : 0, 0, 0); if (pr.down) { o.position.z = pr.z + 0.15; o.position.y = 0.42; } } // 夜 const night = a.env === 'night'; if (night !== lastNight) { lastNight = night; s.background.set(night ? '#1d2229' : '#eceef1'); hemi.intensity = night ? 0.35 : 1.6; sun.intensity = night ? 0.25 : 1.5; } for (const ca of a.cars || []) { const cb = (b.cars || []).find((q) => q.id === ca.id) || ca, o = objs.cars[ca.id]; o.position.set(lerp(ca.x, cb.x, k), 0, lerp(ca.z, cb.z, k)); const dx = cb.x - ca.x, dz = cb.z - ca.z; // 動いているときだけ向きを合わせる(止まっているときは前の向きのまま) if (Math.hypot(dx, dz) > 0.02) o.userData.h = Math.atan2(-dz, dx); o.rotation.y = o.userData.h || 0; } // 踏切 const g = a.gate != null ? lerp(a.gate, b.gate ?? a.gate, k) : 0; for (const p of gates) p.rotation.x = -(1 - g) * Math.PI / 2 * p.userData.dir; const on = !!a.alarm, bl = (performance.now() % 1000) < 500; for (const lm of lamps) { lm[0].material.color.set(on && bl ? '#ff2a2a' : '#4a1a1a'); lm[1].material.color.set(on && !bl ? '#ff2a2a' : '#4a1a1a'); } train.position.z = a.train ? lerp(a.train.z, b.train?.z ?? a.train.z, k) : -400; // 目印 // 事故の赤い輪は最後まで、ひやりの黒い輪は起きてから4秒だけ出す const evs = cur.events.filter((e) => e.t <= T && e.pos && (e.kind === 'hit' || (e.kind === 'near' && T < e.t + 4))); const last = evs[evs.length - 1]; markRed.visible = !!last && last.kind === 'hit'; markBlack.visible = !!last && last.kind === 'near'; if (last) (last.kind === 'hit' ? markRed : markBlack).position.set(last.pos.x, 0.2, last.pos.z); } function screenOf(x, y, z) { const v = new THREE.Vector3(x, y, z).project(cam); return { x: (v.x + 1) / 2 * host.clientWidth, y: (1 - v.y) / 2 * host.clientHeight, vis: v.z < 1 }; } function resize() { const w = host.clientWidth, h = host.clientHeight; if (!w || !h) return; r.setSize(w, h, false); cam.aspect = w / h; cam.updateProjectionMatrix(); } new ResizeObserver(resize).observe(host); resize(); let lastT = performance.now(); const followPos = new THREE.Vector3(), followLook = new THREE.Vector3(); let snap = true; function loop() { const now = performance.now(), dtR = Math.min(0.1, (now - lastT) / 1000); lastT = now; onTick(dtR); if (mode === 'follow') { const id = Object.keys(objs.robots)[0]; const o = objs.robots[id]; if (o) { const h = o.rotation.y, dx = Math.cos(h), dz = -Math.sin(h); const want = new THREE.Vector3(o.position.x - dx * 10, 5.5, o.position.z - dz * 10); const k = snap ? 1 : 1 - Math.exp(-dtR * 3); followPos.lerp(want, k); followLook.lerp(new THREE.Vector3(o.position.x + dx * 5, 0.6, o.position.z + dz * 5), k); snap = false; cam.position.copy(followPos); cam.lookAt(followLook); } } r.render(s, cam); requestAnimationFrame(loop); } let onTick = () => {}; // 操作:ドラッグで回す/右ドラッグ・Shift+ドラッグ・2本指で位置を動かす/ホイール・2本指で寄る const ptrs = new Map(); let pinch0 = 0, mid0 = null, panDrag = false; const cv = r.domElement; cv.addEventListener('contextmenu', (e) => e.preventDefault()); function pan(dx, dy) { // 画面上の動きを、地面の上の動きに直す if (mode === 'follow') setView('custom'); const k = dist * 0.0016; const rx = Math.cos(az), rz = -Math.sin(az), fx = -Math.sin(az), fz = -Math.cos(az); target.x += (-dx * rx + dy * fx) * k; target.z += (-dx * rz + dy * fz) * k; placeCam(); } cv.addEventListener('pointerdown', (e) => { cv.setPointerCapture(e.pointerId); ptrs.set(e.pointerId, { x: e.clientX, y: e.clientY }); panDrag = e.button === 2 || e.shiftKey || handMode; if (ptrs.size === 2) { const [p, q] = [...ptrs.values()]; pinch0 = Math.hypot(p.x - q.x, p.y - q.y); mid0 = { x: (p.x + q.x) / 2, y: (p.y + q.y) / 2 }; } }); cv.addEventListener('pointermove', (e) => { const p = ptrs.get(e.pointerId); if (!p) return; if (ptrs.size === 1) { if (panDrag) pan(e.clientX - p.x, e.clientY - p.y); else { if (mode === 'follow') setView('custom'); az -= (e.clientX - p.x) * 0.006; el = Math.min(1.55, Math.max(0.12, el + (e.clientY - p.y) * 0.005)); placeCam(); } } else if (ptrs.size === 2) { p.x = e.clientX; p.y = e.clientY; const [a, b] = [...ptrs.values()]; const d = Math.hypot(a.x - b.x, a.y - b.y), m = { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 }; if (pinch0) { dist = Math.min(160, Math.max(6, dist * pinch0 / d)); pinch0 = d; } if (mid0) pan(m.x - mid0.x, m.y - mid0.y); mid0 = m; placeCam(); return; } p.x = e.clientX; p.y = e.clientY; }); const up = (e) => { ptrs.delete(e.pointerId); pinch0 = 0; mid0 = null; }; cv.addEventListener('pointerup', up); cv.addEventListener('pointercancel', up); cv.addEventListener('wheel', (e) => { e.preventDefault(); if (mode === 'follow') setView('custom'); dist = Math.min(160, Math.max(6, dist * Math.exp(e.deltaY * 0.0012))); placeCam(); }, { passive: false }); // 手のひら:押すと、1本指・左ドラッグでも位置を動かせる let handMode = false; $('#hand').addEventListener('click', () => { handMode = !handMode; $('#hand').classList.toggle('on', handMode); cv.style.cursor = handMode ? 'move' : 'grab'; }); function setView(v) { mode = v === 'follow' ? 'follow' : 'orbit'; if (mode === 'follow') { snap = true; } if (v === 'iso') { az = home.az; el = home.el; dist = home.dist; target.set(home.x, 0, home.z); } if (v === 'top') { az = 0; el = 1.55; dist = home.dist * 1.15; target.set(home.x, 0, home.z); } placeCam(); document.querySelectorAll('#viewBtns button').forEach((b) => b.classList.toggle('on', b.dataset.v === v)); } $('#viewBtns').addEventListener('click', (e) => { const b = e.target.closest('button'); if (b) setView(b.dataset.v); }); function setScene(id) { home = HOMES[id]; target.set(home.x, 0, home.z); sun.position.set(home.x - 25, 45, home.z + 30); sun.target.position.set(home.x, 0, home.z); clearActors(); cur = null; setView('iso'); for (const p of gates) p.rotation.x = -Math.PI / 2 * p.userData.dir; train.position.z = -400; markRed.visible = markBlack.visible = false; } loop(); return { setScene, setFrame, setCur: (c) => { cur = c; clearActors(); }, screenOf, onTick: (f) => { onTick = f; }, objs }; })();