Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | import { PerspectiveCamera, Vector3, CameraHelper } from "three";
import config from "../core/config";
import Scene from "../core/Scene";
import { ENTITY_TYPES } from "./constants";
import Entity from "./Entity";
import HelperSprite from "./base/HelperSprite";
import { TAGS } from "../lib/constants";
export default class Camera extends Entity {
constructor(options = {}) {
const {
name = "Main PerspectiveCamera",
fov = config.camera().fov,
ratio = config.screen().ratio,
near = config.camera().near,
far = config.camera().far,
serializable = true,
} = options;
super({ name, serializable });
this.extendOptions(options);
const body = new PerspectiveCamera(fov, ratio, near, far);
this.setBody({ body });
this.setEntityType(ENTITY_TYPES.CAMERA.TYPE);
this.setEntitySubtype(ENTITY_TYPES.CAMERA.SUBTYPES.MAIN);
this.setName(name);
// Helper body for this camera
this.isUsingHelper = false;
// Holder body representing the camera (for selection in editor)
this.holder = null;
// THREE.js CameraHelper for visual feedback
this.helper = null;
}
hasHolder() {
return !!this.holder;
}
usingHelper() {
return this.isUsingHelper;
}
addHolder(name = "cameraholder", size = 0.05) {
const holderSprite = new HelperSprite(size, size, name, { name });
if (holderSprite) {
holderSprite.setSizeAttenuation(false);
holderSprite.setDepthTest(false);
holderSprite.setDepthWrite(false);
holderSprite.setSerializable(false);
holderSprite.setPosition(this.getPosition());
holderSprite.addTags([TAGS.HELPER, TAGS.CAMERA_HELPER, name]);
holderSprite.setHelperTarget(this);
this.holder = holderSprite;
return true;
}
console.warn("[Mage] Camera holder texture not found");
return false;
}
addHelpers({ holderName = "cameraholder", holderSize = 0.05 } = {}) {
// Add THREE.js CameraHelper for visual feedback
this.helper = new CameraHelper(this.getBody());
// Set to layer 1 ONLY so mirrors don't render camera helper
this.helper.layers.set(1);
// Disable depth test so helper always renders on top of sky/water
this.helper.renderOrder = 999;
// Helper function to set depth properties on materials
const setMaterialDepth = (material) => {
if (!material) return;
const mats = Array.isArray(material) ? material : [material];
mats.forEach(mat => {
mat.depthTest = false;
mat.depthWrite = false;
mat.transparent = true;
mat.needsUpdate = true;
});
};
setMaterialDepth(this.helper.material);
// Also apply to all children
this.helper.traverse(child => {
child.layers.set(1);
child.renderOrder = 999;
setMaterialDepth(child.material);
});
Scene.add(this.helper, null, false);
// Add holder sprite for selection
this.addHolder(holderName, holderSize);
this.isUsingHelper = true;
}
getPosition() {
return {
x: this.body.position.x,
y: this.body.position.y,
z: this.body.position.z,
};
}
getDirection() {
const vector = new Vector3();
const { x, y, z } = this.getBody().getWorldDirection(vector);
return {
x,
y,
z,
};
}
lookAt(position = {}) {
const { x = 0, y = 0, z = 0 } = position;
this.body.lookAt(x, y, z);
}
setPosition(where, { updateHolder = true } = {}) {
const currentPos = this.getPosition();
const position = {
x: currentPos.x,
y: currentPos.y,
z: currentPos.z,
...where,
};
const { x, y, z } = position;
if (this.hasBody()) {
this.body.position.set(x, y, z);
}
if (this.hasHolder() && updateHolder) {
this.holder.setPosition({ x, y, z });
}
}
update(dt) {
super.update && super.update(dt);
// Update the THREE.js CameraHelper
if (this.usingHelper() && this.helper) {
this.helper.update();
}
// Sync camera position from holder (when user drags the holder)
if (this.hasHolder()) {
// Read directly from the holder's THREE.js body position
const holderBody = this.holder.getBody();
const holderPos = {
x: holderBody.position.x,
y: holderBody.position.y,
z: holderBody.position.z,
};
const cameraPos = this.getPosition();
// Only update if positions differ significantly
const threshold = 0.001;
if (Math.abs(holderPos.x - cameraPos.x) > threshold ||
Math.abs(holderPos.y - cameraPos.y) > threshold ||
Math.abs(holderPos.z - cameraPos.z) > threshold) {
this.setPosition(holderPos, { updateHolder: false });
}
}
}
getFov() {
return this.getBody().fov;
}
setFov(fov) {
const num = Number(fov);
const numericFov = Number.isFinite(num) ? num : 75;
this.getBody().fov = numericFov;
this.getBody().updateProjectionMatrix();
}
getNear() {
return this.getBody().near;
}
setNear(near) {
const num = Number(near);
const numericNear = Number.isFinite(num) ? num : 0.1;
this.getBody().near = numericNear;
this.getBody().updateProjectionMatrix();
}
getFar() {
return this.getBody().far;
}
setFar(far) {
const num = Number(far);
const numericFar = Number.isFinite(num) ? num : 3000000;
this.getBody().far = numericFar;
this.getBody().updateProjectionMatrix();
}
toJSON(parseJSON = false) {
if (this.isSerializable()) {
return {
...super.toJSON(parseJSON),
fov: this.getFov(),
near: this.getNear(),
far: this.getFar(),
};
}
}
}
|