@react-three/drei
Version:
useful add-ons for react-three-fiber
2 lines (1 loc) • 4.79 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@babel/runtime/helpers/extends"),r=require("@react-three/fiber"),t=require("react"),i=require("three"),a=require("./AxisArrow.cjs.js"),n=require("./AxisRotator.cjs.js"),o=require("./PlaneSlider.cjs.js"),c=require("./ScalingSphere.cjs.js"),l=require("./context.cjs.js"),s=require("../../core/calculateScaleFactor.cjs.js");function u(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function d(e){if(e&&e.__esModule)return e;var r=Object.create(null);return e&&Object.keys(e).forEach((function(t){if("default"!==t){var i=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(r,t,i.get?i:{enumerable:!0,get:function(){return e[t]}})}})),r.default=e,Object.freeze(r)}require("../../core/Line.cjs.js"),require("three-stdlib"),require("../Html.cjs.js"),require("react-dom/client");var m=u(e),x=d(t),p=d(i);const f=new p.Matrix4,y=new p.Matrix4,w=new p.Matrix4,g=new p.Matrix4,b=new p.Matrix4,E=new p.Matrix4,j=new p.Matrix4,S=new p.Matrix4,h=new p.Matrix4,v=new p.Box3,M=new p.Box3,A=new p.Vector3,q=new p.Vector3,R=new p.Vector3,V=new p.Vector3,P=new p.Vector3,W=new p.Vector3(1,0,0),D=new p.Vector3(0,1,0),O=new p.Vector3(0,0,1),C=x.forwardRef((({enabled:e=!0,matrix:t,onDragStart:i,onDrag:u,onDragEnd:d,autoTransform:C=!0,anchor:L,disableAxes:B=!1,disableSliders:F=!1,disableRotations:z=!1,disableScaling:T=!1,activeAxes:_=[!0,!0,!0],offset:k=[0,0,0],rotation:H=[0,0,0],scale:I=1,lineWidth:U=4,fixed:G=!1,translationLimits:J,rotationLimits:K,scaleLimits:N,depthTest:Q=!0,renderOrder:X=500,axisColors:Y=["#ff2060","#20df80","#2080ff"],hoveredColor:Z="#ffff40",annotations:$=!1,annotationsClass:ee,opacity:re=1,visible:te=!0,userData:ie,children:ae,...ne},oe)=>{const ce=r.useThree((e=>e.invalidate)),le=x.useRef(null),se=x.useRef(null),ue=x.useRef(null),de=x.useRef(null),me=x.useRef([0,0,0]),xe=x.useRef(new p.Vector3(1,1,1)),pe=x.useRef(new p.Vector3(1,1,1));x.useLayoutEffect((()=>{L&&(de.current.updateWorldMatrix(!0,!0),g.copy(de.current.matrixWorld).invert(),v.makeEmpty(),de.current.traverse((e=>{e.geometry&&(e.geometry.boundingBox||e.geometry.computeBoundingBox(),E.copy(e.matrixWorld).premultiply(g),M.copy(e.geometry.boundingBox),M.applyMatrix4(E),v.union(M))})),A.copy(v.max).add(v.min).multiplyScalar(.5),q.copy(v.max).sub(v.min).multiplyScalar(.5),R.copy(q).multiply(new p.Vector3(...L)).add(A),V.set(...k).add(R),ue.current.position.copy(V),ce())}));const fe=x.useMemo((()=>({onDragStart:e=>{f.copy(se.current.matrix),y.copy(se.current.matrixWorld),i&&i(e),ce()},onDrag:e=>{w.copy(le.current.matrixWorld),g.copy(w).invert(),b.copy(y).premultiply(e),E.copy(b).premultiply(g),j.copy(f).invert(),S.copy(E).multiply(j),C&&se.current.matrix.copy(E),u&&u(E,S,b,e),ce()},onDragEnd:()=>{d&&d(),ce()},translation:me,translationLimits:J,rotationLimits:K,axisColors:Y,hoveredColor:Z,opacity:re,scale:I,lineWidth:U,fixed:G,depthTest:Q,renderOrder:X,userData:ie,annotations:$,annotationsClass:ee})),[i,u,d,me,J,K,N,Q,I,U,G,...Y,Z,re,ie,C,$,ee]),ye=new p.Vector3;return r.useFrame((e=>{if(G){const r=s.calculateScaleFactor(ue.current.getWorldPosition(ye),I,e.camera,e.size);xe.current.setScalar(r)}t&&t instanceof p.Matrix4&&(se.current.matrix=t),se.current.updateWorldMatrix(!0,!0),h.makeRotationFromEuler(ue.current.rotation).setPosition(ue.current.position).premultiply(se.current.matrixWorld),pe.current.setFromMatrixScale(h),P.copy(xe.current).divide(pe.current),(Math.abs(ue.current.scale.x-P.x)>1e-4||Math.abs(ue.current.scale.y-P.y)>1e-4||Math.abs(ue.current.scale.z-P.z)>1e-4)&&(ue.current.scale.copy(P),e.invalidate())})),x.useImperativeHandle(oe,(()=>se.current),[]),x.createElement(l.context.Provider,{value:fe},x.createElement("group",{ref:le},x.createElement("group",m.default({ref:se,matrix:t,matrixAutoUpdate:!1},ne),x.createElement("group",{visible:te,ref:ue,position:k,rotation:H},e&&x.createElement(x.Fragment,null,!B&&_[0]&&x.createElement(a.AxisArrow,{axis:0,direction:W}),!B&&_[1]&&x.createElement(a.AxisArrow,{axis:1,direction:D}),!B&&_[2]&&x.createElement(a.AxisArrow,{axis:2,direction:O}),!F&&_[0]&&_[1]&&x.createElement(o.PlaneSlider,{axis:2,dir1:W,dir2:D}),!F&&_[0]&&_[2]&&x.createElement(o.PlaneSlider,{axis:1,dir1:O,dir2:W}),!F&&_[2]&&_[1]&&x.createElement(o.PlaneSlider,{axis:0,dir1:D,dir2:O}),!z&&_[0]&&_[1]&&x.createElement(n.AxisRotator,{axis:2,dir1:W,dir2:D}),!z&&_[0]&&_[2]&&x.createElement(n.AxisRotator,{axis:1,dir1:O,dir2:W}),!z&&_[2]&&_[1]&&x.createElement(n.AxisRotator,{axis:0,dir1:D,dir2:O}),!T&&_[0]&&x.createElement(c.ScalingSphere,{axis:0,direction:W}),!T&&_[1]&&x.createElement(c.ScalingSphere,{axis:1,direction:D}),!T&&_[2]&&x.createElement(c.ScalingSphere,{axis:2,direction:O}))),x.createElement("group",{ref:de},ae))))}));exports.PivotControls=C;