Comparing version 0.0.45 to 0.0.46
{ | ||
"name": "ogl", | ||
"version": "0.0.45", | ||
"version": "0.0.46", | ||
"description": "WebGL Library", | ||
@@ -5,0 +5,0 @@ "main": "src/index.mjs", |
@@ -72,2 +72,5 @@ import { Geometry } from '../core/Geometry.js'; | ||
// Unbind current VAO so that new buffers don't get added to active mesh | ||
gl.renderer.bindVertexArray(null); | ||
// Create gl buffers from bufferViews | ||
@@ -74,0 +77,0 @@ const bufferViews = this.parseBufferViews(gl, desc, buffers); |
// TODO: barycentric code shouldn't be here, but where? | ||
// TODO: SphereCast? | ||
@@ -10,2 +11,3 @@ import { Vec2 } from '../math/Vec2.js'; | ||
const tempVec2c = new Vec2(); | ||
const tempVec3a = new Vec3(); | ||
@@ -20,2 +22,5 @@ const tempVec3b = new Vec3(); | ||
const tempVec3i = new Vec3(); | ||
const tempVec3j = new Vec3(); | ||
const tempVec3k = new Vec3(); | ||
const tempMat4 = new Mat4(); | ||
@@ -56,3 +61,3 @@ | ||
intersectBounds(meshes, maxDistance) { | ||
intersectBounds(meshes, { maxDistance, output = [] } = {}) { | ||
if (!Array.isArray(meshes)) meshes = [meshes]; | ||
@@ -64,3 +69,4 @@ | ||
const hits = []; | ||
const hits = output; | ||
hits.length = 0; | ||
@@ -90,8 +96,22 @@ meshes.forEach((mesh) => { | ||
let localDistance = 0; | ||
// Check origin isn't inside bounds before testing intersection | ||
if (mesh.geometry.raycast === 'sphere') { | ||
localDistance = this.intersectSphere(bounds, origin, direction); | ||
if (origin.distance(bounds.center) > bounds.radius) { | ||
localDistance = this.intersectSphere(bounds, origin, direction); | ||
if (!localDistance) return; | ||
} | ||
} else { | ||
localDistance = this.intersectBox(bounds, origin, direction); | ||
if ( | ||
origin.x < bounds.min.x || | ||
origin.x > bounds.max.x || | ||
origin.y < bounds.min.y || | ||
origin.y > bounds.max.y || | ||
origin.z < bounds.min.z || | ||
origin.z > bounds.max.z | ||
) { | ||
localDistance = this.intersectBox(bounds, origin, direction); | ||
if (!localDistance) return; | ||
} | ||
} | ||
if (!localDistance) return; | ||
@@ -114,5 +134,5 @@ if (maxDistance && localDistance > localMaxDistance) return; | ||
intersectMeshes(meshes, cullFace = true, maxDistance) { | ||
intersectMeshes(meshes, { cullFace = true, maxDistance, includeUV = true, includeNormal = true, output = [] } = {}) { | ||
// Test bounds first before testing geometry | ||
const hits = this.intersectBounds(meshes, maxDistance); | ||
const hits = this.intersectBounds(meshes, { maxDistance, output }); | ||
if (!hits.length) return hits; | ||
@@ -126,6 +146,8 @@ | ||
const c = tempVec3e; | ||
const barycoord = tempVec3f; | ||
const a2 = tempVec2a; | ||
const b2 = tempVec2b; | ||
const c2 = tempVec2c; | ||
const closestFaceNormal = tempVec3f; | ||
const faceNormal = tempVec3g; | ||
const barycoord = tempVec3h; | ||
const uvA = tempVec2a; | ||
const uvB = tempVec2b; | ||
const uvC = tempVec2c; | ||
@@ -167,4 +189,3 @@ for (let i = hits.length - 1; i >= 0; i--) { | ||
// localDistance = this.intersectTriangle(a, b, c, backfaceCulling, origin, direction); | ||
const distance = this.intersectTriangle(a, b, c, cullFace, origin, direction); | ||
const distance = this.intersectTriangle(a, b, c, cullFace, origin, direction, faceNormal); | ||
if (!distance) continue; | ||
@@ -180,2 +201,3 @@ | ||
closestC = ci; | ||
closestFaceNormal.copy(faceNormal); | ||
} | ||
@@ -192,3 +214,5 @@ } | ||
// Add unique hit objects on mesh to avoid generating lots of objects | ||
if (!mesh.hit.uv) { | ||
if (!mesh.hit.faceNormal) { | ||
mesh.hit.localFaceNormal = new Vec3(); | ||
mesh.hit.faceNormal = new Vec3(); | ||
mesh.hit.uv = new Vec2(); | ||
@@ -199,18 +223,26 @@ mesh.hit.localNormal = new Vec3(); | ||
// Calculate barycoords to find uv and normal values at hit point | ||
a.fromArray(attributes.position.data, closestA * 3); | ||
b.fromArray(attributes.position.data, closestB * 3); | ||
c.fromArray(attributes.position.data, closestC * 3); | ||
this.getBarycoord(mesh.hit.localPoint, a, b, c, barycoord); | ||
// Add face normal data which is already computed | ||
mesh.hit.localFaceNormal.copy(closestFaceNormal); | ||
mesh.hit.faceNormal.copy(mesh.hit.localFaceNormal).transformDirection(mesh.worldMatrix); | ||
if (attributes.uv) { | ||
a2.fromArray(attributes.uv.data, closestA * 2); | ||
b2.fromArray(attributes.uv.data, closestB * 2); | ||
c2.fromArray(attributes.uv.data, closestC * 2); | ||
// Optional data, opt out to optimise a bit if necessary | ||
if (includeUV || includeNormal) { | ||
// Calculate barycoords to find uv values at hit point | ||
a.fromArray(attributes.position.data, closestA * 3); | ||
b.fromArray(attributes.position.data, closestB * 3); | ||
c.fromArray(attributes.position.data, closestC * 3); | ||
this.getBarycoord(mesh.hit.localPoint, a, b, c, barycoord); | ||
} | ||
if (includeUV && attributes.uv) { | ||
uvA.fromArray(attributes.uv.data, closestA * 2); | ||
uvB.fromArray(attributes.uv.data, closestB * 2); | ||
uvC.fromArray(attributes.uv.data, closestC * 2); | ||
mesh.hit.uv.set( | ||
a2.x * barycoord.x + b2.x * barycoord.y + c2.x * barycoord.z, | ||
a2.y * barycoord.x + b2.y * barycoord.y + c2.y * barycoord.z | ||
uvA.x * barycoord.x + uvB.x * barycoord.y + uvC.x * barycoord.z, | ||
uvA.y * barycoord.x + uvB.y * barycoord.y + uvC.y * barycoord.z | ||
); | ||
} | ||
if (attributes.normal) { | ||
if (includeNormal && attributes.normal) { | ||
a.fromArray(attributes.normal.data, closestA * 3); | ||
@@ -272,9 +304,8 @@ b.fromArray(attributes.normal.data, closestB * 3); | ||
intersectTriangle(a, b, c, backfaceCulling = true, origin = this.origin, direction = this.direction) { | ||
intersectTriangle(a, b, c, backfaceCulling = true, origin = this.origin, direction = this.direction, normal = tempVec3g) { | ||
// from https://github.com/mrdoob/three.js/blob/master/src/math/Ray.js | ||
// which is from http://www.geometrictools.com/GTEngine/Include/Mathematics/GteIntrRay3Triangle3.h | ||
const edge1 = tempVec3f; | ||
const edge2 = tempVec3g; | ||
const normal = tempVec3h; | ||
const diff = tempVec3i; | ||
const edge1 = tempVec3h; | ||
const edge2 = tempVec3i; | ||
const diff = tempVec3j; | ||
edge1.sub(b, a); | ||
@@ -304,9 +335,9 @@ edge2.sub(c, a); | ||
getBarycoord(point, a, b, c, target) { | ||
getBarycoord(point, a, b, c, target = tempVec3h) { | ||
// From https://github.com/mrdoob/three.js/blob/master/src/math/Triangle.js | ||
// static/instance method to calculate barycentric coordinates | ||
// based on: http://www.blackpawn.com/texts/pointinpoly/default.html | ||
const v0 = tempVec3g; | ||
const v1 = tempVec3h; | ||
const v2 = tempVec3i; | ||
const v0 = tempVec3i; | ||
const v1 = tempVec3j; | ||
const v2 = tempVec3k; | ||
v0.sub(c, a); | ||
@@ -313,0 +344,0 @@ v1.sub(b, a); |
Sorry, the diff of this file is too big to display
529948
14579