Skip to content

Instantly share code, notes, and snippets.

@mikolalysenko
Created April 7, 2017 06:13
Show Gist options
  • Save mikolalysenko/0e76af96dd15da1844d9bb98190b471a to your computer and use it in GitHub Desktop.
Save mikolalysenko/0e76af96dd15da1844d9bb98190b471a to your computer and use it in GitHub Desktop.
const vec3 = require('gl-vec3')
const camera = require('rtcrshr')({
vars: {
eye: [100, 0, 0],
up: [0, 1, 0],
right: [0, 0, 1],
center: [0, 0, 0]
},
props: [
'distance',
'mouseX',
'mouseY',
'stiffness'
],
rules: [
{ // distance constraint
input: [ 'distance', 'eye', 'center' ],
output: 'eye',
rule ([ distance, inEye, inCenter ], [ outEye ]) {
vec3.sub(outEye, inEye, inCenter)
const s = distance / vec3.length(outEye)
vec3.scaleAndAdd(outEye, inCenter, outEye, s)
}
},
{ // up/right vector orthogonality constraint
input: [ 'up', 'eye', 'center' ],
output: [ 'up', 'right' ],
rule ([ inUp, inEye, inCenter ], [ outUp, outRight ]) {
// let ec = normalize(inEye - inCenter)
// then orthogonalize outUp = inUp - ec * dot(inUp, ec)
// and set outRight = normalize(outUp x ec)
}
},
{ // mouse movement
input: [ 'mouseX', 'mouseY', 'up', 'right', 'eye' ],
output: [ 'eye' ],
rule ([ mouseX, mouseY, inUp, inRight, inEye ], [ outEye ]) {
for (let i = 0; i < 3; ++i) {
outEye[i] = inEye[i] + mouseX * inRight[i] + mouseY * inUp[i]
}
}
},
{ // momentum
input: [ 'stiffness', 'eye', { delay: 1, var: 'eye' } ],
output: [ 'eye' ],
rule ([ stiffness, inEye0, inEye1 ], [ outEye ]) {
for (let i = 0; i < 3; ++i) {
outEye[i] = (1 - stiffness) * (inEye0[i] - inEye0[i]) + inEye0[i]
}
}
}
]
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment