Created
January 28, 2017 22:21
-
-
Save flupe/ff63c6378d47ac0bed27ef636acb827c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const raf = window.requestAnimationFrame | |
| const time = performance ? performance.now.bind(performance) : Date.now | |
| const cvs = document.createElement('canvas') | |
| const ctx = cvs.getContext('2d') | |
| const WIDTH = 240 | |
| const HEIGHT = 160 | |
| let last = time() | |
| cvs.width = WIDTH | |
| cvs.height = HEIGHT | |
| document.body.appendChild(cvs) | |
| ctx.fillStyle = '#fff' | |
| // const imageData = ctx.createImageData(WIDTH, HEIGHT) | |
| // const buf32 = new Uint32Array(imageData.data.buffer) | |
| let points = [ | |
| vec3.create( 20, 20, 20), | |
| vec3.create(-20, 20, 20), | |
| vec3.create( 20, -20, 20), | |
| vec3.create(-20, -20, 20), | |
| vec3.create( 20, 20, -20), | |
| vec3.create(-20, 20, -20), | |
| vec3.create( 20, -20, -20), | |
| vec3.create(-20, -20, -20), | |
| ] | |
| let p = vec3.create(0, 0, 0) | |
| let rot = mat3.identity() | |
| let step = _ => { | |
| raf(step) | |
| let now = time() | |
| let dt = (now - last) / 1000 | |
| last = now | |
| mat3.rotate_z(rot, rot, Math.PI * dt) | |
| mat3.rotate_x(rot, rot, Math.PI * dt * .8) | |
| ctx.clearRect(0, 0, WIDTH, HEIGHT) | |
| points.forEach(pos => { | |
| vec3.apply_mat(p, pos, rot) | |
| ctx.beginPath() | |
| ctx.arc((WIDTH >> 1) + p.x, (HEIGHT >> 1) - p.y, 2, 0, 2 * Math.PI) | |
| ctx.fill() | |
| }) | |
| } | |
| raf(step) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let {sqrt, abs, sin, cos} = Math | |
| /////////////////////////////////////// | |
| let vec3 = {} | |
| vec3.create = (x, y, z) => ({x, y, z}) | |
| vec3.clone = ({x, y, z}) => ({x, y, z}) | |
| vec3.length2 = ({x, y, z}) => x * x + y * y + z * z | |
| vec3.length = ({x, y, z}) => sqrt(x * x + y * y + z * z) | |
| vec3.dot = (u, v) => u.x * v.x + u.y * v.y + u.z * v.z | |
| vec3.apply_mat = (o, {x, y, z}, m) => { | |
| o.x = m.a * x + m.b * y + m.c * z | |
| o.y = m.d * x + m.e * y + m.f * z | |
| o.z = m.g * x + m.h * y + m.i * z | |
| } | |
| /////////////////////////////////////// | |
| let mat3 = {} | |
| mat3.create = (a, b, c, d, e, f, g, h, i) => ({a, b, c, d, e, f, g, h, i}) | |
| mat3.multiply = (o, a, b) => { | |
| let a1 = a.a, b1 = a.b, c1 = a.c, | |
| d1 = a.d, e1 = a.e, f1 = a.f, | |
| g1 = a.g, h1 = a.h, i1 = a.i | |
| let a2 = b.a, b2 = b.b, c2 = b.c, | |
| d2 = b.d, e2 = b.e, f2 = b.f, | |
| g2 = b.g, h2 = b.h, i2 = b.i | |
| o.a = a1 * a2 + b1 * d2 + c1 * g2 | |
| o.b = a1 * b2 + b1 * e2 + c1 * h2 | |
| o.c = a1 * c2 + b1 * f2 + c1 * i2 | |
| o.d = d1 * a2 + e1 * d2 + f1 * g2 | |
| o.e = d1 * b2 + e1 * e2 + f1 * h2 | |
| o.f = d1 * c2 + e1 * f2 + f1 * i2 | |
| o.g = g1 * a2 + h1 * d2 + i1 * g2 | |
| o.h = g1 * b2 + h1 * e2 + i1 * h2 | |
| o.i = g1 * c2 + h1 * f2 + i1 * i2 | |
| } | |
| mat3.identity = _ => { | |
| let o = {} | |
| o.a = o.e = o.i = 1 | |
| o.b = o.c = o.d = o.f = o.g = o.h = 0 | |
| return o | |
| } | |
| mat3.to_identity = o => { | |
| o.a = o.e = o.i = 1 | |
| o.b = o.c = o.d = o.f = o.g = o.h = 0 | |
| } | |
| mat3.det = ({a, b, c, d, e, f, g, h, i}) => a*(e*i-h*f)+d*(h*c-b*i)+g*(b*f-e*c) | |
| mat3.transpose = (o, m) => { | |
| let {a, b, c, d, e, f, g, h, i} = m | |
| o.b = d | |
| o.d = b | |
| o.c = g | |
| o.g = c | |
| o.f = h | |
| o.h = f | |
| if (o !== m) { | |
| o.a = a | |
| o.e = e | |
| o.i = i | |
| } | |
| } | |
| mat3.rotation_x = (o, a) => { | |
| let sa = sin(a) | |
| o.a = 1 | |
| o.b = o.c = o.d = o.g = 0 | |
| o.e = o.i = cos(a) | |
| o.h = sa | |
| o.f = -sa | |
| } | |
| mat3.rotation_y = (o, a) => { | |
| let sa = sin(a) | |
| o.e = 1 | |
| o.b = o.d = o.f = o.h = 0 | |
| o.a = o.i = cos(a) | |
| o.c = sa | |
| o.g = -sa | |
| } | |
| mat3.rotation_z = (o, a) => { | |
| let sa = sin(a) | |
| o.i = 1 | |
| o.c = o.f = o.g = o.h = 0 | |
| o.a = o.e = cos(a) | |
| o.d = sa | |
| o.b = -sa | |
| } | |
| mat3.rotate_x = (o, m, a) => { | |
| let {d, e, f, g, h, i} = m | |
| let s = sin(a), | |
| c = cos(a) | |
| o.d = c * d - s * g | |
| o.e = c * e - s * h | |
| o.f = c * f - s * i | |
| o.g = s * d + c * g | |
| o.h = s * e + c * h | |
| o.i = s * f + c * i | |
| o.g = s * d + c * g | |
| if (o !== m) { | |
| o.a = m.a | |
| o.b = m.b | |
| o.c = m.c | |
| } | |
| } | |
| mat3.rotate_y = (o, m, t) => { | |
| let {a, b, c, g, h, i} = m | |
| let sa = sin(t), | |
| ca = cos(t) | |
| o.a = ca * a + sa * g | |
| o.b = ca * b + sa * h | |
| o.c = ca * c + sa * i | |
| o.g = ca * g - sa * a | |
| o.h = ca * h - sa * b | |
| o.i = ca * i - sa * c | |
| if (o !== m) { | |
| o.d = m.d | |
| o.e = m.e | |
| o.f = m.f | |
| } | |
| } | |
| mat3.rotate_z = (o, m, t) => { | |
| let {a, b, c, d, e, f} = m | |
| let sa = sin(t), | |
| ca = cos(t) | |
| o.a = ca * a - sa * d | |
| o.b = ca * b - sa * e | |
| o.c = ca * c - sa * f | |
| o.d = sa * a + ca * d | |
| o.e = sa * b + ca * e | |
| o.f = sa * c + ca * f | |
| if (o !== m) { | |
| o.g = m.g | |
| o.h = m.h | |
| o.i = m.i | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment