@derschmale/threejs-googly-eyes
Version:
Googly Eyes for ThreeJS
2 lines (1 loc) • 2.5 kB
JavaScript
var GooglyEyes=function(t){"use strict";const i=new t.Vector3;class e{constructor(i,e,s){this._target=i,this._eyeRadius=e,this._irisRadius=s,this._worldPos=i.getWorldPosition(new t.Vector3),this._oldPos=this._worldPos.clone()}update(t,e,s){const r=this._target.parent,h=this._worldPos;i.subVectors(h,this._oldPos),this._oldPos.copy(h),i.y-=e*t,i.multiplyScalar(1-s),h.add(i),r.worldToLocal(h),h.z=0;const a=Math.sqrt(h.x*h.x+h.y*h.y),n=this._eyeRadius-this._irisRadius;a>n&&h.multiplyScalar(n/a),this._target.position.copy(h),r.localToWorld(h)}}class s extends t.Group{constructor(i=.02,s=.05,r,h=.1,a){super(),this._gravity=.981,this._friction=.01,a=a||t.MeshStandardMaterial,r=r||.5*i;const n=new t.SphereBufferGeometry(i,6,12,Math.PI,2*Math.PI),o=new t.SphereBufferGeometry(r,6,12),_=new t.SphereBufferGeometry(i,6,12,0,Math.PI);n.scale(1,1,-.05),o.scale(1,1,.1),_.scale(1,1,.25),n.computeVertexNormals(),o.computeVertexNormals(),_.computeVertexNormals(),this._eyeWhiteMaterial=new a({color:16777215,roughness:.3}),this._eyeBlackMaterial=new a({color:328965,roughness:.2}),this._transparentMaterial=new a({color:0,transparent:!0,blending:t.AdditiveBlending,roughness:.01}),this._whiteLeft=new t.Mesh(n,this._eyeWhiteMaterial),this._whiteRight=new t.Mesh(n,this._eyeWhiteMaterial),this._whiteLeft.position.x=.5*-s,this._whiteRight.position.x=.5*s,this._whiteLeft.rotation.y=h,this._whiteRight.rotation.y=-h,this.add(this._whiteLeft),this.add(this._whiteRight),this._irisLeft=new t.Mesh(o,this._eyeBlackMaterial),this._irisRight=new t.Mesh(o,this._eyeBlackMaterial),this._whiteLeft.add(this._irisLeft),this._whiteRight.add(this._irisRight),this._transLeft=new t.Mesh(_,this._transparentMaterial),this._transRight=new t.Mesh(_,this._transparentMaterial),this._whiteLeft.add(this._transLeft),this._whiteRight.add(this._transRight),this._left=new e(this._irisLeft,i,r),this._right=new e(this._irisRight,i,r)}get eyeWhiteMaterial(){return this._eyeWhiteMaterial}set eyeWhiteMaterial(t){this._eyeWhiteMaterial=t,this._whiteLeft.material=t,this._whiteRight.material=t}get eyeBlackMaterial(){return this._eyeBlackMaterial}get transparentMaterial(){return this._transparentMaterial}set transparentMaterial(t){this._transparentMaterial=t}get gravity(){return this._gravity}set gravity(t){this._gravity=t}get friction(){return this._friction}set friction(t){this._friction=t}update(t){this._left.update(t,this._gravity,this._friction),this._right.update(t,this._gravity,this._friction)}}return s}(THREE);