Skip to content

Instantly share code, notes, and snippets.

@elliotwoods
Created May 20, 2016 02:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elliotwoods/4109fdf94b7900ac194c9b700bea2bfd to your computer and use it in GitHub Desktop.
Save elliotwoods/4109fdf94b7900ac194c9b700bea2bfd to your computer and use it in GitHub Desktop.
Light Field rendering techniques
Texture2D RayStart : PREVIOUS;
Texture2D RayTransmit;
Texture2D MirrorData;
class Ray {
bool valid;
float3 s;
float3 t;
int mirrorIndex;
float mirrorRho;
float mirrorThi;
float getDistanceTo(float3 aPoint) {
return length(cross(aPoint - s, aPoint - (s + t))) / length(t);
}
float3 getClosestPointOnRayTo(float3 aPoint) {
float tLengthSquared = dot(t, t);
return s + (t * dot(aPoint - s, t)) / tLengthSquared;
}
Ray intersect(Ray other) {
Ray intersectRay;
intersectRay.s = this.s;
intersectRay.t = other.s - this.s;
const float3 p1 = s, p2 = s+t, p3 = other.s, p4 = other.s+other.t;
const float EPS = 1.0E-5;
float3 p13,p43,p21;
float d1343,d4321,d1321,d4343,d2121;
float numer,denom;
p13 = p1 - p3;
p43 = p4 - p3;
if (length(p43) < EPS) {
return intersectRay;
}
p21 = p2 - p1;
if (length(p21) < EPS) {
return intersectRay;
}
d1343 = dot(p13, p43);
d4321 = dot(p43, p21);
d1321 = dot(p13, p21);
d4343 = dot(p43, p43);
d2121 = dot(p21, p21);
denom = d2121 * d4343 - d4321 * d4321;
if (abs(denom) < EPS) {
return intersectRay;
}
numer = d1343 * d4321 - d1321 * d4343;
const float ma = numer / denom;
const float mb = (d1343 + d4321 * ma) / d4343;
intersectRay.s = p1 + ma * p21;
intersectRay.t = p3 + mb * p43;
intersectRay.t = intersectRay.t - intersectRay.s;
return intersectRay;
}
float3 getStart() {
return this.s;
}
float3 getEnd() {
return this.s + this.t;
}
float3 getClosestPointOnRayTo(Ray other) {
return this.intersect(other).s;
}
float getDistanceToLineSegment(Ray lineSegment) {
Ray intersectLineToThisRay = lineSegment.intersect(this);
float3 u3 = (intersectLineToThisRay.getEnd() - lineSegment.s) / lineSegment.t;
u3 = clamp(u3, 0.0f, 1.0f);
float3 pointOnLineSegment = lineSegment.s + lineSegment.t * u3;
float3 transportToLineSegment = intersectLineToThisRay.t + (pointOnLineSegment - intersectLineToThisRay.s);
return length(transportToLineSegment);
}
};
struct psInput
{
float4 p : SV_Position;
float2 uv : TEXCOORD0;
};
SamplerState linearSampler : IMMUTABLE
{
Filter = MIN_MAG_MIP_LINEAR;
AddressU = Border;
AddressV = Border;
BorderColor = float4(0,0,0,1.0f);
};
Ray getRay(psInput input, Texture2D RayStart, Texture2D RayTransmit, Texture2D MirrorData) {
float4 mirrorData = MirrorData.Sample(linearSampler,input.uv);
Ray newRay;
if (mirrorData.a) {
newRay.valid = true;
Ray ray;
newRay.s = RayStart.Sample(linearSampler,input.uv).xyz;
newRay.t = RayTransmit.Sample(linearSampler,input.uv).xyz;
newRay.mirrorIndex = mirrorData.x;
newRay.mirrorRho = mirrorData.y;
newRay.mirrorRho = mirrorData.z;
return newRay;
} else {
newRay.valid = false;
newRay.s = (float3) 0.0f;
newRay.t = (float3) 0.0f;
newRay.mirrorIndex = 0;
newRay.mirrorRho = 0;
newRay.mirrorThi = 0;
return newRay;
}
}
#define GETRAY getRay(input, RayStart, RayTransmit, MirrorData)
//@author: elliotwoods
//@help:
//@tags: texture
//@credits:
#include "LightField.fxh"
Texture2D Image;
float Z = 4.0f;
float4x4 TransformInverse;
float4 PS(psInput input) : SV_Target
{
Ray ray = GETRAY;
if (ray.valid) {
float u = Z / ray.t.z;
float3 pointOnPlane = ray.s + ray.t * u;
float4 pointOnPlane4;
pointOnPlane4.xyz = pointOnPlane;
pointOnPlane4.w = 1.0f;
pointOnPlane = mul(pointOnPlane4, TransformInverse).xyz;
float2 uv = float2((pointOnPlane.x + 1.0f) / 2.0f, (1.0f - pointOnPlane.y) / 2.0f);
return Image.Sample(linearSampler, uv);
} else {
return float4(0,0,0,0);
}
}
technique10 Process
{
pass P0
{
SetPixelShader(CompileShader(ps_4_0,PS()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment