UNPKG

qwc2-lts

Version:
214 lines (211 loc) 11.5 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); } function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /** * Copyright 2024 Sourcepole AG * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. */ import React from 'react'; import PropTypes from 'prop-types'; import suncalc from 'suncalc'; import { AmbientLight, DirectionalLight } from 'three'; import CoordinatesUtils from '../../utils/CoordinatesUtils'; import LocaleUtils from '../../utils/LocaleUtils'; import SideBar from '../SideBar'; import Input from '../widgets/Input'; import ToggleSwitch from '../widgets/ToggleSwitch'; import './style/Map3DLight.css'; var Map3DLight = /*#__PURE__*/function (_React$Component) { function Map3DLight(props) { var _this; _classCallCheck(this, Map3DLight); _this = _callSuper(this, Map3DLight, [props]); _defineProperty(_this, "state", { systemTime: false, timestamp: 0 }); _defineProperty(_this, "renderBody", function () { var date = new Date(_this.state.timestamp); var year = date.getFullYear(); var month = String(date.getMonth() + 1).padStart(2, '0'); var day = String(date.getDate()).padStart(2, '0'); var hours = String(date.getHours()).padStart(2, '0'); var minutes = String(date.getMinutes() + date.getSeconds() > 30 ? 1 : 0).padStart(2, '0'); var ymd = "".concat(year, "-").concat(month, "-").concat(day); var hm = "".concat(hours, ":").concat(minutes); return /*#__PURE__*/React.createElement("div", { className: "datetime3d-body" }, /*#__PURE__*/React.createElement("table", null, /*#__PURE__*/React.createElement("tbody", null, /*#__PURE__*/React.createElement("tr", null, /*#__PURE__*/React.createElement("td", null, "System time"), /*#__PURE__*/React.createElement("td", null, /*#__PURE__*/React.createElement(ToggleSwitch, { active: _this.state.systemTime, onChange: _this.toggleSystemTime }))), /*#__PURE__*/React.createElement("tr", null, /*#__PURE__*/React.createElement("td", null, "Date"), /*#__PURE__*/React.createElement("td", null, /*#__PURE__*/React.createElement(Input, { disabled: _this.state.systemTime, onChange: _this.updateDate, type: "date", value: ymd }))), /*#__PURE__*/React.createElement("tr", null, /*#__PURE__*/React.createElement("td", null, "Time"), /*#__PURE__*/React.createElement("td", null, /*#__PURE__*/React.createElement(Input, { disabled: _this.state.systemTime, onChange: _this.updateTime, type: "time", value: hm })))))); }); _defineProperty(_this, "toggleSystemTime", function (active) { if (active) { _this.setCurrentTimestamp(); } else { clearTimeout(_this.setTimestampTimeout); } _this.setState({ systemTime: active }); }); _defineProperty(_this, "updateDate", function (value) { if (!value) { return; } _this.setState(function (state) { var date = new Date(state.timestamp); var ymd = value.split("-").map(Number); date.setFullYear(ymd[0]); date.setMonth(ymd[1] - 1); date.setDate(ymd[2]); return { timestamp: +date }; }); }); _defineProperty(_this, "updateTime", function (value) { if (!value) { return; } _this.setState(function (state) { var date = new Date(state.timestamp); var hm = value.split(":").map(Number); date.setHours(hm[0]); date.setMinutes(hm[1]); return { timestamp: +date }; }); }); _defineProperty(_this, "setCurrentTimestamp", function () { // Set current timestamp and update it every minute var date = new Date(); var secondsRemaining = 60 - date.getSeconds() - date.getMilliseconds() / 1000; _this.setState({ timestamp: +date }); _this.setTimestampTimeout = setTimeout(_this.setCurrentTimestamp, 1000 * secondsRemaining); }); _defineProperty(_this, "updateLightPosition", function () { var force = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false; var sceneContext = _this.props.sceneContext; var directionalLight = sceneContext.getSceneObject("__directionalLight"); // Recompute light if (force || directionalLight.target.position.distanceTo(sceneContext.scene.view.controls.target) > 10000) { var x = sceneContext.scene.view.controls.target.x; var y = sceneContext.scene.view.controls.target.y; var wgsPos = CoordinatesUtils.reproject([x, y], sceneContext.mapCrs, "EPSG:4326"); var sunPos = suncalc.getPosition(new Date(_this.state.timestamp), wgsPos[1], wgsPos[0]); var az = sunPos.azimuth + Math.PI; var alt = sunPos.altitude; // Assume sun at 10000km distance var distance = 10000000; directionalLight.position.x = x + Math.cos(alt) * Math.sin(az) * distance; directionalLight.position.y = y + Math.cos(alt) * Math.cos(az) * distance; directionalLight.position.z = Math.sin(alt) * distance; directionalLight.updateMatrixWorld(); directionalLight.target.position.x = x; directionalLight.target.position.y = y; directionalLight.target.position.z = 0; directionalLight.target.updateMatrixWorld(); var alpha = Math.max(0, alt / (0.5 * Math.PI)); var intensity = 1.15 * alpha / (alpha + 0.15); sceneContext.map.hillshading.azimuth = az / Math.PI * 180; // Azimuth is measured from South sceneContext.map.hillshading.zenith = 90 - intensity * 30; sceneContext.map.hillshading.intensity = 1 - 0.4 * intensity; sceneContext.map.hillshading.elevationLayersOnly = true; // Modulate light intensities based on sun altitude sceneContext.getSceneObject("__ambientLight").intensity = 0.01 + 0.49 * intensity; sceneContext.getSceneObject("__directionalLight").intensity = 0.1 + 1.4 * intensity; sceneContext.scene.notifyChange(sceneContext.map); sceneContext.scene.renderer.render(sceneContext.scene.scene, sceneContext.scene.view.camera); } }); _this.setTimestampTimeout = null; var now = new Date(); now.setHours(12); now.setMinutes(0); now.setSeconds(0); _this.state.timestamp = +now; return _this; } _inherits(Map3DLight, _React$Component); return _createClass(Map3DLight, [{ key: "componentDidMount", value: function componentDidMount() { this.componentDidUpdate({}); } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps, prevState) { var _prevProps$sceneConte, _this2 = this; if (this.props.sceneContext.scene !== ((_prevProps$sceneConte = prevProps.sceneContext) === null || _prevProps$sceneConte === void 0 ? void 0 : _prevProps$sceneConte.scene)) { var ambientLight = new AmbientLight('white', 0.5); this.props.sceneContext.addSceneObject("__ambientLight", ambientLight); var directionalLight = new DirectionalLight(0xffffff, 1.5); this.props.sceneContext.addSceneObject("__directionalLight", directionalLight); this.props.sceneContext.scene.view.controls.addEventListener('change', function () { return _this2.updateLightPosition(); }); // Ensure light position is updated at least once per minute this.lightPositionInterval = setInterval(function () { return _this2.updateLightPosition(true); }, 1000 * 60); this.updateLightPosition(); } else if (this.state.timestamp !== prevState.timestamp) { this.updateLightPosition(true); } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { clearInterval(this.lightPositionInterval); } }, { key: "render", value: function render() { var _this3 = this; return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(SideBar, { icon: "clock", id: "DateTime3D", title: LocaleUtils.tr("appmenu.items.DateTime3D"), width: "20em" }, function () { return { body: _this3.renderBody() }; })); } }]); }(React.Component); _defineProperty(Map3DLight, "propTypes", { sceneContext: PropTypes.object }); export { Map3DLight as default };