controls folder

This commit is contained in:
Zemledelec 2018-03-08 12:34:38 +03:00
parent 7434321492
commit 43170f2d9e
15 changed files with 1927 additions and 2150 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +0,0 @@
/**
* Controls collection.
* @namespace og.control
*/

View File

@ -1,9 +1,49 @@
goog.provide('og.control.EarthCoordinates');
/**
* @module og/control/EarthCoordinates
*/
goog.require('og.inheritance');
goog.require('og.control.BaseControl');
goog.require('og.planetSegment');
goog.require('og.mercator');
'use strict';
import { BaseControl } from './BaseControl.js';
function dec2deg(base) {
var t, t2;
var degrees = base < 0 ? Math.ceil(base) : Math.floor(base);
var minutes = Math.floor(t = Math.abs((base - degrees)) * 60);
var seconds = Math.floor(t2 = (t - minutes) * 6000);
seconds = seconds / 100.00;
return (numToFixedString(degrees, 3) + "\u00B0" +
numToFixedString(minutes, 2) + "\u0027" +
numToFixedString(seconds.toFixed(2), 2) + "\u0022");
};
function numToFixedString(num, fixed) {
var dl = num.toString().split('.')[0].length;
var white = "&nbsp;";
for (var i = dl; i < fixed; i++) {
white += '&nbsp;&nbsp;';
}
return white + num.toString();
};
function toDecimal(ll) {
return ll.lat.toFixed(5) + ", " + ll.lon.toFixed(5);
};
function toDegrees(ll) {
return dec2deg(ll.lat) + ", " + dec2deg(ll.lon);
};
function toMercator(ll) {
var m = ll.forwardMercator();
return m.lat.toFixed(5) + ", " + m.lon.toFixed(5);
};
const DisplayTypesConverters = [
toDecimal,
toDegrees,
toMercator
];
/**
* Control displays mouse or screen center Earth coordinates.
@ -13,184 +53,147 @@ goog.require('og.mercator');
* @param {Boolean} [options.center] - Earth coordiantes by screen center otherwise mouse pointer. False is default.
* @param {Boolean} [options.type] - Coordinates shown: 0 - is decimal degrees, 1 - degrees, 2 - mercator geodetic coordinates.
*/
og.control.EarthCoordinates = function (options) {
og.inheritance.base(this, options);
class EarthCoordinates extends BaseControl {
constructor(options) {
super(options);
options = options || {};
options = options || {};
/**
* Display type.
* @private
* @type {Boolean}
*/
this._displayType = options.type || 0;
/**
* Display type.
* @private
* @type {Boolean}
*/
this._displayType = options.type || 0;
/**
* Current coordinates type converter.
* @private
* @function
*/
this._converter = og.control.EarthCoordinates.DisplayTypesConverters[0];
/**
* Current coordinates type converter.
* @private
* @function
*/
this._converter = DisplayTypesConverters[0];
/**
* Display dom element.
* @private
* @type {Object}
*/
this._display = null;
/**
* Display dom element.
* @private
* @type {Object}
*/
this._display = null;
/**
* Screen center or mouse pointer coordinates show flag.
* @private
* @type {Boolean}
*/
/**
* Screen center or mouse pointer coordinates show flag.
* @private
* @type {Boolean}
*/
var pad = false;
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
pad = true;
}
this._center = options.center || pad;
/**
* Current position.
* @public
* @type {og.math.Vector3}
*/
this.position = null;
};
og.control.earthCoordinates = function (options) {
return new og.control.EarthCoordinates(options);
};
og.inheritance.extend(og.control.EarthCoordinates, og.control.BaseControl);
og.control.EarthCoordinates.toDecimal = function (ll) {
return ll.lat.toFixed(5) + ", " + ll.lon.toFixed(5);
};
og.control.EarthCoordinates.toDegrees = function (ll) {
return og.control.EarthCoordinates.dec2deg(ll.lat) + ", " + og.control.EarthCoordinates.dec2deg(ll.lon);
};
og.control.EarthCoordinates.toMercator = function (ll) {
var m = ll.forwardMercator();
return m.lat.toFixed(5) + ", " + m.lon.toFixed(5);
};
og.control.EarthCoordinates.DisplayTypesConverters = [
og.control.EarthCoordinates.toDecimal,
og.control.EarthCoordinates.toDegrees,
og.control.EarthCoordinates.toMercator
];
og.control.EarthCoordinates.dec2deg = function (base) {
var t, t2;
var degrees = base < 0 ? Math.ceil(base) : Math.floor(base);
var minutes = Math.floor(t = Math.abs((base - degrees)) * 60);
var seconds = Math.floor(t2 = (t - minutes) * 6000);
seconds = seconds / 100.00;
return (og.control.EarthCoordinates.numToFixedString(degrees, 3) + "\u00B0" +
og.control.EarthCoordinates.numToFixedString(minutes, 2) + "\u0027" +
og.control.EarthCoordinates.numToFixedString(seconds.toFixed(2), 2) + "\u0022");
};
og.control.EarthCoordinates.numToFixedString = function (num, fixed) {
var dl = num.toString().split('.')[0].length;
var white = "&nbsp;";
for (var i = dl; i < fixed; i++) {
white += '&nbsp;&nbsp;';
}
return white + num.toString();
};
og.control.EarthCoordinates.prototype.oninit = function () {
this._display = document.createElement('div');
this._display.className = 'ogEarthCoordinatesControl';
var that = this;
function _refresh(el) {
if (that._displayType >= og.control.EarthCoordinates.DisplayTypesConverters.length)
that._displayType = 0;
if (that._displayType == 0) {
el.style.width = "275px";
} else if (that._displayType == 1) {
el.style.width = "355px";
} else if (that._displayType == 2) {
el.style.width = "350px";
var pad = false;
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
pad = true;
}
that._converter = og.control.EarthCoordinates.DisplayTypesConverters[that._displayType];
that._showPosition();
};
this._display.onclick = function (e) {
that._displayType += 1;
_refresh(this);
};
this._center = options.center || pad;
this.renderer.div.appendChild(this._display);
_refresh(this._display);
centerDiv = document.createElement('div');
centerDiv.className = 'ogCenterIcon';
centerDiv.innerHTML = '<svg width="12" height="12"><g><path stroke-width="1" stroke-opacity="1" d="M6 0L6 12M0 6L12 6" stroke="#009DFF"></path></g></svg>';
this.renderer.div.appendChild(centerDiv);
if (this._center) {
this.renderer.activeCamera.events.on("moveend", this._grabCoordinates, this);
centerDiv.style.display = "block";
} else {
this.renderer.events.on("mousemove", this._onMouseMove, this);
centerDiv.style.display = "none";
/**
* Current position.
* @public
* @type {og.math.Vector3}
*/
this.position = null;
}
};
oninit() {
this._display = document.createElement('div');
this._display.className = 'ogEarthCoordinatesControl';
var that = this;
/**
* Sets coordinates capturing type.
* @public
* @param {Boolean} center - True - capture screen center, false - mouse pointer.
*/
og.control.EarthCoordinates.prototype.setCenter = function (center) {
if (center != this._center) {
this._center = center;
if (center) {
this.renderer.events.off("mousemove", this._onMouseMove);
function _refresh(el) {
if (that._displayType >= DisplayTypesConverters.length)
that._displayType = 0;
if (that._displayType == 0) {
el.style.width = "275px";
} else if (that._displayType == 1) {
el.style.width = "355px";
} else if (that._displayType == 2) {
el.style.width = "350px";
}
that._converter = DisplayTypesConverters[that._displayType];
that._showPosition();
};
this._display.onclick = function (e) {
that._displayType += 1;
_refresh(this);
};
this.renderer.div.appendChild(this._display);
_refresh(this._display);
centerDiv = document.createElement('div');
centerDiv.className = 'ogCenterIcon';
centerDiv.innerHTML = '<svg width="12" height="12"><g><path stroke-width="1" stroke-opacity="1" d="M6 0L6 12M0 6L12 6" stroke="#009DFF"></path></g></svg>';
this.renderer.div.appendChild(centerDiv);
if (this._center) {
this.renderer.activeCamera.events.on("moveend", this._grabCoordinates, this);
centerDiv.style.display = "block";
} else {
this.renderer.events.off("draw", this._draw);
this.renderer.events.on("mousemove", this._onMouseMove, this);
centerDiv.style.display = "none";
}
}
/**
* Sets coordinates capturing type.
* @public
* @param {Boolean} center - True - capture screen center, false - mouse pointer.
*/
setCenter(center) {
if (center != this._center) {
this._center = center;
if (center) {
this.renderer.events.off("mousemove", this._onMouseMove);
this.renderer.activeCamera.events.on("moveend", this._grabCoordinates, this);
centerDiv.style.display = "block";
} else {
this.renderer.events.off("draw", this._draw);
this.renderer.events.on("mousemove", this._onMouseMove, this);
centerDiv.style.display = "none";
}
}
}
_showPosition() {
if (this.position) {
this.position.height = ((this.position.height > 10000 || this.position.height < -10000) ? 0 : this.position.height);
this._display.innerHTML = "Lat/Lon: " + this._converter(this.position) + " h(km): " + (this.position.height > 0 ? "~" + (Math.round(this.position.height) / 1000).toFixed(2) : "-");
} else {
this._display.innerHTML = "Lat/Lon: " + "_____________________";
}
}
_grabCoordinates() {
var r = this.renderer;
var ts = r.events.touchState;
this.position = this.planet.getLonLatFromPixelTerrain(r.handler.getCenter());
this._showPosition();
}
_onMouseMove() {
var r = this.renderer;
var ms = r.events.mouseState;
if (!(ms.leftButtonDown || ms.rightButtonDown) &&
r.controlsBag.scaleRot <= 0 &&
!r.activeCamera._flying) {
this.position = this.planet.getLonLatFromPixelTerrain(ms, true);
this._showPosition();
}
}
};
og.control.EarthCoordinates.prototype._showPosition = function () {
if (this.position) {
this.position.height = ((this.position.height > 10000 || this.position.height < -10000) ? 0 : this.position.height);
this._display.innerHTML = "Lat/Lon: " + this._converter(this.position) + " h(km): " + (this.position.height > 0 ? "~" + (Math.round(this.position.height) / 1000).toFixed(2) : "-");
} else {
this._display.innerHTML = "Lat/Lon: " + "_____________________";
}
export function earthCoordinates(options) {
return new EarthCoordinates(options);
};
og.control.EarthCoordinates.prototype._grabCoordinates = function () {
var r = this.renderer;
var ts = r.events.touchState;
this.position = this.planet.getLonLatFromPixelTerrain(r.handler.getCenter());
this._showPosition();
};
og.control.EarthCoordinates.prototype._onMouseMove = function () {
var r = this.renderer;
var ms = r.events.mouseState;
if (!(ms.leftButtonDown || ms.rightButtonDown) &&
r.controlsBag.scaleRot <= 0 &&
!r.activeCamera._flying) {
this.position = this.planet.getLonLatFromPixelTerrain(ms, true);
this._showPosition();
}
};
export { EarthCoordinates };

View File

@ -1,62 +1,62 @@
goog.provide('og.control.GeoImageDragControl');
/**
* @module og/control/GeoImageDragControl
*/
goog.require('og.inheritance');
goog.require('og.control.BaseControl');
goog.require('og.mercator');
goog.require('og.layer.BaseGeoImage');
'use strict';
og.control.GeoImageDragControl = function (options) {
og.inheritance.base(this, options);
import {BaseControl} from './BaseControl.js';
import { BaseGeoImage } from '../layer/BaseGeoImage.js');
options = options || {};
class GeoImageDragControl extends BaseControl {
constructor(options) {
super(options);
this._cornerIndex = -1;
this._catchCorner = false;
options = options || {};
};
this._cornerIndex = -1;
this._catchCorner = false;
og.inheritance.extend(og.control.GeoImageDragControl, og.control.BaseControl);
}
og.control.geoImageDragControl = function (options) {
return new og.control.GeoImageDragControl(options);
};
og.control.GeoImageDragControl.prototype.oninit = function () {
var that = this;
var p = this.planet;
p.events.on('layeradd', function (e) {
if (e instanceof og.layer.BaseGeoImage) {
e.events.on('mousemove', function (ms) {
if (that._active) {
if (that._catchCorner) {
var corners = e.getCornersLonLat();
corners[that._cornerIndex] = p.getLonLatFromPixelTerrain(ms, true);
e.setCornersLonLat(corners);
} else {
that._cornerIndex = -1;
for (var i = 0; i < e._cornersWgs84.length; i++) {
var ground = p.getLonLatFromPixelTerrain(ms, true)
if (ground && p.ellipsoid.getGreatCircleDistance(e._cornersWgs84[i], ground) / p.getDistanceFromPixel(ms, true) <= 0.05) {
that._cornerIndex = i;
break;
oninit() {
var that = this;
var p = this.planet;
p.events.on('layeradd', function (e) {
if (e instanceof BaseGeoImage) {
e.events.on('mousemove', function (ms) {
if (that._active) {
if (that._catchCorner) {
var corners = e.getCornersLonLat();
corners[that._cornerIndex] = p.getLonLatFromPixelTerrain(ms, true);
e.setCornersLonLat(corners);
} else {
that._cornerIndex = -1;
for (var i = 0; i < e._cornersWgs84.length; i++) {
var ground = p.getLonLatFromPixelTerrain(ms, true)
if (ground && p.ellipsoid.getGreatCircleDistance(e._cornersWgs84[i], ground) / p.getDistanceFromPixel(ms, true) <= 0.05) {
that._cornerIndex = i;
break;
}
}
}
}
}
});
e.events.on('ldown', function (ms) {
if (that._active && that._cornerIndex != -1) {
that._catchCorner = true;
p.renderer.controls[0]._active = false;
}
});
});
e.events.on('ldown', function (ms) {
if (that._active && that._cornerIndex != -1) {
that._catchCorner = true;
p.renderer.controls[0]._active = false;
}
});
e.events.on('lup', function (ms) {
if (that._active) {
that._catchCorner = false;
p.renderer.controls[0]._active = true;
}
});
}
});
};
e.events.on('lup', function (ms) {
if (that._active) {
that._catchCorner = false;
p.renderer.controls[0]._active = true;
}
});
}
});
}
};
export { GeoImageDragControl };

View File

@ -1,6 +1,10 @@
goog.provide('og.control.LayerSwitcher');
/**
* @module og/control/LayerSwitcher
*/
goog.require('og.inheritance');
'use strict';
import { BaseControl } from './BaseControl.js';
/**
* Simple(OpenLayers like)layer switcher, includes base layers, overlays, geo images etc. groups.
@ -8,143 +12,148 @@ goog.require('og.inheritance');
* @extends {og.control.BaseControl}
* @param {Object} [options] - Control options.
*/
og.control.LayerSwitcher = function (options) {
og.inheritance.base(this, options);
class LayerSwitcher extends BaseControl {
constructor(options) {
super(options);
options = options || {};
this.dialog = null;
this.baseLayersDiv = null;
this.overlaysDiv = null;
this._id = LayerSwitcher.numSwitches++;
}
this.dialog = null;
this.baseLayersDiv = null;
this.overlaysDiv = null;
this._id = og.control.LayerSwitcher.numSwitches++;
};
og.control.LayerSwitcher.numSwitches = 0;
og.inheritance.extend(og.control.LayerSwitcher, og.control.BaseControl);
og.control.layerSwitcher = function (options) {
return new og.control.LayerSwitcher(options);
};
og.control.LayerSwitcher.prototype.oninit = function () {
this.planet.events.on("layeradd", this.onLayerAdded, this);
this.planet.events.on("layerremove", this.onLayerRemoved, this);
this.createSwitcher();
this.createDialog();
};
og.control.LayerSwitcher.prototype.onLayerAdded = function (layer) {
if (layer.displayInLayerSwitcher) {
if (layer.isBaseLayer()) {
this.addSwitcher("radio", layer, this.baseLayersDiv);
} else {
this.addSwitcher("checkbox", layer, this.overlaysDiv, this._id);
static get numSwitches() {
if (!this._counter && this._counter !== 0) {
this._counter = 0;
}
return this._counter;
}
static set numSwitches(n) {
this._counter = n;
}
oninit() {
this.planet.events.on("layeradd", this.onLayerAdded, this);
this.planet.events.on("layerremove", this.onLayerRemoved, this);
this.createSwitcher();
this.createDialog();
}
onLayerAdded(layer) {
if (layer.displayInLayerSwitcher) {
if (layer.isBaseLayer()) {
this.addSwitcher("radio", layer, this.baseLayersDiv);
} else {
this.addSwitcher("checkbox", layer, this.overlaysDiv, this._id);
}
}
};
onLayerRemoved(layer) {
layer._removeCallback();
layer._removeCallback = null;
}
addSwitcher(type, obj, container, id) {
var lineDiv = document.createElement('div');
var that = this;
var center = document.createElement('div');
center.classList.add('ogViewExtentBtn');
center.onclick = function () {
that.planet.flyExtent(obj.getExtent());
};
var inp = document.createElement('input');
inp.type = type;
inp.name = "ogBaseLayerRadiosId" + (id || "");
inp.checked = obj.getVisibility();
inp.className = "ogLayerSwitcherInput";
inp.onclick = function () {
obj.setVisibility(this.checked);
};
obj.events && obj.events.on("visibilitychange", function (e) {
inp.checked = e.getVisibility();
});
var lbl = document.createElement('label');
lbl.className = "ogLayerSwitcherLabel";
lbl.innerHTML = (obj.name || obj.src || "noname") + "</br>";
obj._removeCallback = function () {
container.removeChild(lineDiv);
}
lineDiv.appendChild(center);
lineDiv.appendChild(inp);
lineDiv.appendChild(lbl);
container.appendChild(lineDiv);
}
createBaseLayersContainer = function () {
var layersDiv = document.createElement('div');
layersDiv.className = "layersDiv";
this.dialog.appendChild(layersDiv);
var baseLayersLbl = document.createElement('div');
baseLayersLbl.className = "layersDiv";
baseLayersLbl.innerHTML = "Base Layer";
layersDiv.appendChild(baseLayersLbl);
this.baseLayersDiv = document.createElement('div');
layersDiv.appendChild(this.baseLayersDiv);
}
createOverlaysContainer() {
var overlaysDiv = document.createElement('div');
overlaysDiv.className = "layersDiv";
this.dialog.appendChild(overlaysDiv);
var overlaysLbl = document.createElement('div');
overlaysLbl.className = "layersDiv";
overlaysLbl.innerHTML = "Overlays";
overlaysDiv.appendChild(overlaysLbl);
this.overlaysDiv = document.createElement('div');
overlaysDiv.appendChild(this.overlaysDiv);
}
createDialog() {
this.dialog = document.createElement('div');
this.dialog.id = "ogLayerSwitcherDialog";
this.dialog.className = "displayNone";
this.renderer.div.appendChild(this.dialog);
this.createBaseLayersContainer();
this.createOverlaysContainer();
if (this.planet) {
for (var i = 0; i < this.planet.layers.length; i++) {
this.onLayerAdded(this.planet.layers[i]);
}
}
}
createSwitcher() {
var button = document.createElement('div');
button.className = 'ogLayerSwitcherButton';
button.id = "ogLayerSwitcherButtonMaximize";
var that = this;
button.onclick = function (e) {
if (this.id === "ogLayerSwitcherButtonMaximize") {
this.id = "ogLayerSwitcherButtonMinimize";
that.dialog.className = "displayBlock";
} else {
this.id = "ogLayerSwitcherButtonMaximize";
that.dialog.className = "displayNone";
}
};
this.renderer.div.appendChild(button);
}
};
og.control.LayerSwitcher.prototype.onLayerRemoved = function (layer) {
layer._removeCallback();
layer._removeCallback = null;
};
og.control.LayerSwitcher.prototype.addSwitcher = function (type, obj, container, id) {
var lineDiv = document.createElement('div');
var that = this;
var center = document.createElement('div');
center.classList.add('ogViewExtentBtn');
center.onclick = function () {
that.planet.flyExtent(obj.getExtent());
};
var inp = document.createElement('input');
inp.type = type;
inp.name = "ogBaseLayerRadiosId" + (id || "");
inp.checked = obj.getVisibility();
inp.className = "ogLayerSwitcherInput";
inp.onclick = function () {
obj.setVisibility(this.checked);
};
obj.events && obj.events.on("visibilitychange", function (e) {
inp.checked = e.getVisibility();
});
var lbl = document.createElement('label');
lbl.className = "ogLayerSwitcherLabel";
lbl.innerHTML = (obj.name || obj.src || "noname") + "</br>";
obj._removeCallback = function () {
container.removeChild(lineDiv);
}
lineDiv.appendChild(center);
lineDiv.appendChild(inp);
lineDiv.appendChild(lbl);
container.appendChild(lineDiv);
};
og.control.LayerSwitcher.prototype.createBaseLayersContainer = function () {
var layersDiv = document.createElement('div');
layersDiv.className = "layersDiv";
this.dialog.appendChild(layersDiv);
var baseLayersLbl = document.createElement('div');
baseLayersLbl.className = "layersDiv";
baseLayersLbl.innerHTML = "Base Layer";
layersDiv.appendChild(baseLayersLbl);
this.baseLayersDiv = document.createElement('div');
layersDiv.appendChild(this.baseLayersDiv);
};
og.control.LayerSwitcher.prototype.createOverlaysContainer = function () {
var overlaysDiv = document.createElement('div');
overlaysDiv.className = "layersDiv";
this.dialog.appendChild(overlaysDiv);
var overlaysLbl = document.createElement('div');
overlaysLbl.className = "layersDiv";
overlaysLbl.innerHTML = "Overlays";
overlaysDiv.appendChild(overlaysLbl);
this.overlaysDiv = document.createElement('div');
overlaysDiv.appendChild(this.overlaysDiv);
};
og.control.LayerSwitcher.prototype.createDialog = function () {
this.dialog = document.createElement('div');
this.dialog.id = "ogLayerSwitcherDialog";
this.dialog.className = "displayNone";
this.renderer.div.appendChild(this.dialog);
this.createBaseLayersContainer();
this.createOverlaysContainer();
if (this.planet) {
for (var i = 0; i < this.planet.layers.length; i++) {
this.onLayerAdded(this.planet.layers[i]);
}
}
};
og.control.LayerSwitcher.prototype.createSwitcher = function () {
var button = document.createElement('div');
button.className = 'ogLayerSwitcherButton';
button.id = "ogLayerSwitcherButtonMaximize";
var that = this;
button.onclick = function (e) {
if (this.id === "ogLayerSwitcherButtonMaximize") {
this.id = "ogLayerSwitcherButtonMinimize";
that.dialog.className = "displayBlock";
} else {
this.id = "ogLayerSwitcherButtonMaximize";
that.dialog.className = "displayNone";
}
};
this.renderer.div.appendChild(button);
};
export { LayerSwitcher };

View File

@ -1,56 +0,0 @@
goog.provide('og.control.LoadingSpinner');
goog.require('og.inheritance');
goog.require('og.control.BaseControl');
og.control.LoadingSpinner = function (options) {
og.inheritance.base(this, options);
options = options || {};
this.spinnerElement = null;
};
og.inheritance.extend(og.control.LoadingSpinner, og.control.BaseControl);
og.control.LoadingSpinner.prototype.oninit = function () {
this.spinnerElement = document.createElement('div');
this.spinnerElement.id = 'circleG';
var a = document.createElement('div');
var b = document.createElement('div');
var c = document.createElement('div');
this.spinnerElement.appendChild(a);
this.spinnerElement.appendChild(b);
this.spinnerElement.appendChild(c);
a.id = "circleG_1"; a.className = "circleG";
b.id = "circleG_2"; b.className = "circleG";
c.id = "circleG_3"; c.className = "circleG";
this.spinnerElement.className = "displayBlock";
document.body.appendChild(this.spinnerElement);
this.renderer.events.on("draw", this.draw, this);
};
og.control.LoadingSpinner.prototype.draw = function () {
var spinning = false,
p = this.planet,
l = p.layers,
i = 0;
while(i < l.length) {
if( l[i++].counter != 0 ) {
spinning = true;
break;
}
}
if(p.terrainProvider.counter != 0 || spinning) {
this.spinnerElement.className = "displayBlock";
} else {
this.spinnerElement.className = "displayNone";
}
};

View File

@ -1,14 +1,19 @@
goog.provide('og.control.MouseNavigation');
/**
* @module og/control/MouseNavigation
*/
goog.require('og.inheritance');
goog.require('og.control.BaseControl');
goog.require('og.math');
goog.require('og.math.Vector3');
goog.require('og.math.Matrix4');
goog.require('og.math.Quaternion');
goog.require('og.bv.Sphere');
goog.require('og.math.Ray');
goog.require('og.idle');
'use strict';
import * as math from '../math.js';
import { BaseControl } from './BaseControl.js';
import { input } from '../input/input.js';
import { Key } from '../Lock.js';
import { LonLat } from '../LonLat.js';
import { Mat4 } from '../math/Mat4.js';
import { Quat } from '../math/Quat.js';
import { Ray } from '../math/Ray.js';
import { Sphere } from '../bv/Sphere.js';
import { Vec3 } from '../math/Vec3.js';
/**
@ -17,308 +22,306 @@ goog.require('og.idle');
* @extends {og.control.BaseControl}
* @param {Object} [options] - Control options.
*/
og.control.MouseNavigation = function (options) {
og.inheritance.base(this, options);
class MouseNavigation extends BaseControl {
constructor(options) {
super(options);
options = options || {};
options = options || {};
this.grabbedPoint = new og.math.Vector3();
this._eye0 = new og.math.Vector3();
this.pointOnEarth = new og.math.Vector3();
this.earthUp = new og.math.Vector3();
this.inertia = 0.007;
this.grabbedSpheroid = new og.bv.Sphere();
this.planet = null;
this.qRot = new og.math.Quaternion();
this.scaleRot = 0;
this.grabbedPoint = new Vec3();
this._eye0 = new Vec3();
this.pointOnEarth = new Vec3();
this.earthUp = new Vec3();
this.inertia = 0.007;
this.grabbedSpheroid = new Sphere();
this.planet = null;
this.qRot = new Quat();
this.scaleRot = 0;
this.distDiff = 0.33;
this.stepsCount = 5;
this.stepsForward = null;
this.stepIndex = 0;
this.distDiff = 0.33;
this.stepsCount = 5;
this.stepsForward = null;
this.stepIndex = 0;
this._keyLock = new og.idle.Key();
};
og.inheritance.extend(og.control.MouseNavigation, og.control.BaseControl);
og.control.mouseNavigation = function (options) {
return new og.control.MouseNavigation(options);
};
og.control.MouseNavigation.getMovePointsFromPixelTerrain = function (cam, planet, stepsCount, delta, point, forward, dir) {
var steps = []
var eye = cam.eye.clone(),
n = cam._n.clone(),
u = cam._u.clone(),
v = cam._v.clone();
var a = planet.getCartesianFromPixelTerrain(point, true);
if (!dir) {
dir = og.math.Vector3.sub(a, cam.eye).normalize();
this._keyLock = new Key();
}
var d = a ? delta * cam.eye.distance(a) / stepsCount : 1000;
static getMovePointsFromPixelTerrain(cam, planet, stepsCount, delta, point, forward, dir) {
if (forward) {
d = -d;
} else {
d *= 2;
}
var steps = []
var scaled_n = n.scaleTo(d);
var eye = cam.eye.clone(),
n = cam._n.clone(),
u = cam._u.clone(),
v = cam._v.clone();
if (a && cam._lonLat.height > 9000 && cam.slope > 0.6) {
var grabbedSpheroid = new og.bv.Sphere();
grabbedSpheroid.radius = a.length();
var a = planet.getCartesianFromPixelTerrain(point, true);
var rotArr = [],
eyeArr = []
var breaked = false;
for (var i = 0; i < stepsCount; i++) {
eye.addA(scaled_n);
var b = new og.math.Ray(eye, dir).hitSphere(grabbedSpheroid);
eyeArr[i] = eye.clone();
if (b) {
rotArr[i] = new og.math.Matrix4().rotateBetweenVectors(a.normal(), b.normal());
} else {
breaked = true;
break;
}
if (!dir) {
dir = Vec3.sub(a, cam.eye).normalize();
}
if (!breaked) {
var d = a ? delta * cam.eye.distance(a) / stepsCount : 1000;
if (forward) {
d = -d;
} else {
d *= 2;
}
var scaled_n = n.scaleTo(d);
if (a && cam._lonLat.height > 9000 && cam.slope > 0.6) {
var grabbedSpheroid = new Sphere();
grabbedSpheroid.radius = a.length();
var rotArr = [],
eyeArr = []
var breaked = false;
for (var i = 0; i < stepsCount; i++) {
var rot = rotArr[i];
steps[i] = {};
steps[i].eye = rot.mulVec3(eyeArr[i]);
steps[i].v = rot.mulVec3(v);
steps[i].u = rot.mulVec3(u);
steps[i].n = rot.mulVec3(n);
eye.addA(scaled_n);
var b = new Ray(eye, dir).hitSphere(grabbedSpheroid);
eyeArr[i] = eye.clone();
if (b) {
rotArr[i] = new Mat4().rotateBetweenVectors(a.normal(), b.normal());
} else {
breaked = true;
break;
}
}
if (!breaked) {
for (var i = 0; i < stepsCount; i++) {
var rot = rotArr[i];
steps[i] = {};
steps[i].eye = rot.mulVec3(eyeArr[i]);
steps[i].v = rot.mulVec3(v);
steps[i].u = rot.mulVec3(u);
steps[i].n = rot.mulVec3(n);
}
} else {
eye = cam.eye.clone();
for (var i = 0; i < stepsCount; i++) {
steps[i] = {};
steps[i].eye = eye.addA(scaled_n).clone();
steps[i].v = v;
steps[i].u = u;
steps[i].n = n;
}
}
} else {
eye = cam.eye.clone();
for (var i = 0; i < stepsCount; i++) {
steps[i] = {};
steps[i].eye = eye.addA(scaled_n).clone();
steps[i].eye = eye.addA(dir.scaleTo(-d)).clone();
steps[i].v = v;
steps[i].u = u;
steps[i].n = n;
}
}
} else {
for (var i = 0; i < stepsCount; i++) {
steps[i] = {};
steps[i].eye = eye.addA(dir.scaleTo(-d)).clone();
steps[i].v = v;
steps[i].u = u;
steps[i].n = n;
}
return steps;
}
return steps;
};
og.control.MouseNavigation.prototype.onactivate = function () {
this.renderer.events.on("mousewheel", this.onMouseWheel, this);
this.renderer.events.on("lhold", this.onMouseLeftButtonDown, this);
this.renderer.events.on("rhold", this.onMouseRightButtonDown, this);
this.renderer.events.on("ldown", this.onMouseLeftButtonClick, this);
this.renderer.events.on("lup", this.onMouseLeftButtonUp, this);
this.renderer.events.on("rdown", this.onMouseRightButtonClick, this);
this.renderer.events.on("ldblclick", this.onMouseLeftButtonDoubleClick, this);
this.renderer.events.on("draw", this.onDraw, this);
};
og.control.MouseNavigation.prototype.ondeactivate = function () {
this.renderer.events.off("mousewheel", this.onMouseWheel);
this.renderer.events.off("lhold", this.onMouseLeftButtonDown);
this.renderer.events.off("rhold", this.onMouseRightButtonDown);
this.renderer.events.off("ldown", this.onMouseLeftButtonClick);
this.renderer.events.off("lup", this.onMouseLeftButtonUp);
this.renderer.events.off("rdown", this.onMouseRightButtonClick);
this.renderer.events.off("ldblclick", this.onMouseLeftButtonDoubleClick);
this.renderer.events.off("draw", this.onDraw);
};
og.control.MouseNavigation.prototype.onMouseWheel = function (event) {
if (this.stepIndex)
return;
this.planet.stopFlying();
this.stopRotation();
this._deactivate = true;
this.planet.layerLock.lock(this._keyLock);
this.planet.terrainLock.lock(this._keyLock);
this.planet._normalMapCreator.lock(this._keyLock);
var ms = this.renderer.events.mouseState;
this.stepIndex = this.stepsCount;
this.stepsForward = og.control.MouseNavigation.getMovePointsFromPixelTerrain(this.renderer.activeCamera,
this.planet, this.stepsCount, this.distDiff, ms, event.wheelDelta > 0, ms.direction);
};
og.control.MouseNavigation.prototype.oninit = function () {
this.activate();
};
og.control.MouseNavigation.prototype.onMouseLeftButtonDoubleClick = function () {
this.planet.stopFlying();
this.stopRotation();
var p = this.planet.getCartesianFromPixelTerrain(this.renderer.events.mouseState, true),
g = this.planet.ellipsoid.cartesianToLonLat(p);
if (this.renderer.events.isKeyPressed(og.input.KEY_SHIFT)) {
this.planet.flyLonLat(new og.LonLat(g.lon, g.lat, this.renderer.activeCamera.eye.distance(p) * 2.0));
} else {
this.planet.flyLonLat(new og.LonLat(g.lon, g.lat, this.renderer.activeCamera.eye.distance(p) * 0.57));
onactivate() {
this.renderer.events.on("mousewheel", this.onMouseWheel, this);
this.renderer.events.on("lhold", this.onMouseLeftButtonDown, this);
this.renderer.events.on("rhold", this.onMouseRightButtonDown, this);
this.renderer.events.on("ldown", this.onMouseLeftButtonClick, this);
this.renderer.events.on("lup", this.onMouseLeftButtonUp, this);
this.renderer.events.on("rdown", this.onMouseRightButtonClick, this);
this.renderer.events.on("ldblclick", this.onMouseLeftButtonDoubleClick, this);
this.renderer.events.on("draw", this.onDraw, this);
}
};
og.control.MouseNavigation.prototype.onMouseLeftButtonClick = function () {
if (this._active) {
this.renderer.handler.canvas.classList.add("ogGrabbingPoiner");
this.grabbedPoint = this.planet.getCartesianFromMouseTerrain(true);
if (this.grabbedPoint) {
this._eye0.copy(this.renderer.activeCamera.eye);
this.grabbedSpheroid.radius = this.grabbedPoint.length();
this.stopRotation();
}
}
};
ondeactivate() {
this.renderer.events.off("mousewheel", this.onMouseWheel);
this.renderer.events.off("lhold", this.onMouseLeftButtonDown);
this.renderer.events.off("rhold", this.onMouseRightButtonDown);
this.renderer.events.off("ldown", this.onMouseLeftButtonClick);
this.renderer.events.off("lup", this.onMouseLeftButtonUp);
this.renderer.events.off("rdown", this.onMouseRightButtonClick);
this.renderer.events.off("ldblclick", this.onMouseLeftButtonDoubleClick);
this.renderer.events.off("draw", this.onDraw);
};
og.control.MouseNavigation.prototype.stopRotation = function () {
this.qRot.clear();
this.planet.layerLock.free(this._keyLock);
this.planet.terrainLock.free(this._keyLock);
this.planet._normalMapCreator.free(this._keyLock);
};
onMouseWheel(event) {
og.control.MouseNavigation.prototype.onMouseLeftButtonUp = function (e) {
this.renderer.handler.canvas.classList.remove("ogGrabbingPoiner");
};
og.control.MouseNavigation.prototype.onMouseLeftButtonDown = function (e) {
if (this._active) {
if (!this.grabbedPoint)
if (this.stepIndex)
return;
this.planet.stopFlying();
if (this.renderer.events.mouseState.moving) {
this.stopRotation();
var cam = this.renderer.activeCamera;
this._deactivate = true;
if (cam.slope > 0.28) {
var targetPoint = new og.math.Ray(cam.eye, e.direction).hitSphere(this.grabbedSpheroid);
if (targetPoint) {
this.scaleRot = 1;
this.qRot = og.math.Quaternion.getRotationBetweenVectors(targetPoint.normal(), this.grabbedPoint.normal());
var rot = this.qRot;
cam.eye = rot.mulVec3(cam.eye);
cam._v = rot.mulVec3(cam._v);
cam._u = rot.mulVec3(cam._u);
cam._n = rot.mulVec3(cam._n);
cam.update();
}
} else {
var p0 = this.grabbedPoint,
p1 = og.math.Vector3.add(p0, cam._u),
p2 = og.math.Vector3.add(p0, p0.normal());
this.planet.layerLock.lock(this._keyLock);
this.planet.terrainLock.lock(this._keyLock);
this.planet._normalMapCreator.lock(this._keyLock);
var px = new og.math.Vector3();
if (new og.math.Ray(cam.eye, e.direction).hitPlane(p0, p1, p2, px) === og.math.Ray.INSIDE) {
cam.eye = this._eye0.addA(px.subA(p0).negate());
cam.update();
}
}
var ms = this.renderer.events.mouseState;
this.stepIndex = this.stepsCount;
this.stepsForward = MouseNavigation.getMovePointsFromPixelTerrain(this.renderer.activeCamera,
this.planet, this.stepsCount, this.distDiff, ms, event.wheelDelta > 0, ms.direction);
}
oninit = function () {
this.activate();
}
onMouseLeftButtonDoubleClick() {
this.planet.stopFlying();
this.stopRotation();
var p = this.planet.getCartesianFromPixelTerrain(this.renderer.events.mouseState, true),
g = this.planet.ellipsoid.cartesianToLonLat(p);
if (this.renderer.events.isKeyPressed(input.KEY_SHIFT)) {
this.planet.flyLonLat(new LonLat(g.lon, g.lat, this.renderer.activeCamera.eye.distance(p) * 2.0));
} else {
this.scaleRot = 0;
this.planet.flyLonLat(new LonLat(g.lon, g.lat, this.renderer.activeCamera.eye.distance(p) * 0.57));
}
}
};
og.control.MouseNavigation.prototype.onMouseRightButtonClick = function (e) {
this.stopRotation();
this.planet.stopFlying();
this.pointOnEarth = this.planet.getCartesianFromPixelTerrain({ x: e.x, y: e.y }, true);
if (this.pointOnEarth) {
this.earthUp = this.pointOnEarth.normal();
onMouseLeftButtonClick() {
if (this._active) {
this.renderer.handler.canvas.classList.add("ogGrabbingPoiner");
this.grabbedPoint = this.planet.getCartesianFromMouseTerrain(true);
if (this.grabbedPoint) {
this._eye0.copy(this.renderer.activeCamera.eye);
this.grabbedSpheroid.radius = this.grabbedPoint.length();
this.stopRotation();
}
}
}
};
og.control.MouseNavigation.prototype.onMouseRightButtonDown = function (e) {
var cam = this.renderer.activeCamera;
if (this.pointOnEarth && this.renderer.events.mouseState.moving) {
this.renderer.controlsBag.scaleRot = 1;
var l = 0.5 / cam.eye.distance(this.pointOnEarth) * cam._lonLat.height * og.math.RADIANS;
if (l > 0.007) l = 0.007;
cam.rotateHorizontal(l * (e.x - e.prev_x), false, this.pointOnEarth, this.earthUp);
cam.rotateVertical(l * (e.y - e.prev_y), this.pointOnEarth);
cam.update();
stopRotation() {
this.qRot.clear();
this.planet.layerLock.free(this._keyLock);
this.planet.terrainLock.free(this._keyLock);
this.planet._normalMapCreator.free(this._keyLock);
}
};
og.control.MouseNavigation.prototype.onDraw = function (e) {
onMouseLeftButtonUp(e) {
this.renderer.handler.canvas.classList.remove("ogGrabbingPoiner");
}
if (this._active) {
onMouseLeftButtonDown(e) {
if (this._active) {
if (!this.grabbedPoint)
return;
var r = this.renderer;
var cam = r.activeCamera;
var prevEye = cam.eye.clone();
this.planet.stopFlying();
if (this.stepIndex) {
r.controlsBag.scaleRot = 1;
var sf = this.stepsForward[this.stepsCount - this.stepIndex--];
cam.eye = sf.eye;
cam._v = sf.v;
cam._u = sf.u;
cam._n = sf.n;
if (this.renderer.events.mouseState.moving) {
var cam = this.renderer.activeCamera;
if (cam.slope > 0.28) {
var targetPoint = new Ray(cam.eye, e.direction).hitSphere(this.grabbedSpheroid);
if (targetPoint) {
this.scaleRot = 1;
this.qRot = Quat.getRotationBetweenVectors(targetPoint.normal(), this.grabbedPoint.normal());
var rot = this.qRot;
cam.eye = rot.mulVec3(cam.eye);
cam._v = rot.mulVec3(cam._v);
cam._u = rot.mulVec3(cam._u);
cam._n = rot.mulVec3(cam._n);
cam.update();
}
} else {
var p0 = this.grabbedPoint,
p1 = Vec3.add(p0, cam._u),
p2 = Vec3.add(p0, p0.normal());
var px = new Vec3();
if (new Ray(cam.eye, e.direction).hitPlane(p0, p1, p2, px) === Ray.INSIDE) {
cam.eye = this._eye0.addA(px.subA(p0).negate());
cam.update();
}
}
} else {
this.scaleRot = 0;
}
}
}
onMouseRightButtonClick(e) {
this.stopRotation();
this.planet.stopFlying();
this.pointOnEarth = this.planet.getCartesianFromPixelTerrain({ x: e.x, y: e.y }, true);
if (this.pointOnEarth) {
this.earthUp = this.pointOnEarth.normal();
}
};
onMouseRightButtonDown(e) {
var cam = this.renderer.activeCamera;
if (this.pointOnEarth && this.renderer.events.mouseState.moving) {
this.renderer.controlsBag.scaleRot = 1;
var l = 0.5 / cam.eye.distance(this.pointOnEarth) * cam._lonLat.height * math.RADIANS;
if (l > 0.007) l = 0.007;
cam.rotateHorizontal(l * (e.x - e.prev_x), false, this.pointOnEarth, this.earthUp);
cam.rotateVertical(l * (e.y - e.prev_y), this.pointOnEarth);
cam.update();
} else {
if (this._deactivate) {
this._deactivate = false;
}
}
onDraw(e) {
if (this._active) {
var r = this.renderer;
var cam = r.activeCamera;
var prevEye = cam.eye.clone();
if (this.stepIndex) {
r.controlsBag.scaleRot = 1;
var sf = this.stepsForward[this.stepsCount - this.stepIndex--];
cam.eye = sf.eye;
cam._v = sf.v;
cam._u = sf.u;
cam._n = sf.n;
cam.update();
} else {
if (this._deactivate) {
this._deactivate = false;
this.planet.layerLock.free(this._keyLock);
this.planet.terrainLock.free(this._keyLock);
this.planet._normalMapCreator.free(this._keyLock);
}
}
if (r.events.mouseState.leftButtonDown || !this.scaleRot)
return;
this.scaleRot -= this.inertia;
if (this.scaleRot <= 0) {
this.scaleRot = 0;
} else {
r.controlsBag.scaleRot = this.scaleRot;
var rot = this.qRot.slerp(Quat.IDENTITY, 1 - this.scaleRot * this.scaleRot * this.scaleRot).normalize();
if (!(rot.x || rot.y || rot.z)) {
this.scaleRot = 0;
}
cam.eye = rot.mulVec3(cam.eye);
cam._v = rot.mulVec3(cam._v);
cam._u = rot.mulVec3(cam._u);
cam._n = rot.mulVec3(cam._n);
cam.update();
}
if (cam.eye.distance(prevEye) / cam._terrainAltitude > 0.01) {
this.planet.layerLock.lock(this._keyLock);
this.planet.terrainLock.lock(this._keyLock);
this.planet._normalMapCreator.lock(this._keyLock);
} else {
this.planet.layerLock.free(this._keyLock);
this.planet.terrainLock.free(this._keyLock);
this.planet._normalMapCreator.free(this._keyLock);
}
}
if (r.events.mouseState.leftButtonDown || !this.scaleRot)
return;
this.scaleRot -= this.inertia;
if (this.scaleRot <= 0) {
this.scaleRot = 0;
} else {
r.controlsBag.scaleRot = this.scaleRot;
var rot = this.qRot.slerp(og.math.Quaternion.IDENTITY, 1 - this.scaleRot * this.scaleRot * this.scaleRot).normalize();
if (!(rot.x || rot.y || rot.z)) {
this.scaleRot = 0;
}
cam.eye = rot.mulVec3(cam.eye);
cam._v = rot.mulVec3(cam._v);
cam._u = rot.mulVec3(cam._u);
cam._n = rot.mulVec3(cam._n);
cam.update();
}
if (cam.eye.distance(prevEye) / cam._terrainAltitude > 0.01) {
this.planet.layerLock.lock(this._keyLock);
this.planet.terrainLock.lock(this._keyLock);
this.planet._normalMapCreator.lock(this._keyLock);
} else {
this.planet.layerLock.free(this._keyLock);
this.planet.terrainLock.free(this._keyLock);
this.planet._normalMapCreator.free(this._keyLock);
}
}
};
};
export { MouseNavigation };

View File

@ -1,7 +1,11 @@
goog.provide('og.control.ShowFps');
/**
* @module og/control/ShowFps
*/
goog.require('og.inheritance');
goog.require('og.control.BaseControl');
'use strict';
import { BaseControl } from './BaseControl.js';
import { print2d } from '../utils/utils.js';
/**
* Frame per second(FPS) display control.
@ -9,28 +13,27 @@ goog.require('og.control.BaseControl');
* @extends {og.control.BaseControl}
* @param {Object} [options] - Control options.
*/
og.control.ShowFps = function (options) {
og.inheritance.base(this, options);
class ShowFps {
constructor(options) {
super(options);
}
options = options || {};
oninit() {
var d = document.createElement('div');
d.className = 'defaultText ';
d.id = "ogShowFpsControl";
document.body.appendChild(d);
this.renderer.events.on("draw", this._draw, this);
}
_draw() {
print2d("ogShowFpsControl", (1000.0 / this.renderer.handler.deltaTime).toFixed(1), this.renderer.handler.canvas.clientWidth - 60, 0);
}
};
og.inheritance.extend(og.control.ShowFps, og.control.BaseControl);
og.control.showFps = function (options) {
return new og.control.ShowFps(options);
};
og.control.ShowFps.prototype.oninit = function () {
var d = document.createElement('div');
d.className = 'defaultText ';
d.id = "ogShowFpsControl";
document.body.appendChild(d);
this.renderer.events.on("draw", this._draw, this);
};
og.control.ShowFps.prototype._draw = function () {
print2d("ogShowFpsControl", (1000.0 / this.renderer.handler.deltaTime).toFixed(1), this.renderer.handler.canvas.clientWidth - 60, 0);
export function showFps(options) {
return new ShowFps(options);
};
export { ShowFps };

View File

@ -1,213 +0,0 @@
goog.provide('og.SpinNavigation');
goog.require('og.inheritance');
goog.require('og.control.BaseControl');
goog.require('og.math');
goog.require('og.math.Vector3');
goog.require('og.math.Matrix4');
goog.require('og.math.Quaternion');
goog.require('og.bv.Sphere');
goog.require('og.math.Ray');
og.SpinNavigation = function (options) {
og.inheritance.base(this, options);
options = options || {};
this.grabbedPoint = new og.math.Vector3();
this.inertia = 0.007;
this.grabbedSpheroid = new og.bv.Sphere();
this.planet = null;
this._vRot = new og.math.Quaternion();
this._hRot = new og.math.Quaternion();
this._a = 0.0;
this.scaleRot = 0;
this._maxPhi = 0.8;
var Touch = function () {
this.x = 0;
this.y = 0;
this.prev_x = 0;
this.prev_y = 0;
this.grabbedPoint = new og.math.Vector3();
this.grabbedSpheroid = new og.bv.Sphere();
this.dX = function () { return this.x - this.prev_x; };
this.dY = function () { return this.y - this.prev_y; };
};
this.touches = [new Touch(), new Touch()];
};
og.inheritance.extend(og.SpinNavigation, og.control.BaseControl);
og.SpinNavigation.prototype.onMouseWheel = function (event) {
};
og.SpinNavigation.prototype.initialize = function () {
this.renderer.events.on("mousewheel", this.onMouseWheel, this);
this.renderer.events.on("mouselbuttonhold", this.onMouseLeftButtonDown, this);
this.renderer.events.on("mouselbuttondown", this.onMouseLeftButtonClick, this);
this.renderer.events.on("mouselbuttonup", this.onMouseLeftButtonUp, this);
this.renderer.events.on("touchstart", this.onTouchStart, this);
this.renderer.events.on("touchend", this.onTouchEnd, this);
this.renderer.events.on("touchmove", this.onTouchMove, this);
this.renderer.events.on("draw", this.onDraw, this);
};
og.SpinNavigation.prototype.onTouchStart = function (e) {
if (e.sys.touches.length == 1) {
var t = this.touches[0];
t.x = e.sys.touches.item(0).pageX - e.sys.offsetLeft;
t.y = e.sys.touches.item(0).pageY - e.sys.offsetTop;
t.prev_x = e.sys.touches.item(0).pageX - e.sys.offsetLeft;
t.prev_y = e.sys.touches.item(0).pageY - e.sys.offsetTop;
t.grabbedPoint = this.planet.getCartesianFromPixelTerrain(t, true);
if (t.grabbedPoint) {
t.grabbedSpheroid.radius = t.grabbedPoint.length();
this.stopRotation();
}
}
};
og.SpinNavigation.prototype.onTouchEnd = function (e) {
if (e.sys.touches.length == 0) {
this.scaleRot = 1;
if (Math.abs(this.touches[0].x - this.touches[0].prev_x) < 3 &&
Math.abs(this.touches[0].y - this.touches[0].prev_y) < 3)
this.stopRotation();
}
};
og.SpinNavigation.prototype.onTouchMove = function (e) {
if (e.sys.touches.length == 1) {
var cam = this.renderer.activeCamera;
var t = this.touches[0];
t.prev_x = t.x;
t.prev_y = t.y;
t.x = e.sys.touches.item(0).pageX - e.sys.offsetLeft;
t.y = e.sys.touches.item(0).pageY - e.sys.offsetTop;
if (!t.grabbedPoint)
return;
var direction = cam.unproject(t.x, t.y);
var targetPoint = new og.math.Ray(cam.eye, direction).hitSphere(t.grabbedSpheroid);
if (targetPoint) {
this._a = Math.acos(t.grabbedPoint.y / t.grabbedSpheroid.radius) - Math.acos(targetPoint.y / t.grabbedSpheroid.radius);
this._vRot = og.math.Quaternion.axisAngleToQuat(cam._u, this._a);
this._hRot = og.math.Quaternion.getRotationBetweenVectors(
(new og.math.Vector3(targetPoint.x, 0.0, targetPoint.z)).normal(),
(new og.math.Vector3(t.grabbedPoint.x, 0.0, t.grabbedPoint.z)).normal());
var rot = this._hRot.mul(this._vRot);
var lim = rot.mulVec3(cam.eye).normal().dot(og.math.Vector3.UP);
if (lim > this._maxPhi || lim < -this._maxPhi) {
rot = og.math.Quaternion.yRotation(rot.getYaw());
}
cam.set(rot.mulVec3(cam.eye), og.math.Vector3.ZERO, og.math.Vector3.UP);
cam.update();
}
}
};
og.SpinNavigation.prototype.onMouseLeftButtonClick = function () {
this.renderer.handler.canvas.classList.add("ogGrabbingPoiner");
this.grabbedPoint = this.planet.getCartesianFromMouseTerrain(true);
if (this.grabbedPoint) {
this.grabbedSpheroid.radius = this.grabbedPoint.length();
this.stopRotation();
}
};
og.SpinNavigation.prototype.stopRotation = function () {
this.scaleRot = 0.0;
this._a = 0.0;
this._vRot.clear();
this._hRot.clear();
};
og.SpinNavigation.prototype.onMouseLeftButtonUp = function (e) {
this.scaleRot = 1;
this.renderer.handler.canvas.classList.remove("ogGrabbingPoiner");
if (Math.abs(e.x - e.prev_x) < 3 &&
Math.abs(e.y - e.prev_y) < 3)
this.stopRotation();
};
og.SpinNavigation.prototype.onMouseLeftButtonDown = function (e) {
var cam = this.renderer.activeCamera;
if (!this.grabbedPoint || cam.isFlying())
return;
if (this.renderer.events.mouseState.moving) {
var targetPoint = new og.math.Ray(cam.eye, e.direction).hitSphere(this.grabbedSpheroid);
if (targetPoint) {
this._a = Math.acos(this.grabbedPoint.y / this.grabbedSpheroid.radius) - Math.acos(targetPoint.y / this.grabbedSpheroid.radius);
this._vRot = og.math.Quaternion.axisAngleToQuat(cam._u, this._a);
this._hRot = og.math.Quaternion.getRotationBetweenVectors(
(new og.math.Vector3(targetPoint.x, 0.0, targetPoint.z)).normal(),
(new og.math.Vector3(this.grabbedPoint.x, 0.0, this.grabbedPoint.z)).normal());
var rot = this._hRot.mul(this._vRot);
var lim = rot.mulVec3(cam.eye).normal().dot(og.math.Vector3.UP);
if (lim > this._maxPhi || lim < -this._maxPhi) {
rot = og.math.Quaternion.yRotation(rot.getYaw());
}
cam.set(rot.mulVec3(cam.eye), og.math.Vector3.ZERO, og.math.Vector3.UP);
cam.update();
}
} else {
this.scaleRot = 0;
}
};
og.SpinNavigation.prototype.onDraw = function (e) {
var r = this.renderer;
var cam = r.activeCamera;
if (r.events.mouseState.leftButtonDown || !this.scaleRot || cam.isFlying())
return;
this.scaleRot -= this.inertia;
if (this.scaleRot <= 0) {
this.scaleRot = 0;
} else {
this._vRot = og.math.Quaternion.axisAngleToQuat(cam._u, this._a);
var rot = this._vRot.mul(this._hRot);
var lim = rot.mulVec3(cam.eye).normal().dot(og.math.Vector3.UP);
if (lim > this._maxPhi || lim < -this._maxPhi) {
rot = og.math.Quaternion.yRotation(rot.getYaw());
}
r.controlsBag.scaleRot = this.scaleRot;
rot = rot.slerp(og.math.Quaternion.IDENTITY, 1 - this.scaleRot * this.scaleRot * this.scaleRot).normalize();
if (!(rot.x || rot.y || rot.z)) {
this.scaleRot = 0;
}
cam.set(rot.mulVec3(cam.eye), og.math.Vector3.ZERO, og.math.Vector3.UP);
cam.update();
}
};

View File

@ -1,10 +1,15 @@
goog.provide('og.control.Sun');
/**
* @module og/control/Sun
*/
goog.require('og.inheritance');
goog.require('og.control.BaseControl');
goog.require('og.LightSource');
goog.require('og.astro.earth');
goog.require('og.math.Quaternion');
'use strict';
import { BaseControl } from './BaseControl';
import { getSunPosition } from '../astro/earth.js';
import { input } from '../input/input.js';
import { LightSource } from '../light/LightSource.js';
import { Quat } from '../math/Quat.js';
import { Vec3 } from '../math/Vec3.js';
/**
* Real Sun geocentric position control that place the Sun on the right place by the Earth.
@ -12,135 +17,137 @@ goog.require('og.math.Quaternion');
* @extends {og.control.BaseControl}
* @param {Object} [options] - Control options.
*/
og.control.Sun = function (options) {
og.inheritance.base(this, options);
class Sun extends BaseControl {
constructor(options) {
super(options);
options = options || {};
options = options || {};
/**
* Earth planet node.
* @public
* @type {og.scene.Planet}
*/
this.planet = null;
/**
* Earth planet node.
* @public
* @type {og.scene.Planet}
*/
this.planet = null;
/**
* Sunlight position placed in the camera eye.
* @private
* @type {boolean}
*/
//this._isCameraSunlight = false;
/**
* Sunlight position placed in the camera eye.
* @private
* @type {boolean}
*/
//this._isCameraSunlight = false;
this.offsetVertical = options.offsetVertical || -5000000;
this.offsetVertical = options.offsetVertical || -5000000;
this.offsetHorizontal = options.offsetHorizontal || 5000000;
this.offsetHorizontal = options.offsetHorizontal || 5000000;
/**
* Light source.
* @public
* @type {og.LightSource}
*/
this.sunlight = null;
/**
* Light source.
* @public
* @type {og.LightSource}
*/
this.sunlight = null;
/**
* Current frame handler clock date and time.
* @private
* @type {Number}
*/
this._currDate = 0;
/**
* Current frame handler clock date and time.
* @private
* @type {Number}
*/
this._currDate = 0;
/**
* Previous frame handler clock date and time.
* @private
* @type {Number}
*/
this._prevDate = 0;
/**
* Previous frame handler clock date and time.
* @private
* @type {Number}
*/
this._prevDate = 0;
this._clockPtr = null;
this._clockPtr = null;
this._lightOn = false;
this._lightOn = false;
this._stopped = options.stopped || false;
};
this._stopped = options.stopped || false;
}
og.inheritance.extend(og.control.Sun, og.control.BaseControl);
oninit() {
og.control.sun = function (options) {
return new og.control.Sun(options);
};
this.planet.lightEnabled = true;
og.control.Sun.prototype.oninit = function () {
//sunlight initialization
this.sunlight = new LightSource("Sun", {
'ambient': new Vec3(0.15, 0.15, 0.25),
'diffuse': new Vec3(0.9, 0.9, 0.8),
'specular': new Vec3(0.1, 0.1, 0.06),
'shininess': 110
});
this.sunlight.addTo(this.planet);
this.planet.lightEnabled = true;
var that = this;
this.renderer.events.on("draw", this._draw, this);
//sunlight initialization
this.sunlight = new og.LightSource("Sun", {
'ambient': new og.math.Vector3(0.15, 0.15, 0.25),
'diffuse': new og.math.Vector3(0.9, 0.9, 0.8),
'specular': new og.math.Vector3(0.1, 0.1, 0.06),
'shininess': 110
});
this.sunlight.addTo(this.planet);
this.renderer.events.on("charkeypress", input.KEY_L, function () {
that.planet.lightEnabled = !that.planet.lightEnabled;
});
var that = this;
this.renderer.events.on("draw", this._draw, this);
if (!this._clockPtr)
this._clockPtr = this.renderer.handler.defaultClock;
}
this.renderer.events.on("charkeypress", og.input.KEY_L, function () {
that.planet.lightEnabled = !that.planet.lightEnabled;
});
stop() {
this._stopped = true;
}
if (!this._clockPtr)
this._clockPtr = this.renderer.handler.defaultClock;
};
onactivate() {
this._stopped = false;
}
og.control.Sun.prototype.stop = function () {
this._stopped = true;
};
bindClock(clock) {
this._clockPtr = clock;
}
og.control.Sun.prototype.onactivate = function () {
this._stopped = false;
};
og.control.Sun.prototype.bindClock = function (clock) {
this._clockPtr = clock;
};
og.control.Sun.prototype._draw = function () {
if (!this._stopped) {
this._currDate = this._clockPtr.currentDate;
var cam = this.renderer.activeCamera;
if (cam.getHeight() < 4650000 || !this._active) {
this._lightOn = true;
this._f = 1;
var n = cam.eye.normal();
var tu = og.math.Vector3.proj_b_to_plane(cam._v, n, cam._v).normalize().scale(this.offsetVertical);
var tr = og.math.Vector3.proj_b_to_plane(cam._u, n, cam._u).normalize().scale(this.offsetHorizontal);
var d = tu.add(tr);
var pos = cam.eye.add(d);
if (this._k > 0) {
this._k -= 0.01;
var rot = og.math.Quaternion.getRotationBetweenVectors(this.sunlight._position.normal(), pos.normal());
var r = rot.slerp(og.math.Quaternion.IDENTITY, this._k).normalize();
this.sunlight.setPosition(r.mulVec3(this.sunlight._position));
_draw() {
if (!this._stopped) {
this._currDate = this._clockPtr.currentDate;
var cam = this.renderer.activeCamera;
if (cam.getHeight() < 4650000 || !this._active) {
this._lightOn = true;
this._f = 1;
var n = cam.eye.normal();
var tu = Vec3.proj_b_to_plane(cam._v, n, cam._v).normalize().scale(this.offsetVertical);
var tr = Vec3.proj_b_to_plane(cam._u, n, cam._u).normalize().scale(this.offsetHorizontal);
var d = tu.add(tr);
var pos = cam.eye.add(d);
if (this._k > 0) {
this._k -= 0.01;
var rot = Quat.getRotationBetweenVectors(this.sunlight._position.normal(), pos.normal());
var r = rot.slerp(Quat.IDENTITY, this._k).normalize();
this.sunlight.setPosition(r.mulVec3(this.sunlight._position));
} else {
this.sunlight.setPosition(pos);
}
} else {
this.sunlight.setPosition(pos);
}
} else {
this._k = 1;
if (this._f > 0) {
this._f -= 0.01;
var rot = og.math.Quaternion.getRotationBetweenVectors(this.sunlight._position.normal(), og.astro.earth.getSunPosition(this._currDate).normal());
var r = rot.slerp(og.math.Quaternion.IDENTITY, this._f).normalize();
this.sunlight.setPosition(r.mulVec3(this.sunlight._position));
} else {
if (Math.abs(this._currDate - this._prevDate) > 0.00034 && this._active || this._lightOn) {
this._lightOn = false;
this._prevDate = this._currDate;
this.sunlight.setPosition(og.astro.earth.getSunPosition(this._currDate));
this._f = 0;
this._k = 1;
if (this._f > 0) {
this._f -= 0.01;
var rot = Quat.getRotationBetweenVectors(this.sunlight._position.normal(), getSunPosition(this._currDate).normal());
var r = rot.slerp(Quat.IDENTITY, this._f).normalize();
this.sunlight.setPosition(r.mulVec3(this.sunlight._position));
} else {
if (Math.abs(this._currDate - this._prevDate) > 0.00034 && this._active || this._lightOn) {
this._lightOn = false;
this._prevDate = this._currDate;
this.sunlight.setPosition(getSunPosition(this._currDate));
this._f = 0;
}
}
}
}
}
};
export function sun(options) {
return Sun(options);
};
export { Sun };

View File

@ -1,8 +1,11 @@
goog.provide('og.control.ToggleWireframe');
/**
* @module og/control/ToggleWireframe
*/
goog.require('og.inheritance');
goog.require('og.webgl');
goog.require('og.input');
'use strict';
import { BaseControl } from './BaseControl.js';
import { input } from '../input/input.js';
/**
* Planet GL draw mode(TRIANGLE_STRIP/LINE_STRING) changer.
@ -10,20 +13,23 @@ goog.require('og.input');
* @extends {og.control.BaseControl}
* @param {Object} [options] - Control options.
*/
og.control.ToggleWireframe = function (options) {
og.inheritance.base(this, options);
};
class ToggleWireframe extends BaseControl {
constructor(options) {
super(options);
}
og.inheritance.extend(og.control.ToggleWireframe, og.control.BaseControl);
og.control.ToggleWireframe.prototype.oninit = function () {
this.renderer.events.on("charkeypress", og.input.KEY_X, this.toogleWireframe, this);
};
oninit() {
this.renderer.events.on("charkeypress", input.KEY_X, this.toogleWireframe, this);
}
og.control.ToggleWireframe.prototype.toogleWireframe = function (e) {
if (this.planet.drawMode === this.renderer.handler.gl.LINE_STRIP) {
this.planet.setDrawMode(this.renderer.handler.gl.TRIANGLE_STRIP);
} else {
this.planet.setDrawMode(this.renderer.handler.gl.LINE_STRIP);
toogleWireframe(e) {
if (this.planet.drawMode === this.renderer.handler.gl.LINE_STRIP) {
this.planet.setDrawMode(this.renderer.handler.gl.TRIANGLE_STRIP);
} else {
this.planet.setDrawMode(this.renderer.handler.gl.LINE_STRIP);
}
}
};
export { ToggleWireframe };

View File

@ -1,15 +1,19 @@
goog.provide('og.control.TouchNavigation');
/**
* @module og/control/TouchNavigation
*/
goog.require('og.inheritance');
goog.require('og.control.BaseControl');
goog.require('og.math');
goog.require('og.math.Vector3');
goog.require('og.math.Matrix4');
goog.require('og.math.Quaternion');
goog.require('og.bv.Sphere');
goog.require('og.math.Ray');
goog.require('og.math.Pixel');
goog.require('og.idle');
'use strict';
import * as math from '../math.js';
import { BaseControl } from './BaseControl.js';
import { input } from '../input/input.js';
import { Key } from '../Lock.js';
import { LonLat } from '../LonLat.js';
import { Mat4 } from '../math/Mat4.js';
import { Quat } from '../math/Quat.js';
import { Ray } from '../math/Ray.js';
import { Sphere } from '../bv/Sphere.js';
import { Vec3 } from '../math/Vec3.js';
/**
@ -18,293 +22,289 @@ goog.require('og.idle');
* @extends {og.control.BaseControl}
* @param {Object} [options] - Control options.
*/
og.control.TouchNavigation = function (options) {
og.inheritance.base(this, options);
class TouchNavigation extends BaseControl {
constructor(options) {
super(options);
options = options || {};
this.grabbedPoint = new og.math.Vector3();
this.inertia = 0.007;
this.grabbedSpheroid = new og.bv.Sphere();
this.planet = null;
this.qRot = new og.math.Quaternion();
this.scaleRot = 0;
this.rot = 1;
this._eye0 = new og.math.Vector3();
this.stepsCount = 5;
this.stepsForward = null;
this.stepIndex = 0;
var Touch = function () {
this.x = 0;
this.y = 0;
this.prev_x = 0;
this.prev_y = 0;
this.grabbedPoint = new og.math.Vector3();
this.grabbedSpheroid = new og.bv.Sphere();
this.dX = function () { return this.x - this.prev_x; };
this.dY = function () { return this.y - this.prev_y; };
};
this.pointOnEarth = null;
this.earthUp = null;
this.touches = [new Touch(), new Touch()];
this._keyLock = new og.idle.Key();
};
og.inheritance.extend(og.control.TouchNavigation, og.control.BaseControl);
og.control.touchNavigation = function (options) {
return new og.control.TouchNavigation(options);
};
og.control.TouchNavigation.prototype.oninit = function () {
this.renderer.events.on("touchstart", this.onTouchStart, this);
this.renderer.events.on("touchend", this.onTouchEnd, this);
this.renderer.events.on("doubletouch", this.onDoubleTouch, this);
this.renderer.events.on("touchcancel", this.onTouchCancel, this);
this.renderer.events.on("touchmove", this.onTouchMove, this);
this.renderer.events.on("draw", this.onDraw, this);
};
og.control.TouchNavigation.prototype.onTouchStart = function (e) {
this._touching = true;
if (e.sys.touches.length === 2) {
var t0 = this.touches[0],
t1 = this.touches[1];
t0.x = e.sys.touches.item(0).clientX - e.sys.offsetLeft;
t0.y = e.sys.touches.item(0).clientY - e.sys.offsetTop;
t0.prev_x = e.sys.touches.item(0).clientX - e.sys.offsetLeft;
t0.prev_y = e.sys.touches.item(0).clientY - e.sys.offsetTop;
t0.grabbedPoint = this.planet.getCartesianFromPixelTerrain(t0, true);
t1.x = e.sys.touches.item(1).clientX - e.sys.offsetLeft;
t1.y = e.sys.touches.item(1).clientY - e.sys.offsetTop;
t1.prev_x = e.sys.touches.item(1).clientX - e.sys.offsetLeft;
t1.prev_y = e.sys.touches.item(1).clientY - e.sys.offsetTop;
t1.grabbedPoint = this.planet.getCartesianFromPixelTerrain(t1, true);
//this.planet._viewChanged = true;
this.pointOnEarth = this.planet.getCartesianFromPixelTerrain(this.renderer.handler.getCenter(), true);
if (this.pointOnEarth) {
this.earthUp = this.pointOnEarth.normal();
}
if (t0.grabbedPoint && t1.grabbedPoint) {
t0.grabbedSpheroid.radius = t0.grabbedPoint.length();
t1.grabbedSpheroid.radius = t1.grabbedPoint.length();
this.stopRotation();
}
} else if (e.sys.touches.length === 1) {
this._startTouchOne(e);
}
};
og.control.TouchNavigation.prototype._startTouchOne = function (e) {
var t = this.touches[0];
t.x = e.sys.touches.item(0).clientX - e.sys.offsetLeft;
t.y = e.sys.touches.item(0).clientY - e.sys.offsetTop;
t.prev_x = e.sys.touches.item(0).clientX - e.sys.offsetLeft;
t.prev_y = e.sys.touches.item(0).clientY - e.sys.offsetTop;
t.grabbedPoint = this.planet.getCartesianFromPixelTerrain(t, true);
this._eye0.copy(this.renderer.activeCamera.eye);
if (t.grabbedPoint) {
t.grabbedSpheroid.radius = t.grabbedPoint.length();
this.stopRotation();
}
};
og.control.TouchNavigation.prototype.stopRotation = function () {
this.qRot.clear();
this.planet.layerLock.free(this._keyLock);
this.planet.terrainLock.free(this._keyLock);
this.planet._normalMapCreator.free(this._keyLock);
};
og.control.TouchNavigation.prototype.onDoubleTouch = function (e) {
if (this.stepIndex)
return;
this.planet.stopFlying();
this.stopRotation();
var p = this.planet.getCartesianFromPixelTerrain(this.touches[0], true),
g = this.planet.ellipsoid.cartesianToLonLat(p);
this.planet.flyLonLat(new og.LonLat(g.lon, g.lat, this.renderer.activeCamera.eye.distance(p) * 0.57));
};
og.control.TouchNavigation.prototype.onTouchEnd = function (e) {
if (e.sys.touches.length === 0)
this._touching = false;
if (e.sys.touches.length === 1) {
this._startTouchOne(e);
}
if (Math.abs(this.touches[0].x - this.touches[0].prev_x) < 3 &&
Math.abs(this.touches[0].y - this.touches[0].prev_y) < 3)
this.grabbedPoint = new Vec3();
this.inertia = 0.007;
this.grabbedSpheroid = new Sphere();
this.planet = null;
this.qRot = new Quat();
this.scaleRot = 0;
};
this.rot = 1;
this._eye0 = new Vec3();
og.control.TouchNavigation.prototype.onTouchCancel = function (e) {
};
this.stepsCount = 5;
this.stepsForward = null;
this.stepIndex = 0;
og.control.TouchNavigation.prototype.onTouchMove = function (e) {
var Touch = function () {
this.x = 0;
this.y = 0;
this.prev_x = 0;
this.prev_y = 0;
this.grabbedPoint = new Vec3();
this.grabbedSpheroid = new Sphere();
this.dX = function () { return this.x - this.prev_x; };
this.dY = function () { return this.y - this.prev_y; };
};
var cam = this.renderer.activeCamera;
this.pointOnEarth = null;
this.earthUp = null;
if (e.sys.touches.length === 2) {
this.touches = [new Touch(), new Touch()];
this.renderer.controlsBag.scaleRot = 1;
this._keyLock = new Key();
}
var t0 = this.touches[0],
t1 = this.touches[1];
oninit() {
this.renderer.events.on("touchstart", this.onTouchStart, this);
this.renderer.events.on("touchend", this.onTouchEnd, this);
this.renderer.events.on("doubletouch", this.onDoubleTouch, this);
this.renderer.events.on("touchcancel", this.onTouchCancel, this);
this.renderer.events.on("touchmove", this.onTouchMove, this);
this.renderer.events.on("draw", this.onDraw, this);
}
if (!t0.grabbedPoint || !t1.grabbedPoint)
return;
onTouchStart(e) {
this.planet.stopFlying();
this._touching = true;
t0.prev_x = t0.x;
t0.prev_y = t0.y;
t0.x = e.sys.touches.item(0).clientX - e.sys.offsetLeft;
t0.y = e.sys.touches.item(0).clientY - e.sys.offsetTop;
if (e.sys.touches.length === 2) {
t1.prev_x = t1.x;
t1.prev_y = t1.y;
t1.x = e.sys.touches.item(1).clientX - e.sys.offsetLeft;
t1.y = e.sys.touches.item(1).clientY - e.sys.offsetTop;
var t0 = this.touches[0],
t1 = this.touches[1];
//var center_x = Math.round(t0.x + (t1.x - t0.x) * 0.5);
//var center_y = Math.round(t0.y + (t1.y - t0.y) * 0.5);
t0.x = e.sys.touches.item(0).clientX - e.sys.offsetLeft;
t0.y = e.sys.touches.item(0).clientY - e.sys.offsetTop;
t0.prev_x = e.sys.touches.item(0).clientX - e.sys.offsetLeft;
t0.prev_y = e.sys.touches.item(0).clientY - e.sys.offsetTop;
t0.grabbedPoint = this.planet.getCartesianFromPixelTerrain(t0, true);
//var dirC = cam.unproject(center_x, center_y);
//var targetPointC = this.planet.getCartesianFromPixelTerrain(new og.math.Pixel(center_x, center_y));
t1.x = e.sys.touches.item(1).clientX - e.sys.offsetLeft;
t1.y = e.sys.touches.item(1).clientY - e.sys.offsetTop;
t1.prev_x = e.sys.touches.item(1).clientX - e.sys.offsetLeft;
t1.prev_y = e.sys.touches.item(1).clientY - e.sys.offsetTop;
t1.grabbedPoint = this.planet.getCartesianFromPixelTerrain(t1, true);
//var dir0 = cam.unproject(t0.x, t0.y);
//var targetPoint0 = new og.math.Ray(cam.eye, dir0).hitSphere(t0.grabbedSpheroid);
//this.planet._viewChanged = true;
this.pointOnEarth = this.planet.getCartesianFromPixelTerrain(this.renderer.handler.getCenter(), true);
//var dir1 = cam.unproject(t1.x, t1.y);
//var targetPoint1 = new og.math.Ray(cam.eye, dir1).hitSphere(t1.grabbedSpheroid);
if (this.pointOnEarth) {
this.earthUp = this.pointOnEarth.normal();
}
//print2d("t1", center_x + "," + center_y, 100, 100);
//print2d("t2", targetPointC.x + "," + targetPointC.y + "," + targetPointC.z, 100, 120);
if (t0.grabbedPoint && t1.grabbedPoint) {
t0.grabbedSpheroid.radius = t0.grabbedPoint.length();
t1.grabbedSpheroid.radius = t1.grabbedPoint.length();
this.stopRotation();
}
if (t0.dY() > 0 && t1.dY() > 0 || t0.dY() < 0 && t1.dY() < 0 ||
t0.dX() > 0 && t1.dX() > 0 || t0.dX() < 0 && t1.dX() < 0) {
var l = 0.5 / cam.eye.distance(this.pointOnEarth) * cam._lonLat.height * og.math.RADIANS;
if (l > 0.007) l = 0.007;
cam.rotateHorizontal(l * t0.dX(), false, this.pointOnEarth, this.earthUp);
cam.rotateVertical(l * t0.dY(), this.pointOnEarth);
cam.update();
} else if (e.sys.touches.length === 1) {
this._startTouchOne(e);
}
}
this.scaleRot = 0;
} else if (e.sys.touches.length === 1) {
_startTouchOne(e) {
var t = this.touches[0];
t.prev_x = t.x;
t.prev_y = t.y;
t.x = e.sys.touches.item(0).clientX - e.sys.offsetLeft;
t.y = e.sys.touches.item(0).clientY - e.sys.offsetTop;
t.prev_x = e.sys.touches.item(0).clientX - e.sys.offsetLeft;
t.prev_y = e.sys.touches.item(0).clientY - e.sys.offsetTop;
if (!t.grabbedPoint)
return;
t.grabbedPoint = this.planet.getCartesianFromPixelTerrain(t, true);
this._eye0.copy(this.renderer.activeCamera.eye);
this.planet.stopFlying();
var direction = cam.unproject(t.x, t.y);
var targetPoint = new og.math.Ray(cam.eye, direction).hitSphere(t.grabbedSpheroid);
if (targetPoint) {
if (cam._n.dot(cam.eye.normal()) > 0.28) {
this.qRot = og.math.Quaternion.getRotationBetweenVectors(targetPoint.normal(), t.grabbedPoint.normal());
var rot = this.qRot;
cam.eye = rot.mulVec3(cam.eye);
cam._v = rot.mulVec3(cam._v);
cam._u = rot.mulVec3(cam._u);
cam._n = rot.mulVec3(cam._n);
cam.update();
this.scaleRot = 1;
} else {
var p0 = t.grabbedPoint,
p1 = og.math.Vector3.add(p0, cam._u),
p2 = og.math.Vector3.add(p0, p0.normal());
var dir = cam.unproject(t.x, t.y);
var px = new og.math.Vector3();
if (new og.math.Ray(cam.eye, dir).hitPlane(p0, p1, p2, px) === og.math.Ray.INSIDE) {
cam.eye = this._eye0.addA(px.subA(p0).negate());
cam.update();
this.scaleRot = 0;
}
}
if (t.grabbedPoint) {
t.grabbedSpheroid.radius = t.grabbedPoint.length();
this.stopRotation();
}
}
};
og.control.TouchNavigation.prototype.onDraw = function (e) {
this.renderer.controlsBag.scaleRot = this.scaleRot;
if (this._touching)
return;
var r = this.renderer;
var cam = r.activeCamera;
var prevEye = cam.eye.clone();
if (this.stepIndex) {
r.controlsBag.scaleRot = 1;
var sf = this.stepsForward[this.stepsCount - this.stepIndex--];
var cam = this.renderer.activeCamera;
cam.eye = sf.eye;
cam._v = sf.v;
cam._u = sf.u;
cam._n = sf.n;
cam.update();
}
if (r.events.mouseState.leftButtonDown || !this.scaleRot)
return;
this.scaleRot -= this.inertia;
if (this.scaleRot <= 0)
this.scaleRot = 0;
else {
r.controlsBag.scaleRot = this.scaleRot;
var cam = r.activeCamera;
var rot = this.qRot.slerp(og.math.Quaternion.IDENTITY, 1 - this.scaleRot * this.scaleRot * this.scaleRot).normalize();
if (!(rot.x || rot.y || rot.z)) {
this.scaleRot = 0;
}
cam.eye = rot.mulVec3(cam.eye);
cam._v = rot.mulVec3(cam._v);
cam._u = rot.mulVec3(cam._u);
cam._n = rot.mulVec3(cam._n);
cam.update();
}
if (cam.eye.distance(prevEye) / cam._terrainAltitude > 0.01) {
this.planet.layerLock.lock(this._keyLock);
this.planet.terrainLock.lock(this._keyLock);
this.planet._normalMapCreator.lock(this._keyLock);
} else {
stopRotation() {
this.qRot.clear();
this.planet.layerLock.free(this._keyLock);
this.planet.terrainLock.free(this._keyLock);
this.planet._normalMapCreator.free(this._keyLock);
}
};
onDoubleTouch(e) {
if (this.stepIndex)
return;
this.planet.stopFlying();
this.stopRotation();
var p = this.planet.getCartesianFromPixelTerrain(this.touches[0], true),
g = this.planet.ellipsoid.cartesianToLonLat(p);
this.planet.flyLonLat(new LonLat(g.lon, g.lat, this.renderer.activeCamera.eye.distance(p) * 0.57));
}
onTouchEnd(e) {
if (e.sys.touches.length === 0)
this._touching = false;
if (e.sys.touches.length === 1) {
this._startTouchOne(e);
}
if (Math.abs(this.touches[0].x - this.touches[0].prev_x) < 3 &&
Math.abs(this.touches[0].y - this.touches[0].prev_y) < 3)
this.scaleRot = 0;
}
onTouchCancel(e) {
}
onTouchMove(e) {
var cam = this.renderer.activeCamera;
if (e.sys.touches.length === 2) {
this.renderer.controlsBag.scaleRot = 1;
var t0 = this.touches[0],
t1 = this.touches[1];
if (!t0.grabbedPoint || !t1.grabbedPoint)
return;
this.planet.stopFlying();
t0.prev_x = t0.x;
t0.prev_y = t0.y;
t0.x = e.sys.touches.item(0).clientX - e.sys.offsetLeft;
t0.y = e.sys.touches.item(0).clientY - e.sys.offsetTop;
t1.prev_x = t1.x;
t1.prev_y = t1.y;
t1.x = e.sys.touches.item(1).clientX - e.sys.offsetLeft;
t1.y = e.sys.touches.item(1).clientY - e.sys.offsetTop;
//var center_x = Math.round(t0.x + (t1.x - t0.x) * 0.5);
//var center_y = Math.round(t0.y + (t1.y - t0.y) * 0.5);
//var dirC = cam.unproject(center_x, center_y);
//var targetPointC = this.planet.getCartesianFromPixelTerrain(new og.math.Pixel(center_x, center_y));
//var dir0 = cam.unproject(t0.x, t0.y);
//var targetPoint0 = new og.math.Ray(cam.eye, dir0).hitSphere(t0.grabbedSpheroid);
//var dir1 = cam.unproject(t1.x, t1.y);
//var targetPoint1 = new og.math.Ray(cam.eye, dir1).hitSphere(t1.grabbedSpheroid);
//print2d("t1", center_x + "," + center_y, 100, 100);
//print2d("t2", targetPointC.x + "," + targetPointC.y + "," + targetPointC.z, 100, 120);
if (t0.dY() > 0 && t1.dY() > 0 || t0.dY() < 0 && t1.dY() < 0 ||
t0.dX() > 0 && t1.dX() > 0 || t0.dX() < 0 && t1.dX() < 0) {
var l = 0.5 / cam.eye.distance(this.pointOnEarth) * cam._lonLat.height * math.RADIANS;
if (l > 0.007) l = 0.007;
cam.rotateHorizontal(l * t0.dX(), false, this.pointOnEarth, this.earthUp);
cam.rotateVertical(l * t0.dY(), this.pointOnEarth);
cam.update();
}
this.scaleRot = 0;
} else if (e.sys.touches.length === 1) {
var t = this.touches[0];
t.prev_x = t.x;
t.prev_y = t.y;
t.x = e.sys.touches.item(0).clientX - e.sys.offsetLeft;
t.y = e.sys.touches.item(0).clientY - e.sys.offsetTop;
if (!t.grabbedPoint)
return;
this.planet.stopFlying();
var direction = cam.unproject(t.x, t.y);
var targetPoint = new Ray(cam.eye, direction).hitSphere(t.grabbedSpheroid);
if (targetPoint) {
if (cam._n.dot(cam.eye.normal()) > 0.28) {
this.qRot = Quat.getRotationBetweenVectors(targetPoint.normal(), t.grabbedPoint.normal());
var rot = this.qRot;
cam.eye = rot.mulVec3(cam.eye);
cam._v = rot.mulVec3(cam._v);
cam._u = rot.mulVec3(cam._u);
cam._n = rot.mulVec3(cam._n);
cam.update();
this.scaleRot = 1;
} else {
var p0 = t.grabbedPoint,
p1 = Vec3.add(p0, cam._u),
p2 = Vec3.add(p0, p0.normal());
var dir = cam.unproject(t.x, t.y);
var px = new Vec3();
if (new Ray(cam.eye, dir).hitPlane(p0, p1, p2, px) === Ray.INSIDE) {
cam.eye = this._eye0.addA(px.subA(p0).negate());
cam.update();
this.scaleRot = 0;
}
}
}
}
};
onDraw(e) {
this.renderer.controlsBag.scaleRot = this.scaleRot;
if (this._touching)
return;
var r = this.renderer;
var cam = r.activeCamera;
var prevEye = cam.eye.clone();
if (this.stepIndex) {
r.controlsBag.scaleRot = 1;
var sf = this.stepsForward[this.stepsCount - this.stepIndex--];
var cam = this.renderer.activeCamera;
cam.eye = sf.eye;
cam._v = sf.v;
cam._u = sf.u;
cam._n = sf.n;
cam.update();
}
if (r.events.mouseState.leftButtonDown || !this.scaleRot)
return;
this.scaleRot -= this.inertia;
if (this.scaleRot <= 0)
this.scaleRot = 0;
else {
r.controlsBag.scaleRot = this.scaleRot;
var cam = r.activeCamera;
var rot = this.qRot.slerp(Quat.IDENTITY, 1 - this.scaleRot * this.scaleRot * this.scaleRot).normalize();
if (!(rot.x || rot.y || rot.z)) {
this.scaleRot = 0;
}
cam.eye = rot.mulVec3(cam.eye);
cam._v = rot.mulVec3(cam._v);
cam._u = rot.mulVec3(cam._u);
cam._n = rot.mulVec3(cam._n);
cam.update();
}
if (cam.eye.distance(prevEye) / cam._terrainAltitude > 0.01) {
this.planet.layerLock.lock(this._keyLock);
this.planet.terrainLock.lock(this._keyLock);
this.planet._normalMapCreator.lock(this._keyLock);
} else {
this.planet.layerLock.free(this._keyLock);
this.planet.terrainLock.free(this._keyLock);
this.planet._normalMapCreator.free(this._keyLock);
}
}
};
export { TouchNavigation };

View File

@ -1,9 +1,12 @@
goog.provide('og.control.ZoomControl');
/**
* @module og/control/ZoomControl
*/
goog.require('og.inheritance');
goog.require('og.control.BaseControl');
goog.require('og.control.MouseNavigation');
goog.require('og.idle');
'use strict';
import { BaseControl } from './BaseControl.js';
import { MouseNavigation } from './MouseNavigation.js';
import { Key } from './Lock.js';
/**
@ -12,105 +15,106 @@ goog.require('og.idle');
* @extends {og.control.BaseControl}
* @params {Object} [options] - Control options.
*/
og.control.ZoomControl = function (options) {
og.inheritance.base(this, options);
class ZoomControl extends BaseControl {
constructor(options) {
super(options);
options = options || {};
options = options || {};
this.distDiff = 0.33;
this.stepsCount = 5;
this.stepsForward = null;
this.stepIndex = 0;
this._keyLock = new og.idle.Key();
this.distDiff = 0.33;
this.stepsCount = 5;
this.stepsForward = null;
this.stepIndex = 0;
this._keyLock = new Key();
this.planet = null;
};
this.planet = null;
}
og.inheritance.extend(og.control.ZoomControl, og.control.BaseControl);
oninit() {
var zoomDiv = document.createElement('div'),
btnZoomIn = document.createElement('button'),
btnZoomOut = document.createElement('button');
og.control.zoomControl = function (options) {
return new og.control.ZoomControl(options);
};
zoomDiv.className = 'ogZoomControl';
btnZoomIn.className = 'ogZoomButton ogZoomIn';
btnZoomOut.className = 'ogZoomButton ogZoomOut';
og.control.ZoomControl.prototype.oninit = function () {
var zoomDiv = document.createElement('div'),
btnZoomIn = document.createElement('button'),
btnZoomOut = document.createElement('button');
zoomDiv.appendChild(btnZoomIn);
zoomDiv.appendChild(btnZoomOut);
zoomDiv.className = 'ogZoomControl';
btnZoomIn.className = 'ogZoomButton ogZoomIn';
btnZoomOut.className = 'ogZoomButton ogZoomOut';
this.renderer.div.appendChild(zoomDiv);
zoomDiv.appendChild(btnZoomIn);
zoomDiv.appendChild(btnZoomOut);
var that = this;
btnZoomIn.onclick = function (e) {
that.zoomIn();
};
this.renderer.div.appendChild(zoomDiv);
btnZoomOut.onclick = function (e) {
that.zoomOut();
};
var that = this;
btnZoomIn.onclick = function (e) {
that.zoomIn();
};
this.renderer.events.on("draw", this._draw, this);
}
btnZoomOut.onclick = function (e) {
that.zoomOut();
};
/**
* Planet zoom in.
* @public
*/
zoomIn() {
this.renderer.events.on("draw", this._draw, this);
};
this._deactivate = true;
/**
* Planet zoom in.
* @public
*/
og.control.ZoomControl.prototype.zoomIn = function () {
this.planet.layerLock.lock(this._keyLock);
this.planet.terrainLock.lock(this._keyLock);
this.planet._normalMapCreator.lock(this._keyLock);
this._deactivate = true;
this.stepIndex = this.stepsCount;
this.stepsForward = MouseNavigation.getMovePointsFromPixelTerrain(this.renderer.activeCamera,
this.planet, this.stepsCount, this.distDiff * 1.7, this.renderer.getCenter(), true, this.renderer.activeCamera._n.negateTo());
}
this.planet.layerLock.lock(this._keyLock);
this.planet.terrainLock.lock(this._keyLock);
this.planet._normalMapCreator.lock(this._keyLock);
/**
* Planet zoom out.
* @public
*/
zoomOut() {
this.stepIndex = this.stepsCount;
this.stepsForward = og.control.MouseNavigation.getMovePointsFromPixelTerrain(this.renderer.activeCamera,
this.planet, this.stepsCount, this.distDiff * 1.7, this.renderer.getCenter(), true, this.renderer.activeCamera._n.negateTo());
};
this._deactivate = true;
/**
* Planet zoom out.
* @public
*/
og.control.ZoomControl.prototype.zoomOut = function () {
this.planet.layerLock.lock(this._keyLock);
this.planet.terrainLock.lock(this._keyLock);
this.planet._normalMapCreator.lock(this._keyLock);
this._deactivate = true;
this.stepIndex = this.stepsCount;
this.stepsForward = MouseNavigation.getMovePointsFromPixelTerrain(this.renderer.activeCamera,
this.planet, this.stepsCount, this.distDiff * 2, this.renderer.getCenter(), false, this.renderer.activeCamera._n.negateTo());
}
this.planet.layerLock.lock(this._keyLock);
this.planet.terrainLock.lock(this._keyLock);
this.planet._normalMapCreator.lock(this._keyLock);
_draw(e) {
this.stepIndex = this.stepsCount;
this.stepsForward = og.control.MouseNavigation.getMovePointsFromPixelTerrain(this.renderer.activeCamera,
this.planet, this.stepsCount, this.distDiff * 2, this.renderer.getCenter(), false, this.renderer.activeCamera._n.negateTo());
};
var cam = this.renderer.activeCamera;
og.control.ZoomControl.prototype._draw = function (e) {
if (this.stepIndex) {
var sf = this.stepsForward[this.stepsCount - this.stepIndex--];
cam.eye = sf.eye;
cam._v = sf.v;
cam._u = sf.u;
cam._n = sf.n;
cam.update();
} else if (!cam._flying) {
if (this._deactivate) {
var cam = this.renderer.activeCamera;
this.planet.layerLock.free(this._keyLock);
this.planet.terrainLock.free(this._keyLock);
this.planet._normalMapCreator.free(this._keyLock);
if (this.stepIndex) {
var sf = this.stepsForward[this.stepsCount - this.stepIndex--];
cam.eye = sf.eye;
cam._v = sf.v;
cam._u = sf.u;
cam._n = sf.n;
cam.update();
} else if (!cam._flying) {
if (this._deactivate) {
this.planet.layerLock.free(this._keyLock);
this.planet.terrainLock.free(this._keyLock);
this.planet._normalMapCreator.free(this._keyLock);
this._deactivate = false;
this._deactivate = false;
}
}
}
};
};
export function zoomControl(options) {
return new ZoomControl(options);
};

View File

@ -1,6 +1,10 @@
goog.provide('og.LightSource');
/**
* @module og/light/LightSource
*/
goog.require('og.math.Vector3');
'use strict';
import { Vec3 } from '../math/Vec3.js';
/**
* Represents basic light source.
@ -13,297 +17,299 @@ goog.require('og.math.Vector3');
* @param {og.math.Vector3} [params.specular] - Specular RGB color.
* @param {number} [params.shininess] - Specular shininess.
*/
og.LightSource = function (name, params) {
params = params || {};
/**
* Light name.
* @protected
* @type {string}
*/
this._name = name || ("light_" + og.LightSource._counter++);
/**
* Render node where light is shines.
* @protected
* @type {og.scene.RenderNode}
*/
this._renderNode = null;
/**
* Light position.
* @protected
* @type {og.math.Vector3}
*/
this._position = params.position || new og.math.Vector3();
/**
* True if the light is directional.
* @public
* @type {boolean}
*/
this.directional = params.derectional != undefined ? params.derectional : true;
/**
* Ambient color.
* @protected
* @type {og.math.Vector3}
*/
this._ambient = params.ambient || new og.math.Vector3();
/**
* Diffuse color.
* @protected
* @type {og.math.Vector3}
*/
this._diffuse = params.diffuse || new og.math.Vector3(0.8, 0.8, 0.8);
/**
* Specular color.
* @protected
* @type {og.math.Vector3}
*/
this._specular = params.specular || new og.math.Vector3(0.18, 0.18, 0.18);
/**
* Shininess.
* @protected
* @type {number}
*/
this._shininess = params.shininess != undefined ? params.shininess : 3.3;
/**
* Light activity.
* @protected
* @type {boolean}
*/
this._active = true;
this._tempAmbient = this._ambient.clone();
this._tempDiffuse = this._diffuse.clone();
this._tempSpecular = this._specular.clone();
this._tempShininess = this._shininess;
};
og.LightSource._counter = 0;
/**
* Creates light source object.
* @function
* @param {string} [name] - Light name.
* @param {Object} [params] - Light parameters.
* @returns {og.LightSource}
*/
og.lightSource = function (name, params) {
return new og.LightSource(name, params);
};
/**
* Creates clone of the current light object.
* @public
* @returns {og.LightSource}
*/
og.LightSource.prototype.clone = function () {
};
/**
* Set light activity. If activity is false the light doesn't shine.
* @public
* @param {boolean} active - Light activity.
*/
og.LightSource.prototype.setActive = function (active) {
if (active && !this._active) {
var rn = this._renderNode;
if (rn) {
var index = rn._lightsNames.indexOf(this._name);
this._shininess = rn._lightsParamsf[index] = this._tempShininess;
if (index != -1) {
index *= 9;
this._ambient.x = rn._lightsParamsv[index] = this._tempAmbient.x;
this._ambient.y = rn._lightsParamsv[index + 1] = this._tempAmbient.y;
this._ambient.z = rn._lightsParamsv[index + 2] = this._tempAmbient.z;
this._diffuse.x = rn._lightsParamsv[index + 3] = this._tempDiffuse.x;
this._diffuse.y = rn._lightsParamsv[index + 4] = this._tempDiffuse.y;
this._diffuse.z = rn._lightsParamsv[index + 5] = this._tempDiffuse.z;
this._specular.x = rn._lightsParamsv[index + 6] = this._tempSpecular.x;
this._specular.y = rn._lightsParamsv[index + 7] = this._tempSpecular.y;
this._specular.z = rn._lightsParamsv[index + 8] = this._tempSpecular.z;
}
class LightSource {
static get _staticCounter() {
if (!this._counter && this._counter !== 0) {
this._counter = 0;
}
return this._counter;
}
static set _staticCounter(n) {
this._counter = n;
}
constructor(name, params) {
params = params || {};
/**
* Light name.
* @protected
* @type {string}
*/
this._name = name || ("light_" + LightSource._staticCounter++);
/**
* Render node where light is shines.
* @protected
* @type {og.scene.RenderNode}
*/
this._renderNode = null;
/**
* Light position.
* @protected
* @type {og.math.Vector3}
*/
this._position = params.position || Vec3();
/**
* True if the light is directional.
* @public
* @type {boolean}
*/
this.directional = params.derectional != undefined ? params.derectional : true;
/**
* Ambient color.
* @protected
* @type {og.math.Vector3}
*/
this._ambient = params.ambient || new Vec3();
/**
* Diffuse color.
* @protected
* @type {og.math.Vector3}
*/
this._diffuse = params.diffuse || new Vec3(0.8, 0.8, 0.8);
/**
* Specular color.
* @protected
* @type {og.math.Vector3}
*/
this._specular = params.specular || new Vec3(0.18, 0.18, 0.18);
/**
* Shininess.
* @protected
* @type {number}
*/
this._shininess = params.shininess != undefined ? params.shininess : 3.3;
/**
* Light activity.
* @protected
* @type {boolean}
*/
this._active = true;
} else if (!active && this._active) {
this._tempAmbient = this._ambient.clone();
this._tempDiffuse = this._diffuse.clone();
this._tempSpecular = this._specular.clone();
this._tempShininess = this._shininess;
this.setBlack();
this._active = false;
}
};
/**
* Gets light activity.
* @public
* @returns {boolean}
*/
og.LightSource.prototype.isActive = function () {
return this._active;
};
/**
* Creates clone of the current light object.
* @todo: TODO
* @public
* @returns {og.LightSource}
*/
clone() {
//TODO
}
/**
* Set light source position, or if it is a directional type sets light direction vector.
* @public
* @param {og.math.Vector3} position - Light position or direction vector.
* @returns {og.LightSource}
*/
og.LightSource.prototype.setPosition = function (position) {
this._position.x = position.x;
this._position.y = position.y;
this._position.z = position.z;
return this;
};
/**
* Returns light source position, or if it is a directional type sets light direction vector.
* @public
* @returns {og.math.Vector3} - Light source position/direction.
*/
og.LightSource.prototype.getPosition = function () {
return this._position.clone();
};
/**
* Set ambient color.
* @public
* @param {og.math.Vector3} rgb - Ambient color.
* @returns {og.LightSource}
*/
og.LightSource.prototype.setAmbient = function (rgb) {
this._ambient = rgb;
var rn = this._renderNode;
if (rn) {
var index = 9 * rn._lightsNames.indexOf(this._name);
if (index != -1) {
rn._lightsParamsv[index] = rgb.x;
rn._lightsParamsv[index + 1] = rgb.y;
rn._lightsParamsv[index + 2] = rgb.z;
/**
* Set light activity. If activity is false the light doesn't shine.
* @public
* @param {boolean} active - Light activity.
*/
setActive(active) {
if (active && !this._active) {
var rn = this._renderNode;
if (rn) {
var index = rn._lightsNames.indexOf(this._name);
this._shininess = rn._lightsParamsf[index] = this._tempShininess;
if (index != -1) {
index *= 9;
this._ambient.x = rn._lightsParamsv[index] = this._tempAmbient.x;
this._ambient.y = rn._lightsParamsv[index + 1] = this._tempAmbient.y;
this._ambient.z = rn._lightsParamsv[index + 2] = this._tempAmbient.z;
this._diffuse.x = rn._lightsParamsv[index + 3] = this._tempDiffuse.x;
this._diffuse.y = rn._lightsParamsv[index + 4] = this._tempDiffuse.y;
this._diffuse.z = rn._lightsParamsv[index + 5] = this._tempDiffuse.z;
this._specular.x = rn._lightsParamsv[index + 6] = this._tempSpecular.x;
this._specular.y = rn._lightsParamsv[index + 7] = this._tempSpecular.y;
this._specular.z = rn._lightsParamsv[index + 8] = this._tempSpecular.z;
}
}
this._active = true;
} else if (!active && this._active) {
this._tempAmbient = this._ambient.clone();
this._tempDiffuse = this._diffuse.clone();
this._tempSpecular = this._specular.clone();
this._tempShininess = this._shininess;
this.setBlack();
this._active = false;
}
}
return this;
};
/**
* Set diffuse color.
* @public
* @param {og.math.Vector3} rgb - Diffuse color.
* @returns {og.LightSource}
*/
og.LightSource.prototype.setDiffuse = function (rgb) {
this._diffuse = rgb;
var rn = this._renderNode;
if (rn) {
var index = 9 * rn._lightsNames.indexOf(this._name);
if (index != -1) {
rn._lightsParamsv[index + 3] = rgb.x;
rn._lightsParamsv[index + 4] = rgb.y;
rn._lightsParamsv[index + 5] = rgb.z;
}
/**
* Gets light activity.
* @public
* @returns {boolean}
*/
isActive() {
return this._active;
}
return this;
};
/**
* Set specular color.
* @public
* @param {og.math.Vector3} rgb - Specular color.
* @returns {og.LightSource}
*/
og.LightSource.prototype.setSpecular = function (rgb) {
this._specular = rgb;
var rn = this._renderNode;
if (rn) {
var index = 9 * rn._lightsNames.indexOf(this._name);
if (index != -1) {
rn._lightsParamsv[index + 6] = rgb.x;
rn._lightsParamsv[index + 7] = rgb.y;
rn._lightsParamsv[index + 8] = rgb.z;
}
/**
* Set light source position, or if it is a directional type sets light direction vector.
* @public
* @param {og.math.Vector3} position - Light position or direction vector.
* @returns {og.LightSource}
*/
setPosition(position) {
this._position.x = position.x;
this._position.y = position.y;
this._position.z = position.z;
return this;
}
return this;
};
/**
* Set material shininess.
* @public
* @param {number} shininess - Material shininess.
* @returns {og.LightSource}
*/
og.LightSource.prototype.setShininess = function (shininess) {
this._shininess = shininess;
var rn = this._renderNode;
if (rn) {
var index = rn._lightsNames.indexOf(this._name);
if (index != -1) {
rn._lightsParamsf[index] = shininess;
}
/**
* Returns light source position, or if it is a directional type sets light direction vector.
* @public
* @returns {og.math.Vector3} - Light source position/direction.
*/
getPosition() {
return this._position.clone();
}
return this;
};
/**
* Sets light to black.
* @public
* @returns {og.LightSource}
*/
og.LightSource.prototype.setBlack = function () {
this._ambient.clear();
this._diffuse.clear();
this._specular.clear();
this._shininess = 0;
var rn = this._renderNode;
if (rn) {
var index = 9 * rn._lightsNames.indexOf(this._name);
if (index != -1) {
rn._lightsParamsv[index] = rn._lightsParamsv[index + 1] = rn._lightsParamsv[index + 2] =
rn._lightsParamsv[index + 3] = rn._lightsParamsv[index + 4] = rn._lightsParamsv[index + 5] =
rn._lightsParamsv[index + 6] = rn._lightsParamsv[index + 7] = rn._lightsParamsv[index + 8] = 0;
/**
* Set ambient color.
* @public
* @param {og.math.Vector3} rgb - Ambient color.
* @returns {og.LightSource}
*/
setAmbient(rgb) {
this._ambient = rgb;
var rn = this._renderNode;
if (rn) {
var index = 9 * rn._lightsNames.indexOf(this._name);
if (index != -1) {
rn._lightsParamsv[index] = rgb.x;
rn._lightsParamsv[index + 1] = rgb.y;
rn._lightsParamsv[index + 2] = rgb.z;
}
}
return this;
}
return this;
};
/**
* Adds current light to the render node scene.
* @public
* @param {og.scene.RenderNode} renderNode - Render node scene.
* @returns {og.LightSource}
*/
og.LightSource.prototype.addTo = function (renderNode) {
this._renderNode = renderNode;
renderNode._lights.push(this);
renderNode._lightsNames.push(this._name);
renderNode._lightsParamsf.push(this._shininess);
renderNode._lightsParamsv.push.apply(renderNode._lightsParamsv, this._ambient.toVec());
renderNode._lightsParamsv.push.apply(renderNode._lightsParamsv, this._diffuse.toVec());
renderNode._lightsParamsv.push.apply(renderNode._lightsParamsv, this._specular.toVec());
return this;
};
/**
* Removes from render node scene.
* @public
*/
og.LightSource.prototype.remove = function () {
var rn = this.renderNode;
if (rn) {
var li = rn.getLightById(this._name);
if (li != -1) {
rn._lights.splice(li, 1);
rn._lightsNames.splice(li, 1);
rn._lightsParamsf.splice(li, 1);
rn._lightsParamsv.splice(li, 9);
/**
* Set diffuse color.
* @public
* @param {og.math.Vector3} rgb - Diffuse color.
* @returns {og.LightSource}
*/
setDiffuse(rgb) {
this._diffuse = rgb;
var rn = this._renderNode;
if (rn) {
var index = 9 * rn._lightsNames.indexOf(this._name);
if (index != -1) {
rn._lightsParamsv[index + 3] = rgb.x;
rn._lightsParamsv[index + 4] = rgb.y;
rn._lightsParamsv[index + 5] = rgb.z;
}
}
return this;
}
this._renderNode = null;
};
/**
* Set specular color.
* @public
* @param {og.math.Vector3} rgb - Specular color.
* @returns {og.LightSource}
*/
setSpecular(rgb) {
this._specular = rgb;
var rn = this._renderNode;
if (rn) {
var index = 9 * rn._lightsNames.indexOf(this._name);
if (index != -1) {
rn._lightsParamsv[index + 6] = rgb.x;
rn._lightsParamsv[index + 7] = rgb.y;
rn._lightsParamsv[index + 8] = rgb.z;
}
}
return this;
}
/**
* Set material shininess.
* @public
* @param {number} shininess - Material shininess.
* @returns {og.LightSource}
*/
setShininess(shininess) {
this._shininess = shininess;
var rn = this._renderNode;
if (rn) {
var index = rn._lightsNames.indexOf(this._name);
if (index != -1) {
rn._lightsParamsf[index] = shininess;
}
}
return this;
}
/**
* Sets light to black.
* @public
* @returns {og.LightSource}
*/
setBlack() {
this._ambient.clear();
this._diffuse.clear();
this._specular.clear();
this._shininess = 0;
var rn = this._renderNode;
if (rn) {
var index = 9 * rn._lightsNames.indexOf(this._name);
if (index != -1) {
rn._lightsParamsv[index] = rn._lightsParamsv[index + 1] = rn._lightsParamsv[index + 2] =
rn._lightsParamsv[index + 3] = rn._lightsParamsv[index + 4] = rn._lightsParamsv[index + 5] =
rn._lightsParamsv[index + 6] = rn._lightsParamsv[index + 7] = rn._lightsParamsv[index + 8] = 0;
}
}
return this;
}
/**
* Adds current light to the render node scene.
* @public
* @param {og.scene.RenderNode} renderNode - Render node scene.
* @returns {og.LightSource}
*/
addTo(renderNode) {
this._renderNode = renderNode;
renderNode._lights.push(this);
renderNode._lightsNames.push(this._name);
renderNode._lightsParamsf.push(this._shininess);
renderNode._lightsParamsv.push.apply(renderNode._lightsParamsv, this._ambient.toVec());
renderNode._lightsParamsv.push.apply(renderNode._lightsParamsv, this._diffuse.toVec());
renderNode._lightsParamsv.push.apply(renderNode._lightsParamsv, this._specular.toVec());
return this;
}
/**
* Removes from render node scene.
* @public
*/
remove() {
var rn = this.renderNode;
if (rn) {
var li = rn.getLightById(this._name);
if (li != -1) {
rn._lights.splice(li, 1);
rn._lightsNames.splice(li, 1);
rn._lightsParamsf.splice(li, 1);
rn._lightsParamsv.splice(li, 9);
}
}
this._renderNode = null;
}
};
export { LightSource };

View File

@ -5,6 +5,7 @@
'use strict';
class Lock {
constructor() {
this._lock = 0;
}