Created
          September 16, 2017 10:02 
        
      - 
      
- 
        Save mahulst/64f4725cb76c3f459357fb7b20784a27 to your computer and use it in GitHub Desktop. 
    rayTriangleIntersect
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | rayTriangleIntersect : Vec3 -> Vec3 -> ( Vec3, Vec3, Vec3 ) -> Maybe Vec3 | |
| rayTriangleIntersect rayOrigin rayDirection ( triangle0, triangle1, triangle2 ) = | |
| let | |
| epsilon = | |
| 0.000001 | |
| edge1 = | |
| Vec3.sub triangle1 triangle0 | |
| edge2 = | |
| Vec3.sub triangle2 triangle0 | |
| pvec = | |
| Vec3.cross rayDirection edge2 | |
| det = | |
| Vec3.dot edge1 pvec | |
| in | |
| if det < epsilon then | |
| Nothing | |
| else | |
| let | |
| tvec = | |
| Vec3.sub rayOrigin triangle0 | |
| u = | |
| Vec3.dot tvec pvec | |
| in | |
| if u < 0 || u > det then | |
| Nothing | |
| else | |
| let | |
| qvec = | |
| Vec3.cross tvec edge1 | |
| v = | |
| Vec3.dot rayDirection qvec | |
| in | |
| if v < 0 || u + v > det then | |
| Nothing | |
| else | |
| let | |
| t = | |
| (Vec3.dot edge2 qvec) / det | |
| v0 = | |
| (Vec3.getX rayOrigin) + t * (Vec3.getX rayDirection) | |
| v1 = | |
| (Vec3.getY rayOrigin) + t * (Vec3.getY rayDirection) | |
| v2 = | |
| (Vec3.getZ rayOrigin) + t * (Vec3.getZ rayDirection) | |
| in | |
| Just (vec3 v0 v1 v2) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment