Advertisement
Guest User

Untitled

a guest
May 4th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.79 KB | None | 0 0
  1. let hitSphere radius (texture:ITexture) =
  2.     let innerHit o dir =
  3.         match o, dir with
  4.         | p, v ->
  5.             let a = (pown (Vector.getX v) 2) + (pown (Vector.getY v) 2) + (pown (Vector.getZ v) 2)
  6.             let b = 2.0*(Point.getX p * Vector.getX v + Point.getY p * Vector.getY v + Point.getZ p * Vector.getZ v )
  7.             let c = (pown (Point.getX p) 2) + (pown (Point.getY p) 2) + (pown (Point.getZ p) 2) - (pown radius 2)  
  8.             let t1 = (-b + (sqrt((pown b 2) - 4.0 * a*c))) / (2.0 * a)
  9.             let t2 = (-b - (sqrt((pown b 2) - 4.0 * a * c))) / (2.0 * a)
  10.                
  11.             let correctSolution = getCorrectPolySolution t1 t2
  12.                
  13.             if (correctSolution.IsSome) then
  14.                 let t = correctSolution.Value
  15.                 let hp = o + (t * dir)
  16.                 let v =  Vector.normalize (Vector.mkVector ((Point.getX hp) / radius) ((Point.getY hp) / radius) ((Point.getZ hp) / radius))
  17.                 let n =
  18.                     if Vector.dotProduct v dir > 0.0 then
  19.                             let x = - Vector.getX v
  20.                             let y = - Vector.getY v
  21.                             let z = - Vector.getZ v
  22.                             Vector.mkVector x y z
  23.                             else v
  24.                
  25.  
  26.                  //Texture mapping
  27.                 let nx, ny, nz = Vector.getCoord n
  28.                 let theta = acos ny
  29.                 let phi' = atan2 nx nz
  30.                let phi = if phi' < 0.0
  31.                           then phi' + 2.0 * pi
  32.                          else phi'
  33.                
  34.                 let u = theta / (2.0 * pi)
  35.                 let v = (1.0 - phi) / pi
  36.                 let mat = texture.f u v
  37.  
  38.                 Some (t, n, mat, hp)
  39.             else None
  40.     innerHit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement