UNPKG

@blooooork/three-ui

Version:

Professional 3D user interface components for Three.js applications

2 lines (1 loc) 3.32 kB
(function(n,d){typeof exports=="object"&&typeof module<"u"?d(exports,require("three")):typeof define=="function"&&define.amd?define(["exports","three"],d):(n=typeof globalThis<"u"?globalThis:n||self,d(n.ThreeUI={},n.THREE))})(this,function(n,d){"use strict";function y(a){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(a){for(const t in a)if(t!=="default"){const s=Object.getOwnPropertyDescriptor(a,t);Object.defineProperty(e,t,s.get?s:{enumerable:!0,get:()=>a[t]})}}return e.default=a,Object.freeze(e)}const o=y(d);class p{constructor(e,t){this.parent=e,this.camera=t,this.object=new o.Object3D,this.parent.add(this.object)}dispose(){this.parent.remove(this.object),this.object.traverse(e=>{e.geometry&&e.geometry.dispose(),e.material&&(Array.isArray(e.material)?e.material.forEach(t=>t.dispose()):e.material.dispose())})}update(){}}class M extends p{constructor(e,t,s={}){super(e,t);const{width:i=10,height:c=2.75,depth:h=.2,color:r=16777215,wireframe:l=!0,position:f=new o.Vector3(0,9,0)}=s,u=new o.BoxGeometry(i,c,h),m=new o.MeshBasicMaterial({color:r,wireframe:l,transparent:!0,opacity:l?1:.8});this.titleMesh=new o.Mesh(u,m),this.titleMesh.position.copy(f),this.object.add(this.titleMesh)}setTitle(e){console.log("Title set to:",e)}resize(e){this.titleMesh.geometry.dispose(),this.titleMesh.geometry=new o.BoxGeometry(e,this.titleMesh.geometry.parameters.height,this.titleMesh.geometry.parameters.depth)}}class g extends p{constructor(e,t,s={}){super(e,t);const{radius:i=.44,spacing:c=3.5,links:h=[]}=s;this.links=new Map,h.forEach((r,l)=>{const f=new o.CircleGeometry(i),u=new o.MeshBasicMaterial({map:r.texture,transparent:!0,depthTest:!1}),m=new o.Mesh(f,u);m.position.x=i*(c*l),m.userData.url=r.url,this.links.set(r.id,m),this.object.add(m)})}setPosition(e,t,s){this.object.position.set(e,t,s)}onClick(e){const t=new o.Raycaster;t.setFromCamera(e,this.camera);const s=t.intersectObjects(Array.from(this.links.values()));if(s.length>0){const i=s[0].object.userData.url;i&&window.open(i,"_blank")}}}class w extends p{constructor(e,t,s={}){super(e,t);const{width:i=10,height:c=5,color:h=16777215,opacity:r=.8}=s,l=new o.PlaneGeometry(i,c),f=new o.MeshBasicMaterial({color:h,transparent:!0,opacity:r,depthTest:!1});this.frameMesh=new o.Mesh(l,f),this.object.add(this.frameMesh)}setText(e){console.log("Text set to:",e)}setSize(e,t){this.frameMesh.geometry.dispose(),this.frameMesh.geometry=new o.PlaneGeometry(e,t)}animate(e,t=1e3){const s=this.object.position.clone(),i=Date.now(),c=()=>{const h=Date.now(),r=Math.min((h-i)/t,1);this.object.position.lerpVectors(s,e,r),r<1&&requestAnimationFrame(c)};c()}}class b extends p{constructor(e,t,s={}){super(e,t);const{width:i=1,height:c=1,color:h=16777215,hoverColor:r=65280}=s,l=new o.PlaneGeometry(i,c),f=new o.MeshBasicMaterial({color:h,transparent:!0,opacity:.8,depthTest:!1});this.buttonMesh=new o.Mesh(l,f),this.object.add(this.buttonMesh),this.originalColor=h,this.hoverColor=r,this.isHidden=!1}onHover(e){this.buttonMesh.material.color.setHex(e?this.hoverColor:this.originalColor)}onClick(e){this.isHidden=!this.isHidden,e&&e(this.isHidden)}setPosition(e,t,s){this.object.position.set(e,t,s)}}n.HideButton=b,n.LinkContainer=g,n.TextFrame=w,n.TitleBlock=M,n.UIComponent=p,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})});