/** * skinview3d (https://github.com/to2mbn/skinview3d) * * MIT License * * Original work Copyright (c) 2014-2018 Kent Rasmussen * Modified work Copyright (c) 2017-2018 Haowei Wen, Sean Boult, Pig Fang and contributors * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("three")):"function"==typeof define&&define.amd?define(["exports","three"],t):t(e.skinview3d={},e.THREE)}(this,function(e,t){"use strict";var n=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},a=function(){function e(e,t){for(var n=0;nb||8*(1-d.dot(l.object.quaternion))>b)&&(l.dispatchEvent(m),h.copy(l.object.position),d.copy(l.object.quaternion),x=!1,!0)}),u.dispose=function(){l.domElement.removeEventListener("contextmenu",Q,!1),l.domElement.removeEventListener("mousedown",D,!1),l.domElement.removeEventListener("wheel",H,!1),l.domElement.removeEventListener("touchstart",Z,!1),l.domElement.removeEventListener("touchend",q,!1),l.domElement.removeEventListener("touchmove",W,!1),document.removeEventListener("mousemove",F,!1),document.removeEventListener("mouseup",Y,!1),window.removeEventListener("keydown",X,!1)};var l=u,m={type:"change"},p={type:"start"},f={type:"end"},g={NONE:-1,ROTATE:0,DOLLY:1,PAN:2,TOUCH_ROTATE:3,TOUCH_DOLLY:4,TOUCH_PAN:5},v=g.NONE,b=1e-6,w=new t.Spherical,y=new t.Spherical,M=1,k=new t.Vector3,x=!1,E=new t.Vector2,O=new t.Vector2,C=new t.Vector2,P=new t.Vector2,I=new t.Vector2,j=new t.Vector2,A=new t.Vector2,T=new t.Vector2,L=new t.Vector2;function _(){return Math.pow(.95,l.zoomSpeed)}function V(e){y.theta-=e}function N(e){y.phi-=e}var U,S=(U=new t.Vector3,function(e,t){U.setFromMatrixColumn(t,0),U.multiplyScalar(-e),k.add(U)}),z=function(){var e=new t.Vector3;return function(t,n){e.setFromMatrixColumn(n,1),e.multiplyScalar(t),k.add(e)}}(),R=function(){var e=new t.Vector3;return function(n,a){var o=l.domElement===document?l.domElement.body:l.domElement;if(l.object instanceof t.PerspectiveCamera){var i=l.object.position;e.copy(i).sub(l.target);var r=e.length();r*=Math.tan(l.object.fov/2*Math.PI/180),S(2*n*r/o.clientHeight,l.object.matrix),z(2*a*r/o.clientHeight,l.object.matrix)}else l.object instanceof t.OrthographicCamera?(S(n*(l.object.right-l.object.left)/l.object.zoom/o.clientWidth,l.object.matrix),z(a*(l.object.top-l.object.bottom)/l.object.zoom/o.clientHeight,l.object.matrix)):(console.warn("WARNING: OrbitControls.js encountered an unknown camera type - pan disabled."),l.enablePan=!1)}}();function G(e){l.object instanceof t.PerspectiveCamera?M/=e:l.object instanceof t.OrthographicCamera?(l.object.zoom=Math.max(l.minZoom,Math.min(l.maxZoom,l.object.zoom*e)),l.object.updateProjectionMatrix(),x=!0):(console.warn("WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled."),l.enableZoom=!1)}function B(e){l.object instanceof t.PerspectiveCamera?M*=e:l.object instanceof t.OrthographicCamera?(l.object.zoom=Math.max(l.minZoom,Math.min(l.maxZoom,l.object.zoom/e)),l.object.updateProjectionMatrix(),x=!0):(console.warn("WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled."),l.enableZoom=!1)}function D(e){if(!1!==l.enabled){switch(e.button){case l.mouseButtons.ORBIT:if(!1===l.enableRotate)return;!function(e){E.set(e.clientX,e.clientY)}(e),v=g.ROTATE;break;case l.mouseButtons.ZOOM:if(!1===l.enableZoom)return;!function(e){A.set(e.clientX,e.clientY)}(e),v=g.DOLLY;break;case l.mouseButtons.PAN:if(!1===l.enablePan)return;!function(e){P.set(e.clientX,e.clientY)}(e),v=g.PAN}e.preventDefault(),v!==g.NONE&&(document.addEventListener("mousemove",F,!1),document.addEventListener("mouseup",Y,!1),l.dispatchEvent(p))}}function F(e){if(!1!==l.enabled){switch(v){case g.ROTATE:if(!1===l.enableRotate)return;!function(e){O.set(e.clientX,e.clientY),C.subVectors(O,E);var t=l.domElement===document?l.domElement.body:l.domElement;V(2*Math.PI*C.x/t.clientWidth*l.rotateSpeed),N(2*Math.PI*C.y/t.clientHeight*l.rotateSpeed),E.copy(O),l.update()}(e);break;case g.DOLLY:if(!1===l.enableZoom)return;!function(e){T.set(e.clientX,e.clientY),L.subVectors(T,A),L.y>0?G(_()):L.y<0&&B(_()),A.copy(T),l.update()}(e);break;case g.PAN:if(!1===l.enablePan)return;!function(e){I.set(e.clientX,e.clientY),j.subVectors(I,P),R(j.x,j.y),P.copy(I),l.update()}(e)}e.preventDefault()}}function Y(e){!1!==l.enabled&&(document.removeEventListener("mousemove",F,!1),document.removeEventListener("mouseup",Y,!1),l.dispatchEvent(f),v=g.NONE)}function H(e){!1===l.enabled||!1===l.enableZoom||v!==g.NONE&&v!==g.ROTATE||(e.preventDefault(),e.stopPropagation(),function(e){e.deltaY<0?B(_()):e.deltaY>0&&G(_()),l.update()}(e),l.dispatchEvent(p),l.dispatchEvent(f))}function X(e){!1!==l.enabled&&!1!==l.enableKeys&&!1!==l.enablePan&&function(e){switch(e.keyCode){case l.keys.UP:R(0,l.keyPanSpeed),l.update();break;case l.keys.BOTTOM:R(0,-l.keyPanSpeed),l.update();break;case l.keys.LEFT:R(l.keyPanSpeed,0),l.update();break;case l.keys.RIGHT:R(-l.keyPanSpeed,0),l.update()}}(e)}function Z(e){if(!1!==l.enabled){switch(e.touches.length){case 1:if(!1===l.enableRotate)return;!function(e){E.set(e.touches[0].pageX,e.touches[0].pageY)}(e),v=g.TOUCH_ROTATE;break;case 2:if(!1===l.enableZoom)return;!function(e){var t=e.touches[0].pageX-e.touches[1].pageX,n=e.touches[0].pageY-e.touches[1].pageY,a=Math.sqrt(t*t+n*n);A.set(0,a)}(e),v=g.TOUCH_DOLLY;break;case 3:if(!1===l.enablePan)return;!function(e){P.set(e.touches[0].pageX,e.touches[0].pageY)}(e),v=g.TOUCH_PAN;break;default:v=g.NONE}v!==g.NONE&&l.dispatchEvent(p)}}function W(e){if(!1!==l.enabled){switch(e.touches.length){case 1:if(!1===l.enableRotate)return;if(v!==g.TOUCH_ROTATE)return;!function(e){O.set(e.touches[0].pageX,e.touches[0].pageY),C.subVectors(O,E);var t=l.domElement===document?l.domElement.body:l.domElement;V(2*Math.PI*C.x/t.clientWidth*l.rotateSpeed),N(2*Math.PI*C.y/t.clientHeight*l.rotateSpeed),E.copy(O),l.update()}(e);break;case 2:if(!1===l.enableZoom)return;if(v!==g.TOUCH_DOLLY)return;!function(e){var t=e.touches[0].pageX-e.touches[1].pageX,n=e.touches[0].pageY-e.touches[1].pageY,a=Math.sqrt(t*t+n*n);T.set(0,a),L.subVectors(T,A),L.y>0?B(_()):L.y<0&&G(_()),A.copy(T),l.update()}(e);break;case 3:if(!1===l.enablePan)return;if(v!==g.TOUCH_PAN)return;!function(e){I.set(e.touches[0].pageX,e.touches[0].pageY),j.subVectors(I,P),R(j.x,j.y),P.copy(I),l.update()}(e);break;default:v=g.NONE}e.preventDefault(),e.stopPropagation()}}function q(e){!1!==l.enabled&&(l.dispatchEvent(f),v=g.NONE)}function Q(e){!1!==l.enabled&&!1!==l.enablePan&&e.preventDefault()}return l.domElement.addEventListener("contextmenu",Q,!1),l.domElement.addEventListener("mousedown",D,!1),l.domElement.addEventListener("wheel",H,!1),l.domElement.addEventListener("touchstart",Z,!1),l.domElement.addEventListener("touchend",q,!1),l.domElement.addEventListener("touchmove",W,!1),window.addEventListener("keydown",X,!1),u.update(),u}return o(a,e),a}(t.EventDispatcher);function f(e,t,n){if(e instanceof v)e.play(t,n);else{if(!(e instanceof Function))throw"Not an animation: "+e;e(t,n)}}var g=function(){function e(t){n(this,e),this.animation=t,this.paused=this._paused=!1,this.speed=this._speed=1,this._lastChange=null,this._lastChangeX=null}return a(e,[{key:"play",value:function(e,t){if(null===this._lastChange)this._lastChange=t,this._lastChangeX=0;else if(this.paused!==this._paused||this.speed!==this._speed){var n=t-this._lastChange;!1===this._paused&&(this._lastChangeX+=n*this._speed),this._paused=this.paused,this._speed=this.speed,this._lastChange=t}if(!1===this.paused){var a=t-this._lastChange,o=this._lastChangeX+this.speed*a;f(this.animation,e,o)}}},{key:"reset",value:function(){this._lastChange=null}}]),e}(),v=function(){function e(){n(this,e),this.handles=new Set}return a(e,[{key:"add",value:function(e){var t=this,n=new g(e);return n.remove=function(){return t.handles.delete(n)},this.handles.add(n),n}},{key:"play",value:function(e,t){this.handles.forEach(function(n){return n.play(e,t)})}}]),e}();var b=function(){function e(a){var o=this;n(this,e),this.domElement=a.domElement,this.animation=a.animation||null,this.animationPaused=!1,this.animationTime=0,this.disposed=!1,this.skinImg=new Image,this.skinCanvas=document.createElement("canvas"),this.skinTexture=new t.Texture(this.skinCanvas),this.skinTexture.magFilter=t.NearestFilter,this.skinTexture.minFilter=t.NearestMipMapNearestFilter,this.capeImg=new Image,this.capeCanvas=document.createElement("canvas"),this.capeTexture=new t.Texture(this.capeCanvas),this.capeTexture.magFilter=t.NearestFilter,this.capeTexture.minFilter=t.NearestMipMapNearestFilter,this.layer1Material=new t.MeshBasicMaterial({map:this.skinTexture,side:t.FrontSide}),this.layer2Material=new t.MeshBasicMaterial({map:this.skinTexture,transparent:!0,opacity:1,side:t.DoubleSide}),this.capeMaterial=new t.MeshBasicMaterial({map:this.capeTexture}),this.scene=new t.Scene,this.camera=new t.PerspectiveCamera(40),this.camera.position.y=-12,this.camera.position.z=60,this.renderer=new t.WebGLRenderer({angleRot:!0,alpha:!0,antialias:!1}),this.renderer.setSize(300,300),this.renderer.context.getShaderInfoLog=function(){return""},this.domElement.appendChild(this.renderer.domElement),this.playerObject=new m(!0===a.slim,this.layer1Material,this.layer2Material,this.capeMaterial),this.scene.add(this.playerObject),this.skinImg.crossOrigin="",this.skinImg.onerror=function(){return console.error("Failed loading "+o.skinImg.src)},this.skinImg.onload=function(){var e=!1;if(o.skinImg.width!==o.skinImg.height){if(o.skinImg.width!==2*o.skinImg.height)return void console.error("Bad skin size");e=!0}var t=o.skinCanvas.getContext("2d");if(e){var n=o.skinImg.width;o.skinCanvas.width=n,o.skinCanvas.height=n,t.clearRect(0,0,n,n),t.drawImage(o.skinImg,0,0,n,n/2),function(e,t){var n=t/64,a=function(e,t,a,o,i,r,s,c){return function(e,t,n,a,o,i,r,s){var c=e.getImageData(t,n,a,o);if(s)for(var h=0;h