2021-02-19 16:23:11 +01:00
|
|
|
/*
|
|
|
|
* This file is part of BlueMap, licensed under the MIT License (MIT).
|
|
|
|
*
|
|
|
|
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
|
|
|
|
* Copyright (c) contributors
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2021-01-28 02:56:26 +01:00
|
|
|
import {Color, DoubleSide, Mesh, ShaderMaterial, Shape, ShapeBufferGeometry, Vector2} from "three";
|
2021-01-18 16:51:03 +01:00
|
|
|
import {LineMaterial} from "../util/lines/LineMaterial";
|
2021-01-28 02:56:26 +01:00
|
|
|
import {MARKER_FILL_VERTEX_SHADER} from "./MarkerFillVertexShader";
|
|
|
|
import {MARKER_FILL_FRAGMENT_SHADER} from "./MarkerFillFragmentShader";
|
2021-01-18 16:51:03 +01:00
|
|
|
import {LineGeometry} from "../util/lines/LineGeometry";
|
|
|
|
import {Line2} from "../util/lines/Line2";
|
2021-01-28 02:56:26 +01:00
|
|
|
import {deepEquals} from "../util/Utils";
|
|
|
|
import {ObjectMarker} from "./ObjectMarker";
|
2021-01-18 16:51:03 +01:00
|
|
|
|
2021-01-28 02:56:26 +01:00
|
|
|
export class ShapeMarker extends ObjectMarker {
|
2021-01-18 16:51:03 +01:00
|
|
|
|
2021-01-28 02:56:26 +01:00
|
|
|
/**
|
|
|
|
* @param markerId {string}
|
|
|
|
*/
|
|
|
|
constructor(markerId) {
|
|
|
|
super(markerId);
|
2021-01-18 16:51:03 +01:00
|
|
|
Object.defineProperty(this, 'isShapeMarker', {value: true});
|
2021-01-28 02:56:26 +01:00
|
|
|
this.markerType = "shape";
|
|
|
|
|
|
|
|
let zero = new Vector2();
|
|
|
|
let shape = new Shape([zero, zero, zero]);
|
|
|
|
this.fill = new ShapeMarkerFill(shape);
|
|
|
|
this.border = new ShapeMarkerBorder(shape);
|
|
|
|
this.border.renderOrder = -1; // render border before fill
|
|
|
|
|
|
|
|
this.add(this.border, this.fill);
|
|
|
|
|
|
|
|
this._markerData = {};
|
|
|
|
}
|
2021-01-18 16:51:03 +01:00
|
|
|
|
2021-01-28 02:56:26 +01:00
|
|
|
/**
|
|
|
|
* @param y {number}
|
|
|
|
*/
|
|
|
|
setShapeY(y) {
|
|
|
|
let relativeY = y - this.position.y;
|
|
|
|
this.fill.position.y = relativeY;
|
|
|
|
this.border.position.y = relativeY;
|
|
|
|
}
|
2021-01-18 16:51:03 +01:00
|
|
|
|
2021-01-28 02:56:26 +01:00
|
|
|
/**
|
|
|
|
* @param shape {Shape}
|
|
|
|
*/
|
|
|
|
setShape(shape) {
|
|
|
|
this.fill.updateGeometry(shape);
|
|
|
|
this.border.updateGeometry(shape);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {{r: number, g: number, b: number, a: number}} ColorLike
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param markerData {{
|
|
|
|
* position: {x: number, y: number, z: number},
|
|
|
|
* label: string,
|
|
|
|
* shape: {x: number, z: number}[],
|
|
|
|
* shapeY: number,
|
|
|
|
* height: number,
|
|
|
|
* link: string,
|
|
|
|
* newTab: boolean,
|
|
|
|
* depthTest: boolean,
|
|
|
|
* lineWidth: number,
|
|
|
|
* borderColor: ColorLike,
|
|
|
|
* lineColor: ColorLike,
|
|
|
|
* fillColor: ColorLike,
|
|
|
|
* minDistance: number,
|
|
|
|
* maxDistance: number
|
|
|
|
* }}
|
|
|
|
*/
|
|
|
|
updateFromData(markerData) {
|
|
|
|
super.updateFromData(markerData);
|
|
|
|
|
|
|
|
// update shape only if needed, based on last update-data
|
|
|
|
if (
|
|
|
|
!this._markerData.shape || !deepEquals(markerData.shape, this._markerData.shape) ||
|
|
|
|
!this._markerData.position || !deepEquals(markerData.position, this._markerData.position)
|
|
|
|
){
|
|
|
|
this.setShape(this.createShapeFromData(markerData.shape));
|
|
|
|
}
|
2021-01-18 16:51:03 +01:00
|
|
|
|
2021-01-28 02:56:26 +01:00
|
|
|
// update shapeY
|
|
|
|
this.setShapeY((markerData.shapeY || markerData.height || 0) + 0.01); //"height" for backwards compatibility, adding 0.01 to avoid z-fighting
|
2021-01-18 16:51:03 +01:00
|
|
|
|
2021-01-28 02:56:26 +01:00
|
|
|
// update depthTest
|
|
|
|
this.border.depthTest = !!markerData.depthTest;
|
|
|
|
this.fill.depthTest = !!markerData.depthTest;
|
|
|
|
|
|
|
|
// update border-width
|
|
|
|
this.border.linewidth = markerData.lineWidth !== undefined ? markerData.lineWidth : 2;
|
|
|
|
|
|
|
|
// update border-color
|
|
|
|
let bc = markerData.lineColor || markerData.borderColor || {}; //"borderColor" for backwards compatibility
|
|
|
|
this.border.color.setRGB((bc.r || 0) / 255, (bc.g || 0) / 255, (bc.b || 0) / 255);
|
|
|
|
this.border.opacity = bc.a || 0;
|
|
|
|
|
|
|
|
// update fill-color
|
|
|
|
let fc = markerData.fillColor || {};
|
|
|
|
this.fill.color.setRGB((fc.r || 0) / 255, (fc.g || 0) / 255, (fc.b || 0) / 255);
|
|
|
|
this.fill.opacity = fc.a || 0;
|
|
|
|
|
|
|
|
// update min/max distances
|
|
|
|
let minDist = markerData.minDistance || 0;
|
|
|
|
let maxDist = markerData.maxDistance !== undefined ? markerData.maxDistance : Number.MAX_VALUE;
|
|
|
|
this.border.fadeDistanceMin = minDist;
|
|
|
|
this.border.fadeDistanceMax = maxDist;
|
|
|
|
this.fill.fadeDistanceMin = minDist;
|
|
|
|
this.fill.fadeDistanceMax = maxDist;
|
|
|
|
|
|
|
|
// save used marker data for next update
|
|
|
|
this._markerData = markerData;
|
|
|
|
}
|
|
|
|
|
|
|
|
dispose() {
|
|
|
|
super.dispose();
|
|
|
|
|
|
|
|
this.fill.dispose();
|
|
|
|
this.border.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* Creates a shape from a data object, usually parsed json from a markers.json
|
|
|
|
* @param shapeData {object}
|
|
|
|
* @returns {Shape}
|
|
|
|
*/
|
|
|
|
createShapeFromData(shapeData) {
|
|
|
|
/** @type {THREE.Vector2[]} **/
|
|
|
|
let points = [];
|
|
|
|
|
|
|
|
if (Array.isArray(shapeData)){
|
|
|
|
shapeData.forEach(point => {
|
|
|
|
let x = (point.x || 0) - this.position.x;
|
|
|
|
let z = (point.z || 0) - this.position.z;
|
|
|
|
|
|
|
|
points.push(new Vector2(x, z));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Shape(points);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class ShapeMarkerFill extends Mesh {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param shape {Shape}
|
|
|
|
*/
|
|
|
|
constructor(shape) {
|
|
|
|
let geometry = ShapeMarkerFill.createGeometry(shape);
|
|
|
|
let material = new ShaderMaterial({
|
2021-01-18 16:51:03 +01:00
|
|
|
vertexShader: MARKER_FILL_VERTEX_SHADER,
|
|
|
|
fragmentShader: MARKER_FILL_FRAGMENT_SHADER,
|
|
|
|
side: DoubleSide,
|
2021-01-28 02:56:26 +01:00
|
|
|
depthTest: true,
|
2021-01-18 16:51:03 +01:00
|
|
|
transparent: true,
|
|
|
|
uniforms: {
|
2021-01-28 02:56:26 +01:00
|
|
|
markerColor: { value: new Color() },
|
|
|
|
markerOpacity: { value: 0 },
|
|
|
|
fadeDistanceMin: { value: 0 },
|
|
|
|
fadeDistanceMax: { value: Number.MAX_VALUE },
|
2021-01-18 16:51:03 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-01-28 02:56:26 +01:00
|
|
|
super(geometry, material);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {Color}
|
|
|
|
*/
|
|
|
|
get color(){
|
|
|
|
return this.material.uniforms.markerColor.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
|
|
|
get opacity() {
|
|
|
|
return this.material.uniforms.markerOpacity.value;
|
2021-01-18 16:51:03 +01:00
|
|
|
}
|
|
|
|
|
2021-01-28 02:56:26 +01:00
|
|
|
/**
|
|
|
|
* @param opacity {number}
|
|
|
|
*/
|
|
|
|
set opacity(opacity) {
|
|
|
|
this.material.uniforms.markerOpacity.value = opacity;
|
|
|
|
this.visible = opacity > 0;
|
|
|
|
}
|
2021-01-18 16:51:03 +01:00
|
|
|
|
2021-01-28 02:56:26 +01:00
|
|
|
/**
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
get depthTest() {
|
|
|
|
return this.material.depthTest;
|
|
|
|
}
|
2021-01-18 16:51:03 +01:00
|
|
|
|
2021-01-28 02:56:26 +01:00
|
|
|
/**
|
|
|
|
* @param test {boolean}
|
|
|
|
*/
|
|
|
|
set depthTest(test) {
|
|
|
|
this.material.depthTest = test;
|
|
|
|
}
|
2021-01-18 16:51:03 +01:00
|
|
|
|
2021-01-28 02:56:26 +01:00
|
|
|
/**
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
|
|
|
get fadeDistanceMin() {
|
|
|
|
return this.material.uniforms.fadeDistanceMin.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param min {number}
|
|
|
|
*/
|
|
|
|
set fadeDistanceMin(min) {
|
|
|
|
this.material.uniforms.fadeDistanceMin.value = min;
|
2021-01-18 16:51:03 +01:00
|
|
|
}
|
|
|
|
|
2021-01-28 02:56:26 +01:00
|
|
|
/**
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
|
|
|
get fadeDistanceMax() {
|
|
|
|
return this.material.uniforms.fadeDistanceMax.value;
|
|
|
|
}
|
2021-01-18 16:51:03 +01:00
|
|
|
|
2021-01-28 02:56:26 +01:00
|
|
|
/**
|
|
|
|
* @param max {number}
|
|
|
|
*/
|
|
|
|
set fadeDistanceMax(max) {
|
|
|
|
this.material.uniforms.fadeDistanceMax.value = max;
|
|
|
|
}
|
|
|
|
|
|
|
|
onClick(event) {
|
|
|
|
if (event.intersection) {
|
|
|
|
if (event.intersection.distance > this.fadeDistanceMax) return false;
|
|
|
|
if (event.intersection.distance < this.fadeDistanceMin) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.onClick(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param shape {Shape}
|
|
|
|
*/
|
|
|
|
updateGeometry(shape) {
|
|
|
|
this.geometry.dispose();
|
|
|
|
this.geometry = ShapeMarkerFill.createGeometry(shape);
|
2021-01-18 16:51:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
dispose() {
|
2021-01-28 02:56:26 +01:00
|
|
|
this.geometry.dispose();
|
|
|
|
this.material.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param shape {Shape}
|
|
|
|
* @returns {ShapeBufferGeometry}
|
|
|
|
*/
|
|
|
|
static createGeometry(shape) {
|
|
|
|
let geometry = new ShapeBufferGeometry(shape, 5);
|
|
|
|
geometry.rotateX(Math.PI / 2); //make y to z
|
|
|
|
|
|
|
|
return geometry;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class ShapeMarkerBorder extends Line2 {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param shape {Shape}
|
|
|
|
*/
|
|
|
|
constructor(shape) {
|
|
|
|
let geometry = new LineGeometry();
|
|
|
|
geometry.setPositions(ShapeMarkerBorder.createLinePoints(shape));
|
|
|
|
|
|
|
|
let material = new LineMaterial({
|
|
|
|
color: new Color(),
|
|
|
|
opacity: 0,
|
|
|
|
transparent: true,
|
|
|
|
linewidth: 1,
|
|
|
|
depthTest: true,
|
|
|
|
vertexColors: false,
|
|
|
|
dashed: false,
|
2021-01-18 16:51:03 +01:00
|
|
|
});
|
2021-01-28 02:56:26 +01:00
|
|
|
material.uniforms.fadeDistanceMin = { value: 0 };
|
|
|
|
material.uniforms.fadeDistanceMax = { value: Number.MAX_VALUE };
|
2021-01-18 16:51:03 +01:00
|
|
|
|
2021-01-28 02:56:26 +01:00
|
|
|
material.resolution.set(window.innerWidth, window.innerHeight);
|
2021-01-18 16:51:03 +01:00
|
|
|
|
2021-01-28 02:56:26 +01:00
|
|
|
super(geometry, material);
|
|
|
|
|
|
|
|
this.computeLineDistances();
|
2021-01-18 16:51:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-28 02:56:26 +01:00
|
|
|
* @returns {Color}
|
2021-01-18 16:51:03 +01:00
|
|
|
*/
|
2021-01-28 02:56:26 +01:00
|
|
|
get color(){
|
|
|
|
return this.material.color;
|
|
|
|
}
|
2021-01-18 16:51:03 +01:00
|
|
|
|
2021-01-28 02:56:26 +01:00
|
|
|
/**
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
|
|
|
get opacity() {
|
|
|
|
return this.material.opacity;
|
2021-01-18 16:51:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-28 02:56:26 +01:00
|
|
|
* @param opacity {number}
|
2021-01-18 16:51:03 +01:00
|
|
|
*/
|
2021-01-28 02:56:26 +01:00
|
|
|
set opacity(opacity) {
|
|
|
|
this.material.opacity = opacity;
|
|
|
|
this.visible = opacity > 0;
|
|
|
|
}
|
2021-01-18 16:51:03 +01:00
|
|
|
|
2021-01-28 02:56:26 +01:00
|
|
|
/**
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
|
|
|
get linewidth() {
|
|
|
|
return this.material.linewidth;
|
2021-01-18 16:51:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param width {number}
|
|
|
|
*/
|
2021-01-28 02:56:26 +01:00
|
|
|
set linewidth(width) {
|
|
|
|
this.material.linewidth = width;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
get depthTest() {
|
|
|
|
return this.material.depthTest;
|
2021-01-18 16:51:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param test {boolean}
|
|
|
|
*/
|
|
|
|
set depthTest(test) {
|
2021-01-28 02:56:26 +01:00
|
|
|
this.material.depthTest = test;
|
|
|
|
}
|
2021-01-18 16:51:03 +01:00
|
|
|
|
2021-01-28 02:56:26 +01:00
|
|
|
/**
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
|
|
|
get fadeDistanceMin() {
|
|
|
|
return this.material.uniforms.fadeDistanceMin.value;
|
2021-01-18 16:51:03 +01:00
|
|
|
}
|
|
|
|
|
2021-01-28 02:56:26 +01:00
|
|
|
/**
|
|
|
|
* @param min {number}
|
|
|
|
*/
|
|
|
|
set fadeDistanceMin(min) {
|
|
|
|
this.material.uniforms.fadeDistanceMin.value = min;
|
2021-01-18 16:51:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-28 02:56:26 +01:00
|
|
|
* @returns {number}
|
2021-01-18 16:51:03 +01:00
|
|
|
*/
|
2021-01-28 02:56:26 +01:00
|
|
|
get fadeDistanceMax() {
|
|
|
|
return this.material.uniforms.fadeDistanceMax.value;
|
2021-01-18 16:51:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-28 02:56:26 +01:00
|
|
|
* @param max {number}
|
2021-01-18 16:51:03 +01:00
|
|
|
*/
|
2021-01-28 02:56:26 +01:00
|
|
|
set fadeDistanceMax(max) {
|
|
|
|
this.material.uniforms.fadeDistanceMax.value = max;
|
|
|
|
}
|
|
|
|
|
|
|
|
onClick(event) {
|
|
|
|
if (event.intersection) {
|
|
|
|
if (event.intersection.distance > this.fadeDistanceMax) return false;
|
|
|
|
if (event.intersection.distance < this.fadeDistanceMin) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.onClick(event);
|
|
|
|
}
|
2021-01-18 16:51:03 +01:00
|
|
|
|
2021-01-28 02:56:26 +01:00
|
|
|
/**
|
|
|
|
* @param shape {Shape}
|
|
|
|
*/
|
|
|
|
updateGeometry(shape) {
|
|
|
|
this.geometry.setPositions(ShapeMarkerBorder.createLinePoints(shape));
|
|
|
|
this.computeLineDistances();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param renderer {THREE.WebGLRenderer}
|
|
|
|
*/
|
|
|
|
onBeforeRender(renderer) {
|
|
|
|
renderer.getSize(this.material.resolution);
|
|
|
|
}
|
2021-01-18 16:51:03 +01:00
|
|
|
|
2021-01-28 02:56:26 +01:00
|
|
|
dispose() {
|
|
|
|
this.geometry.dispose();
|
|
|
|
this.material.dispose();
|
|
|
|
}
|
2021-01-18 16:51:03 +01:00
|
|
|
|
2021-01-28 02:56:26 +01:00
|
|
|
/**
|
|
|
|
* @param shape {Shape}
|
|
|
|
* @returns {number[]}
|
|
|
|
*/
|
|
|
|
static createLinePoints(shape) {
|
2021-01-18 16:51:03 +01:00
|
|
|
let points3d = [];
|
2021-01-28 02:56:26 +01:00
|
|
|
let points = shape.getPoints(5);
|
2021-01-18 16:51:03 +01:00
|
|
|
points.forEach(point => points3d.push(point.x, 0, point.y));
|
2021-01-28 02:56:26 +01:00
|
|
|
points3d.push(points[0].x, 0, points[0].y);
|
2021-01-18 16:51:03 +01:00
|
|
|
|
2021-01-28 02:56:26 +01:00
|
|
|
return points3d;
|
2021-01-18 16:51:03 +01:00
|
|
|
}
|
|
|
|
|
2021-01-28 02:56:26 +01:00
|
|
|
}
|