mirror of
https://github.com/HifiExperiments/overte.git
synced 2025-08-05 00:13:10 +02:00
securityCamera.js - Camera Entity example
This commit is contained in:
parent
184f793cdd
commit
128a0c2397
1 changed files with 55 additions and 0 deletions
55
examples/example/securityCamera.js
Normal file
55
examples/example/securityCamera.js
Normal file
|
@ -0,0 +1,55 @@
|
|||
//
|
||||
// securityCamera.js
|
||||
// examples/example
|
||||
//
|
||||
// Created by Thijs Wenker on November 4, 2015
|
||||
// Copyright 2015 High Fidelity, Inc.
|
||||
//
|
||||
// Distributed under the Apache License, Version 2.0.
|
||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
//
|
||||
|
||||
const CAMERA_OFFSET = {x: 0, y: 4, z: -14};
|
||||
const LOOKAT_START_OFFSET = {x: -10, y: 0, z: 14};
|
||||
const LOOKAT_END_OFFSET = {x: 10, y: 0, z: 14};
|
||||
|
||||
var lookatTargets = [Vec3.sum(MyAvatar.position, LOOKAT_START_OFFSET), Vec3.sum(MyAvatar.position, LOOKAT_END_OFFSET)];
|
||||
var currentTarget = 0;
|
||||
|
||||
var forward = true;
|
||||
|
||||
var oldCameraMode = Camera.mode;
|
||||
|
||||
var cameraLookAt = function(cameraPos, lookAtPos) {
|
||||
var lookAtRaw = Quat.lookAt(cameraPos, lookAtPos, Vec3.UP);
|
||||
lookAtRaw.w = -lookAtRaw.w;
|
||||
return lookAtRaw;
|
||||
};
|
||||
|
||||
cameraEntity = Entities.addEntity({
|
||||
type: "Camera",
|
||||
position: Vec3.sum(MyAvatar.position, CAMERA_OFFSET)
|
||||
});
|
||||
|
||||
Camera.mode = "camera entity";
|
||||
Camera.cameraEntity = cameraEntity;
|
||||
|
||||
Script.update.connect(function(deltaTime) {
|
||||
var cameraProperties = Entities.getEntityProperties(cameraEntity, ["position", "rotation"]);
|
||||
var targetOrientation = cameraLookAt(cameraProperties.position, lookatTargets[currentTarget]);
|
||||
if (Math.abs(targetOrientation.x - cameraProperties.rotation.x) < 0.01 &&
|
||||
Math.abs(targetOrientation.y - cameraProperties.rotation.y) < 0.01 &&
|
||||
Math.abs(targetOrientation.z - cameraProperties.rotation.z) < 0.01 &&
|
||||
Math.abs(targetOrientation.w - cameraProperties.rotation.w) < 0.01
|
||||
) {
|
||||
currentTarget = (currentTarget + 1) % lookatTargets.length;
|
||||
return;
|
||||
}
|
||||
Entities.editEntity(cameraEntity, {rotation: Quat.mix(cameraProperties.rotation, targetOrientation, deltaTime / 3)});
|
||||
});
|
||||
|
||||
Script.scriptEnding.connect(function() {
|
||||
Entities.deleteEntity(cameraEntity);
|
||||
Camera.mode = oldCameraMode;
|
||||
Camera.cameraEntity = null;
|
||||
});
|
Loading…
Reference in a new issue