public void getUV(V3 P, out float u, out float v) { V3 AB = B - A; V3 AC = C - A; V3 AP = P - A; //alfa u = V3.prod_scal(ref AP, ref AB) / AB.Norme2(); //beta V3 aux = AP - u * AB; v = V3.prod_scal(ref aux, ref AC) / AC.Norme2(); }
public bool intersection_RayObject(V3 R0, V3 Rd, out V3 P, out V3 N, out float t) { V3 CR0; float A, B, C, delta, t1, t2; P = new V3(0, 0, 0); N = new V3(0, 0, 0); t = -1; CR0 = R0 - Center; // (t^2) rd^2 + 2*t*rd*b +b^2 = R^2 A = Rd.Norme2(); B = 2f * V3.prod_scal(ref Rd, ref CR0); C = CR0.Norme2() - (R * R); delta = (B * B) - (4 * A * C); if (delta < 0) { return(false); } t1 = (-B + (float)Math.Sqrt(delta)) / (2 * A); t2 = (-B - (float)Math.Sqrt(delta)) / (2 * A); t = (t1 > 0 && t1 < t2) ? t1 : t2; if (t < MIN_DISTANCE) { return(false); } P = R0 + t * Rd; N = getPixelSphereNormal(P); return(true); }
public override bool testColition(V3 positionStart, V3 direction, out double TPositionColition) { // need aproximation to calculate float // calculate the aproximation double a; double b; double c; a = (direction.Norme2()); b = 2 * direction * (positionStart - this.getPosition()); c = (positionStart - this.getPosition()).Norme2() - (this.rayon * this.rayon); // calcul alpha = B²-4ac double alpha = (b * b) - (4 * a * c); // 0 result no colition alpha = 0 if (alpha < 0) { TPositionColition = 0; return(false); } // 1 colition tangante if (alpha == 0) { TPositionColition = (-b) / (2 * a); return(true); } // 2 colition if (alpha > 0) { //Console.WriteLine("alpha"); double t2 = (-b + Math.Sqrt(alpha)) / (2 * a); double t1 = (-b - Math.Sqrt(alpha)) / (2 * a); if (0 < t1 && t1 < t2) { TPositionColition = t1; return(true); } if (t1 < 0 && 0 < t2) { TPositionColition = t2; return(true); } if (t1 < 0 && t2 < 0) { TPositionColition = 0; return(false); } } TPositionColition = 0; return(false); }
public override float getIntersect(V3 R, V3 cam) { float t, t1, t2; float A = R.Norme2(); float B = 2*(R.x * (cam.x - origin.x) + R.y * (cam.y - origin.y) + R.z * (cam.z - origin.z)); float C = (cam.x - origin.x) * (cam.x - origin.x) + (cam.y - origin.y) * (cam.y - origin.y) + (cam.z - origin.z) * (cam.z - origin.z) - (this.rayon * this.rayon); float D = B * B - 4 * A * C; if (D < 0) { return -1f; } else { t = -1f; if (D > 0) { t1 = (-B - IMA.Sqrtf(D)) / (2 * A); t2 = (-B + IMA.Sqrtf(D)) / (2 * A); if (t1 < t2 && t1 >= 0) { t = t1; } else if (t2 < t1 && t2 >= 0) { t = t2; } } else { t = -B / (2 * A); } return t; } }