UNPKG

three-laser-pointer

Version:
1 lines 7.82 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.Laser={})}(this,(function(t){"use strict";var e,r,n,o;r=(t,e,r)=>{r.d(e,{default:()=>b});const n=THREE;function o(t){return o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},o(t)}function i(t){return function(t){if(Array.isArray(t))return a(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||function(t,e){if(t){if("string"==typeof t)return a(t,e);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?a(t,e):void 0}}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function a(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n}function u(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function c(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function l(t,e,r){return e&&c(t.prototype,e),r&&c(t,r),Object.defineProperty(t,"prototype",{writable:!1}),t}function s(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&&f(t,e)}function f(t,e){return f=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t},f(t,e)}function y(t){var e=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}();return function(){var r,n=p(t);if(e){var o=p(this).constructor;r=Reflect.construct(n,arguments,o)}else r=n.apply(this,arguments);return h(this,r)}}function h(t,e){if(e&&("object"===o(e)||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t)}function p(t){return p=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)},p(t)}var v="1.2.3",m=function(t){s(r,t);var e=y(r);function r(t){var o,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:16711680;u(this,r);var a=new n.BufferGeometry;return a.setAttribute("position",new n.BufferAttribute(new Float32Array(3*t),3)),(o=e.call(this,a,new n.LineBasicMaterial({color:i})))._maxPoints=t,o._numPoints=0,o}return l(r,[{key:"setColor",value:function(t){this.material.color.setHex(t)}},{key:"getColor",value:function(){return this.material.color}},{key:"getPoints",value:function(){for(var t=this.geometry.attributes.position.array,e=[],r=0;r<this._numPoints;r++)e.push(new n.Vector3(t[3*r],t[3*r+1],t[3*r+2]));return e}},{key:"updatePoints",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]&&arguments[1];e||(t=d.flattenPoints(t));var r=this.geometry.attributes.position,n=r.count,o=t.length/3;o>n&&(o=n);for(var i=0;i<o;i++)r.array[3*i]=t[3*i],r.array[3*i+1]=t[3*i+1],r.array[3*i+2]=t[3*i+2];r.needsUpdate=!0,this.geometry.setDrawRange(0,o),this._frustumCullingWorkaround(),this._numPoints=o}},{key:"clearPoints",value:function(){this.updatePoints([],!0)}},{key:"updatePointsRandomWalk",value:function(t){this.updatePoints(r._getPointsRandomWalk(t),!0)}},{key:"_frustumCullingWorkaround",value:function(){this.geometry.computeBoundingSphere()}}],[{key:"flattenPoints",value:function(t){return t.map((function(t){return[t.x,t.y,t.z]})).reduce((function(t,e){return t.concat(e)}))}},{key:"_getPointsRandomWalk",value:function(t){for(var e=[],r=0,n=0,o=0,i=0;i<t;i++)e.push(r),e.push(n),e.push(o),r+=2*(Math.random()-.5),n+=2*(Math.random()-.5),o+=2*(Math.random()-.5);return e}}]),r}(n.Line),d=function(t){s(r,t);var e=y(r);function r(){var t,o=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};u(this,r);var i={color:16711680,maxPoints:256,infLength:9999},a=Object.assign({},i,o);return t=e.call(this,a.maxPoints,a.color),console.info("Laser ".concat(v," with THREE r").concat(n.REVISION)),t.version=v,t._src=new n.Vector3(0,0,0),t._raycaster=new n.Raycaster,t._infLen=a.infLength,t._meshes=[],t}return l(r,[{key:"setSource",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;this._src=e?t.clone().applyMatrix4(e.matrixWorld):t.clone()}},{key:"getSource",value:function(){return this._src.clone()}},{key:"_raycast",value:function(t,e,r){var n=this._raycaster.intersectObjects(t,e);if(r){for(var o=0;o<n.length;o++)if(n[o].face!==r)return n[o];return null}return n.length>0?n[0]:null}},{key:"raycast",value:function(t,e,r){var n=arguments.length>3&&void 0!==arguments[3]?arguments[3]:null,o=arguments.length>4&&void 0!==arguments[4]&&arguments[4];return this._raycaster.set(t,e),this._raycast(r,o,n)}},{key:"raycastFromCamera",value:function(t,e,r,o,i,a){var u=arguments.length>6&&void 0!==arguments[6]&&arguments[6],c=new n.Vector2(t/r*2-1,-e/o*2+1);return this._raycaster.setFromCamera(c,i),this._raycast(a,u,null)}},{key:"getMeshesHit",value:function(){return this._meshes}},{key:"point",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;this.updatePoints([this._src.x,this._src.y,this._src.z,t.x,t.y,t.z],!0),this._meshes.length=0,e&&this.material.color.setHex(e)}},{key:"pointWithRaytrace",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[],n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null,o=arguments.length>3&&void 0!==arguments[3]?arguments[3]:16;if(this.point(t,n),!(o<1)){var a=this.getSource(),u=r.direct(a,t),c=this.raycast(a,u,e);if(c){var l=this.computeReflections(t,u,c,e,o);this.updatePoints([a.x,a.y,a.z,t.x,t.y,t.z].concat(i(l)),!0)}}}},{key:"_computeReflectionsRecursive",value:function(t,e,o,i,a){var u=[],c=this;return function t(e,o,l){var s=(new n.Matrix3).getNormalMatrix(l.object.matrixWorld),f=l.face.normal.clone().applyMatrix3(s).normalize(),y=r.reflect(o,f),h=c.raycast(e,y,i,l.face);if(h){var p=h.point;u.push(p.x,p.y,p.z),u.length/3<a&&t(p,y,h)}else{var v=e.clone().add(y.multiplyScalar(c._infLen));u.push(v.x,v.y,v.z)}}(t,e,o),u}},{key:"_computeReflections",value:function(t,e,o,i,a){var u=[];for(this._meshes=[o.object];;){var c=(new n.Matrix3).getNormalMatrix(o.object.matrixWorld),l=o.face.normal.clone().applyMatrix3(c).normalize(),s=r.reflect(e,l),f=this.raycast(t,s,i,o.face);if(f){var y=f.point;if(u.push(y.x,y.y,y.z),this._meshes.push(f.object),u.length/3<a){t=y,e=s,o=f;continue}break}var h=t.clone().add(s.multiplyScalar(this._infLen));u.push(h.x,h.y,h.z);break}return u}},{key:"computeReflections",value:function(t,e,r,n,o){return this._computeReflections(t,e,r,n,o)}}],[{key:"direct",value:function(t,e){return e.clone().sub(t).normalize()}},{key:"reflect",value:function(t,e){return t.clone().sub(e.clone().multiplyScalar(2*t.dot(e)))}}]),r}(m);const b=d},n={d:(t,e)=>{for(var r in e)n.o(e,r)&&!n.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:e[r]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},r(0,o={},n),e=o.default,t.default=e,Object.defineProperty(t,"__esModule",{value:!0})}));