public
Created

webgl_extrapolation_pick - fixed

  • Download Gist
webgl_extrapolation_pick-fixed.html
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebGL Pick - Extrapolation</title>
<style type='text/css'>
body{
position: relative;
left: 10px;
top: 10px;
padding: 0px;
margin: 0px;
}
.container{
padding: 0px;
margin: 0px;
}
canvas{
border-style:dashed;
border-width:thin;
cursor:crosshair;
z-index: 0;
}
</style>
<script type='text/javascript' src='./scripts/jquery.min.js'></script>
<script type='text/javascript' src='./scripts/three.min.js'></script>
<script type='text/javascript'>
 
$(document).ready(function() {
var camera, scene, renderer;
var background;
var projector;
 
var quad;
var follow = false;
 
var mouseX, mouseY;
 
//extrapolation
var posX, posY;
var R = [[0, 0], [0, 0]];
var dR = [0, 0];
var d2R = [0, 0];
var dt = [0, 0];
 
function Push(arr, v){
for(var i = arr.length - 1; i > 0; --i){
arr[i] = arr[i - 1];
}
arr[0] = v;
}
 
var clock = new THREE.Clock();
var animClock = new THREE.Clock();
 
var width = 800, height = 600;
 
init();
animate();
function animate(){
requestAnimationFrame( animate );
 
var delta = animClock.getDelta();
 
if(follow == true){
var ray = projector.pickingRay(new THREE.Vector2(mouseX, mouseY), camera);
console.log(delta, dR[0], d2R[0]);
quad.position.x = ray.origin.x + delta * dR[0] + 0.5 * delta * delta * d2R[0];
quad.position.y = ray.origin.y + delta * dR[1] + 0.5 * delta * delta * d2R[1];
}
 
renderer.render( scene, camera );
}
function onDocumentMouseMove(event){
event.preventDefault();
var elem = $("canvas");
var offset = elem.offset();
var offsetX = offset.left;
var offsetY = offset.top;
 
Push(dt, clock.getDelta());
 
var prevX = posX;
var prevY = posY;
//NDC
mouseX = ((event.clientX - offsetX) / width) * 2 - 1;
mouseY = -((event.clientY - offsetY) / height) * 2 + 1;
var ray = projector.pickingRay(new THREE.Vector2(mouseX, mouseY), camera);
posX = ray.origin.x;
posY = ray.origin.y;
 
Push(R, [posX - prevX, posY - prevY]);
 
dR = [R[0][0]/dt[0], R[0][1]/dt[0]];
d2R = [(R[1][0] - R[0][0])/(dt[0] + dt[1]), (R[1][1] - R[0][1])/(dt[0] + dt[1])];
}
function onDocumentMouseDown(event){
console.log("DOWN");
event.preventDefault();
var ray = projector.pickingRay(new THREE.Vector2(mouseX, mouseY), camera);
 
var intersects = ray.intersectObjects(scene.children);
 
if(intersects.length > 0){
follow = true;
}
}
function onDocumentMouseUp(event){
event.preventDefault();
 
console.log("UP");
follow = false;
}
 
function init(){
camera = new THREE.OrthographicCamera(-width/2.0, width/2.0, height/2.0, -height/2.0, -1, 10000);
camera.position.z = 1000;
 
scene = new THREE.Scene();
 
projector = new THREE.Projector();
 
var material = new THREE.MeshBasicMaterial({color: 0xff0000});
quad = new THREE.Mesh(new THREE.PlaneGeometry(100, 100), material);
quad.position.z = -500;
scene.add(quad);
 
renderer = new THREE.WebGLRenderer();
//renderer = new THREE.CanvasRenderer();
renderer.setSize(width, height);
 
$(".container").append(renderer.domElement);
 
$(".container")[0].addEventListener('mousedown', onDocumentMouseDown, false);
$(".container")[0].addEventListener('mousemove', onDocumentMouseMove, false);
$(".container")[0].addEventListener('mouseup', onDocumentMouseUp, false);
 
clock.start();
animClock.start();
}
});
</script>
</head>
<body>
<div class = 'container'>
</div>
</body>
</html>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.