react-xrplayer
Version:
An excellent xr player for react
165 lines (130 loc) • 6.82 kB
JavaScript
;
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var THREE = _interopRequireWildcard(require("three"));
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var HotSpotHelper = function HotSpotHelper(sence, _container, _camera) {
var _this = this;
_classCallCheck(this, HotSpotHelper);
_defineProperty(this, "addIcons", function (iconList) {
for (var i = 0; i < iconList.length; i++) {
var img = iconList.img,
position = iconList.position,
name = iconList.name,
title = iconList.title,
width = iconList.width,
height = iconList.height;
_this.markIcon(img, position, name, title, width, height);
}
});
_defineProperty(this, "removeAllIcons", function () {
for (var i = 0; i < _this.markIconGroup.children.length; i++) {
var tip = document.getElementById(_this.markIconGroup.children[i].name);
if (tip) {
_this.container.removeChild(tip);
}
_this.markIconGroup.remove(_this.markIconGroup.children[i]);
}
});
_defineProperty(this, "markIcon", function (img, position, name, title, width, height) {
var w = width || 0.08;
var h = height || 0.08;
var textureLoader = new THREE.TextureLoader();
var material = new THREE.MeshBasicMaterial({
map: textureLoader.load(img),
side: THREE.DoubleSide,
transparent: true
});
var geometry = new THREE.PlaneGeometry(w, h);
var mesh = new THREE.Mesh(geometry, material);
mesh.position.set(position.x, position.y, position.z);
mesh.name = name;
mesh.meshType = 'markIcon';
_this.markIconGroup.add(mesh);
var div = document.createElement("div");
div.id = name;
div.style = "padding:4px 4px;background:rgba(0,0,0,.5);color:#fff;display:none;position:absolute;border-radius:6px; -webkit-user-select:none; -moz-user-select:none; -ms-user-select:none; user-select:none;font-size:0.75rem;";
div.innerHTML = title;
_this.container.appendChild(div);
});
_defineProperty(this, "removeIcon", function (name) {
for (var i = 0; i < _this.markIconGroup.children.length; i++) {
if (_this.markIconGroup.children[i].name === name) {
_this.markIconGroup.remove(_this.markIconGroup.children[i]);
var tip = document.getElementById(name);
if (tip) {
_this.container.removeChild(tip);
}
break;
}
}
});
_defineProperty(this, "markIconInViews", function () {
var camera = _this.camera;
for (var i = 0; i < _this.markIconGroup.children.length; i++) {
var wpVector = new THREE.Vector3();
var pos = _this.markIconGroup.children[i].getWorldPosition(wpVector).applyMatrix4(camera.matrixWorldInverse).applyMatrix4(camera.projectionMatrix);
var name = _this.markIconGroup.children[i].name;
if (pos.x >= -1 && pos.x <= 1 && pos.y >= -1 && pos.y <= 1 && pos.z >= -1 && pos.z <= 1) {
var screenPos = _this.objectPosToScreenPos(_this.markIconGroup.children[i], _this.container, _this.camera);
var tip = document.getElementById(name);
if (tip) {
tip.style.display = "block";
tip.style.left = screenPos.x - tip.clientWidth / 2 + "px";
tip.style.top = screenPos.y - tip.clientHeight * 2 + "px";
}
} else {
tip = document.getElementById(name);
if (tip) {
tip.style.display = "none";
}
}
}
});
_defineProperty(this, "objectPosToScreenPos", function (object, container, camera) {
var vector = new THREE.Vector3();
vector.setFromMatrixPosition(object.matrixWorld).project(camera);
var x2hat = vector.x,
y2hat = vector.y;
var W = container.clientWidth;
var H = container.clientHeight;
var pos = new THREE.Vector2();
pos.x = W / 2 * (x2hat + 1);
pos.y = H / 2 * (1 - y2hat);
return pos;
});
_defineProperty(this, "update", function () {
if (_this.markIconGroup) {
_this.markIconInViews();
for (var i = 0; i < _this.markIconGroup.children.length; i++) {
_this.markIconGroup.children[i].lookAt(_this.camera.position);
}
}
});
this.camera = _camera;
this.sence = sence;
this.container = _container;
this.markIconGroup = new THREE.Group();
this.markIconGroup.name = "markIconGroup";
this.sence.add(this.markIconGroup);
} // bindRaycaster = function (event, callback) {
// var vector = AVR.screenPosTo3DCoordinate(event, this.vr.container, this.vr.camera);
// //在视点坐标系中形成射线,射线的起点向量是照相机, 射线的方向向量是照相机到点击的点,这个向量应该归一标准化。
// var raycaster = new THREE.Raycaster(this.vr.camera.position, vector.sub(this.vr.camera.position).normalize());
// //射线和模型求交,选中一系列直线
// var intersects = raycaster.intersectObjects(this.vr.scene.children, true);
// if (intersects.length) {
// callback.success(intersects);
// } else {
// callback.empty();
// }
// }
;
var _default = HotSpotHelper;
exports.default = _default;