📄 trianglemesh.java
字号:
float va = nx * edge2x + ny * edge2y + nz * edge2z;
float t = iv * va;
if (!r.isInside(t))
return;
float ix = edge2y * r.dz - edge2z * r.dy;
float iy = edge2z * r.dx - edge2x * r.dz;
float iz = edge2x * r.dy - edge2y * r.dx;
float v1 = ix * edge1x + iy * edge1y + iz * edge1z;
float beta = iv * v1;
if (beta < 0)
return;
float v2 = ix * edge0x + iy * edge0y + iz * edge0z;
if ((v1 + v2) * v > v * v)
return;
float gamma = iv * v2;
if (gamma < 0)
return;
r.setMax(t);
state.setIntersection(primID, beta, gamma);
}
public void intersectPrimitive(Ray r, int primID, IntersectionState state) {
// alternative test -- disabled for now
// intersectPrimitiveRobust(r, primID, state);
if (triaccel != null) {
// optional fast intersection method
triaccel[primID].intersect(r, primID, state);
return;
}
intersectTriangleKensler(r, primID, state);
}
public int getNumPrimitives() {
return triangles.length / 3;
}
public void prepareShadingState(ShadingState state) {
state.init();
Instance parent = state.getInstance();
int primID = state.getPrimitiveID();
float u = state.getU();
float v = state.getV();
float w = 1 - u - v;
state.getRay().getPoint(state.getPoint());
int tri = 3 * primID;
int index0 = triangles[tri + 0];
int index1 = triangles[tri + 1];
int index2 = triangles[tri + 2];
Point3 v0p = getPoint(index0);
Point3 v1p = getPoint(index1);
Point3 v2p = getPoint(index2);
Vector3 ng = Point3.normal(v0p, v1p, v2p);
ng = parent.transformNormalObjectToWorld(ng);
ng.normalize();
state.getGeoNormal().set(ng);
switch (normals.interp) {
case NONE:
case FACE: {
state.getNormal().set(ng);
break;
}
case VERTEX: {
int i30 = 3 * index0;
int i31 = 3 * index1;
int i32 = 3 * index2;
float[] normals = this.normals.data;
state.getNormal().x = w * normals[i30 + 0] + u * normals[i31 + 0] + v * normals[i32 + 0];
state.getNormal().y = w * normals[i30 + 1] + u * normals[i31 + 1] + v * normals[i32 + 1];
state.getNormal().z = w * normals[i30 + 2] + u * normals[i31 + 2] + v * normals[i32 + 2];
state.getNormal().set(parent.transformNormalObjectToWorld(state.getNormal()));
state.getNormal().normalize();
break;
}
case FACEVARYING: {
int idx = 3 * tri;
float[] normals = this.normals.data;
state.getNormal().x = w * normals[idx + 0] + u * normals[idx + 3] + v * normals[idx + 6];
state.getNormal().y = w * normals[idx + 1] + u * normals[idx + 4] + v * normals[idx + 7];
state.getNormal().z = w * normals[idx + 2] + u * normals[idx + 5] + v * normals[idx + 8];
state.getNormal().set(parent.transformNormalObjectToWorld(state.getNormal()));
state.getNormal().normalize();
break;
}
}
float uv00 = 0, uv01 = 0, uv10 = 0, uv11 = 0, uv20 = 0, uv21 = 0;
switch (uvs.interp) {
case NONE:
case FACE: {
state.getUV().x = 0;
state.getUV().y = 0;
break;
}
case VERTEX: {
int i20 = 2 * index0;
int i21 = 2 * index1;
int i22 = 2 * index2;
float[] uvs = this.uvs.data;
uv00 = uvs[i20 + 0];
uv01 = uvs[i20 + 1];
uv10 = uvs[i21 + 0];
uv11 = uvs[i21 + 1];
uv20 = uvs[i22 + 0];
uv21 = uvs[i22 + 1];
break;
}
case FACEVARYING: {
int idx = tri << 1;
float[] uvs = this.uvs.data;
uv00 = uvs[idx + 0];
uv01 = uvs[idx + 1];
uv10 = uvs[idx + 2];
uv11 = uvs[idx + 3];
uv20 = uvs[idx + 4];
uv21 = uvs[idx + 5];
break;
}
}
if (uvs.interp != InterpolationType.NONE) {
// get exact uv coords and compute tangent vectors
state.getUV().x = w * uv00 + u * uv10 + v * uv20;
state.getUV().y = w * uv01 + u * uv11 + v * uv21;
float du1 = uv00 - uv20;
float du2 = uv10 - uv20;
float dv1 = uv01 - uv21;
float dv2 = uv11 - uv21;
Vector3 dp1 = Point3.sub(v0p, v2p, new Vector3()), dp2 = Point3.sub(v1p, v2p, new Vector3());
float determinant = du1 * dv2 - dv1 * du2;
if (determinant == 0.0f) {
// create basis in world space
state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal()));
} else {
float invdet = 1.f / determinant;
// Vector3 dpdu = new Vector3();
// dpdu.x = (dv2 * dp1.x - dv1 * dp2.x) * invdet;
// dpdu.y = (dv2 * dp1.y - dv1 * dp2.y) * invdet;
// dpdu.z = (dv2 * dp1.z - dv1 * dp2.z) * invdet;
Vector3 dpdv = new Vector3();
dpdv.x = (-du2 * dp1.x + du1 * dp2.x) * invdet;
dpdv.y = (-du2 * dp1.y + du1 * dp2.y) * invdet;
dpdv.z = (-du2 * dp1.z + du1 * dp2.z) * invdet;
dpdv = parent.transformVectorObjectToWorld(dpdv);
// create basis in world space
state.setBasis(OrthoNormalBasis.makeFromWV(state.getNormal(), dpdv));
}
} else
state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal()));
int shaderIndex = faceShaders == null ? 0 : (faceShaders[primID] & 0xFF);
state.setShader(parent.getShader(shaderIndex));
state.setModifier(parent.getModifier(shaderIndex));
}
public void init() {
triaccel = null;
int nt = getNumPrimitives();
if (!smallTriangles) {
// too many triangles? -- don't generate triaccel to save memory
if (nt > 2000000) {
UI.printWarning(Module.GEOM, "TRI - Too many triangles -- triaccel generation skipped");
return;
}
triaccel = new WaldTriangle[nt];
for (int i = 0; i < nt; i++)
triaccel[i] = new WaldTriangle(this, i);
}
}
protected Point3 getPoint(int i) {
i *= 3;
return new Point3(points[i], points[i + 1], points[i + 2]);
}
public void getPoint(int tri, int i, Point3 p) {
int index = 3 * triangles[3 * tri + i];
p.set(points[index], points[index + 1], points[index + 2]);
}
private static final class WaldTriangle {
// private data for fast triangle intersection testing
private int k;
private float nu, nv, nd;
private float bnu, bnv, bnd;
private float cnu, cnv, cnd;
private WaldTriangle(TriangleMesh mesh, int tri) {
k = 0;
tri *= 3;
int index0 = mesh.triangles[tri + 0];
int index1 = mesh.triangles[tri + 1];
int index2 = mesh.triangles[tri + 2];
Point3 v0p = mesh.getPoint(index0);
Point3 v1p = mesh.getPoint(index1);
Point3 v2p = mesh.getPoint(index2);
Vector3 ng = Point3.normal(v0p, v1p, v2p);
if (Math.abs(ng.x) > Math.abs(ng.y) && Math.abs(ng.x) > Math.abs(ng.z))
k = 0;
else if (Math.abs(ng.y) > Math.abs(ng.z))
k = 1;
else
k = 2;
float ax, ay, bx, by, cx, cy;
switch (k) {
case 0: {
nu = ng.y / ng.x;
nv = ng.z / ng.x;
nd = v0p.x + (nu * v0p.y) + (nv * v0p.z);
ax = v0p.y;
ay = v0p.z;
bx = v2p.y - ax;
by = v2p.z - ay;
cx = v1p.y - ax;
cy = v1p.z - ay;
break;
}
case 1: {
nu = ng.z / ng.y;
nv = ng.x / ng.y;
nd = (nv * v0p.x) + v0p.y + (nu * v0p.z);
ax = v0p.z;
ay = v0p.x;
bx = v2p.z - ax;
by = v2p.x - ay;
cx = v1p.z - ax;
cy = v1p.x - ay;
break;
}
case 2:
default: {
nu = ng.x / ng.z;
nv = ng.y / ng.z;
nd = (nu * v0p.x) + (nv * v0p.y) + v0p.z;
ax = v0p.x;
ay = v0p.y;
bx = v2p.x - ax;
by = v2p.y - ay;
cx = v1p.x - ax;
cy = v1p.y - ay;
}
}
float det = bx * cy - by * cx;
bnu = -by / det;
bnv = bx / det;
bnd = (by * ax - bx * ay) / det;
cnu = cy / det;
cnv = -cx / det;
cnd = (cx * ay - cy * ax) / det;
}
void intersectBox(Ray r, float hx, float hy, float hz, int primID, IntersectionState state) {
switch (k) {
case 0: {
float hu = hy;
float hv = hz;
float u = hu * bnu + hv * bnv + bnd;
if (u < 0.0f)
u = 0;
float v = hu * cnu + hv * cnv + cnd;
if (v < 0.0f)
v = 0;
state.setIntersection(primID, u, v);
return;
}
case 1: {
float hu = hz;
float hv = hx;
float u = hu * bnu + hv * bnv + bnd;
if (u < 0.0f)
u = 0;
float v = hu * cnu + hv * cnv + cnd;
if (v < 0.0f)
v = 0;
state.setIntersection(primID, u, v);
return;
}
case 2: {
float hu = hx;
float hv = hy;
float u = hu * bnu + hv * bnv + bnd;
if (u < 0.0f)
u = 0;
float v = hu * cnu + hv * cnv + cnd;
if (v < 0.0f)
v = 0;
state.setIntersection(primID, u, v);
return;
}
}
}
void intersect(Ray r, int primID, IntersectionState state) {
switch (k) {
case 0: {
float det = 1.0f / (r.dx + nu * r.dy + nv * r.dz);
float t = (nd - r.ox - nu * r.oy - nv * r.oz) * det;
if (!r.isInside(t))
return;
float hu = r.oy + t * r.dy;
float hv = r.oz + t * r.dz;
float u = hu * bnu + hv * bnv + bnd;
if (u < 0.0f)
return;
float v = hu * cnu + hv * cnv + cnd;
if (v < 0.0f)
return;
if (u + v > 1.0f)
return;
r.setMax(t);
state.setIntersection(primID, u, v);
return;
}
case 1: {
float det = 1.0f / (r.dy + nu * r.dz + nv * r.dx);
float t = (nd - r.oy - nu * r.oz - nv * r.ox) * det;
if (!r.isInside(t))
return;
float hu = r.oz + t * r.dz;
float hv = r.ox + t * r.dx;
float u = hu * bnu + hv * bnv + bnd;
if (u < 0.0f)
return;
float v = hu * cnu + hv * cnv + cnd;
if (v < 0.0f)
return;
if (u + v > 1.0f)
return;
r.setMax(t);
state.setIntersection(primID, u, v);
return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -