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 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 Color GetRadiance(ShadingState state) { // make sure we are on the right side of the material state.faceforward(); // direct lighting state.initLightSamples(); state.initCausticSamples(); Color d = getDiffuse(state); Color lr = state.diffuse(d); if (!state.includeSpecular) return lr; if (glossyness == 0) { float cos = state.getCosND(); float dn = 2 * cos; Vector3 refDir = new Vector3(); refDir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x; refDir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y; refDir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z; Ray refRay = new Ray(state.getPoint(), refDir); // compute Fresnel term cos = 1 - cos; float cos2 = cos * cos; float cos5 = cos2 * cos2 * cos; Color spec = getSpecular(state); Color ret = Color.white(); ret.sub(spec); ret.mul(cos5); ret.add(spec); return lr.add(ret.mul(state.traceReflection(refRay, 0))); } else return lr.add(state.specularPhong(getSpecular(state), 2 / glossyness, numSamples)); }
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 Color getRadiance(ShadingState state) { if (!state.includeSpecular) { return(Color.BLACK); } state.faceforward(); float cos = state.getCosND(); float dn = 2 * cos; Vector3 refDir = new Vector3(); refDir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x; refDir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y; refDir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z; Ray refRay = new Ray(state.getPoint(), refDir); // compute Fresnel term cos = 1 - cos; float cos2 = cos * cos; float cos5 = cos2 * cos2 * cos; Color ret = Color.white(); ret.sub(color); ret.mul(cos5); ret.add(color); return(ret.mul(state.traceReflection(refRay, 0))); }
public void scatterPhoton(ShadingState state, Color power) { Color diffuse, specular; // make sure we are on the right side of the material state.faceforward(); diffuse = getDiffuse(state); specular = getSpecular(state); state.storePhoton(state.getRay().getDirection(), power, diffuse); float d = diffuse.getAverage(); float r = specular.getAverage(); double rnd = state.getRandom(0, 0, 1); if (rnd < d) { // photon is scattered power.mul(diffuse).mul(1.0f / d); OrthoNormalBasis onb = state.getBasis(); double u = 2 * Math.PI * rnd / d; 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); } else if (rnd < d + r) { if (glossyness == 0) { float cos = -Vector3.dot(state.getNormal(), state.getRay().getDirection()); power.mul(diffuse).mul(1.0f / d); // photon is reflected float dn = 2 * cos; Vector3 dir = new Vector3(); dir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x; dir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y; dir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z; state.traceReflectionPhoton(new Ray(state.getPoint(), dir), power); } else { float dn = 2.0f * state.getCosND(); // reflected direction Vector3 refDir = new Vector3(); refDir.x = (dn * state.getNormal().x) + state.getRay().dx; refDir.y = (dn * state.getNormal().y) + state.getRay().dy; refDir.z = (dn * state.getNormal().z) + state.getRay().dz; power.mul(spec).mul(1.0f / r); OrthoNormalBasis onb = state.getBasis(); double u = 2 * Math.PI * (rnd - r) / r; double v = state.getRandom(0, 1, 1); float s = (float)Math.Pow(v, 1 / ((1.0f / glossyness) + 1)); float s1 = (float)Math.Sqrt(1 - s * s); Vector3 w = new Vector3((float)Math.Cos(u) * s1, (float)Math.Sin(u) * s1, s); w = onb.transform(w, new Vector3()); state.traceReflectionPhoton(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 Color GetRadiance(ShadingState state) { Vector3 n = state.getNormal(); float f = n == null ? 1.0f : Math.Abs(state.getRay().dot(n)); return(BORDERS[state.getPrimitiveID() % BORDERS.Length].copy().mul(f)); }
public Color getRadiance(ShadingState state) { // don't use these - gather lights for sphere of directions // gather lights state.initLightSamples(); state.initCausticSamples(); Vector3 v = state.getRay().getDirection(); v.negate(); Vector3 h = new Vector3(); Vector3 t = state.getBasis().transform(new Vector3(0, 1, 0)); Color diff = Color.black(); Color spec = Color.black(); foreach (LightSample ls in state) { Vector3 l = ls.getShadowRay().getDirection(); float dotTL = Vector3.dot(t, l); float sinTL = (float)Math.Sqrt(1 - dotTL * dotTL); // float dotVL = Vector3.dot(v, l); diff.madd(sinTL, ls.getDiffuseRadiance()); Vector3.add(v, l, h); h.normalize(); float dotTH = Vector3.dot(t, h); float sinTH = (float)Math.Sqrt(1 - dotTH * dotTH); float s = (float)Math.Pow(sinTH, 10.0f); spec.madd(s, ls.getSpecularRadiance()); } Color c = Color.add(diff, spec, new Color()); // transparency return Color.blend(c, state.traceTransparency(), state.getV(), new Color()); }
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 Color GetRadiance(ShadingState state) { Vector3 n = state.getNormal(); float f = n == null ? 1.0f : Math.Abs(state.getRay().dot(n)); return(new Color(state.getInstance().GetHashCode()).mul(f)); }
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 Color GetRadiance(ShadingState state) { // don't use these - gather lights for sphere of directions // gather lights state.initLightSamples(); state.initCausticSamples(); Vector3 v = state.getRay().getDirection(); v.negate(); Vector3 h = new Vector3(); Vector3 t = state.getBasis().transform(new Vector3(0, 1, 0)); Color diff = Color.black(); Color spec = Color.black(); foreach (LightSample ls in state) { Vector3 l = ls.getShadowRay().getDirection(); float dotTL = Vector3.dot(t, l); float sinTL = (float)Math.Sqrt(1 - dotTL * dotTL); // float dotVL = Vector3.dot(v, l); diff.madd(sinTL, ls.getDiffuseRadiance()); Vector3.add(v, l, h); h.normalize(); float dotTH = Vector3.dot(t, h); float sinTH = (float)Math.Sqrt(1 - dotTH * dotTH); float s = (float)Math.Pow(sinTH, 10.0f); spec.madd(s, ls.getSpecularRadiance()); } Color c = Color.add(diff, spec, new Color()); // transparency return(Color.blend(c, state.traceTransparency(), state.getV(), new Color())); }
public void ScatterPhoton(ShadingState state, Color power) { float avg = color.getAverage(); double rnd = state.getRandom(0, 0, 1); if (rnd >= avg) return; state.faceforward(); float cos = state.getCosND(); power.mul(color).mul(1.0f / avg); // photon is reflected float dn = 2 * cos; Vector3 dir = new Vector3(); dir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x; dir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y; dir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z; state.traceReflectionPhoton(new Ray(state.getPoint(), dir), power); }
public void scatterPhoton(ShadingState state, Color power) { // make sure we are on the right side of the material state.faceforward(); Color d = getDiffuse(state); state.storePhoton(state.getRay().getDirection(), power, d); float avgD = d.getAverage(); float avgS = spec.getAverage(); double rnd = state.getRandom(0, 0, 1); if (rnd < avgD) { // photon is scattered diffusely power.mul(d).mul(1.0f / avgD); OrthoNormalBasis onb = state.getBasis(); double u = 2 * Math.PI * rnd / avgD; double v = state.getRandom(0, 1, 1); float s = (float)Math.Sqrt(v); float s1 = (float)Math.Sqrt(1.0f - 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); } else if (rnd < avgD + avgS) { // photon is scattered specularly float dn = 2.0f * state.getCosND(); // reflected direction Vector3 refDir = new Vector3(); refDir.x = (dn * state.getNormal().x) + state.getRay().dx; refDir.y = (dn * state.getNormal().y) + state.getRay().dy; refDir.z = (dn * state.getNormal().z) + state.getRay().dz; power.mul(spec).mul(1.0f / avgS); OrthoNormalBasis onb = state.getBasis(); double u = 2 * Math.PI * (rnd - avgD) / avgS; double v = state.getRandom(0, 1, 1); float s = (float)Math.Pow(v, 1 / (this.power + 1)); float s1 = (float)Math.Sqrt(1 - s * s); Vector3 w = new Vector3((float)Math.Cos(u) * s1, (float)Math.Sin(u) * s1, s); w = onb.transform(w, new Vector3()); state.traceReflectionPhoton(new Ray(state.getPoint(), w), power); } }
public Color GetRadiance(ShadingState state) { if (!state.includeSpecular) return Color.BLACK; Vector3 reflDir = new Vector3(); Vector3 refrDir = new Vector3(); state.faceforward(); float cos = state.getCosND(); bool inside = state.isBehind(); float neta = inside ? eta : 1.0f / eta; float dn = 2 * cos; reflDir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x; reflDir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y; reflDir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z; // refracted ray float arg = 1 - (neta * neta * (1 - (cos * cos))); bool tir = arg < 0; if (tir) refrDir.x = refrDir.y = refrDir.z = 0; else { float nK = (neta * cos) - (float)Math.Sqrt(arg); refrDir.x = (neta * state.getRay().dx) + (nK * state.getNormal().x); refrDir.y = (neta * state.getRay().dy) + (nK * state.getNormal().y); refrDir.z = (neta * state.getRay().dz) + (nK * state.getNormal().z); } // compute Fresnel terms float cosTheta1 = Vector3.dot(state.getNormal(), reflDir); float cosTheta2 = -Vector3.dot(state.getNormal(), refrDir); float pPara = (cosTheta1 - eta * cosTheta2) / (cosTheta1 + eta * cosTheta2); float pPerp = (eta * cosTheta1 - cosTheta2) / (eta * cosTheta1 + cosTheta2); float kr = 0.5f * (pPara * pPara + pPerp * pPerp); float kt = 1 - kr; Color absorption = null; if (inside && absorptionDistance > 0) { // this ray is inside the object and leaving it // compute attenuation that occured along the ray absorption = Color.mul(-state.getRay().getMax() / absorptionDistance, absorptionColor.copy().opposite()).exp(); if (absorption.isBlack()) return Color.BLACK; // nothing goes through } // refracted ray Color ret = Color.black(); if (!tir) { ret.madd(kt, state.traceRefraction(new Ray(state.getPoint(), refrDir), 0)).mul(color); } if (!inside || tir) ret.add(Color.mul(kr, state.traceReflection(new Ray(state.getPoint(), reflDir), 0)).mul(color)); return absorption != null ? ret.mul(absorption) : ret; }
public Color GetRadiance(ShadingState state) { // make sure we are on the right side of the material state.faceforward(); // direct lighting state.initLightSamples(); state.initCausticSamples(); Color d = getDiffuse(state); Color lr = state.diffuse(d); if (!state.includeSpecular) { return(lr); } if (glossyness == 0) { float cos = state.getCosND(); float dn = 2 * cos; Vector3 refDir = new Vector3(); refDir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x; refDir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y; refDir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z; Ray refRay = new Ray(state.getPoint(), refDir); // compute Fresnel term cos = 1 - cos; float cos2 = cos * cos; float cos5 = cos2 * cos2 * cos; Color spec = getSpecular(state); Color ret = Color.white(); ret.sub(spec); ret.mul(cos5); ret.add(spec); return(lr.add(ret.mul(state.traceReflection(refRay, 0)))); } else { return(lr.add(state.specularPhong(getSpecular(state), 2 / glossyness, numSamples))); } }
public void scatterPhoton(ShadingState state, Color power) { float avg = color.getAverage(); double rnd = state.getRandom(0, 0, 1); if (rnd >= avg) { return; } state.faceforward(); float cos = state.getCosND(); power.mul(color).mul(1.0f / avg); // photon is reflected float dn = 2 * cos; Vector3 dir = new Vector3(); dir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x; dir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y; dir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z; state.traceReflectionPhoton(new Ray(state.getPoint(), dir), power); }
public void scatterPhoton(ShadingState state, Color power) { Color diffuse; // make sure we are on the right side of the material state.faceforward(); diffuse = getDiffuse(state); state.storePhoton(state.getRay().getDirection(), power, diffuse); float d = diffuse.getAverage(); float r = d * refl; double rnd = state.getRandom(0, 0, 1); if (rnd < d) { // photon is scattered power.mul(diffuse).mul(1.0f / d); OrthoNormalBasis onb = state.getBasis(); double u = 2 * Math.PI * rnd / d; 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); } else if (rnd < d + r) { float cos = -Vector3.dot(state.getNormal(), state.getRay().getDirection()); power.mul(diffuse).mul(1.0f / d); // photon is reflected float dn = 2 * cos; Vector3 dir = new Vector3(); dir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x; dir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y; dir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z; state.traceReflectionPhoton(new Ray(state.getPoint(), dir), power); } }
public Color GetRadiance(ShadingState state) { if (!state.includeSpecular) return Color.BLACK; state.faceforward(); float cos = state.getCosND(); float dn = 2 * cos; Vector3 refDir = new Vector3(); refDir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x; refDir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y; refDir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z; Ray refRay = new Ray(state.getPoint(), refDir); // compute Fresnel term cos = 1 - cos; float cos2 = cos * cos; float cos5 = cos2 * cos2 * cos; Color ret = Color.white(); ret.sub(color); ret.mul(cos5); ret.add(color); return ret.mul(state.traceReflection(refRay, 0)); }
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 Color GetRadiance(ShadingState state) { 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(state.includeLights ? radiance : Color.BLACK); } kd = top; break; default: Debug.Assert(false); break; } // make sure we are on the right side of the material state.faceforward(); // setup lighting state.initLightSamples(); state.initCausticSamples(); return(state.diffuse(kd)); }
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()); 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(); 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 ScatterPhoton(ShadingState state, Color power) { Color diffuse; // make sure we are on the right side of the material state.faceforward(); diffuse = getDiffuse(state); state.storePhoton(state.getRay().getDirection(), power, diffuse); float d = diffuse.getAverage(); float r = d * refl; double rnd = state.getRandom(0, 0, 1); if (rnd < d) { // photon is scattered power.mul(diffuse).mul(1.0f / d); OrthoNormalBasis onb = state.getBasis(); double u = 2 * Math.PI * rnd / d; 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); } else if (rnd < d + r) { float cos = -Vector3.dot(state.getNormal(), state.getRay().getDirection()); power.mul(diffuse).mul(1.0f / d); // photon is reflected float dn = 2 * cos; Vector3 dir = new Vector3(); dir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x; dir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y; dir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z; state.traceReflectionPhoton(new Ray(state.getPoint(), dir), power); } }
public Color getRadiance(ShadingState state) { return getSkyRGB(basis.untransform(state.getRay().getDirection())).constrainRGB(); }
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(); 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 ScatterPhoton(ShadingState state, Color power) { Color diffuse, specular; // make sure we are on the right side of the material state.faceforward(); diffuse = getDiffuse(state); specular = getSpecular(state); state.storePhoton(state.getRay().getDirection(), power, diffuse); float d = diffuse.getAverage(); float r = specular.getAverage(); double rnd = state.getRandom(0, 0, 1); if (rnd < d) { // photon is scattered power.mul(diffuse).mul(1.0f / d); OrthoNormalBasis onb = state.getBasis(); double u = 2 * Math.PI * rnd / d; 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); } else if (rnd < d + r) { if (glossyness == 0) { float cos = -Vector3.dot(state.getNormal(), state.getRay().getDirection()); power.mul(diffuse).mul(1.0f / d); // photon is reflected float dn = 2 * cos; Vector3 dir = new Vector3(); dir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x; dir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y; dir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z; state.traceReflectionPhoton(new Ray(state.getPoint(), dir), power); } else { float dn = 2.0f * state.getCosND(); // reflected direction Vector3 refDir = new Vector3(); refDir.x = (dn * state.getNormal().x) + state.getRay().dx; refDir.y = (dn * state.getNormal().y) + state.getRay().dy; refDir.z = (dn * state.getNormal().z) + state.getRay().dz; power.mul(spec).mul(1.0f / r); OrthoNormalBasis onb = state.getBasis(); double u = 2 * Math.PI * (rnd - r) / r; double v = state.getRandom(0, 1, 1); float s = (float)Math.Pow(v, 1 / ((1.0f / glossyness) + 1)); float s1 = (float)Math.Sqrt(1 - s * s); Vector3 w = new Vector3((float)Math.Cos(u) * s1, (float)Math.Sin(u) * s1, s); w = onb.transform(w, new Vector3()); state.traceReflectionPhoton(new Ray(state.getPoint(), w), power); } } }
public Color getRadiance(ShadingState state) { Vector3 n = state.getNormal(); float f = n == null ? 1.0f : Math.Abs(state.getRay().dot(n)); return BORDERS[state.getPrimitiveID() % BORDERS.Length].copy().mul(f); }
public Color getRadiance(ShadingState state) { return(getSkyRGB(basis.untransform(state.getRay().getDirection())).constrainRGB()); }
public Color getRadiance(ShadingState state) { return new Color(Math.Abs(state.getRay().dot(state.getNormal()))); }
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 Color GetRadiance(ShadingState state) { // make sure we are on the right side of the material state.faceforward(); OrthoNormalBasis onb = state.getBasis(); // direct lighting and caustics state.initLightSamples(); state.initCausticSamples(); Color lr = Color.black(); // compute specular contribution if (state.includeSpecular) { Vector3 inv = state.getRay().getDirection().negate(new Vector3()); foreach (LightSample sample in state) { float cosNL = sample.dot(state.getNormal()); float fr = brdf(inv, sample.getShadowRay().getDirection(), onb); lr.madd(cosNL * fr, sample.getSpecularRadiance()); } // indirect lighting - specular if (numRays > 0) { int n = state.getDepth() == 0 ? numRays : 1; for (int i = 0; i < n; i++) { // specular indirect lighting double r1 = state.getRandom(i, 0, n); double r2 = state.getRandom(i, 1, n); float alphaRatio = alphaY / alphaX; float phi = 0; if (r1 < 0.25) { double val = 4 * r1; phi = (float)Math.Atan(alphaRatio * Math.Tan(Math.PI / 2 * val)); } else if (r1 < 0.5) { double val = 1 - 4 * (0.5 - r1); phi = (float)Math.Atan(alphaRatio * Math.Tan(Math.PI / 2 * val)); phi = (float)Math.PI - phi; } else if (r1 < 0.75) { double val = 4 * (r1 - 0.5); phi = (float)Math.Atan(alphaRatio * Math.Tan(Math.PI / 2 * val)); phi += (float)Math.PI; } else { double val = 1 - 4 * (1 - r1); phi = (float)Math.Atan(alphaRatio * Math.Tan(Math.PI / 2 * val)); phi = 2 * (float)Math.PI - phi; } float cosPhi = (float)Math.Cos(phi); float sinPhi = (float)Math.Sin(phi); float denom = (cosPhi * cosPhi) / (alphaX * alphaX) + (sinPhi * sinPhi) / (alphaY * alphaY); float theta = (float)Math.Atan(Math.Sqrt(-Math.Log(1 - r2) / denom)); float sinTheta = (float)Math.Sin(theta); float cosTheta = (float)Math.Cos(theta); Vector3 h = new Vector3(); h.x = sinTheta * cosPhi; h.y = sinTheta * sinPhi; h.z = cosTheta; onb.transform(h); Vector3 o = new Vector3(); float ih = Vector3.dot(h, inv); o.x = 2 * ih * h.x - inv.x; o.y = 2 * ih * h.y - inv.y; o.z = 2 * ih * h.z - inv.z; float no = onb.untransformZ(o); float ni = onb.untransformZ(inv); float w = ih * cosTheta * cosTheta * cosTheta * (float)Math.Sqrt(Math.Abs(no / ni)); Ray r = new Ray(state.getPoint(), o); lr.madd(w / n, state.traceGlossy(r, i)); } } lr.mul(rhoS); } // add diffuse contribution lr.add(state.diffuse(getDiffuse(state))); return(lr); }
public Color getIrradiance(ShadingState state, Color diffuseReflectance) { float b = (float)Math.PI * c / diffuseReflectance.getMax(); Color irr = Color.black(); Point3 p = state.getPoint(); Vector3 n = state.getNormal(); int set = (int)(state.getRandom(0, 1, 1) * numSets); foreach (PointLight vpl in virtualLights[set]) { Ray r = new Ray(p, vpl.p); float dotNlD = -(r.dx * vpl.n.x + r.dy * vpl.n.y + r.dz * vpl.n.z); float dotND = r.dx * n.x + r.dy * n.y + r.dz * n.z; if (dotNlD > 0 && dotND > 0) { float r2 = r.getMax() * r.getMax(); Color opacity = state.traceShadow(r); Color power = Color.blend(vpl.power, Color.BLACK, opacity); float g = (dotND * dotNlD) / r2; irr.madd(0.25f * Math.Min(g, b), power); } } // bias compensation int nb = (state.getDiffuseDepth() == 0 || numBias <= 0) ? numBias : 1; if (nb <= 0) { return(irr); } OrthoNormalBasis onb = state.getBasis(); Vector3 w = new Vector3(); float scale = (float)Math.PI / nb; for (int i = 0; i < nb; i++) { float xi = (float)state.getRandom(i, 0, nb); float xj = (float)state.getRandom(i, 1, nb); float phi = (float)(xi * 2 * Math.PI); float cosPhi = (float)Math.Cos(phi); float sinPhi = (float)Math.Sin(phi); float sinTheta = (float)Math.Sqrt(xj); float cosTheta = (float)Math.Sqrt(1.0f - xj); w.x = cosPhi * sinTheta; w.y = sinPhi * sinTheta; w.z = cosTheta; onb.transform(w); Ray r = new Ray(state.getPoint(), w); r.setMax((float)Math.Sqrt(cosTheta / b)); ShadingState temp = state.traceFinalGather(r, i); if (temp != null) { temp.getInstance().prepareShadingState(temp); if (temp.getShader() != null) { float dist = temp.getRay().getMax(); float r2 = dist * dist; float cosThetaY = -Vector3.dot(w, temp.getNormal()); if (cosThetaY > 0) { float g = (cosTheta * cosThetaY) / r2; // was this path accounted for yet? if (g > b) { irr.madd(scale * (g - b) / g, temp.getShader().GetRadiance(temp)); } } } } } return(irr); }
public void ScatterPhoton(ShadingState state, Color power) { Color refr = Color.mul(1 - f0, color); Color refl = Color.mul(f0, color); float avgR = refl.getAverage(); float avgT = refr.getAverage(); double rnd = state.getRandom(0, 0, 1); if (rnd < avgR) { state.faceforward(); // don't reflect internally if (state.isBehind()) return; // photon is reflected float cos = state.getCosND(); power.mul(refl).mul(1.0f / avgR); float dn = 2 * cos; Vector3 dir = new Vector3(); dir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x; dir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y; dir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z; state.traceReflectionPhoton(new Ray(state.getPoint(), dir), power); } else if (rnd < avgR + avgT) { state.faceforward(); // photon is refracted float cos = state.getCosND(); float neta = state.isBehind() ? eta : 1.0f / eta; power.mul(refr).mul(1.0f / avgT); float wK = -neta; float arg = 1 - (neta * neta * (1 - (cos * cos))); Vector3 dir = new Vector3(); if (state.isBehind() && absorptionDistance > 0) { // this ray is inside the object and leaving it // compute attenuation that occured along the ray power.mul(Color.mul(-state.getRay().getMax() / absorptionDistance, absorptionColor.copy().opposite()).exp()); } if (arg < 0) { // TIR float dn = 2 * cos; dir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x; dir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y; dir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z; state.traceReflectionPhoton(new Ray(state.getPoint(), dir), power); } else { float nK = (neta * cos) - (float)Math.Sqrt(arg); dir.x = (-wK * state.getRay().dx) + (nK * state.getNormal().x); dir.y = (-wK * state.getRay().dy) + (nK * state.getNormal().y); dir.z = (-wK * state.getRay().dz) + (nK * state.getNormal().z); state.traceRefractionPhoton(new Ray(state.getPoint(), dir), power); } } }
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 Color getRadiance(ShadingState state) { // make sure we are on the right side of the material state.faceforward(); OrthoNormalBasis onb = state.getBasis(); // direct lighting and caustics state.initLightSamples(); state.initCausticSamples(); Color lr = Color.black(); // compute specular contribution if (state.includeSpecular) { Vector3 inv = state.getRay().getDirection().negate(new Vector3()); foreach (LightSample sample in state) { float cosNL = sample.dot(state.getNormal()); float fr = brdf(inv, sample.getShadowRay().getDirection(), onb); lr.madd(cosNL * fr, sample.getSpecularRadiance()); } // indirect lighting - specular if (numRays > 0) { int n = state.getDepth() == 0 ? numRays : 1; for (int i = 0; i < n; i++) { // specular indirect lighting double r1 = state.getRandom(i, 0, n); double r2 = state.getRandom(i, 1, n); float alphaRatio = alphaY / alphaX; float phi = 0; if (r1 < 0.25) { double val = 4 * r1; phi = (float)Math.Atan(alphaRatio * Math.Tan(Math.PI / 2 * val)); } else if (r1 < 0.5) { double val = 1 - 4 * (0.5 - r1); phi = (float)Math.Atan(alphaRatio * Math.Tan(Math.PI / 2 * val)); phi = (float)Math.PI - phi; } else if (r1 < 0.75) { double val = 4 * (r1 - 0.5); phi = (float)Math.Atan(alphaRatio * Math.Tan(Math.PI / 2 * val)); phi += (float)Math.PI; } else { double val = 1 - 4 * (1 - r1); phi = (float)Math.Atan(alphaRatio * Math.Tan(Math.PI / 2 * val)); phi = 2 * (float)Math.PI - phi; } float cosPhi = (float)Math.Cos(phi); float sinPhi = (float)Math.Sin(phi); float denom = (cosPhi * cosPhi) / (alphaX * alphaX) + (sinPhi * sinPhi) / (alphaY * alphaY); float theta = (float)Math.Atan(Math.Sqrt(-Math.Log(1 - r2) / denom)); float sinTheta = (float)Math.Sin(theta); float cosTheta = (float)Math.Cos(theta); Vector3 h = new Vector3(); h.x = sinTheta * cosPhi; h.y = sinTheta * sinPhi; h.z = cosTheta; onb.transform(h); Vector3 o = new Vector3(); float ih = Vector3.dot(h, inv); o.x = 2 * ih * h.x - inv.x; o.y = 2 * ih * h.y - inv.y; o.z = 2 * ih * h.z - inv.z; float no = onb.untransformZ(o); float ni = onb.untransformZ(inv); float w = ih * cosTheta * cosTheta * cosTheta * (float)Math.Sqrt(Math.Abs(no / ni)); Ray r = new Ray(state.getPoint(), o); lr.madd(w / n, state.traceGlossy(r, i)); } } lr.mul(rhoS); } // add diffuse contribution lr.add(state.diffuse(getDiffuse(state))); return lr; }
public Color GetRadiance(ShadingState state) { 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 state.includeLights ? radiance : Color.BLACK; kd = top; break; default: Debug.Assert(false); break; } // make sure we are on the right side of the material state.faceforward(); // setup lighting state.initLightSamples(); state.initCausticSamples(); return state.diffuse(kd); }
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 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[] normals1 = this.normals.data; state.getNormal().x = w * normals1[i30 + 0] + u * normals1[i31 + 0] + v * normals1[i32 + 0]; state.getNormal().y = w * normals1[i30 + 1] + u * normals1[i31 + 1] + v * normals1[i32 + 1]; state.getNormal().z = w * normals1[i30 + 2] + u * normals1[i31 + 2] + v * normals1[i32 + 2]; state.getNormal().set(parent.transformNormalObjectToWorld(state.getNormal())); state.getNormal().normalize(); break; } case ParameterList.InterpolationType.FACEVARYING: { int idx = 3 * tri; float[] normals1 = this.normals.data; state.getNormal().x = w * normals1[idx + 0] + u * normals1[idx + 3] + v * normals1[idx + 6]; state.getNormal().y = w * normals1[idx + 1] + u * normals1[idx + 4] + v * normals1[idx + 7]; state.getNormal().z = w * normals1[idx + 2] + u * normals1[idx + 5] + v * normals1[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 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[] 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]; break; } case ParameterList.InterpolationType.FACEVARYING: { int idx = tri << 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]; break; } } if (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; 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 Color GetRadiance(ShadingState state) { return(new Color(Math.Abs(state.getRay().dot(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 = 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 = state.transformNormalObjectToWorld(ng); ng.normalize(); state.getGeoNormal().set(ng); 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; float[] normals1 = this.normals.data; state.getNormal().x = w * normals1[i30 + 0] + u * normals1[i31 + 0] + v * normals1[i32 + 0]; state.getNormal().y = w * normals1[i30 + 1] + u * normals1[i31 + 1] + v * normals1[i32 + 1]; state.getNormal().z = w * normals1[i30 + 2] + u * normals1[i31 + 2] + v * normals1[i32 + 2]; state.getNormal().set(state.transformNormalObjectToWorld(state.getNormal())); state.getNormal().normalize(); break; } case ParameterList.InterpolationType.FACEVARYING: { int idx = 3 * tri; float[] normals1 = this.normals.data; state.getNormal().x = w * normals1[idx + 0] + u * normals1[idx + 3] + v * normals1[idx + 6]; state.getNormal().y = w * normals1[idx + 1] + u * normals1[idx + 4] + v * normals1[idx + 7]; state.getNormal().z = w * normals1[idx + 2] + u * normals1[idx + 5] + v * normals1[idx + 8]; 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 (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[] 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]; break; } case ParameterList.InterpolationType.FACEVARYING: { int idx = tri << 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]; break; } } if (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; 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 Color getRadiance(ShadingState state) { Vector3 n = state.getNormal(); float f = n == null ? 1.0f : Math.Abs(state.getRay().dot(n)); return new Color(state.getInstance().GetHashCode()).mul(f); }
public Color getRadiance(ShadingState state) { // lookup texture based on ray direction return state.includeLights ? getColor(basis.untransform(state.getRay().getDirection(), new Vector3())) : Color.BLACK; }
public Color GetRadiance(ShadingState state) { // lookup texture based on ray direction return(state.includeLights ? getColor(basis.untransform(state.getRay().getDirection(), new Vector3())) : Color.BLACK); }
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 scatterPhoton(ShadingState state, Color power) { // make sure we are on the right side of the material state.faceforward(); Color d = getDiffuse(state); state.storePhoton(state.getRay().getDirection(), power, d); float avgD = d.getAverage(); float avgS = rhoS.getAverage(); double rnd = state.getRandom(0, 0, 1); if (rnd < avgD) { // photon is scattered diffusely power.mul(d).mul(1.0f / avgD); OrthoNormalBasis onb = state.getBasis(); double u = 2 * Math.PI * rnd / avgD; double v = state.getRandom(0, 1, 1); float s = (float)Math.Sqrt(v); float s1 = (float)Math.Sqrt(1.0f - 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); } else if (rnd < avgD + avgS) { // photon is scattered specularly power.mul(rhoS).mul(1 / avgS); OrthoNormalBasis basis = state.getBasis(); Vector3 inv = state.getRay().getDirection().negate(new Vector3()); double r1 = rnd / avgS; double r2 = state.getRandom(0, 1, 1); float alphaRatio = alphaY / alphaX; float phi = 0; if (r1 < 0.25) { double val = 4 * r1; phi = (float)Math.Atan(alphaRatio * Math.Tan(Math.PI / 2 * val)); } else if (r1 < 0.5) { double val = 1 - 4 * (0.5 - r1); phi = (float)Math.Atan(alphaRatio * Math.Tan(Math.PI / 2 * val)); phi = (float)Math.PI - phi; } else if (r1 < 0.75) { double val = 4 * (r1 - 0.5); phi = (float)Math.Atan(alphaRatio * Math.Tan(Math.PI / 2 * val)); phi += (float)Math.PI; } else { double val = 1 - 4 * (1 - r1); phi = (float)Math.Atan(alphaRatio * Math.Tan(Math.PI / 2 * val)); phi = 2 * (float)Math.PI - phi; } float cosPhi = (float)Math.Cos(phi); float sinPhi = (float)Math.Sin(phi); float denom = (cosPhi * cosPhi) / (alphaX * alphaX) + (sinPhi * sinPhi) / (alphaY * alphaY); float theta = (float)Math.Atan(Math.Sqrt(-Math.Log(1 - r2) / denom)); float sinTheta = (float)Math.Sin(theta); float cosTheta = (float)Math.Cos(theta); Vector3 h = new Vector3(); h.x = sinTheta * cosPhi; h.y = sinTheta * sinPhi; h.z = cosTheta; basis.transform(h); Vector3 o = new Vector3(); float ih = Vector3.dot(h, inv); o.x = 2 * ih * h.x - inv.x; o.y = 2 * ih * h.y - inv.y; o.z = 2 * ih * h.z - inv.z; Ray r = new Ray(state.getPoint(), o); state.traceReflectionPhoton(r, power); } }
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); } }
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); } }