From 3b43c79679e0fb557f98b99df7617a3a06796ff5 Mon Sep 17 00:00:00 2001 From: alteredq Date: Sat, 2 Apr 2011 01:32:53 +0200 Subject: [PATCH] Added possibility of enforcing vertical angle constraint to QuakeCamera. --- build/Three.js | 15 +++++++------- build/custom/ThreeExtras.js | 15 +++++++------- examples/webgl_geometry_minecraft_ao.html | 1 + src/extras/cameras/QuakeCamera.js | 24 +++++++++++++++++++++++ 4 files changed, 41 insertions(+), 14 deletions(-) diff --git a/build/Three.js b/build/Three.js index 7036335d82..d4378dd8b2 100755 --- a/build/Three.js +++ b/build/Three.js @@ -353,13 +353,14 @@ value:0,texture:null},uImageIncrement:{type:"v2",value:new THREE.Vector2(0.00195 film:{uniforms:{tDiffuse:{type:"t",value:0,texture:null},time:{type:"f",value:0},nIntensity:{type:"f",value:0.5},sIntensity:{type:"f",value:0.05},sCount:{type:"f",value:4096},grayscale:{type:"i",value:1}},vertexShader:"varying vec2 vUv;\nvoid main() {\nvUv = vec2( uv.x, 1.0 - uv.y );\ngl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n}",fragmentShader:"varying vec2 vUv;\nuniform sampler2D tDiffuse;\nuniform float time;\nuniform bool grayscale;\nuniform float nIntensity;\nuniform float sIntensity;\nuniform float sCount;\nvoid main() {\nvec4 cTextureScreen = texture2D( tDiffuse, vUv );\nfloat x = vUv.x * vUv.y * time * 1000.0;\nx = mod( x, 13.0 ) * mod( x, 123.0 );\nfloat dx = mod( x, 0.01 );\nvec3 cResult = cTextureScreen.rgb + cTextureScreen.rgb * clamp( 0.1 + dx * 100.0, 0.0, 1.0 );\nvec2 sc = vec2( sin( vUv.y * sCount ), cos( vUv.y * sCount ) );\ncResult += cTextureScreen.rgb * vec3( sc.x, sc.y, sc.x ) * sIntensity;\ncResult = cTextureScreen.rgb + clamp( nIntensity, 0.0,1.0 ) * ( cResult - cTextureScreen.rgb );\nif( grayscale ) {\ncResult = vec3( cResult.r * 0.3 + cResult.g * 0.59 + cResult.b * 0.11 );\n}\ngl_FragColor = vec4( cResult, cTextureScreen.a );\n}"}, screen:{uniforms:{tDiffuse:{type:"t",value:0,texture:null},opacity:{type:"f",value:1}},vertexShader:"varying vec2 vUv;\nvoid main() {\nvUv = vec2( uv.x, 1.0 - uv.y );\ngl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n}",fragmentShader:"varying vec2 vUv;\nuniform sampler2D tDiffuse;\nuniform float opacity;\nvoid main() {\nvec4 texel = texture2D( tDiffuse, vUv );\ngl_FragColor = opacity * texel;\n}"},basic:{uniforms:{},vertexShader:"void main() {\ngl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n}", fragmentShader:"void main() {\ngl_FragColor = vec4( 1.0, 0.0, 0.0, 0.5 );\n}"}},buildKernel:function(b){var d,c,f,g,h=2*Math.ceil(b*3)+1;h>25&&(h=25);g=(h-1)*0.5;c=Array(h);for(d=f=0;dthis.heightMax?this.heightMax:this.position.y)-this.heightMin)*this.heightCoef:0;(this.moveForward||this.autoForward)&&this.translateZ(-(this.movementSpeed+this.autoSpeedFactor));this.moveBackward&&this.translateZ(this.movementSpeed);this.moveLeft&&this.translateX(-this.movementSpeed);this.moveRight&&this.translateX(this.movementSpeed); -var c=this.lookSpeed;this.activeLook||(c=0);this.lon+=this.mouseX*c;this.lookVertical&&(this.lat-=this.mouseY*c);this.lat=Math.max(-85,Math.min(85,this.lat));this.phi=(90-this.lat)*Math.PI/180;this.theta=this.lon*Math.PI/180;c=this.target.position;var f=this.position;c.x=f.x+100*Math.sin(this.phi)*Math.cos(this.theta);c.y=f.y+100*Math.cos(this.phi);c.z=f.z+100*Math.sin(this.phi)*Math.sin(this.theta);this.supr.update.call(this)};this.domElement.addEventListener("contextmenu",function(c){c.preventDefault()}, -!1);this.domElement.addEventListener("mousemove",d(this,this.onMouseMove),!1);this.domElement.addEventListener("mousedown",d(this,this.onMouseDown),!1);this.domElement.addEventListener("mouseup",d(this,this.onMouseUp),!1);this.domElement.addEventListener("keydown",d(this,this.onKeyDown),!1);this.domElement.addEventListener("keyup",d(this,this.onKeyUp),!1)};THREE.QuakeCamera.prototype=new THREE.Camera;THREE.QuakeCamera.prototype.constructor=THREE.QuakeCamera;THREE.QuakeCamera.prototype.supr=THREE.Camera.prototype; +THREE.QuakeCamera=function(b){function d(c,f){return function(){f.apply(c,arguments)}}THREE.Camera.call(this,b.fov,b.aspect,b.near,b.far,b.target);this.movementSpeed=1;this.lookSpeed=0.0050;this.noFly=!1;this.lookVertical=!0;this.autoForward=!1;this.activeLook=!0;this.heightSpeed=!1;this.heightCoef=1;this.heightMin=0;this.constrainVertical=!1;this.verticalMin=0;this.verticalMax=3.14;this.domElement=document;if(b){if(b.movementSpeed!==undefined)this.movementSpeed=b.movementSpeed;if(b.lookSpeed!==undefined)this.lookSpeed= +b.lookSpeed;if(b.noFly!==undefined)this.noFly=b.noFly;if(b.lookVertical!==undefined)this.lookVertical=b.lookVertical;if(b.autoForward!==undefined)this.autoForward=b.autoForward;if(b.activeLook!==undefined)this.activeLook=b.activeLook;if(b.heightSpeed!==undefined)this.heightSpeed=b.heightSpeed;if(b.heightCoef!==undefined)this.heightCoef=b.heightCoef;if(b.heightMin!==undefined)this.heightMin=b.heightMin;if(b.heightMax!==undefined)this.heightMax=b.heightMax;if(b.constrainVertical!==undefined)this.constrainVertical= +b.constrainVertical;if(b.verticalMin!==undefined)this.verticalMin=b.verticalMin;if(b.verticalMax!==undefined)this.verticalMax=b.verticalMax;if(b.domElement!==undefined)this.domElement=b.domElement}this.theta=this.phi=this.lon=this.lat=this.mouseY=this.mouseX=this.autoSpeedFactor=0;this.moveForward=!1;this.moveBackward=!1;this.moveLeft=!1;this.moveRight=!1;this.mouseDragOn=!1;this.windowHalfX=window.innerWidth/2;this.windowHalfY=window.innerHeight/2;this.onMouseDown=function(c){c.preventDefault(); +c.stopPropagation();if(this.activeLook)switch(c.button){case 0:this.moveForward=!0;break;case 2:this.moveBackward=!0}this.mouseDragOn=!0};this.onMouseUp=function(c){c.preventDefault();c.stopPropagation();if(this.activeLook)switch(c.button){case 0:this.moveForward=!1;break;case 2:this.moveBackward=!1}this.mouseDragOn=!1};this.onMouseMove=function(c){this.mouseX=c.clientX-this.windowHalfX;this.mouseY=c.clientY-this.windowHalfY};this.onKeyDown=function(c){switch(c.keyCode){case 38:case 87:this.moveForward= +!0;break;case 37:case 65:this.moveLeft=!0;break;case 40:case 83:this.moveBackward=!0;break;case 39:case 68:this.moveRight=!0}};this.onKeyUp=function(c){switch(c.keyCode){case 38:case 87:this.moveForward=!1;break;case 37:case 65:this.moveLeft=!1;break;case 40:case 83:this.moveBackward=!1;break;case 39:case 68:this.moveRight=!1}};this.update=function(){this.autoSpeedFactor=this.heightSpeed?((this.position.ythis.heightMax?this.heightMax:this.position.y)- +this.heightMin)*this.heightCoef:0;(this.moveForward||this.autoForward)&&this.translateZ(-(this.movementSpeed+this.autoSpeedFactor));this.moveBackward&&this.translateZ(this.movementSpeed);this.moveLeft&&this.translateX(-this.movementSpeed);this.moveRight&&this.translateX(this.movementSpeed);var c=this.lookSpeed;this.activeLook||(c=0);this.lon+=this.mouseX*c;this.lookVertical&&(this.lat-=this.mouseY*c);this.lat=Math.max(-85,Math.min(85,this.lat));this.phi=(90-this.lat)*Math.PI/180;this.theta=this.lon* +Math.PI/180;if(this.constrainVertical)this.phi=(this.phi-0)*(this.verticalMax-this.verticalMin)/3.14+this.verticalMin;c=this.target.position;var f=this.position;c.x=f.x+100*Math.sin(this.phi)*Math.cos(this.theta);c.y=f.y+100*Math.cos(this.phi);c.z=f.z+100*Math.sin(this.phi)*Math.sin(this.theta);this.supr.update.call(this)};this.domElement.addEventListener("contextmenu",function(c){c.preventDefault()},!1);this.domElement.addEventListener("mousemove",d(this,this.onMouseMove),!1);this.domElement.addEventListener("mousedown", +d(this,this.onMouseDown),!1);this.domElement.addEventListener("mouseup",d(this,this.onMouseUp),!1);this.domElement.addEventListener("keydown",d(this,this.onKeyDown),!1);this.domElement.addEventListener("keyup",d(this,this.onKeyUp),!1)};THREE.QuakeCamera.prototype=new THREE.Camera;THREE.QuakeCamera.prototype.constructor=THREE.QuakeCamera;THREE.QuakeCamera.prototype.supr=THREE.Camera.prototype; THREE.QuakeCamera.prototype.translate=function(b,d){this.matrix.rotateAxis(d);if(this.noFly)d.y=0;this.position.addSelf(d.multiplyScalar(b));this.target.position.addSelf(d.multiplyScalar(b))}; THREE.PathCamera=function(b){function d(p,o,u,y){var v={name:u,fps:0.6,length:y,hierarchy:[]},B,C=o.getControlPointsArray(),G=o.getLength(),H=C.length,U=0;B=H-1;o={parent:-1,keys:[]};o.keys[0]={time:0,pos:C[0],rot:[0,0,0,1],scl:[1,1,1]};o.keys[B]={time:y,pos:C[B],rot:[0,0,0,1],scl:[1,1,1]};for(B=1;B25&&(d=25);e=(d-1)*0.5;b=Array(d);for(f=c=0;fthis.heightMax?this.heightMax:this.position.y)-this.heightMin)*this.heightCoef:0;(this.moveForward||this.autoForward)&&this.translateZ(-(this.movementSpeed+this.autoSpeedFactor));this.moveBackward&&this.translateZ(this.movementSpeed);this.moveLeft&&this.translateX(-this.movementSpeed);this.moveRight&&this.translateX(this.movementSpeed); -var b=this.lookSpeed;this.activeLook||(b=0);this.lon+=this.mouseX*b;this.lookVertical&&(this.lat-=this.mouseY*b);this.lat=Math.max(-85,Math.min(85,this.lat));this.phi=(90-this.lat)*Math.PI/180;this.theta=this.lon*Math.PI/180;b=this.target.position;var c=this.position;b.x=c.x+100*Math.sin(this.phi)*Math.cos(this.theta);b.y=c.y+100*Math.cos(this.phi);b.z=c.z+100*Math.sin(this.phi)*Math.sin(this.theta);this.supr.update.call(this)};this.domElement.addEventListener("contextmenu",function(b){b.preventDefault()}, -!1);this.domElement.addEventListener("mousemove",f(this,this.onMouseMove),!1);this.domElement.addEventListener("mousedown",f(this,this.onMouseDown),!1);this.domElement.addEventListener("mouseup",f(this,this.onMouseUp),!1);this.domElement.addEventListener("keydown",f(this,this.onKeyDown),!1);this.domElement.addEventListener("keyup",f(this,this.onKeyUp),!1)};THREE.QuakeCamera.prototype=new THREE.Camera;THREE.QuakeCamera.prototype.constructor=THREE.QuakeCamera;THREE.QuakeCamera.prototype.supr=THREE.Camera.prototype; +THREE.QuakeCamera=function(a){function f(b,c){return function(){c.apply(b,arguments)}}THREE.Camera.call(this,a.fov,a.aspect,a.near,a.far,a.target);this.movementSpeed=1;this.lookSpeed=0.0050;this.noFly=!1;this.lookVertical=!0;this.autoForward=!1;this.activeLook=!0;this.heightSpeed=!1;this.heightCoef=1;this.heightMin=0;this.constrainVertical=!1;this.verticalMin=0;this.verticalMax=3.14;this.domElement=document;if(a){if(a.movementSpeed!==undefined)this.movementSpeed=a.movementSpeed;if(a.lookSpeed!==undefined)this.lookSpeed= +a.lookSpeed;if(a.noFly!==undefined)this.noFly=a.noFly;if(a.lookVertical!==undefined)this.lookVertical=a.lookVertical;if(a.autoForward!==undefined)this.autoForward=a.autoForward;if(a.activeLook!==undefined)this.activeLook=a.activeLook;if(a.heightSpeed!==undefined)this.heightSpeed=a.heightSpeed;if(a.heightCoef!==undefined)this.heightCoef=a.heightCoef;if(a.heightMin!==undefined)this.heightMin=a.heightMin;if(a.heightMax!==undefined)this.heightMax=a.heightMax;if(a.constrainVertical!==undefined)this.constrainVertical= +a.constrainVertical;if(a.verticalMin!==undefined)this.verticalMin=a.verticalMin;if(a.verticalMax!==undefined)this.verticalMax=a.verticalMax;if(a.domElement!==undefined)this.domElement=a.domElement}this.theta=this.phi=this.lon=this.lat=this.mouseY=this.mouseX=this.autoSpeedFactor=0;this.moveForward=!1;this.moveBackward=!1;this.moveLeft=!1;this.moveRight=!1;this.mouseDragOn=!1;this.windowHalfX=window.innerWidth/2;this.windowHalfY=window.innerHeight/2;this.onMouseDown=function(b){b.preventDefault(); +b.stopPropagation();if(this.activeLook)switch(b.button){case 0:this.moveForward=!0;break;case 2:this.moveBackward=!0}this.mouseDragOn=!0};this.onMouseUp=function(b){b.preventDefault();b.stopPropagation();if(this.activeLook)switch(b.button){case 0:this.moveForward=!1;break;case 2:this.moveBackward=!1}this.mouseDragOn=!1};this.onMouseMove=function(b){this.mouseX=b.clientX-this.windowHalfX;this.mouseY=b.clientY-this.windowHalfY};this.onKeyDown=function(b){switch(b.keyCode){case 38:case 87:this.moveForward= +!0;break;case 37:case 65:this.moveLeft=!0;break;case 40:case 83:this.moveBackward=!0;break;case 39:case 68:this.moveRight=!0}};this.onKeyUp=function(b){switch(b.keyCode){case 38:case 87:this.moveForward=!1;break;case 37:case 65:this.moveLeft=!1;break;case 40:case 83:this.moveBackward=!1;break;case 39:case 68:this.moveRight=!1}};this.update=function(){this.autoSpeedFactor=this.heightSpeed?((this.position.ythis.heightMax?this.heightMax:this.position.y)- +this.heightMin)*this.heightCoef:0;(this.moveForward||this.autoForward)&&this.translateZ(-(this.movementSpeed+this.autoSpeedFactor));this.moveBackward&&this.translateZ(this.movementSpeed);this.moveLeft&&this.translateX(-this.movementSpeed);this.moveRight&&this.translateX(this.movementSpeed);var b=this.lookSpeed;this.activeLook||(b=0);this.lon+=this.mouseX*b;this.lookVertical&&(this.lat-=this.mouseY*b);this.lat=Math.max(-85,Math.min(85,this.lat));this.phi=(90-this.lat)*Math.PI/180;this.theta=this.lon* +Math.PI/180;if(this.constrainVertical)this.phi=(this.phi-0)*(this.verticalMax-this.verticalMin)/3.14+this.verticalMin;b=this.target.position;var c=this.position;b.x=c.x+100*Math.sin(this.phi)*Math.cos(this.theta);b.y=c.y+100*Math.cos(this.phi);b.z=c.z+100*Math.sin(this.phi)*Math.sin(this.theta);this.supr.update.call(this)};this.domElement.addEventListener("contextmenu",function(b){b.preventDefault()},!1);this.domElement.addEventListener("mousemove",f(this,this.onMouseMove),!1);this.domElement.addEventListener("mousedown", +f(this,this.onMouseDown),!1);this.domElement.addEventListener("mouseup",f(this,this.onMouseUp),!1);this.domElement.addEventListener("keydown",f(this,this.onKeyDown),!1);this.domElement.addEventListener("keyup",f(this,this.onKeyUp),!1)};THREE.QuakeCamera.prototype=new THREE.Camera;THREE.QuakeCamera.prototype.constructor=THREE.QuakeCamera;THREE.QuakeCamera.prototype.supr=THREE.Camera.prototype; THREE.QuakeCamera.prototype.translate=function(a,f){this.matrix.rotateAxis(f);if(this.noFly)f.y=0;this.position.addSelf(f.multiplyScalar(a));this.target.position.addSelf(f.multiplyScalar(a))}; THREE.PathCamera=function(a){function f(k,j,n,p){var t={name:n,fps:0.6,length:p,hierarchy:[]},x,w=j.getControlPointsArray(),u=j.getLength(),B=w.length,y=0;x=B-1;j={parent:-1,keys:[]};j.keys[0]={time:0,pos:w[0],rot:[0,0,0,1],scl:[1,1,1]};j.keys[x]={time:p,pos:w[x],rot:[0,0,0,1],scl:[1,1,1]};for(x=1;x, * autoForward: , + * constrainVertical: , + * verticalMin: , + * verticalMax: , + * heightSpeed: , * heightCoef: , * heightMin: , @@ -43,6 +47,10 @@ THREE.QuakeCamera = function ( parameters ) { this.heightCoef = 1.0; this.heightMin = 0.0; + this.constrainVertical = false; + this.verticalMin = 0; + this.verticalMax = 3.14; + this.domElement = document; if ( parameters ) { @@ -60,6 +68,10 @@ THREE.QuakeCamera = function ( parameters ) { if ( parameters.heightCoef !== undefined ) this.heightCoef = parameters.heightCoef; if ( parameters.heightMin !== undefined ) this.heightMin = parameters.heightMin; if ( parameters.heightMax !== undefined ) this.heightMax = parameters.heightMax; + + if ( parameters.constrainVertical !== undefined ) this.constrainVertical = parameters.constrainVertical; + if ( parameters.verticalMin !== undefined ) this.verticalMin = parameters.verticalMin; + if ( parameters.verticalMax !== undefined ) this.verticalMax = parameters.verticalMax; if ( parameters.domElement !== undefined ) this.domElement = parameters.domElement; @@ -207,6 +219,12 @@ THREE.QuakeCamera = function ( parameters ) { this.phi = ( 90 - this.lat ) * Math.PI / 180; this.theta = this.lon * Math.PI / 180; + if ( this.constrainVertical ) { + + this.phi = map_linear( this.phi, 0, 3.14, this.verticalMin, this.verticalMax ); + + } + var targetPosition = this.target.position, position = this.position; @@ -237,6 +255,12 @@ THREE.QuakeCamera = function ( parameters ) { }; + function map_linear( x, sa, sb, ea, eb ) { + + return ( x - sa ) * ( eb - ea ) / ( sb - sa ) + ea; + + }; + function clamp_bottom( x, a ) { return x < a ? a : x; -- GitLab