forked from cgvr/DeltaVR
		
	
		
			
				
	
	
		
			57 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			HLSL
		
	
	
	
	
	
			
		
		
	
	
			57 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			HLSL
		
	
	
	
	
	
// The MIT License
 | 
						|
// Copyright © 2016 Inigo Quilez
 | 
						|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
 | 
						|
// and associated documentation files (the "Software"), to deal in the Software without restriction,
 | 
						|
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
 | 
						|
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
 | 
						|
//subject to the following conditions: The above copyright notice and this permission notice shall be
 | 
						|
// included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS",
 | 
						|
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 | 
						|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
 | 
						|
// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 | 
						|
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
 | 
						|
// OR OTHER DEALINGS IN THE SOFTWARE.
 | 
						|
//
 | 
						|
// From https://www.shadertoy.com/view/Xt3SzX
 | 
						|
 | 
						|
// Compute a ray capsule intersection
 | 
						|
float capIntersect( float3 ro, float3 rd, float3 pa, float3 pb, float r )
 | 
						|
{
 | 
						|
    float3  ba = pb - pa;
 | 
						|
    float3  oa = ro - pa;
 | 
						|
 | 
						|
    float baba = dot(ba,ba);
 | 
						|
    float bard = dot(ba,rd);
 | 
						|
    float baoa = dot(ba,oa);
 | 
						|
    float rdoa = dot(rd,oa);
 | 
						|
    float oaoa = dot(oa,oa);
 | 
						|
 | 
						|
    float a = baba      - bard*bard;
 | 
						|
    float b = baba*rdoa - baoa*bard;
 | 
						|
    float c = baba*oaoa - baoa*baoa - r*r*baba;
 | 
						|
    float h = b*b - a*c;
 | 
						|
    if( h>=0.0 )
 | 
						|
    {
 | 
						|
        float t = (-b-sqrt(h))/a;
 | 
						|
        float y = baoa + t*bard;
 | 
						|
        // body
 | 
						|
        if( y>0.0 && y<baba ) return t;
 | 
						|
        // caps
 | 
						|
        float3 oc = (y<=0.0) ? oa : ro - pb;
 | 
						|
        b = dot(rd,oc);
 | 
						|
        c = dot(oc,oc) - r*r;
 | 
						|
        h = b*b - c;
 | 
						|
        if( h>0.0 ) return -b - sqrt(h);
 | 
						|
    }
 | 
						|
    return -1.0;
 | 
						|
}
 | 
						|
 | 
						|
// Compute capsule normal given a position
 | 
						|
float3 capNormal( float3 pos, float3 a, float3 b, float r )
 | 
						|
{
 | 
						|
    float3  ba = b - a;
 | 
						|
    float3  pa = pos - a;
 | 
						|
    float h = clamp(dot(pa,ba)/dot(ba,ba),0.0,1.0);
 | 
						|
    return (pa - h*ba)/r;
 | 
						|
}
 |