Skip to content

Instantly share code, notes, and snippets.

@CodyJasonBennett
Created August 12, 2022 20:38
Show Gist options
  • Save CodyJasonBennett/cddf54565af011fe567498ee727ab216 to your computer and use it in GitHub Desktop.
Save CodyJasonBennett/cddf54565af011fe567498ee727ab216 to your computer and use it in GitHub Desktop.
four.js UMD
(function(E,I){typeof exports=="object"&&typeof module<"u"?I(exports):typeof define=="function"&&define.amd?define(["exports"],I):(E=typeof globalThis<"u"?globalThis:E||self,I(E.FOUR={}))})(this,function(E){"use strict";var I=1e-6,R=typeof Float32Array<"u"?Float32Array:Array;Math.hypot||(Math.hypot=function(){for(var r=0,i=arguments.length;i--;)r+=arguments[i]*arguments[i];return Math.sqrt(r)});function G(){var r=new R(9);return R!=Float32Array&&(r[1]=0,r[2]=0,r[3]=0,r[5]=0,r[6]=0,r[7]=0),r[0]=1,r[4]=1,r[8]=1,r}function j(r,i){var t=i[0],e=i[1],a=i[2],n=i[3],s=i[4],l=i[5],h=i[6],c=i[7],x=i[8],w=i[9],A=i[10],m=i[11],M=i[12],p=i[13],y=i[14],b=i[15],S=t*l-e*s,d=t*h-a*s,f=t*c-n*s,g=e*h-a*l,v=e*c-n*l,T=a*c-n*h,V=x*p-w*M,_=x*y-A*M,u=x*b-m*M,U=w*y-A*p,z=w*b-m*p,F=A*b-m*y,o=S*F-d*z+f*U+g*u-v*_+T*V;return o?(o=1/o,r[0]=(l*F-h*z+c*U)*o,r[1]=(h*u-s*F-c*_)*o,r[2]=(s*z-l*u+c*V)*o,r[3]=(a*z-e*F-n*U)*o,r[4]=(t*F-a*u+n*_)*o,r[5]=(e*u-t*z-n*V)*o,r[6]=(p*T-y*v+b*g)*o,r[7]=(y*f-M*T-b*d)*o,r[8]=(M*v-p*f+b*S)*o,r):null}function L(){var r=new R(16);return R!=Float32Array&&(r[1]=0,r[2]=0,r[3]=0,r[4]=0,r[6]=0,r[7]=0,r[8]=0,r[9]=0,r[11]=0,r[12]=0,r[13]=0,r[14]=0),r[0]=1,r[5]=1,r[10]=1,r[15]=1,r}function P(r,i){return r[0]=i[0],r[1]=i[1],r[2]=i[2],r[3]=i[3],r[4]=i[4],r[5]=i[5],r[6]=i[6],r[7]=i[7],r[8]=i[8],r[9]=i[9],r[10]=i[10],r[11]=i[11],r[12]=i[12],r[13]=i[13],r[14]=i[14],r[15]=i[15],r}function W(r,i){var t=i[0],e=i[1],a=i[2],n=i[3],s=i[4],l=i[5],h=i[6],c=i[7],x=i[8],w=i[9],A=i[10],m=i[11],M=i[12],p=i[13],y=i[14],b=i[15],S=t*l-e*s,d=t*h-a*s,f=t*c-n*s,g=e*h-a*l,v=e*c-n*l,T=a*c-n*h,V=x*p-w*M,_=x*y-A*M,u=x*b-m*M,U=w*y-A*p,z=w*b-m*p,F=A*b-m*y,o=S*F-d*z+f*U+g*u-v*_+T*V;return o?(o=1/o,r[0]=(l*F-h*z+c*U)*o,r[1]=(a*z-e*F-n*U)*o,r[2]=(p*T-y*v+b*g)*o,r[3]=(A*v-w*T-m*g)*o,r[4]=(h*u-s*F-c*_)*o,r[5]=(t*F-a*u+n*_)*o,r[6]=(y*f-M*T-b*d)*o,r[7]=(x*T-A*f+m*d)*o,r[8]=(s*z-l*u+c*V)*o,r[9]=(e*u-t*z-n*V)*o,r[10]=(M*v-p*f+b*S)*o,r[11]=(w*f-x*v-m*S)*o,r[12]=(l*_-s*U-h*V)*o,r[13]=(t*U-e*_+a*V)*o,r[14]=(p*d-M*g-y*S)*o,r[15]=(x*g-w*d+A*S)*o,r):null}function $(r,i,t){var e=i[0],a=i[1],n=i[2],s=i[3],l=i[4],h=i[5],c=i[6],x=i[7],w=i[8],A=i[9],m=i[10],M=i[11],p=i[12],y=i[13],b=i[14],S=i[15],d=t[0],f=t[1],g=t[2],v=t[3];return r[0]=d*e+f*l+g*w+v*p,r[1]=d*a+f*h+g*A+v*y,r[2]=d*n+f*c+g*m+v*b,r[3]=d*s+f*x+g*M+v*S,d=t[4],f=t[5],g=t[6],v=t[7],r[4]=d*e+f*l+g*w+v*p,r[5]=d*a+f*h+g*A+v*y,r[6]=d*n+f*c+g*m+v*b,r[7]=d*s+f*x+g*M+v*S,d=t[8],f=t[9],g=t[10],v=t[11],r[8]=d*e+f*l+g*w+v*p,r[9]=d*a+f*h+g*A+v*y,r[10]=d*n+f*c+g*m+v*b,r[11]=d*s+f*x+g*M+v*S,d=t[12],f=t[13],g=t[14],v=t[15],r[12]=d*e+f*l+g*w+v*p,r[13]=d*a+f*h+g*A+v*y,r[14]=d*n+f*c+g*m+v*b,r[15]=d*s+f*x+g*M+v*S,r}function K(r,i,t,e){var a=i[0],n=i[1],s=i[2],l=i[3],h=a+a,c=n+n,x=s+s,w=a*h,A=a*c,m=a*x,M=n*c,p=n*x,y=s*x,b=l*h,S=l*c,d=l*x,f=e[0],g=e[1],v=e[2];return r[0]=(1-(M+y))*f,r[1]=(A+d)*f,r[2]=(m-S)*f,r[3]=0,r[4]=(A-d)*g,r[5]=(1-(w+y))*g,r[6]=(p+b)*g,r[7]=0,r[8]=(m+S)*v,r[9]=(p-b)*v,r[10]=(1-(w+M))*v,r[11]=0,r[12]=t[0],r[13]=t[1],r[14]=t[2],r[15]=1,r}function X(r,i,t,e,a){var n=1/Math.tan(i/2),s;return r[0]=n/t,r[1]=0,r[2]=0,r[3]=0,r[4]=0,r[5]=n,r[6]=0,r[7]=0,r[8]=0,r[9]=0,r[11]=-1,r[12]=0,r[13]=0,r[15]=0,a!=null&&a!==1/0?(s=1/(e-a),r[10]=(a+e)*s,r[14]=2*a*e*s):(r[10]=-1,r[14]=-2*e),r}function O(){var r=new R(3);return R!=Float32Array&&(r[0]=0,r[1]=0,r[2]=0),r}function J(r){var i=r[0],t=r[1],e=r[2];return Math.hypot(i,t,e)}function q(r,i,t){var e=new R(3);return e[0]=r,e[1]=i,e[2]=t,e}function Q(r,i,t,e){return r[0]=i,r[1]=t,r[2]=e,r}function Z(r,i){var t=i[0],e=i[1],a=i[2],n=t*t+e*e+a*a;return n>0&&(n=1/Math.sqrt(n)),r[0]=i[0]*n,r[1]=i[1]*n,r[2]=i[2]*n,r}function k(r,i){return r[0]*i[0]+r[1]*i[1]+r[2]*i[2]}function C(r,i,t){var e=i[0],a=i[1],n=i[2],s=t[0],l=t[1],h=t[2];return r[0]=a*h-n*l,r[1]=n*s-e*h,r[2]=e*l-a*s,r}var rr=J;(function(){var r=O();return function(i,t,e,a,n,s){var l,h;for(t||(t=3),e||(e=0),a?h=Math.min(a*t+e,i.length):h=i.length,l=e;l<h;l+=t)r[0]=i[l],r[1]=i[l+1],r[2]=i[l+2],n(r,r,s),i[l]=r[0],i[l+1]=r[1],i[l+2]=r[2];return i}})();function ir(){var r=new R(4);return R!=Float32Array&&(r[0]=0,r[1]=0,r[2]=0,r[3]=0),r}function er(r,i){var t=i[0],e=i[1],a=i[2],n=i[3],s=t*t+e*e+a*a+n*n;return s>0&&(s=1/Math.sqrt(s)),r[0]=t*s,r[1]=e*s,r[2]=a*s,r[3]=n*s,r}(function(){var r=ir();return function(i,t,e,a,n,s){var l,h;for(t||(t=4),e||(e=0),a?h=Math.min(a*t+e,i.length):h=i.length,l=e;l<h;l+=t)r[0]=i[l],r[1]=i[l+1],r[2]=i[l+2],r[3]=i[l+3],n(r,r,s),i[l]=r[0],i[l+1]=r[1],i[l+2]=r[2],i[l+3]=r[3];return i}})();function N(){var r=new R(4);return R!=Float32Array&&(r[0]=0,r[1]=0,r[2]=0),r[3]=1,r}function tr(r,i,t){t=t*.5;var e=Math.sin(t);return r[0]=e*i[0],r[1]=e*i[1],r[2]=e*i[2],r[3]=Math.cos(t),r}function B(r,i,t,e){var a=i[0],n=i[1],s=i[2],l=i[3],h=t[0],c=t[1],x=t[2],w=t[3],A,m,M,p,y;return m=a*h+n*c+s*x+l*w,m<0&&(m=-m,h=-h,c=-c,x=-x,w=-w),1-m>I?(A=Math.acos(m),M=Math.sin(A),p=Math.sin((1-e)*A)/M,y=Math.sin(e*A)/M):(p=1-e,y=e),r[0]=p*a+y*h,r[1]=p*n+y*c,r[2]=p*s+y*x,r[3]=p*l+y*w,r}function ar(r,i){var t=i[0]+i[4]+i[8],e;if(t>0)e=Math.sqrt(t+1),r[3]=.5*e,e=.5/e,r[0]=(i[5]-i[7])*e,r[1]=(i[6]-i[2])*e,r[2]=(i[1]-i[3])*e;else{var a=0;i[4]>i[0]&&(a=1),i[8]>i[a*3+a]&&(a=2);var n=(a+1)%3,s=(a+2)%3;e=Math.sqrt(i[a*3+a]-i[n*3+n]-i[s*3+s]+1),r[a]=.5*e,e=.5/e,r[3]=(i[n*3+s]-i[s*3+n])*e,r[n]=(i[n*3+a]+i[a*3+n])*e,r[s]=(i[s*3+a]+i[a*3+s])*e}return r}var H=er;(function(){var r=O(),i=q(1,0,0),t=q(0,1,0);return function(e,a,n){var s=k(a,n);return s<-.999999?(C(r,i,a),rr(r)<1e-6&&C(r,t,a),Z(r,r),tr(e,r,Math.PI),e):s>.999999?(e[0]=0,e[1]=0,e[2]=0,e[3]=1,e):(C(r,a,n),e[0]=r[0],e[1]=r[1],e[2]=r[2],e[3]=1+s,H(e,e))}})(),function(){var r=N(),i=N();return function(t,e,a,n,s,l){return B(r,e,s,l),B(i,a,n,l),B(t,r,i,2*l*(1-l)),t}}(),function(){var r=G();return function(i,t,e,a){return r[0]=e[0],r[3]=e[1],r[6]=e[2],r[1]=a[0],r[4]=a[1],r[7]=a[2],r[2]=-t[0],r[5]=-t[1],r[8]=-t[2],H(i,ar(i,r))}}();class D{constructor(){this.matrix=L(),this.quaternion=N(),this.position=O(),this.scale=Q(O(),1,1,1),this.children=[],this.parent=null,this.matrixAutoUpdate=!0,this.visible=!0}updateMatrix(){this.matrixAutoUpdate&&K(this.matrix,this.quaternion,this.position,this.scale),this.parent&&$(this.matrix,this.matrix,this.parent.matrix);for(const i of this.children)i.updateMatrix()}add(...i){for(const t of i)this.children.push(t),t.parent=this}remove(...i){for(const t of i){const e=this.children.indexOf(t);e!==-1&&this.children.splice(e,1),t.parent=null}}traverse(i){if(!i(this))for(const t of this.children)t.traverse(i)}}class nr extends D{constructor(i=45,t=1,e=.1,a=1e3){super(),this.projectionMatrix=L(),this.viewMatrix=L(),this.fov=i,this.aspect=t,this.near=e,this.far=a}updateMatrix(){super.updateMatrix(),this.matrixAutoUpdate&&X(this.projectionMatrix,this.fov,this.aspect,this.near,this.far),P(this.viewMatrix,this.matrix),W(this.viewMatrix,this.viewMatrix)}}class sr{constructor(i){this.attributes=i}}class lr{constructor(i){this.uniforms={},Object.assign(this,i)}}class Y extends D{constructor(i,t){super(),this.modelViewMatrix=L(),this.normalMatrix=G(),this.geometry=i,this.material=t}}const hr=(r,i=0)=>r.replace(/^/gm,()=>`${i++}:`);class cr{constructor(i=document.createElement("canvas")){this.autoClear=!0,this._compiled=new WeakMap,this.canvas=i,this.gl=i.getContext("webgl2",{antialias:!0,powerPreference:"high-performance"}),this.gl.enable(this.gl.CULL_FACE),this.gl.cullFace(this.gl.BACK),this.gl.enable(this.gl.DEPTH_TEST),this.gl.depthFunc(this.gl.LESS)}setSize(i,t){this.canvas.width=i,this.canvas.height=t,this.gl.viewport(0,0,i,t)}setUniform(i,t,e){const a=this.gl.getUniformLocation(i,t);if(a!==-1){if(typeof e=="number")return this.gl.uniform1f(a,e);switch(e.length){case 2:return this.gl.uniform2fv(a,e);case 3:return this.gl.uniform3fv(a,e);case 4:return this.gl.uniform4fv(a,e);case 9:return this.gl.uniformMatrix3fv(a,!1,e);case 16:return this.gl.uniformMatrix4fv(a,!1,e)}}}compile(i,t){i.material.uniforms.modelMatrix=i.matrix,t&&(i.material.uniforms.projectionMatrix=t.projectionMatrix,i.material.uniforms.viewMatrix=t.viewMatrix,i.material.uniforms.normalMatrix=i.normalMatrix,i.material.uniforms.modelViewMatrix=i.modelViewMatrix,P(i.modelViewMatrix,t.viewMatrix),$(i.modelViewMatrix,i.modelViewMatrix,i.matrix),P(i.modelViewMatrix,t.viewMatrix),$(i.modelViewMatrix,i.modelViewMatrix,i.matrix),j(i.normalMatrix,i.modelViewMatrix));let e=this._compiled.get(i);if(!e){const a=this.gl.createProgram(),n=this.gl.createVertexArray(),s=this.gl.createShader(this.gl.VERTEX_SHADER);this.gl.shaderSource(s,i.material.vertex),this.gl.compileShader(s),this.gl.attachShader(a,s);const l=this.gl.createShader(this.gl.FRAGMENT_SHADER);this.gl.shaderSource(l,i.material.fragment),this.gl.compileShader(l),this.gl.attachShader(a,l),this.gl.linkProgram(a);for(const c of[s,l]){const x=this.gl.getShaderInfoLog(c);if(x)throw`Error compiling shader: ${x}
${hr(this.gl.getShaderSource(c))}`}if(this.gl.getProgramInfoLog(a))throw`Error compiling program: ${this.gl.getProgramInfoLog(a)}`;this.gl.deleteShader(s),this.gl.deleteShader(l),this.gl.bindVertexArray(n);for(const c in i.geometry.attributes){const{data:x,size:w}=i.geometry.attributes[c],A=this.gl.createBuffer(),m=c==="index"?this.gl.ELEMENT_ARRAY_BUFFER:this.gl.ARRAY_BUFFER;this.gl.bindBuffer(m,A),this.gl.bufferData(m,x,this.gl.STATIC_DRAW);const M=this.gl.getAttribLocation(a,c);M!==-1&&(this.gl.enableVertexAttribArray(M),this.gl.vertexAttribPointer(M,w,this.gl.FLOAT,!1,0,0))}e={program:a,VAO:n},this._compiled.set(i,e)}this.gl.bindVertexArray(e.VAO),this.gl.useProgram(e.program);for(const a in i.material.uniforms)this.setUniform(e.program,a,i.material.uniforms[a]);return e}clear(i=this.gl.COLOR_BUFFER_BIT|this.gl.DEPTH_BUFFER_BIT){this.gl.clear(i)}render(i,t){this.autoClear&&this.clear(),t?.updateMatrix(),i.updateMatrix(),i.traverse(e=>{if(!e.visible)return!0;if(!(e instanceof Y))return;this.compile(e,t);const{index:a,position:n}=e.geometry.attributes;a?this.gl.drawElements(this.gl.TRIANGLES,a.data.length/a.size,this.gl.UNSIGNED_INT,0):this.gl.drawArrays(this.gl.TRIANGLES,0,n.data.length/n.size)})}}E.Camera=nr,E.Geometry=sr,E.Material=lr,E.Mesh=Y,E.Object3D=D,E.Renderer=cr,Object.defineProperties(E,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment