Published
Edited
Oct 21, 2021
Insert cell
Insert cell
Insert cell
Vec3 = twgl.v3
Insert cell
Insert cell
v0 = Vec3.create()
Insert cell
X = Array.from(Vec3.create(...[1, 2, 3]))
Insert cell
test = []
Insert cell
test.push(X, X, X)
Insert cell
test.flat()
Insert cell
v1 = Vec3.create(1, 1, 1)
Insert cell
v2 = Vec3.create(1, 2, 3)
Insert cell
Insert cell
Vec3.add(v1, v2)
Insert cell
Vec3.subtract(v1, v2)
Insert cell
### Dot product of Vectors
Insert cell
Vec3.dot(v1, v2)
Insert cell
### Length of a Vector
Insert cell
Vec3.length(v1)
Insert cell
Vec3.length(v2)
Insert cell
Insert cell
Vec3.normalize(v1)
Insert cell
Vec3.dot(Vec3.normalize(v1), Vec3.normalize(v2))
Insert cell
Insert cell
Vec3.cross(v1, v2)
Insert cell
Vec3.cross(v2, v1)
Insert cell
Vec3.cross(v1, v1)
Insert cell
Vec3.dot(v1, Vec3.cross(v1, v2))
Insert cell
Insert cell
Insert cell
Insert cell
m0 = Mat4.create() // Create a zero matrix
// For some reason create function is not available in the library. I have added it.
Insert cell
m1 = Mat4.identity() // Create an identity matrix
Insert cell
m2 = Mat4.translation(v1) // Create a translation matrix
Insert cell
deg2rad = (deg) => (deg * Math.PI) / 180
Insert cell
m3 = Mat4.rotationX(deg2rad(45)) // rotate around X-axis by an angle (radian).
Insert cell
m4 = Mat4.rotationY(deg2rad(45)) // rotate around Y-axis by an angle (radian).
Insert cell
m5 = Mat4.rotationZ(deg2rad(45)) // rotate around Z-axis by an angle (radian).
Insert cell
m6 = Mat4.axisRotation(v1, deg2rad(45)) // rotate around axis (3D vector) by an angle (radian)
Insert cell
v2
Insert cell
m7 = Mat4.scaling(v2) // scale each dimension by an amount given by the corresponding entry in the 3D vector
Insert cell
Insert cell
v0
Insert cell
Mat4.transformDirection(m2, v0)
Insert cell
Mat4.transformPoint(m2, v0)
Insert cell
Insert cell
Mat4.negate(m2)
Insert cell
Mat4.transpose(m2)
Insert cell
Mat4.inverse(m2)
Insert cell
Mat4.multiply(m2, Mat4.inverse(m2)) // Product of a matrix with its inverse is Identity matrix.
Insert cell
Insert cell
eye = Vec3.create(0,0,5)
Insert cell
target = Vec3.create()
Insert cell
up = Vec3.create(0, 1, 0)
Insert cell
cameraMatrix = Mat4.lookAt(eye, target, up)
Insert cell
viewMatrix = Mat4.inverse(cameraMatrix)
Insert cell
Mat4.transformPoint(viewMatrix, eye)
Insert cell
Mat4.transformPoint(viewMatrix, target)
Insert cell
fieldOfViewYInRadians = deg2rad(90) // The angle the vertical extent of the view window makes at the camera eye.
Insert cell
aspect = 1 // it is the (view-window-width / view-windo-height)
Insert cell
zNear = 0.1 // Minimum Z-extent of the view volume. Z distance from the eye smaller than this length will be outside clip volume.
Insert cell
zFar = 10 // Maximum Z-extent of the view volume. Z distance from the eye larger than this length will be outside clip volume.
Insert cell
projMatrix = Mat4.perspective(fieldOfViewYInRadians, aspect, zNear, zFar) // Transorms the points inside the view volume to a rectangular box extending from -1 to 1 in the x, y and z dimensions.
Insert cell
Mat4.transformPoint(projMatrix, Vec3.create(0, 0, -zNear))
Insert cell
Mat4.transformPoint(projMatrix, Vec3.create(0, 0, -zFar))
Insert cell
Mat4.transformPoint(projMatrix, Vec3.create(-zFar, 0, -zFar))
Insert cell
Mat4.transformPoint(projMatrix, Vec3.create(-zNear, 0, -zNear))
Insert cell
Mat4.transformPoint(projMatrix, Vec3.create(zNear, 0, -zNear))
Insert cell
Mat4.transformPoint(projMatrix, Vec3.create(0, zFar, -zFar))
Insert cell
Insert cell
twgl = require("twgl.js")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more