Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
vec2.subtract(texP2, this.texCoords.slice(texId3 * 2,
texId3 * 2 + 2), texOrigin);
// Honestly I don't know what this does.
let f = 1 / (texP1[0] * texP2[1] - texP2[0] * texP1[1]);
let tangent = new Float32Array(4);
tangent[0] = f * (texP2[1] * p1[0] - texP1[1] * p2[0]);
tangent[1] = f * (texP2[1] * p1[1] - texP1[1] * p2[1]);
tangent[2] = f * (texP2[1] * p1[2] - texP1[1] * p2[2]);
// Calculate bi-tangent. To save vertex array, it can be calculated in
// vertex shader; however we have to specify the cross order to get right
// result. This can be done by using a modifier... I think.
// To calculate modifier, we have to calculate dot product with
// bi-tangent from vertex shader and bi-tangent we calculated.
let normal = vec3.create();
vec3.cross(normal, p1, p2);
vec3.normalize(normal, normal);
let leftBitangent = vec3.create();
vec3.cross(leftBitangent, tangent, normal);
// Then calculate bi-tangent with texture coords.
let bitangent = vec3.create();
bitangent[0] = f * (texP2[0] * p1[0] - texP1[0] * p2[0]);
bitangent[1] = f * (texP2[0] * p1[1] - texP1[0] * p2[1]);
bitangent[2] = f * (texP2[0] * p1[2] - texP1[0] * p2[2]);
let modifier = vec3.dot(bitangent, leftBitangent);
tangent[3] = modifier < 0 ? -1 : 1;
// Done. Store them in tangents buffer, and set indices to it.
tangents.set(tangent, tangentId * 4);
tangentIndices[faceId] = tangentId;
tangentIndices[faceId + 1] = tangentId;
tangentIndices[faceId + 2] = tangentId;
tangentId ++;
}
private camera(k: number = this.cameraK): void {
const tico = this.ticos[this.ticoIndex];
// Camera hax
vec3.copy(scratchVec3a, tico.direction);
vec3.set(scratchVec3b, 0, 1, 0);
// XZ plane
vecKillElement(scratchVec3c, scratchVec3a, scratchVec3b);
// Jam the direction vector by this a ton to smooth out the Y axis.
vec3.scaleAndAdd(scratchVec3a, scratchVec3a, scratchVec3c, 1);
vec3.normalize(scratchVec3a, scratchVec3a);
vec3.scaleAndAdd(scratchVec3a, tico.translation, scratchVec3a, -this.currentZoom);
scratchVec3a[1] += 500;
explerp(this.cameraEye, scratchVec3a, k);
explerp(this.cameraCenter, tico.translation, k);
}
export function eyeWithPointInView(camera: Camera, point: vec3): vec3 {
const ray_direction = vec3.subtract(v3(), camera.center, camera.eye);
const ray_normalized = vec3.normalize(v3(), ray_direction);
/* Retrieve u and v for an orthonormal basis. */
const ortho_v = vec3.normalize(v3(), vec3.cross(v3(), ray_normalized, camera.up));
const ortho_u = vec3.normalize(v3(), vec3.cross(v3(), ortho_v, ray_normalized));
const distance = distancePointToRay(camera.eye, camera.center, point);
/* Compute the closest point c on the ray. */
const closest = vec3.add(v3(), camera.eye, vec3.scale(v3(), ray_direction, distance));
const t = vec3.subtract(v3(), point, closest);
const part_v = Math.abs(vec3.dot(t, ortho_v)) / camera.aspect;
const part_u = Math.abs(vec3.dot(t, ortho_u));
/* Retrieve max distance to camera with required fov. */
const part_max = Math.max(part_v, part_u);
/* Require distance from closest to new camera position. */
const a = part_max / Math.tan(camera.fovy * DEG2RAD * 0.5);
return vec3.subtract(v3(), closest, vec3.scale(v3(), ray_normalized, a));
}
function calcCameraQuat () {
vec3.set(cameraPosProjected, cameraPos[0], cameraPos[1], 0);
vec3.subtract(cameraPos, cameraPos, cameraTarget);
vec3.normalize(cameraPosProjected, cameraPosProjected);
vec3.normalize(cameraPos, cameraPos);
quat.rotationTo(cameraQuat, fromCameraBaseVec, cameraPosProjected);
quat.rotationTo(verticalQuat, cameraPosProjected, cameraPos);
quat.mul(cameraQuat, verticalQuat, cameraQuat);
}
function Calc_BILLBOARD_ROT(m: mat4, nodeMatrix: mat4, parentNodeMatrix: mat4 | null, vy: vec3 = scratchVec3[0]): void {
GetModelLocalAxisY(vy, parentNodeMatrix, nodeMatrix);
vy[2] = 0;
vec3.normalize(vy, vy);
const yx = vy[0], yy = vy[1];
const scaleX = GetMtx34Scale(nodeMatrix, MtxCol.X);
const scaleY = GetMtx34Scale(nodeMatrix, MtxCol.Y);
const scaleZ = GetMtx34Scale(nodeMatrix, MtxCol.Z);
SetMdlViewMtxSR(m, scaleX, scaleY, scaleZ,
yy, yx, 0,
-yx, yy, 0,
0, 0, 1);
}
generateRay(mScreenPosition, mRay) {
const proj = this.projectionMatrix;
const view = this.viewMatrix;
mat4.multiply(mInverseViewProj, proj, view);
mat4.invert(mInverseViewProj, mInverseViewProj);
vec3.transformMat4(cameraDir, mScreenPosition, mInverseViewProj);
vec3.sub(cameraDir, cameraDir, this.position);
vec3.normalize(cameraDir, cameraDir);
if (!mRay) {
mRay = new Ray(this.position, cameraDir);
} else {
mRay.origin = this.position;
mRay.direction = cameraDir;
}
return mRay;
}
}
if(this._isRotateZ === 1) {
angle = -this._diffX.value * this._offset;
angle *= (this.preMouse.y < this._rotateZMargin) ? -1 : 1;
_quat = quat.clone([0, 0, Math.sin(angle), Math.cos(angle)]);
quat.multiply(_quat, mTempRotation, _quat);
} else {
angle = -this._diffY.value * this._offset;
angle *= (this.preMouse.x < this._rotateZMargin) ? 1 : -1;
_quat = quat.clone([0, 0, Math.sin(angle), Math.cos(angle)]);
quat.multiply(_quat, mTempRotation, _quat);
}
} else {
const v = vec3.clone([this._diffX.value, this._diffY.value, 0]);
const axis = vec3.create();
vec3.cross(axis, v, this._zAxis);
vec3.normalize(axis, axis);
angle = vec3.length(v) * this._offset;
_quat = quat.clone([Math.sin(angle) * axis[0], Math.sin(angle) * axis[1], Math.sin(angle) * axis[2], Math.cos(angle)]);
quat.multiply(mTempRotation, _quat, mTempRotation);
}
}
pan(angle: number) {
const n = vec3.create();
vec3.normalize(n, vec3.sub(n, this.eye, this.center));
const u = vec3.create();
vec3.cross(u, this.up, n);
vec3.normalize(u, u);
let v = vec3.create();
vec3.cross(v, n, u);
vec3.normalize(v, v);
const panmat4 = mat4.create();
mat4.fromRotation(panmat4, angle, v);
let newCenter = vec3.create();
vec3.sub(newCenter, this.center, this.eye);
vec3.transformMat4(newCenter, newCenter, panmat4);
vec3.add(this.center, newCenter, this.eye);
if (Math.abs(vec3.dot(n, this.up)) >= 0.985) {
vec3.transformMat4(this.up, this.up, panmat4);
}
this.updateTransform();
}
let bottomLeft = corners[cellY][cellX].groundHeight;
let bottomRight = corners[cellY][cellX + 1].groundHeight;
let topLeft = corners[cellY + 1][cellX].groundHeight;
let topRight = corners[cellY + 1][cellX + 1].groundHeight;
let sqX = x - cellX;
let sqY = y - cellY;
if (sqX + sqY < 1) {
vec3.set(normalHeap1, 1, 0, bottomRight - bottomLeft);
vec3.set(normalHeap2, 0, 1, topLeft - bottomLeft);
} else {
vec3.set(normalHeap1, -1, 0, topRight - topLeft);
vec3.set(normalHeap2, 0, 1, topRight - bottomRight);
}
vec3.normalize(out, vec3.cross(out, normalHeap1, normalHeap2));
} else {
vec3.set(out, 0, 0, 1);
}
return out;
}
}