public void getSamples(ShadingState state) { if (Vector3.dot(sunDirWorld, state.getGeoNormal()) > 0 && Vector3.dot(sunDirWorld, state.getNormal()) > 0) { LightSample dest = new LightSample(); dest.setShadowRay(new Ray(state.getPoint(), sunDirWorld)); dest.getShadowRay().setMax(float.MaxValue); dest.setRadiance(sunColor, sunColor); dest.traceShadow(state); state.addSample(dest); } int n = state.getDiffuseDepth() > 0 ? 1 : numSkySamples; for (int i = 0; i < n; i++) { // random offset on unit square, we use the infinite version of // getRandom because the light sampling is adaptive double randX = state.getRandom(i, 0, n); double randY = state.getRandom(i, 1, n); int x = 0; while (randX >= colHistogram[x] && x < colHistogram.Length - 1) { x++; } float[] rowHistogram = imageHistogram[x]; int y = 0; while (randY >= rowHistogram[y] && y < rowHistogram.Length - 1) { y++; } // sample from (x, y) float u = (float)((x == 0) ? (randX / colHistogram[0]) : ((randX - colHistogram[x - 1]) / (colHistogram[x] - colHistogram[x - 1]))); float v = (float)((y == 0) ? (randY / rowHistogram[0]) : ((randY - rowHistogram[y - 1]) / (rowHistogram[y] - rowHistogram[y - 1]))); float px = ((x == 0) ? colHistogram[0] : (colHistogram[x] - colHistogram[x - 1])); float py = ((y == 0) ? rowHistogram[0] : (rowHistogram[y] - rowHistogram[y - 1])); float su = (x + u) / colHistogram.Length; float sv = (y + v) / rowHistogram.Length; float invP = (float)Math.Sin(sv * Math.PI) * jacobian / (n * px * py); Vector3 localDir = getDirection(su, sv); Vector3 dir = basis.transform(localDir, new Vector3()); if (Vector3.dot(dir, state.getGeoNormal()) > 0 && Vector3.dot(dir, state.getNormal()) > 0) { LightSample dest = new LightSample(); dest.setShadowRay(new Ray(state.getPoint(), dir)); dest.getShadowRay().setMax(float.MaxValue); Color radiance = getSkyRGB(localDir); dest.setRadiance(radiance, radiance); dest.getDiffuseRadiance().mul(invP); dest.getSpecularRadiance().mul(invP); dest.traceShadow(state); state.addSample(dest); } } }
public void scatterPhoton(ShadingState state, Color power) { Color diffuse; // make sure we are on the right side of the material if (Vector3.dot(state.getNormal(), state.getRay().getDirection()) > 0.0) { state.getNormal().negate(); state.getGeoNormal().negate(); } diffuse = getDiffuse(state); state.storePhoton(state.getRay().getDirection(), power, diffuse); float avg = diffuse.getAverage(); double rnd = state.getRandom(0, 0, 1); if (rnd < avg) { // photon is scattered power.mul(diffuse).mul(1.0f / avg); OrthoNormalBasis onb = state.getBasis(); double u = 2 * Math.PI * rnd / avg; double v = state.getRandom(0, 1, 1); float s = (float)Math.Sqrt(v); float s1 = (float)Math.Sqrt(1.0 - v); Vector3 w = new Vector3((float)Math.Cos(u) * s, (float)Math.Sin(u) * s, s1); w = onb.transform(w, new Vector3()); state.traceDiffusePhoton(new Ray(state.getPoint(), w), power); } }
public void ScatterPhoton(ShadingState state, Color power) { Color diffuse; // make sure we are on the right side of the material if (Vector3.dot(state.getNormal(), state.getRay().getDirection()) > 0.0) { state.getNormal().negate(); state.getGeoNormal().negate(); } diffuse = Color.GRAY; state.storePhoton(state.getRay().getDirection(), power, diffuse); float avg = diffuse.getAverage(); double rnd = state.getRandom(0, 0, 1); if (rnd < avg) { // photon is scattered power.mul(diffuse).mul(1.0f / avg); OrthoNormalBasis onb = state.getBasis(); double u = 2 * Math.PI * rnd / avg; double v = state.getRandom(0, 1, 1); float s = (float)Math.Sqrt(v); float s1 = (float)Math.Sqrt(1.0 - v); Vector3 w = new Vector3((float)Math.Cos(u) * s, (float)Math.Sin(u) * s, s1); w = onb.transform(w, new Vector3()); state.traceDiffusePhoton(new Ray(state.getPoint(), w), power); } }
public void prepareShadingState(ShadingState state) { state.init(); state.getRay().getPoint(state.getPoint()); Instance parent = state.getInstance(); // get local point Point3 p = state.transformWorldToObject(state.getPoint()); // compute local normal float deriv = p.x * p.x + p.y * p.y + p.z * p.z - ri2 - ro2; state.getNormal().set(p.x * deriv, p.y * deriv, p.z * deriv + 2 * ro2 * p.z); state.getNormal().normalize(); double phi = Math.Asin(MathUtils.clamp(p.z / ri, -1, 1)); double theta = Math.Atan2(p.y, p.x); if (theta < 0) { theta += 2 * Math.PI; } state.getUV().x = (float)(theta / (2 * Math.PI)); state.getUV().y = (float)((phi + Math.PI / 2) / Math.PI); state.setShader(parent.getShader(0)); state.setModifier(parent.getModifier(0)); // into world space Vector3 worldNormal = state.transformNormalObjectToWorld(state.getNormal()); state.getNormal().set(worldNormal); state.getNormal().normalize(); state.getGeoNormal().set(state.getNormal()); // make basis in world space state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal())); }
public void getSamples(ShadingState state) { if (Vector3.dot(dir, state.getGeoNormal()) < 0 && Vector3.dot(dir, state.getNormal()) < 0) { // project point onto source plane float x = state.getPoint().x - src.x; float y = state.getPoint().y - src.y; float z = state.getPoint().z - src.z; float t = ((x * dir.x) + (y * dir.y) + (z * dir.z)); if (t >= 0.0) { x -= (t * dir.x); y -= (t * dir.y); z -= (t * dir.z); if (((x * x) + (y * y) + (z * z)) <= r2) { Point3 p = new Point3(); p.x = src.x + x; p.y = src.y + y; p.z = src.z + z; LightSample dest = new LightSample(); dest.setShadowRay(new Ray(state.getPoint(), p)); dest.setRadiance(radiance, radiance); dest.traceShadow(state); state.addSample(dest); } } } }
public void prepareShadingState(ShadingState state) { state.init(); state.getRay().getPoint(state.getPoint()); Instance parent = state.getInstance(); Point3 localPoint = parent.transformWorldToObject(state.getPoint()); state.getNormal().set(localPoint.x, localPoint.y, localPoint.z); state.getNormal().normalize(); float phi = (float)Math.Atan2(state.getNormal().y, state.getNormal().x); if (phi < 0) phi += (float)(2 * Math.PI); float theta = (float)Math.Acos(state.getNormal().z); state.getUV().y = theta / (float)Math.PI; state.getUV().x = phi / (float)(2 * Math.PI); Vector3 v = new Vector3(); v.x = -2 * (float)Math.PI * state.getNormal().y; v.y = 2 * (float)Math.PI * state.getNormal().x; v.z = 0; state.setShader(parent.getShader(0)); state.setModifier(parent.getModifier(0)); // into world space Vector3 worldNormal = parent.transformNormalObjectToWorld(state.getNormal()); v = parent.transformVectorObjectToWorld(v); state.getNormal().set(worldNormal); state.getNormal().normalize(); state.getGeoNormal().set(state.getNormal()); // compute basis in world space state.setBasis(OrthoNormalBasis.makeFromWV(state.getNormal(), v)); }
public void prepareShadingState(ShadingState state) { state.init(); Instance i = state.getInstance(); state.getRay().getPoint(state.getPoint()); Ray r = state.getRay(); IShader s = i.getShader(0); state.setShader(s != null ? s : this); int primID = state.getPrimitiveID(); int hair = primID / numSegments; int line = primID % numSegments; int vRoot = hair * 3 * (numSegments + 1); int v0 = vRoot + line * 3; // tangent vector Vector3 v = getTangent(line, v0, state.getV()); v = state.transformVectorObjectToWorld(v); state.setBasis(OrthoNormalBasis.makeFromWV(v, new Vector3(-r.dx, -r.dy, -r.dz))); state.getBasis().swapVW(); // normal state.getNormal().set(0, 0, 1); state.getBasis().transform(state.getNormal()); state.getGeoNormal().set(state.getNormal()); state.getUV().set(0, (line + state.getV()) / numSegments); }
public void prepareShadingState(ShadingState state) { state.init(); state.getRay().getPoint(state.getPoint()); Instance parent = state.getInstance(); Point3 localPoint = state.transformWorldToObject(state.getPoint()); state.getNormal().set(localPoint.x, localPoint.y, 0); state.getNormal().normalize(); float phi = (float)Math.Atan2(state.getNormal().y, state.getNormal().x); if (phi < 0) { phi += (float)(2.0 * Math.PI); } state.getUV().x = phi / (float)(2 * Math.PI); state.getUV().y = (localPoint.z + 1) * 0.5f; state.setShader(parent.getShader(0)); state.setModifier(parent.getModifier(0)); // into world space Vector3 worldNormal = state.transformNormalObjectToWorld(state.getNormal()); Vector3 v = state.transformVectorObjectToWorld(new Vector3(0, 0, 1)); state.getNormal().set(worldNormal); state.getNormal().normalize(); state.getGeoNormal().set(state.getNormal()); // compute basis in world space state.setBasis(OrthoNormalBasis.makeFromWV(state.getNormal(), v)); }
public void getSamples(ShadingState state) { Vector3 d = Point3.sub(lightPoint, state.getPoint(), new Vector3()); if (Vector3.dot(d, state.getNormal()) > 0 && Vector3.dot(d, state.getGeoNormal()) > 0) { LightSample dest = new LightSample(); // prepare shadow ray dest.setShadowRay(new Ray(state.getPoint(), lightPoint)); float scale = 1.0f / (float)(4 * Math.PI * lightPoint.distanceToSquared(state.getPoint())); dest.setRadiance(power, power); dest.getDiffuseRadiance().mul(scale); dest.getSpecularRadiance().mul(scale); dest.traceShadow(state); state.addSample(dest); } }
public void prepareShadingState(ShadingState state) { state.init(); state.getRay().getPoint(state.getPoint()); Instance parent = state.getInstance(); Vector3 worldNormal = state.transformNormalObjectToWorld(normal); state.getNormal().set(worldNormal); state.getGeoNormal().set(worldNormal); state.setShader(parent.getShader(0)); state.setModifier(parent.getModifier(0)); Point3 p = state.transformWorldToObject(state.getPoint()); float hu, hv; switch (k) { case 0: { hu = p.y; hv = p.z; break; } case 1: { hu = p.z; hv = p.x; break; } case 2: { hu = p.x; hv = p.y; break; } default: hu = hv = 0; break; } state.getUV().x = hu * bnu + hv * bnv + bnd; state.getUV().y = hu * cnu + hv * cnv + cnd; state.setBasis(OrthoNormalBasis.makeFromW(normal)); }
public void prepareShadingState(ShadingState state) { state.init(); state.getRay().getPoint(state.getPoint()); Instance parent = state.getInstance(); Point3 n = parent.transformWorldToObject(state.getPoint()); state.getNormal().set(n.x * (2 * n.x * n.x - 1), n.y * (2 * n.y * n.y - 1), n.z * (2 * n.z * n.z - 1)); state.getNormal().normalize(); state.setShader(parent.getShader(0)); state.setModifier(parent.getModifier(0)); // into world space Vector3 worldNormal = parent.transformNormalObjectToWorld(state.getNormal()); state.getNormal().set(worldNormal); state.getNormal().normalize(); state.getGeoNormal().set(state.getNormal()); // create basis in world space state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal())); }
public void prepareShadingState(ShadingState state) { state.init(); state.getRay().getPoint(state.getPoint()); int n = state.getPrimitiveID(); /* * * * switch (n) * { * case 0: * state.getNormal().set(new Vector3(1, 0, 0)); * break; * case 1: * state.getNormal().set(new Vector3(-1, 0, 0)); * break; * case 2: * state.getNormal().set(new Vector3(0, 1, 0)); * break; * case 3: * state.getNormal().set(new Vector3(0, -1, 0)); * break; * case 4: * state.getNormal().set(new Vector3(0, 0, 1)); * break; * case 5: * state.getNormal().set(new Vector3(0, 0, -1)); * break; * default: * state.getNormal().set(new Vector3(0, 0, 0)); * break; * } * */ state.getNormal().set(normalVectors[n]); state.getGeoNormal().set(state.getNormal()); state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal())); state.setShader(state.getInstance().getShader(0)); state.setModifier(state.getInstance().getModifier(0)); }
public void prepareShadingState(ShadingState state) { state.init(); state.getRay().getPoint(state.getPoint()); Instance parent = state.getInstance(); Vector3 normal; switch (state.getPrimitiveID()) { case 0: normal = new Vector3(-1, 0, 0); break; case 1: normal = new Vector3(1, 0, 0); break; case 2: normal = new Vector3(0, -1, 0); break; case 3: normal = new Vector3(0, 1, 0); break; case 4: normal = new Vector3(0, 0, -1); break; case 5: normal = new Vector3(0, 0, 1); break; default: normal = new Vector3(0, 0, 0); break; } state.getNormal().set(state.transformNormalObjectToWorld(normal)); state.getGeoNormal().set(state.getNormal()); state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal())); state.setShader(parent.getShader(0)); state.setModifier(parent.getModifier(0)); }
public void prepareShadingState(ShadingState state) { state.init(); state.getRay().getPoint(state.getPoint()); Instance parent = state.getInstance(); Point3 n = state.transformWorldToObject(state.getPoint()); state.getNormal().set(n.x * (2 * n.x * n.x - 1), n.y * (2 * n.y * n.y - 1), n.z * (2 * n.z * n.z - 1)); state.getNormal().normalize(); state.setShader(parent.getShader(0)); state.setModifier(parent.getModifier(0)); // into world space Vector3 worldNormal = state.transformNormalObjectToWorld(state.getNormal()); state.getNormal().set(worldNormal); state.getNormal().normalize(); state.getGeoNormal().set(state.getNormal()); // create basis in world space state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal())); }
public void prepareShadingState(ShadingState state) { state.init(); state.getRay().getPoint(state.getPoint()); int n = state.getPrimitiveID(); switch (n) { case 0: state.getNormal().set(new Vector3(1, 0, 0)); break; case 1: state.getNormal().set(new Vector3(-1, 0, 0)); break; case 2: state.getNormal().set(new Vector3(0, 1, 0)); break; case 3: state.getNormal().set(new Vector3(0, -1, 0)); break; case 4: state.getNormal().set(new Vector3(0, 0, 1)); break; case 5: state.getNormal().set(new Vector3(0, 0, -1)); break; default: state.getNormal().set(new Vector3(0, 0, 0)); break; } state.getGeoNormal().set(state.getNormal()); state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal())); state.setShader(state.getInstance().getShader(0)); state.setModifier(state.getInstance().getModifier(0)); }
public void prepareShadingState(ShadingState state) { state.init(); state.getRay().getPoint(state.getPoint()); Instance parent = state.getInstance(); Point3 localPoint = state.transformWorldToObject(state.getPoint()); float cx = state.getU(); float cy = state.getV(); float cz = state.getW(); state.getNormal().set(localPoint.x - cx, localPoint.y - cy, localPoint.z - cz); state.getNormal().normalize(); float phi = (float)Math.Atan2(state.getNormal().y, state.getNormal().x); if (phi < 0) { phi += (float)(2.0 * Math.PI); } float theta = (float)Math.Acos(state.getNormal().z); state.getUV().y = theta / (float)Math.PI; state.getUV().x = phi / (float)(2 * Math.PI); Vector3 v = new Vector3(); v.x = -2 * (float)Math.PI * state.getNormal().y; v.y = 2 * (float)Math.PI * state.getNormal().x; v.z = 0; state.setShader(parent.getShader(0)); state.setModifier(parent.getModifier(0)); // into world space Vector3 worldNormal = state.transformNormalObjectToWorld(state.getNormal()); v = state.transformVectorObjectToWorld(v); state.getNormal().set(worldNormal); state.getNormal().normalize(); state.getGeoNormal().set(state.getNormal()); // compute basis in world space state.setBasis(OrthoNormalBasis.makeFromWV(state.getNormal(), v)); }
public void prepareShadingState(ShadingState state) { state.init(); state.getRay().getPoint(state.getPoint()); Instance parent = state.getInstance(); float u = state.getU(); float v = state.getV(); float[] bu = bernstein(u); float[] bdu = bernsteinDeriv(u); float[] bv = bernstein(v); float[] bdv = bernsteinDeriv(v); getPatchPoint(u, v, patches[state.getPrimitiveID()], bu, bv, bdu, bdv, new Point3(), state.getNormal()); state.getNormal().set(parent.transformNormalObjectToWorld(state.getNormal())); state.getNormal().normalize(); state.getGeoNormal().set(state.getNormal()); state.getUV().set(u, v); state.setShader(parent.getShader(0)); state.setModifier(parent.getModifier(0)); // FIXME: use actual derivatives to create basis state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal())); }
public void prepareShadingState(ShadingState state) { state.init(); state.getRay().getPoint(state.getPoint()); Point3 localPoint = state.transformWorldToObject(state.getPoint()); localPoint.x -= particles[3 * state.getPrimitiveID() + 0]; localPoint.y -= particles[3 * state.getPrimitiveID() + 1]; localPoint.z -= particles[3 * state.getPrimitiveID() + 2]; state.getNormal().set(localPoint.x, localPoint.y, localPoint.z); state.getNormal().normalize(); state.setShader(state.getInstance().getShader(0)); state.setModifier(state.getInstance().getModifier(0)); // into object space Vector3 worldNormal = state.transformNormalObjectToWorld(state.getNormal()); state.getNormal().set(worldNormal); state.getNormal().normalize(); state.getGeoNormal().set(state.getNormal()); state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal())); }
public void getSamples(ShadingState state) { if (meshlight.numSamples == 0) return; Vector3 n = state.getNormal(); Point3 p = state.getPoint(); // vector towards each vertex of the light source Vector3 p0 = Point3.sub(meshlight.getPoint(meshlight.triangles[tri3 + 0]), p, new Vector3()); // cull triangle if it is facing the wrong way if (Vector3.dot(p0, ng) >= 0) return; Vector3 p1 = Point3.sub(meshlight.getPoint(meshlight.triangles[tri3 + 1]), p, new Vector3()); Vector3 p2 = Point3.sub(meshlight.getPoint(meshlight.triangles[tri3 + 2]), p, new Vector3()); // if all three vertices are below the hemisphere, stop if (Vector3.dot(p0, n) <= 0 && Vector3.dot(p1, n) <= 0 && Vector3.dot(p2, n) <= 0) return; p0.normalize(); p1.normalize(); p2.normalize(); float dot = Vector3.dot(p2, p0); Vector3 h = new Vector3(); h.x = p2.x - dot * p0.x; h.y = p2.y - dot * p0.y; h.z = p2.z - dot * p0.z; float hlen = h.Length(); if (hlen > 1e-6f) h.div(hlen); else return; Vector3 n0 = Vector3.cross(p0, p1, new Vector3()); float len0 = n0.Length(); if (len0 > 1e-6f) n0.div(len0); else return; Vector3 n1 = Vector3.cross(p1, p2, new Vector3()); float len1 = n1.Length(); if (len1 > 1e-6f) n1.div(len1); else return; Vector3 n2 = Vector3.cross(p2, p0, new Vector3()); float len2 = n2.Length(); if (len2 > 1e-6f) n2.div(len2); else return; float cosAlpha = MathUtils.clamp(-Vector3.dot(n2, n0), -1.0f, 1.0f); float cosBeta = MathUtils.clamp(-Vector3.dot(n0, n1), -1.0f, 1.0f); float cosGamma = MathUtils.clamp(-Vector3.dot(n1, n2), -1.0f, 1.0f); float alpha = (float)Math.Acos(cosAlpha); float beta = (float)Math.Acos(cosBeta); float gamma = (float)Math.Acos(cosGamma); float area = alpha + beta + gamma - (float)Math.PI; float cosC = MathUtils.clamp(Vector3.dot(p0, p1), -1.0f, 1.0f); float salpha = (float)Math.Sin(alpha); float product = salpha * cosC; // use lower sampling depth for diffuse bounces int samples = state.getDiffuseDepth() > 0 ? 1 : meshlight.numSamples; Color c = Color.mul(area / samples, meshlight.radiance); for (int i = 0; i < samples; i++) { // random offset on unit square double randX = state.getRandom(i, 0, samples); double randY = state.getRandom(i, 1, samples); float phi = (float)randX * area - alpha + (float)Math.PI; float sinPhi = (float)Math.Sin(phi); float cosPhi = (float)Math.Cos(phi); float u = cosPhi + cosAlpha; float v = sinPhi - product; float q = (-v + cosAlpha * (cosPhi * -v + sinPhi * u)) / (salpha * (sinPhi * -v - cosPhi * u)); float q1 = 1.0f - q * q; if (q1 < 0.0f) q1 = 0.0f; float sqrtq1 = (float)Math.Sqrt(q1); float ncx = q * p0.x + sqrtq1 * h.x; float ncy = q * p0.y + sqrtq1 * h.y; float ncz = q * p0.z + sqrtq1 * h.z; dot = p1.dot(ncx, ncy, ncz); float z = 1.0f - (float)randY * (1.0f - dot); float z1 = 1.0f - z * z; if (z1 < 0.0f) z1 = 0.0f; Vector3 nd = new Vector3(); nd.x = ncx - dot * p1.x; nd.y = ncy - dot * p1.y; nd.z = ncz - dot * p1.z; nd.normalize(); float sqrtz1 = (float)Math.Sqrt(z1); Vector3 result = new Vector3(); result.x = z * p1.x + sqrtz1 * nd.x; result.y = z * p1.y + sqrtz1 * nd.y; result.z = z * p1.z + sqrtz1 * nd.z; // make sure the sample is in the right hemisphere - facing in // the right direction if (Vector3.dot(result, n) > 0 && Vector3.dot(result, state.getGeoNormal()) > 0 && Vector3.dot(result, ng) < 0) { // compute intersection with triangle (if any) Ray shadowRay = new Ray(state.getPoint(), result); if (!intersectTriangleKensler(shadowRay)) continue; LightSample dest = new LightSample(); dest.setShadowRay(shadowRay); // prepare sample dest.setRadiance(c, c); dest.traceShadow(state); state.addSample(dest); } } }
public void getSamples(ShadingState state) { if (samples == null) { int n = state.getDiffuseDepth() > 0 ? 1 : numSamples; for (int i = 0; i < n; i++) { // random offset on unit square, we use the infinite version of // getRandom because the light sampling is adaptive double randX = state.getRandom(i, 0, n); double randY = state.getRandom(i, 1, n); int x = 0; while (randX >= colHistogram[x] && x < colHistogram.Length - 1) x++; float[] rowHistogram = imageHistogram[x]; int y = 0; while (randY >= rowHistogram[y] && y < rowHistogram.Length - 1) y++; // sample from (x, y) float u = (float)((x == 0) ? (randX / colHistogram[0]) : ((randX - colHistogram[x - 1]) / (colHistogram[x] - colHistogram[x - 1]))); float v = (float)((y == 0) ? (randY / rowHistogram[0]) : ((randY - rowHistogram[y - 1]) / (rowHistogram[y] - rowHistogram[y - 1]))); float px = ((x == 0) ? colHistogram[0] : (colHistogram[x] - colHistogram[x - 1])); float py = ((y == 0) ? rowHistogram[0] : (rowHistogram[y] - rowHistogram[y - 1])); float su = (x + u) / colHistogram.Length; float sv = (y + v) / rowHistogram.Length; float invP = (float)Math.Sin(sv * Math.PI) * jacobian / (n * px * py); Vector3 dir = getDirection(su, sv); basis.transform(dir); if (Vector3.dot(dir, state.getGeoNormal()) > 0) { LightSample dest = new LightSample(); dest.setShadowRay(new Ray(state.getPoint(), dir)); dest.getShadowRay().setMax(float.MaxValue); Color radiance = texture.getPixel(su, sv); dest.setRadiance(radiance, radiance); dest.getDiffuseRadiance().mul(invP); dest.getSpecularRadiance().mul(invP); dest.traceShadow(state); state.addSample(dest); } } } else { for (int i = 0; i < numSamples; i++) { if (Vector3.dot(samples[i], state.getGeoNormal()) > 0 && Vector3.dot(samples[i], state.getNormal()) > 0) { LightSample dest = new LightSample(); dest.setShadowRay(new Ray(state.getPoint(), samples[i])); dest.getShadowRay().setMax(float.MaxValue); dest.setRadiance(colors[i], colors[i]); dest.traceShadow(state); state.addSample(dest); } } } }
public void prepareShadingState(ShadingState state) { state.init(); state.getRay().getPoint(state.getPoint()); Instance parent = state.getInstance(); // get local point Point3 p = parent.transformWorldToObject(state.getPoint()); // compute local normal float deriv = p.x * p.x + p.y * p.y + p.z * p.z - ri2 - ro2; state.getNormal().set(p.x * deriv, p.y * deriv, p.z * deriv + 2 * ro2 * p.z); state.getNormal().normalize(); double phi = Math.Asin(MathUtils.clamp(p.z / ri, -1, 1)); double theta = Math.Atan2(p.y, p.x); if (theta < 0) theta += 2 * Math.PI; state.getUV().x = (float)(theta / (2 * Math.PI)); state.getUV().y = (float)((phi + Math.PI / 2) / Math.PI); state.setShader(parent.getShader(0)); state.setModifier(parent.getModifier(0)); // into world space Vector3 worldNormal = parent.transformNormalObjectToWorld(state.getNormal()); state.getNormal().set(worldNormal); state.getNormal().normalize(); state.getGeoNormal().set(state.getNormal()); // make basis in world space state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal())); }
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 = triangleMesh.triangles[tri + 0]; int index1 = triangleMesh.triangles[tri + 1]; int index2 = triangleMesh.triangles[tri + 2]; Point3 v0p = triangleMesh.getPoint(index0); Point3 v1p = triangleMesh.getPoint(index1); Point3 v2p = triangleMesh.getPoint(index2); // get object space point from barycentric coordinates state.getPoint().x = w * v0p.x + u * v1p.x + v * v2p.x; state.getPoint().y = w * v0p.y + u * v1p.y + v * v2p.y; state.getPoint().z = w * v0p.z + u * v1p.z + v * v2p.z; // move into world space state.getPoint().set(state.transformObjectToWorld(state.getPoint())); Vector3 ng = Point3.normal(v0p, v1p, v2p); if (parent != null) { ng = state.transformNormalObjectToWorld(ng); } ng.normalize(); state.getGeoNormal().set(ng); switch (triangleMesh.normals.interp) { case ParameterList.InterpolationType.NONE: case ParameterList.InterpolationType.FACE: { state.getNormal().set(ng); break; } case ParameterList.InterpolationType.VERTEX: { int i30 = 3 * index0; int i31 = 3 * index1; int i32 = 3 * index2; float[] normals = triangleMesh.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]; if (parent != null) { state.getNormal().set(state.transformNormalObjectToWorld(state.getNormal())); } state.getNormal().normalize(); break; } case ParameterList.InterpolationType.FACEVARYING: { int idx = 3 * tri; float[] normals = triangleMesh.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]; if (parent != null) { state.getNormal().set(state.transformNormalObjectToWorld(state.getNormal())); } state.getNormal().normalize(); break; } } float uv00 = 0, uv01 = 0, uv10 = 0, uv11 = 0, uv20 = 0, uv21 = 0; switch (triangleMesh.uvs.interp) { case ParameterList.InterpolationType.NONE: case ParameterList.InterpolationType.FACE: { state.getUV().x = 0; state.getUV().y = 0; break; } case ParameterList.InterpolationType.VERTEX: { int i20 = 2 * index0; int i21 = 2 * index1; int i22 = 2 * index2; float[] uvs = triangleMesh.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 ParameterList.InterpolationType.FACEVARYING: { int idx = tri << 1; float[] uvs = triangleMesh.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 (triangleMesh.uvs.interp != ParameterList.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 / 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; if (parent != null) { dpdv = state.transformVectorObjectToWorld(dpdv); } // create basis in world space state.setBasis(OrthoNormalBasis.makeFromWV(state.getNormal(), dpdv)); } } else { state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal())); } int shaderIndex = triangleMesh.faceShaders == null ? 0 : (triangleMesh.faceShaders[primID] & 0xFF); state.setShader(parent.getShader(shaderIndex)); }
public void prepareShadingState(ShadingState state) { state.init(); Instance parent = state.getInstance(); int primID = state.getPrimitiveID(); float u = state.getU(); float v = state.getV(); state.getRay().getPoint(state.getPoint()); int quad = 4 * primID; int index0 = quads[quad + 0]; int index1 = quads[quad + 1]; int index2 = quads[quad + 2]; int index3 = quads[quad + 3]; Point3 v0p = getPoint(index0); Point3 v1p = getPoint(index1); Point3 v2p = getPoint(index2); Point3 v3p = getPoint(index3); float tanux = (1 - v) * (v1p.x - v0p.x) + v * (v2p.x - v3p.x); float tanuy = (1 - v) * (v1p.y - v0p.y) + v * (v2p.y - v3p.y); float tanuz = (1 - v) * (v1p.z - v0p.z) + v * (v2p.z - v3p.z); float tanvx = (1 - u) * (v3p.x - v0p.x) + u * (v2p.x - v1p.x); float tanvy = (1 - u) * (v3p.y - v0p.y) + u * (v2p.y - v1p.y); float tanvz = (1 - u) * (v3p.z - v0p.z) + u * (v2p.z - v1p.z); float nx = tanuy * tanvz - tanuz * tanvy; float ny = tanuz * tanvx - tanux * tanvz; float nz = tanux * tanvy - tanuy * tanvx; Vector3 ng = new Vector3(nx, ny, nz); ng = state.transformNormalObjectToWorld(ng); ng.normalize(); state.getGeoNormal().set(ng); float k00 = (1 - u) * (1 - v); float k10 = u * (1 - v); float k01 = (1 - u) * v; float k11 = u * v; switch (normals.interp) { case ParameterList.InterpolationType.NONE: case ParameterList.InterpolationType.FACE: { state.getNormal().set(ng); break; } case ParameterList.InterpolationType.VERTEX: { int i30 = 3 * index0; int i31 = 3 * index1; int i32 = 3 * index2; int i33 = 3 * index3; float[] normals1 = this.normals.data; state.getNormal().x = k00 * normals1[i30 + 0] + k10 * normals1[i31 + 0] + k11 * normals1[i32 + 0] + k01 * normals1[i33 + 0]; state.getNormal().y = k00 * normals1[i30 + 1] + k10 * normals1[i31 + 1] + k11 * normals1[i32 + 1] + k01 * normals1[i33 + 1]; state.getNormal().z = k00 * normals1[i30 + 2] + k10 * normals1[i31 + 2] + k11 * normals1[i32 + 2] + k01 * normals1[i33 + 2]; state.getNormal().set(state.transformNormalObjectToWorld(state.getNormal())); state.getNormal().normalize(); break; } case ParameterList.InterpolationType.FACEVARYING: { int idx = 3 * quad; float[] normals1 = this.normals.data; state.getNormal().x = k00 * normals1[idx + 0] + k10 * normals1[idx + 3] + k11 * normals1[idx + 6] + k01 * normals1[idx + 9]; state.getNormal().y = k00 * normals1[idx + 1] + k10 * normals1[idx + 4] + k11 * normals1[idx + 7] + k01 * normals1[idx + 10]; state.getNormal().z = k00 * normals1[idx + 2] + k10 * normals1[idx + 5] + k11 * normals1[idx + 8] + k01 * normals1[idx + 11]; state.getNormal().set(state.transformNormalObjectToWorld(state.getNormal())); state.getNormal().normalize(); break; } } float uv00 = 0, uv01 = 0, uv10 = 0, uv11 = 0, uv20 = 0, uv21 = 0, uv30 = 0, uv31 = 0; switch (uvs.interp) { case ParameterList.InterpolationType.NONE: case ParameterList.InterpolationType.FACE: { state.getUV().x = 0; state.getUV().y = 0; break; } case ParameterList.InterpolationType.VERTEX: { int i20 = 2 * index0; int i21 = 2 * index1; int i22 = 2 * index2; int i23 = 2 * index3; float[] uvs1 = this.uvs.data; uv00 = uvs1[i20 + 0]; uv01 = uvs1[i20 + 1]; uv10 = uvs1[i21 + 0]; uv11 = uvs1[i21 + 1]; uv20 = uvs1[i22 + 0]; uv21 = uvs1[i22 + 1]; uv20 = uvs1[i23 + 0]; uv21 = uvs1[i23 + 1]; break; } case ParameterList.InterpolationType.FACEVARYING: { int idx = quad << 1; float[] uvs1 = this.uvs.data; uv00 = uvs1[idx + 0]; uv01 = uvs1[idx + 1]; uv10 = uvs1[idx + 2]; uv11 = uvs1[idx + 3]; uv20 = uvs1[idx + 4]; uv21 = uvs1[idx + 5]; uv30 = uvs1[idx + 6]; uv31 = uvs1[idx + 7]; break; } } if (uvs.interp != ParameterList.InterpolationType.NONE) { // get exact uv coords and compute tangent vectors state.getUV().x = k00 * uv00 + k10 * uv10 + k11 * uv20 + k01 * uv30; state.getUV().y = k00 * uv01 + k10 * uv11 + k11 * uv21 + k01 * uv31; 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 / 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 = state.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 prepareShadingState(ShadingState state) { state.init(); Instance i = state.getInstance(); state.getRay().getPoint(state.getPoint()); Ray r = state.getRay(); IShader s = i.getShader(0); state.setShader(s != null ? s : this); int primID = state.getPrimitiveID(); int hair = primID / numSegments; int line = primID % numSegments; int vRoot = hair * 3 * (numSegments + 1); int v0 = vRoot + line * 3; // tangent vector Vector3 v = getTangent(line, v0, state.getV()); v = i.transformVectorObjectToWorld(v); state.setBasis(OrthoNormalBasis.makeFromWV(v, new Vector3(-r.dx, -r.dy, -r.dz))); state.getBasis().swapVW(); // normal state.getNormal().set(0, 0, 1); state.getBasis().transform(state.getNormal()); state.getGeoNormal().set(state.getNormal()); state.getUV().set(0, (line + state.getV()) / numSegments); }
public void prepareShadingState(ShadingState state) { state.init(); Instance parent = state.getInstance(); int primID = state.getPrimitiveID(); float u = state.getU(); float v = state.getV(); state.getRay().getPoint(state.getPoint()); int quad = 4 * primID; int index0 = quads[quad + 0]; int index1 = quads[quad + 1]; int index2 = quads[quad + 2]; int index3 = quads[quad + 3]; Point3 v0p = getPoint(index0); Point3 v1p = getPoint(index1); Point3 v2p = getPoint(index2); Point3 v3p = getPoint(index3); float tanux = (1 - v) * (v1p.x - v0p.x) + v * (v2p.x - v3p.x); float tanuy = (1 - v) * (v1p.y - v0p.y) + v * (v2p.y - v3p.y); float tanuz = (1 - v) * (v1p.z - v0p.z) + v * (v2p.z - v3p.z); float tanvx = (1 - u) * (v3p.x - v0p.x) + u * (v2p.x - v1p.x); float tanvy = (1 - u) * (v3p.y - v0p.y) + u * (v2p.y - v1p.y); float tanvz = (1 - u) * (v3p.z - v0p.z) + u * (v2p.z - v1p.z); float nx = tanuy * tanvz - tanuz * tanvy; float ny = tanuz * tanvx - tanux * tanvz; float nz = tanux * tanvy - tanuy * tanvx; Vector3 ng = new Vector3(nx, ny, nz); ng = state.transformNormalObjectToWorld(ng); ng.normalize(); state.getGeoNormal().set(ng); float k00 = (1 - u) * (1 - v); float k10 = u * (1 - v); float k01 = (1 - u) * v; float k11 = u * v; switch (normals.interp) { case ParameterList.InterpolationType.NONE: case ParameterList.InterpolationType.FACE: { state.getNormal().set(ng); break; } case ParameterList.InterpolationType.VERTEX: { int i30 = 3 * index0; int i31 = 3 * index1; int i32 = 3 * index2; int i33 = 3 * index3; float[] normals1 = this.normals.data; state.getNormal().x = k00 * normals1[i30 + 0] + k10 * normals1[i31 + 0] + k11 * normals1[i32 + 0] + k01 * normals1[i33 + 0]; state.getNormal().y = k00 * normals1[i30 + 1] + k10 * normals1[i31 + 1] + k11 * normals1[i32 + 1] + k01 * normals1[i33 + 1]; state.getNormal().z = k00 * normals1[i30 + 2] + k10 * normals1[i31 + 2] + k11 * normals1[i32 + 2] + k01 * normals1[i33 + 2]; state.getNormal().set(state.transformNormalObjectToWorld(state.getNormal())); state.getNormal().normalize(); break; } case ParameterList.InterpolationType.FACEVARYING: { int idx = 3 * quad; float[] normals1 = this.normals.data; state.getNormal().x = k00 * normals1[idx + 0] + k10 * normals1[idx + 3] + k11 * normals1[idx + 6] + k01 * normals1[idx + 9]; state.getNormal().y = k00 * normals1[idx + 1] + k10 * normals1[idx + 4] + k11 * normals1[idx + 7] + k01 * normals1[idx + 10]; state.getNormal().z = k00 * normals1[idx + 2] + k10 * normals1[idx + 5] + k11 * normals1[idx + 8] + k01 * normals1[idx + 11]; state.getNormal().set(state.transformNormalObjectToWorld(state.getNormal())); state.getNormal().normalize(); break; } } float uv00 = 0, uv01 = 0, uv10 = 0, uv11 = 0, uv20 = 0, uv21 = 0, uv30 = 0, uv31 = 0; switch (uvs.interp) { case ParameterList.InterpolationType.NONE: case ParameterList.InterpolationType.FACE: { state.getUV().x = 0; state.getUV().y = 0; break; } case ParameterList.InterpolationType.VERTEX: { int i20 = 2 * index0; int i21 = 2 * index1; int i22 = 2 * index2; int i23 = 2 * index3; float[] uvs1 = this.uvs.data; uv00 = uvs1[i20 + 0]; uv01 = uvs1[i20 + 1]; uv10 = uvs1[i21 + 0]; uv11 = uvs1[i21 + 1]; uv20 = uvs1[i22 + 0]; uv21 = uvs1[i22 + 1]; uv20 = uvs1[i23 + 0]; uv21 = uvs1[i23 + 1]; break; } case ParameterList.InterpolationType.FACEVARYING: { int idx = quad << 1; float[] uvs1 = this.uvs.data; uv00 = uvs1[idx + 0]; uv01 = uvs1[idx + 1]; uv10 = uvs1[idx + 2]; uv11 = uvs1[idx + 3]; uv20 = uvs1[idx + 4]; uv21 = uvs1[idx + 5]; uv30 = uvs1[idx + 6]; uv31 = uvs1[idx + 7]; break; } } if (uvs.interp != ParameterList.InterpolationType.NONE) { // get exact uv coords and compute tangent vectors state.getUV().x = k00 * uv00 + k10 * uv10 + k11 * uv20 + k01 * uv30; state.getUV().y = k00 * uv01 + k10 * uv11 + k11 * uv21 + k01 * uv31; 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 / 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 = state.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 getSamples(ShadingState state) { if (meshlight.numSamples == 0) { return; } Vector3 n = state.getNormal(); Point3 p = state.getPoint(); // vector towards each vertex of the light source Vector3 p0 = Point3.sub(meshlight.getPoint(meshlight.triangles[tri3 + 0]), p, new Vector3()); // cull triangle if it is facing the wrong way if (Vector3.dot(p0, ng) >= 0) { return; } Vector3 p1 = Point3.sub(meshlight.getPoint(meshlight.triangles[tri3 + 1]), p, new Vector3()); Vector3 p2 = Point3.sub(meshlight.getPoint(meshlight.triangles[tri3 + 2]), p, new Vector3()); // if all three vertices are below the hemisphere, stop if (Vector3.dot(p0, n) <= 0 && Vector3.dot(p1, n) <= 0 && Vector3.dot(p2, n) <= 0) { return; } p0.normalize(); p1.normalize(); p2.normalize(); float dot = Vector3.dot(p2, p0); Vector3 h = new Vector3(); h.x = p2.x - dot * p0.x; h.y = p2.y - dot * p0.y; h.z = p2.z - dot * p0.z; float hlen = h.Length(); if (hlen > 1e-6f) { h.div(hlen); } else { return; } Vector3 n0 = Vector3.cross(p0, p1, new Vector3()); float len0 = n0.Length(); if (len0 > 1e-6f) { n0.div(len0); } else { return; } Vector3 n1 = Vector3.cross(p1, p2, new Vector3()); float len1 = n1.Length(); if (len1 > 1e-6f) { n1.div(len1); } else { return; } Vector3 n2 = Vector3.cross(p2, p0, new Vector3()); float len2 = n2.Length(); if (len2 > 1e-6f) { n2.div(len2); } else { return; } float cosAlpha = MathUtils.clamp(-Vector3.dot(n2, n0), -1.0f, 1.0f); float cosBeta = MathUtils.clamp(-Vector3.dot(n0, n1), -1.0f, 1.0f); float cosGamma = MathUtils.clamp(-Vector3.dot(n1, n2), -1.0f, 1.0f); float alpha = (float)Math.Acos(cosAlpha); float beta = (float)Math.Acos(cosBeta); float gamma = (float)Math.Acos(cosGamma); float area = alpha + beta + gamma - (float)Math.PI; float cosC = MathUtils.clamp(Vector3.dot(p0, p1), -1.0f, 1.0f); float salpha = (float)Math.Sin(alpha); float product = salpha * cosC; // use lower sampling depth for diffuse bounces int samples = state.getDiffuseDepth() > 0 ? 1 : meshlight.numSamples; Color c = Color.mul(area / samples, meshlight.radiance); for (int i = 0; i < samples; i++) { // random offset on unit square double randX = state.getRandom(i, 0, samples); double randY = state.getRandom(i, 1, samples); float phi = (float)randX * area - alpha + (float)Math.PI; float sinPhi = (float)Math.Sin(phi); float cosPhi = (float)Math.Cos(phi); float u = cosPhi + cosAlpha; float v = sinPhi - product; float q = (-v + cosAlpha * (cosPhi * -v + sinPhi * u)) / (salpha * (sinPhi * -v - cosPhi * u)); float q1 = 1.0f - q * q; if (q1 < 0.0f) { q1 = 0.0f; } float sqrtq1 = (float)Math.Sqrt(q1); float ncx = q * p0.x + sqrtq1 * h.x; float ncy = q * p0.y + sqrtq1 * h.y; float ncz = q * p0.z + sqrtq1 * h.z; dot = p1.dot(ncx, ncy, ncz); float z = 1.0f - (float)randY * (1.0f - dot); float z1 = 1.0f - z * z; if (z1 < 0.0f) { z1 = 0.0f; } Vector3 nd = new Vector3(); nd.x = ncx - dot * p1.x; nd.y = ncy - dot * p1.y; nd.z = ncz - dot * p1.z; nd.normalize(); float sqrtz1 = (float)Math.Sqrt(z1); Vector3 result = new Vector3(); result.x = z * p1.x + sqrtz1 * nd.x; result.y = z * p1.y + sqrtz1 * nd.y; result.z = z * p1.z + sqrtz1 * nd.z; // make sure the sample is in the right hemisphere - facing in // the right direction if (Vector3.dot(result, n) > 0 && Vector3.dot(result, state.getGeoNormal()) > 0 && Vector3.dot(result, ng) < 0) { // compute intersection with triangle (if any) Ray shadowRay = new Ray(state.getPoint(), result); if (!intersectTriangleKensler(shadowRay)) { continue; } LightSample dest = new LightSample(); dest.setShadowRay(shadowRay); // prepare sample dest.setRadiance(c, c); dest.traceShadow(state); state.addSample(dest); } } }
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 = triangleMesh.triangles[tri + 0]; int index1 = triangleMesh.triangles[tri + 1]; int index2 = triangleMesh.triangles[tri + 2]; Point3 v0p = triangleMesh.getPoint(index0); Point3 v1p = triangleMesh.getPoint(index1); Point3 v2p = triangleMesh.getPoint(index2); // get object space point from barycentric coordinates state.getPoint().x = w * v0p.x + u * v1p.x + v * v2p.x; state.getPoint().y = w * v0p.y + u * v1p.y + v * v2p.y; state.getPoint().z = w * v0p.z + u * v1p.z + v * v2p.z; // move into world space state.getPoint().set(parent.transformObjectToWorld(state.getPoint())); Vector3 ng = Point3.normal(v0p, v1p, v2p); if (parent != null) ng = parent.transformNormalObjectToWorld(ng); ng.normalize(); state.getGeoNormal().set(ng); switch (triangleMesh.normals.interp) { case ParameterList.InterpolationType.NONE: case ParameterList.InterpolationType.FACE: { state.getNormal().set(ng); break; } case ParameterList.InterpolationType.VERTEX: { int i30 = 3 * index0; int i31 = 3 * index1; int i32 = 3 * index2; float[] normals = triangleMesh.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]; if (parent != null) state.getNormal().set(parent.transformNormalObjectToWorld(state.getNormal())); state.getNormal().normalize(); break; } case ParameterList.InterpolationType.FACEVARYING: { int idx = 3 * tri; float[] normals = triangleMesh.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]; if (parent != null) 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 (triangleMesh.uvs.interp) { case ParameterList.InterpolationType.NONE: case ParameterList.InterpolationType.FACE: { state.getUV().x = 0; state.getUV().y = 0; break; } case ParameterList.InterpolationType.VERTEX: { int i20 = 2 * index0; int i21 = 2 * index1; int i22 = 2 * index2; float[] uvs = triangleMesh.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 ParameterList.InterpolationType.FACEVARYING: { int idx = tri << 1; float[] uvs = triangleMesh.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 (triangleMesh.uvs.interp != ParameterList.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 / 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; if (parent != null) 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 = triangleMesh.faceShaders == null ? 0 : (triangleMesh.faceShaders[primID] & 0xFF); state.setShader(parent.getShader(shaderIndex)); }
public void prepareShadingState(ShadingState state) { state.init(); state.getRay().getPoint(state.getPoint()); Instance parent = state.getInstance(); // compute local normal Point3 p = parent.transformWorldToObject(state.getPoint()); float gx1w = p.x - DELTA; float gx1x = p.y; float gx1y = p.z; float gx1z = 0; float gx2w = p.x + DELTA; float gx2x = p.y; float gx2y = p.z; float gx2z = 0; float gy1w = p.x; float gy1x = p.y - DELTA; float gy1y = p.z; float gy1z = 0; float gy2w = p.x; float gy2x = p.y + DELTA; float gy2y = p.z; float gy2z = 0; float gz1w = p.x; float gz1x = p.y; float gz1y = p.z - DELTA; float gz1z = 0; float gz2w = p.x; float gz2x = p.y; float gz2y = p.z + DELTA; float gz2z = 0; for (int i = 0; i < maxIterations; i++) { { // z = z*z + c float nw = gx1w * gx1w - gx1x * gx1x - gx1y * gx1y - gx1z * gx1z + cw; gx1x = 2 * gx1w * gx1x + cx; gx1y = 2 * gx1w * gx1y + cy; gx1z = 2 * gx1w * gx1z + cz; gx1w = nw; } { // z = z*z + c float nw = gx2w * gx2w - gx2x * gx2x - gx2y * gx2y - gx2z * gx2z + cw; gx2x = 2 * gx2w * gx2x + cx; gx2y = 2 * gx2w * gx2y + cy; gx2z = 2 * gx2w * gx2z + cz; gx2w = nw; } { // z = z*z + c float nw = gy1w * gy1w - gy1x * gy1x - gy1y * gy1y - gy1z * gy1z + cw; gy1x = 2 * gy1w * gy1x + cx; gy1y = 2 * gy1w * gy1y + cy; gy1z = 2 * gy1w * gy1z + cz; gy1w = nw; } { // z = z*z + c float nw = gy2w * gy2w - gy2x * gy2x - gy2y * gy2y - gy2z * gy2z + cw; gy2x = 2 * gy2w * gy2x + cx; gy2y = 2 * gy2w * gy2y + cy; gy2z = 2 * gy2w * gy2z + cz; gy2w = nw; } { // z = z*z + c float nw = gz1w * gz1w - gz1x * gz1x - gz1y * gz1y - gz1z * gz1z + cw; gz1x = 2 * gz1w * gz1x + cx; gz1y = 2 * gz1w * gz1y + cy; gz1z = 2 * gz1w * gz1z + cz; gz1w = nw; } { // z = z*z + c float nw = gz2w * gz2w - gz2x * gz2x - gz2y * gz2y - gz2z * gz2z + cw; gz2x = 2 * gz2w * gz2x + cx; gz2y = 2 * gz2w * gz2y + cy; gz2z = 2 * gz2w * gz2z + cz; gz2w = nw; } } float gradX = Length(gx2w, gx2x, gx2y, gx2z) - Length(gx1w, gx1x, gx1y, gx1z); float gradY = Length(gy2w, gy2x, gy2y, gy2z) - Length(gy1w, gy1x, gy1y, gy1z); float gradZ = Length(gz2w, gz2x, gz2y, gz2z) - Length(gz1w, gz1x, gz1y, gz1z); Vector3 n = new Vector3((float)gradX, (float)gradY, (float)gradZ); state.getNormal().set(parent.transformNormalObjectToWorld(n)); state.getNormal().normalize(); state.getGeoNormal().set(state.getNormal()); state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal())); state.getPoint().x += state.getNormal().x * epsilon * 20; state.getPoint().y += state.getNormal().y * epsilon * 20; state.getPoint().z += state.getNormal().z * epsilon * 20; state.setShader(parent.getShader(0)); state.setModifier(parent.getModifier(0)); }
public void ScatterPhoton(ShadingState state, Color power) { int side = state.getPrimitiveID(); Color kd = null; switch (side) { case 0: kd = left; break; case 1: kd = right; break; case 3: kd = back; break; case 4: kd = bottom; break; case 5: float lx = state.getPoint().x; float ly = state.getPoint().y; if (lx >= lxmin && lx < lxmax && ly >= lymin && ly < lymax && state.getRay().dz > 0) return; kd = top; break; default: Debug.Assert(false); break; } // make sure we are on the right side of the material if (Vector3.dot(state.getNormal(), state.getRay().getDirection()) > 0) { state.getNormal().negate(); state.getGeoNormal().negate(); } state.storePhoton(state.getRay().getDirection(), power, kd); double avg = kd.getAverage(); double rnd = state.getRandom(0, 0, 1); if (rnd < avg) { // photon is scattered power.mul(kd).mul(1 / (float)avg); OrthoNormalBasis onb = OrthoNormalBasis.makeFromW(state.getNormal()); double u = 2 * Math.PI * rnd / avg; double v = state.getRandom(0, 1, 1); float s = (float)Math.Sqrt(v); float s1 = (float)Math.Sqrt(1.0 - v); Vector3 w = new Vector3((float)Math.Cos(u) * s, (float)Math.Sin(u) * s, s1); w = onb.transform(w, new Vector3()); state.traceDiffusePhoton(new Ray(state.getPoint(), w), power); } }
/** * Prepare the shading state for shader invocation. This also runs the * currently attached surface modifier. * * @param state shading state to be prepared */ public void prepareShadingState(ShadingState state) { geometry.prepareShadingState(state); if (state.getNormal() != null && state.getGeoNormal() != null) state.correctShadingNormal(); // run modifier if it was provided if (state.getModifier() != null) state.getModifier().modify(state); }
public void prepareShadingState(ShadingState state) { state.init(); state.getRay().getPoint(state.getPoint()); Instance parent = state.getInstance(); // compute local normal Point3 p = state.transformWorldToObject(state.getPoint()); float gx1w = p.x - DELTA; float gx1x = p.y; float gx1y = p.z; float gx1z = 0; float gx2w = p.x + DELTA; float gx2x = p.y; float gx2y = p.z; float gx2z = 0; float gy1w = p.x; float gy1x = p.y - DELTA; float gy1y = p.z; float gy1z = 0; float gy2w = p.x; float gy2x = p.y + DELTA; float gy2y = p.z; float gy2z = 0; float gz1w = p.x; float gz1x = p.y; float gz1y = p.z - DELTA; float gz1z = 0; float gz2w = p.x; float gz2x = p.y; float gz2y = p.z + DELTA; float gz2z = 0; for (int i = 0; i < maxIterations; i++) { { // z = z*z + c float nw = gx1w * gx1w - gx1x * gx1x - gx1y * gx1y - gx1z * gx1z + cw; gx1x = 2 * gx1w * gx1x + cx; gx1y = 2 * gx1w * gx1y + cy; gx1z = 2 * gx1w * gx1z + cz; gx1w = nw; } { // z = z*z + c float nw = gx2w * gx2w - gx2x * gx2x - gx2y * gx2y - gx2z * gx2z + cw; gx2x = 2 * gx2w * gx2x + cx; gx2y = 2 * gx2w * gx2y + cy; gx2z = 2 * gx2w * gx2z + cz; gx2w = nw; } { // z = z*z + c float nw = gy1w * gy1w - gy1x * gy1x - gy1y * gy1y - gy1z * gy1z + cw; gy1x = 2 * gy1w * gy1x + cx; gy1y = 2 * gy1w * gy1y + cy; gy1z = 2 * gy1w * gy1z + cz; gy1w = nw; } { // z = z*z + c float nw = gy2w * gy2w - gy2x * gy2x - gy2y * gy2y - gy2z * gy2z + cw; gy2x = 2 * gy2w * gy2x + cx; gy2y = 2 * gy2w * gy2y + cy; gy2z = 2 * gy2w * gy2z + cz; gy2w = nw; } { // z = z*z + c float nw = gz1w * gz1w - gz1x * gz1x - gz1y * gz1y - gz1z * gz1z + cw; gz1x = 2 * gz1w * gz1x + cx; gz1y = 2 * gz1w * gz1y + cy; gz1z = 2 * gz1w * gz1z + cz; gz1w = nw; } { // z = z*z + c float nw = gz2w * gz2w - gz2x * gz2x - gz2y * gz2y - gz2z * gz2z + cw; gz2x = 2 * gz2w * gz2x + cx; gz2y = 2 * gz2w * gz2y + cy; gz2z = 2 * gz2w * gz2z + cz; gz2w = nw; } } float gradX = Length(gx2w, gx2x, gx2y, gx2z) - Length(gx1w, gx1x, gx1y, gx1z); float gradY = Length(gy2w, gy2x, gy2y, gy2z) - Length(gy1w, gy1x, gy1y, gy1z); float gradZ = Length(gz2w, gz2x, gz2y, gz2z) - Length(gz1w, gz1x, gz1y, gz1z); Vector3 n = new Vector3(gradX, gradY, gradZ); state.getNormal().set(state.transformNormalObjectToWorld(n)); state.getNormal().normalize(); state.getGeoNormal().set(state.getNormal()); state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal())); state.getPoint().x += state.getNormal().x *epsilon * 20; state.getPoint().y += state.getNormal().y *epsilon * 20; state.getPoint().z += state.getNormal().z *epsilon * 20; state.setShader(parent.getShader(0)); state.setModifier(parent.getModifier(0)); }
public void ScatterPhoton(ShadingState state, Color power) { int side = state.getPrimitiveID(); Color kd = null; switch (side) { case 0: kd = left; break; case 1: kd = right; break; case 3: kd = back; break; case 4: kd = bottom; break; case 5: float lx = state.getPoint().x; float ly = state.getPoint().y; if (lx >= lxmin && lx < lxmax && ly >= lymin && ly < lymax && state.getRay().dz > 0) { return; } kd = top; break; default: Debug.Assert(false); break; } // make sure we are on the right side of the material if (Vector3.dot(state.getNormal(), state.getRay().getDirection()) > 0) { state.getNormal().negate(); state.getGeoNormal().negate(); } state.storePhoton(state.getRay().getDirection(), power, kd); double avg = kd.getAverage(); double rnd = state.getRandom(0, 0, 1); if (rnd < avg) { // photon is scattered power.mul(kd).mul(1 / (float)avg); OrthoNormalBasis onb = OrthoNormalBasis.makeFromW(state.getNormal()); double u = 2 * Math.PI * rnd / avg; double v = state.getRandom(0, 1, 1); float s = (float)Math.Sqrt(v); float s1 = (float)Math.Sqrt(1.0 - v); Vector3 w = new Vector3((float)Math.Cos(u) * s, (float)Math.Sin(u) * s, s1); w = onb.transform(w, new Vector3()); state.traceDiffusePhoton(new Ray(state.getPoint(), w), power); } }