Skip to content

Commit

Permalink
support cameraSet/cameraSave
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanhab committed Jun 14, 2024
1 parent 5e69078 commit 6118de4
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 23 deletions.
85 changes: 62 additions & 23 deletions editor/js/OpenSimEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function OpenSimEditor () {
// Container for all the lights, geometry, floor that are part of the scene
this.environment = undefined;
this.models = [];
this.savedCameras = {};
this.currentModel = undefined; //uuid of current model call getCurrentModel for actualobject
this.currentModelColor = new THREE.Color(0xffffff);
this.nonCurrentModelColor = new THREE.Color(0x888888);
Expand All @@ -42,7 +43,7 @@ function OpenSimEditor () {
//this.cameraEye.name = 'CameraEye';

var Signal = signals.Signal;

this.signals = {

// script
Expand Down Expand Up @@ -127,7 +128,7 @@ function OpenSimEditor () {
this.storage = new _Storage();
this.strings = new Strings( this.config );
this.loader = new OpenSimLoader();

this.camera = _DEFAULT_CAMERA.clone();
this.scene = new THREE.Scene();
// Ortho Scene and Camera for Logo and text
Expand All @@ -146,12 +147,12 @@ function OpenSimEditor () {
this.materials = {};
this.textures = {};
this.scripts = {};

this.mixer = new THREE.AnimationMixer( this.scene );

this.selected = null;
this.helpers = {};

this.groundPlane = null;
this.groundMaterial = null;
this.modelsGroup = undefined;
Expand Down Expand Up @@ -587,7 +588,7 @@ OpenSimEditor.prototype = {
this.models.push(model.uuid);
this.setCurrentModel(model.uuid);
this.adjustSceneAfterModelLoading();

//this.scripts = json.scripts;
// The next 2 line has to be made after helper was added to scene to fix helper display
var modelLight = model.getObjectByName('ModelLight');
Expand All @@ -597,7 +598,7 @@ OpenSimEditor.prototype = {
this.signals.sceneGraphChanged.dispatch();
if (!this.isExperimentalDataModel(model) || this.models.length==1)
this.viewFitAll();

this.signals.windowResize.dispatch();

this.buildCache(model);
Expand All @@ -606,7 +607,7 @@ OpenSimEditor.prototype = {
"uuid": model.uuid
};
sendText(JSON.stringify(msg));

}
},
buildCache: function( model) {
Expand All @@ -623,7 +624,7 @@ OpenSimEditor.prototype = {
//editor.clear();
editor.addfromJSON( json );
editor.signals.sceneGraphChanged.dispatch();
} );
} );
},
enableShadows: function (modeluuid, newSetting) {
var modelobject = editor.objectByUuid(modeluuid);
Expand All @@ -645,13 +646,13 @@ OpenSimEditor.prototype = {
this.signals.sceneGraphChanged.dispatch();
},
setCurrentModel: function ( modeluuid ) {
if (this.currentModel === modeluuid)
if (this.currentModel === modeluuid)
return; // Nothing to do
this.currentModel = modeluuid;
if (this.currentModel === undefined)
return;
var newCurrentModel = editor.objectByUuid(modeluuid);
// Dim light for all other models and make the model have shadows,
// Dim light for all other models and make the model have shadows,
// Specififc light
for ( var modindex = 0; modindex < this.models.length; modindex++ ) {
if (this.models[modindex] == modeluuid){
Expand All @@ -660,7 +661,7 @@ OpenSimEditor.prototype = {
modelLight.visible = true;
this.enableShadows(modeluuid, true);
}
else if (this.onlyCurrentModelCastsShadow) {
else if (this.onlyCurrentModelCastsShadow) {
other_uuid = this.models[modindex];
nonCurrentModel = editor.objectByUuid(other_uuid);
modelLight = nonCurrentModel.getObjectByName('ModelLight');
Expand Down Expand Up @@ -753,15 +754,15 @@ OpenSimEditor.prototype = {
textureloader.setPath(path);
// and then set your CORS config
var textureCube = textureloader.load( ["px.jpg",
"nx.jpg", "py.jpg", "ny.jpg",
"nx.jpg", "py.jpg", "ny.jpg",
"pz.jpg", "nz.jpg"], function () {
scope.refresh();
});
textureCube.format = THREE.RGBFormat;
textureloader.mapping = THREE.CubeRefactionMapping;
this.scene.background = textureCube;
},

createGroundPlane: function(choice) {
if (choice == 'nofloor')
return;
Expand All @@ -786,7 +787,7 @@ OpenSimEditor.prototype = {
this.addObject(groundPlane);
this.groundPlane = groundPlane;
},

createWall: function() {
var textureLoader = new THREE.TextureLoader();
var texture1 = textureLoader.load( "textures/wall.jpg" );
Expand Down Expand Up @@ -912,7 +913,7 @@ OpenSimEditor.prototype = {
this.groundPlane.visible = true;
var textureLoader = new THREE.TextureLoader();
textureLoader.load("textures/" + choice + ".jpg",
function (texture1) {
function (texture1) {
texture1.wrapS = texture1.wrapT = THREE.RepeatWrapping;
texture1.repeat.set(64, 64);
texture1.encoding = THREE.sRGBEncoding;
Expand All @@ -928,7 +929,7 @@ OpenSimEditor.prototype = {
getGroundSelection: function () {
return this.config.getKey('floor');
},

createLogoSprite: function() {
var getLogoTexture = function () {
var loader = new THREE.TextureLoader();
Expand Down Expand Up @@ -980,12 +981,12 @@ OpenSimEditor.prototype = {
},

viewZoom: function(in_out) {
// Debug
// Debug
var vector = new THREE.Vector3(0, 0, -1 * in_out);
vector.applyQuaternion(this.camera.quaternion);
var newPos = this.camera.position.add(vector);
this.camera.position.copy(newPos);

this.signals.cameraChanged.dispatch(this.camera);
},

Expand Down Expand Up @@ -1101,7 +1102,7 @@ OpenSimEditor.prototype = {
var modelLight = new THREE.PointLight( {color: this.currentModelColor});
//modelLight.castShadow = true;
//modelLight.angle = 0.5;
modelLight.intensity = this.modelLightIntensity;
modelLight.intensity = this.modelLightIntensity;
modelLight.name = 'ModelLight';
/*
modelLight.shadow.camera.bottom = -1000;
Expand All @@ -1110,7 +1111,7 @@ OpenSimEditor.prototype = {
modelLight.shadow.camera.right = 1000;
modelLight.shadow.camera.top = 1000;
*/
modelLight.position.copy(new THREE.Vector3((modelbbox.max.x+modelbbox.min.x)/2,
modelLight.position.copy(new THREE.Vector3((modelbbox.max.x+modelbbox.min.x)/2,
modelbbox.max.y+100, (modelbbox.min.z+modelbbox.max.z)/2));
modelLight.target = modelCenterGroup;
model.add(modelLight);
Expand Down Expand Up @@ -1202,7 +1203,7 @@ OpenSimEditor.prototype = {
var pptSpec = points[ppIndex];
var parentUuid = pptSpec.parent;
var parent = this.objectByUuid(parentUuid);
// create pathPoint and add it to Parent; assign uuid and keep a list
// create pathPoint and add it to Parent; assign uuid and keep a list
var geom = this.geometries[ pptSpec.geometry ];
var mat = this.materials[ pptSpec.material ];
var pptObject = new THREE.Mesh(geom, mat);
Expand Down Expand Up @@ -1259,7 +1260,7 @@ OpenSimEditor.prototype = {
var newPointJson = pathEditJson.NewPoint;
var pointParent = newPointJson.parent_uuid;
parentFrame = this.objectByUuid(pointParent);

var newPointGeometry = newPointJson.geometry;
var newPointMaterial = newPointJson.material;
var existingPoint = undefined;
Expand Down Expand Up @@ -1309,7 +1310,7 @@ OpenSimEditor.prototype = {
newGeometry.uuid = UUID;
sceneObject.geometry = newGeometry;
this.signals.geometryChanged.dispatch(sceneObject);
}
}
},
toggleRecord: function () {
if (this.recording){
Expand Down Expand Up @@ -1385,6 +1386,44 @@ OpenSimEditor.prototype = {
},
executeCommandJson: function(json) {
new CommandFactory().createCommand(this, json);
},
processCameraOp: function(json) {
switch (json.command) {
case "save":
let camName = json.name
// clone current/default Camera to camName
// and save a copy in the savedCameras dictionary
let camClone = this.camera.clone().toJSON()
camClone.object.name = camName
this.savedCameras[camName] = camClone;
break;
case "set":
let targetCamName = json.name
let targetCamJSON = this.savedCameras[targetCamName]
if (targetCamJSON !== undefined) {
this.camera.fov = targetCamJSON.object.fov
this.camera.zoom = targetCamJSON.object.zoom
this.camera.near = targetCamJSON.object.near
this.camera.far = targetCamJSON.object.far
this.camera.focus = targetCamJSON.object.focus
this.camera.aspect = targetCamJSON.object.aspect
let mat4 = new THREE.Matrix4().set(
targetCamJSON.object.matrix[0], targetCamJSON.object.matrix[4], targetCamJSON.object.matrix[8], targetCamJSON.object.matrix[12],
targetCamJSON.object.matrix[1], targetCamJSON.object.matrix[5], targetCamJSON.object.matrix[9], targetCamJSON.object.matrix[13],
targetCamJSON.object.matrix[2], targetCamJSON.object.matrix[6], targetCamJSON.object.matrix[10], targetCamJSON.object.matrix[14],
targetCamJSON.object.matrix[3], targetCamJSON.object.matrix[7], targetCamJSON.object.matrix[11], targetCamJSON.object.matrix[15]
)
mat4.decompose(this.camera.position, this.camera.quaternion, this.camera.scale)
console.log(this.camera.matrix)
this.camera.updateProjectionMatrix()
// need to update the camera control in sync
var changeEvent = { type: 'change' };
this.control.dispatchEvent( changeEvent );
}
break;


}
}

};
Expand Down
4 changes: 4 additions & 0 deletions editor/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@ async function onMessage(evt) {
};
sendText(JSON.stringify(camInfo));
break;
case "cameraOp":
editor.processCameraOp(msg);
break;
}

processing = false; // Defensive in case render never finishes/errors
}
// End test functions
Expand Down

0 comments on commit 6118de4

Please sign in to comment.