本文整理匯總了TypeScript中gl-matrix.vec3.sub方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript vec3.sub方法的具體用法?TypeScript vec3.sub怎麽用?TypeScript vec3.sub使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類gl-matrix.vec3
的用法示例。
在下文中一共展示了vec3.sub方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: barycoordFromPoint
export function barycoordFromPoint(point: vec3, a: vec3, b: vec3, c: vec3) {
const v0 = vec3.create();
const v1 = vec3.create();
const v2 = vec3.create();
vec3.sub(v0, c, a);
vec3.sub(v1, b, a);
vec3.sub(v2, point, a);
const dot00 = vec3.dot(v0, v0);
const dot01 = vec3.dot(v0, v1);
const dot02 = vec3.dot(v0, v2);
const dot11 = vec3.dot(v1, v1);
const dot12 = vec3.dot(v1, v2);
const denom = dot00 * dot11 - dot01 * dot01;
const result = new Vector3();
// collinear or singular triangle
if (denom === 0) {
// arbitrary location outside of triangle?
// not sure if this is the best idea, maybe should be returning undefined
return result.set(-2, -1, -1);
}
const invDenom = 1 / denom;
const u = (dot11 * dot02 - dot01 * dot12) * invDenom;
const v = (dot00 * dot12 - dot01 * dot02) * invDenom;
// barycentric coordinates must always sum to 1
return result.set(1 - u - v, v, u);
}
示例2: lookAt
export function lookAt(eye: vec3, target: vec3, up: vec3) {
const quatOut = quat.create();
const x = vec3.create();
const y = vec3.create();
const z = vec3.create();
vec3.sub(z, eye, target);
if (vec3.squaredLength(z) === 0) {
// eye and target are in the same position
z[2] = 1;
}
vec3.normalize(z, z);
vec3.cross(x, up, z);
if (vec3.squaredLength(x) === 0) {
// eye and target are in the same vertical
z[2] += 0.0001;
vec3.cross(x, up, z);
}
vec3.normalize(x, x);
vec3.cross(y, z, x);
quat.setAxes(quatOut, z, x, y);
quat.invert(quatOut, quatOut);
return quatOut;
}
示例3: setFromCamera
public setFromCamera(
coords: Vector2,
scene: Scene,
camera: PerspectiveCamera,
object: Mesh
) {
if (camera && camera.isPespectiveCamera) {
this.ray.origin.copy(camera.position);
vec3.copy(cameraDirection, [coords.x, coords.y, 0.5]);
mat4.multiply(
inversedProjectionViewMatrix,
camera.projectionMatrix,
camera.worldInverseMatrix
);
mat4.invert(inversedProjectionViewMatrix, inversedProjectionViewMatrix);
vec3.transformMat4(
cameraDirection,
cameraDirection,
inversedProjectionViewMatrix
);
vec3.sub(cameraDirection, cameraDirection, camera.position.v);
vec3.normalize(cameraDirection, cameraDirection);
directionVector.set(
cameraDirection[0],
cameraDirection[1],
cameraDirection[2]
);
this.ray.direction.copy(directionVector);
}
}
示例4:
outVec2 = vec2.transformMat4(outVec2, vec2A, mat4A);
vecArray = vec2.forEach(vecArray, 0, 0, 0, vec2.normalize);
outStr = vec2.str(vec2A);
outBool = vec2.exactEquals(vec2A, vec2B);
outBool = vec2.equals(vec2A, vec2B);
outVec2 = vec2.add(outVec2, [0, 1], [2, 3]); // test one method with number array input
// vec3
outVec3 = vec3.create();
outVec3 = vec3.clone(vec3A);
outVec3 = vec3.fromValues(1, 2, 3);
outVec3 = vec3.copy(outVec3, vec3A);
outVec3 = vec3.set(outVec3, 1, 2, 3);
outVec3 = vec3.add(outVec3, vec3A, vec3B);
outVec3 = vec3.subtract(outVec3, vec3A, vec3B);
outVec3 = vec3.sub(outVec3, vec3A, vec3B);
outVec3 = vec3.multiply(outVec3, vec3A, vec3B);
outVec3 = vec3.mul(outVec3, vec3A, vec3B);
outVec3 = vec3.divide(outVec3, vec3A, vec3B);
outVec3 = vec3.div(outVec3, vec3A, vec3B);
outVec3 = vec3.ceil(outVec3, vec3A);
outVec3 = vec3.floor(outVec3, vec3A);
outVec3 = vec3.min(outVec3, vec3A, vec3B);
outVec3 = vec3.max(outVec3, vec3A, vec3B);
outVec3 = vec3.round(outVec3, vec3A);
outVec3 = vec3.scale(outVec3, vec3A, 2);
outVec3 = vec3.scaleAndAdd(outVec3, vec3A, vec3B, 0.5);
outVal = vec3.distance(vec3A, vec3B);
outVal = vec3.dist(vec3A, vec3B);
outVal = vec3.squaredDistance(vec3A, vec3B);
outVal = vec3.sqrDist(vec3A, vec3B);
示例5: intersectTriangle
public intersectTriangle(a: Vector3, b: Vector3, c: Vector3, culling = true) {
vec3.sub(edge1, b.v, a.v);
vec3.sub(edge2, c.v, a.v);
vec3.cross(normal, edge1, edge2);
// Solve Q + t*D = b1*E1 + b2*E2 (Q = kDiff, D = ray direction,
// E1 = kEdge1, E2 = kEdge2, N = Cross(E1,E2)) by
// |Dot(D,N)|*b1 = sign(Dot(D,N))*Dot(D,Cross(Q,E2))
// |Dot(D,N)|*b2 = sign(Dot(D,N))*Dot(D,Cross(E1,Q))
// |Dot(D,N)|*t = -sign(Dot(D,N))*Dot(Q,N)
// console.log('normal', normal);
let DdN = vec3.dot(this.direction.v, normal);
let sign;
// console.log('normal', normal);
if (DdN > 0) {
if (culling) return null;
sign = 1;
} else if (DdN < 0) {
sign = -1;
DdN = -DdN;
} else {
return null;
}
vec3.sub(diff, this.origin.v, a.v);
vec3.cross(edge2, diff, edge2);
const DdQxE2 = sign * vec3.dot(this.direction.v, edge2);
// b1 < 0, no intersection
if (DdQxE2 < 0) {
return null;
}
vec3.cross(edge1, edge1, diff);
const DdE1xQ = sign * vec3.dot(this.direction.v, edge1);
// b2 < 0, no intersection
if (DdE1xQ < 0) {
return null;
}
// b1+b2 > 1, no intersection
if (DdQxE2 + DdE1xQ > DdN) {
return null;
}
// Line intersects triangle, check if ray does.
const QdN = -sign * vec3.dot(diff, normal);
// t < 0, no intersection
if (QdN < 0) {
return null;
}
const result = new Vector3();
result
.copy(this.direction)
.scale(QdN / DdN)
.add(this.origin);
return result;
}