Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kg/2352649 to your computer and use it in GitHub Desktop.
Save kg/2352649 to your computer and use it in GitHub Desktop.
/* Generated by JSIL v0.5.0 build 13468. See http://jsil.org/ for more information. */
var $asm01 = JSIL.DeclareAssembly("SimpleRaytracer, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null");
JSIL.DeclareNamespace("simpleray");
JSIL.MakeClass($asm00.TypeRef("System.Object"), "simpleray.Vector3f", true, [], function ($) {
$.Method({Static:false, Public:true }, ".ctor",
$sig.get(15, null, [
$.Single, $.Single,
$.Single
], []),
function _ctor (x, y, z) {
this.x = x;
this.y = y;
this.z = z;
}
);
$.Method({Static:false, Public:true }, "Dot",
$sig.get(17, $.Single, [$.Type], []),
function Dot (b) {
return (((this.x * b.x) + (this.y * b.y)) + (this.z * b.z));
}
);
$.Method({Static:false, Public:true }, "Magnitude",
$sig.get(20, $.Single, [], []),
function Magnitude () {
return Math.sqrt((((this.x * this.x) + (this.y * this.y)) + (this.z * this.z)));
}
);
$.Method({Static:false, Public:true }, "Normalise",
$sig.get(18, null, [], []),
function Normalise () {
var f = (1 / (Math.sqrt(this.Dot(this))));
this.x *= f;
this.y *= f;
this.z *= f;
}
);
$.Method({Static:true , Public:true }, "op_Addition",
$sig.get(33, $.Type, [$.Type, $.Type], []),
function op_Addition (a, b) {
return new $asm01.simpleray.Vector3f((a.x + b.x), (a.y + b.y), (a.z + b.z));
}
);
$.Method({Static:true , Public:true }, "op_Division",
$sig.get(30, $.Type, [$.Type, $.Single], []),
function op_Division (a, b) {
return new $asm01.simpleray.Vector3f((a.x / b), (a.y / b), (a.z / b));
}
);
$.Method({Static:true , Public:true }, "op_Multiply",
$sig.get(27, $.Type, [$.Type, $.Single], []),
function op_Multiply (a, b) {
return new $asm01.simpleray.Vector3f((a.x * b), (a.y * b), (a.z * b));
}
);
$.Method({Static:true , Public:true }, "op_Subtraction",
$sig.get(23, $.Type, [$.Type, $.Type], []),
function op_Subtraction (a, b) {
return new $asm01.simpleray.Vector3f((a.x - b.x), (a.y - b.y), (a.z - b.z));
}
);
$.Method({Static:true , Public:true }, "op_UnaryNegation",
$sig.get(25, $.Type, [$.Type], []),
function op_UnaryNegation (a) {
return new $asm01.simpleray.Vector3f(-a.x, -a.y, -a.z);
}
);
$.Method({Static:false, Public:true }, "ReflectIn",
$sig.get(36, $.Type, [$.Type], []),
function ReflectIn (normal) {
var negVector = $asm01.simpleray.Vector3f.op_UnaryNegation(this);
return $asm01.simpleray.Vector3f.op_Subtraction($asm01.simpleray.Vector3f.op_Multiply(normal, (2 * negVector.Dot(normal))), negVector);
}
);
$.Field({Static:false, Public:true }, "x", $.Single, function ($) {
return 0;
});
$.Field({Static:false, Public:true }, "y", $.Single, function ($) {
return 0;
});
$.Field({Static:false, Public:true }, "z", $.Single, function ($) {
return 0;
});
});
JSIL.MakeClass($asm00.TypeRef("System.Object"), "simpleray.Light", true, [], function ($) {
$.Method({Static:false, Public:true }, ".ctor",
$sig.get(21, null, [$asm01.TypeRef("simpleray.Vector3f")], []),
function _ctor (p) {
this.position = p;
}
);
$.Field({Static:false, Public:true }, "position", $asm01.TypeRef("simpleray.Vector3f"), function ($) {
return null;
});
});
JSIL.MakeClass($asm00.TypeRef("System.Object"), "simpleray.Ray", true, [], function ($) {
$.Method({Static:false, Public:true }, ".ctor",
$sig.get(45, null, [$asm01.TypeRef("simpleray.Vector3f"), $asm01.TypeRef("simpleray.Vector3f")], []),
function _ctor (o, d) {
this.origin = o;
this.direction = d;
this.closestHitDistance = 1000;
this.closestHitObject = null;
}
);
$.Field({Static:false, Public:true }, "closestHitDistance", $.Single, function ($) {
return 0;
});
$.Field({Static:false, Public:true }, "closestHitObject", $asm01.TypeRef("simpleray.RTObject"), function ($) {
return null;
});
$.Field({Static:false, Public:true }, "direction", $asm01.TypeRef("simpleray.Vector3f"), function ($) {
return null;
});
$.Field({Static:false, Public:true }, "hitPoint", $asm01.TypeRef("simpleray.Vector3f"), function ($) {
return null;
});
$.Field({Static:false, Public:true }, "origin", $asm01.TypeRef("simpleray.Vector3f"), function ($) {
return null;
});
$.Constant({Static:true , Public:true }, "WORLD_MAX", 1000);
});
JSIL.MakeClass($asm00.TypeRef("System.Object"), "simpleray.RTObject", true, [], function ($) {
$.Method({Static:false, Public:false}, ".ctor",
$sig.get(22, null, [], []),
function _ctor () {
}
);
$.Field({Static:false, Public:true }, "color", $asm05.TypeRef("System.Drawing.Color"), null);
});
JSIL.MakeClass($asm01.TypeRef("simpleray.RTObject"), "simpleray.Sphere", false, [], function ($) {
$.Method({Static:false, Public:true }, ".ctor",
$sig.get(93, null, [
$asm01.TypeRef("simpleray.Vector3f"), $.Single,
$asm05.TypeRef("System.Drawing.Color")
], []),
function _ctor (p, r, c) {
$sig.get(22, null, [], []).Call($asm01.simpleray.RTObject.prototype, "_ctor", null, this);
this.position = p;
this.radius = r;
this.color = c;
}
);
$.Method({Static:false, Public:true }, "GetSurfaceNormalAtPoint",
$sig.get(95, $asm01.TypeRef("simpleray.Vector3f"), [$asm01.TypeRef("simpleray.Vector3f")], []),
function GetSurfaceNormalAtPoint (p) {
var normal = $asm01.simpleray.Vector3f.op_Subtraction(p, this.position);
normal.Normalise();
return normal;
}
);
$.Method({Static:false, Public:true }, "Intersect",
$sig.get(94, $.Single, [$asm01.TypeRef("simpleray.Ray")], []),
function Intersect (ray) {
var lightFromOrigin = $asm01.simpleray.Vector3f.op_Subtraction(this.position, ray.origin);
var v = lightFromOrigin.Dot(ray.direction);
var hitDistance = (((((this.radius * this.radius) + (v * v)) - (lightFromOrigin.x * lightFromOrigin.x)) - (lightFromOrigin.y * lightFromOrigin.y)) - (lightFromOrigin.z * lightFromOrigin.z));
if (hitDistance < 0) {
return -1;
}
hitDistance = (v - (Math.sqrt(hitDistance)));
if (hitDistance < 0) {
return -1;
}
return hitDistance;
}
);
$.Field({Static:false, Public:true }, "position", $asm01.TypeRef("simpleray.Vector3f"), function ($) {
return null;
});
$.Field({Static:false, Public:true }, "radius", $.Single, function ($) {
return 0;
});
});
JSIL.MakeClass($asm01.TypeRef("simpleray.RTObject"), "simpleray.Plane", false, [], function ($) {
$.Method({Static:false, Public:true }, ".ctor",
$sig.get(41, null, [
$asm01.TypeRef("simpleray.Vector3f"), $.Single,
$asm05.TypeRef("System.Drawing.Color")
], []),
function _ctor (n, d, c) {
$sig.get(22, null, [], []).Call($asm01.simpleray.RTObject.prototype, "_ctor", null, this);
this.normal = n;
this.distance = d;
this.color = c;
}
);
$.Method({Static:false, Public:true }, "GetSurfaceNormalAtPoint",
$sig.get(43, $asm01.TypeRef("simpleray.Vector3f"), [$asm01.TypeRef("simpleray.Vector3f")], []),
function GetSurfaceNormalAtPoint (p) {
return this.normal;
}
);
$.Method({Static:false, Public:true }, "Intersect",
$sig.get(42, $.Single, [$asm01.TypeRef("simpleray.Ray")], []),
function Intersect (ray) {
var normalDotRayDir = this.normal.Dot(ray.direction);
if (normalDotRayDir === 0) {
return -1;
}
var hitDistance = (-(this.normal.Dot(ray.origin) - this.distance) / normalDotRayDir);
if (hitDistance < 0) {
return -1;
}
return hitDistance;
}
);
$.Field({Static:false, Public:true }, "distance", $.Single, function ($) {
return 0;
});
$.Field({Static:false, Public:true }, "normal", $asm01.TypeRef("simpleray.Vector3f"), function ($) {
return null;
});
});
JSIL.MakeClass($asm00.TypeRef("System.Object"), "simpleray.RayTracer", false, [], function ($) {
$.Method({Static:false, Public:true }, ".ctor",
$sig.get(6121, null, [], []),
function _ctor () {
}
);
$.Method({Static:true , Public:false}, "CheckIntersection",
$sig.get(6114, null, [$jsilcore.TypeRef("JSIL.Reference", [$asm01.TypeRef("simpleray.Ray")])], []),
function CheckIntersection (/* ref */ ray) {
var enumerator = $asm01.simpleray.RayTracer.objects.GetEnumerator().MemberwiseClone();
try {
while (enumerator.MoveNext()) {
var obj = enumerator.Current;
var hitDistance = obj.Intersect(ray.value);
if (!((hitDistance >= ray.value.closestHitDistance) || (hitDistance <= 0))) {
ray.value.closestHitObject = obj;
ray.value.closestHitDistance = hitDistance;
}
}
} finally {
enumerator.IDisposable_Dispose();
}
ray.value.hitPoint = $asm01.simpleray.Vector3f.op_Addition(ray.value.origin, $asm01.simpleray.Vector3f.op_Multiply(ray.value.direction, ray.value.closestHitDistance));
}
);
$.Method({Static:true , Public:false}, "Main",
$sig.get(6105, null, [$jsilcore.TypeRef("System.Array", [$.String])], []),
function Main (args) {
$asm01.simpleray.RayTracer.objects = $sig.get(6150, null, [], []).Construct($asm00.System.Collections.Generic.List$b1.Of($asm01.simpleray.RTObject));
$asm01.simpleray.RayTracer.lights = $sig.get(6150, null, [], []).Construct($asm00.System.Collections.Generic.List$b1.Of($asm01.simpleray.Light));
$asm01.simpleray.RayTracer.random = $sig.get(6380, null, [$asm00.System.Int32], []).Construct($asm00.System.Random, 1478650229);
var canvas = $sig.get(6692, null, [$asm00.System.Int32, $asm00.System.Int32], []).Construct($asm05.System.Drawing.Bitmap, 640, 480);
for (var i = 0; i < 30; ++i) {
var x = (($asm01.simpleray.RayTracer.random.NextDouble() * 10) - 5);
var y = (($asm01.simpleray.RayTracer.random.NextDouble() * 10) - 5);
var z = ($asm01.simpleray.RayTracer.random.NextDouble() * 10);
var c = $sig.get(540, $asm05.System.Drawing.Color, [
$asm00.System.Int32, $asm00.System.Int32,
$asm00.System.Int32, $asm00.System.Int32
], []).CallStatic($asm05.System.Drawing.Color, "FromArgb", null,
255,
$sig.get(6394, $asm00.System.Int32, [$asm00.System.Int32], []).CallVirtual("Next", null, $asm01.simpleray.RayTracer.random, 255),
$sig.get(6394, $asm00.System.Int32, [$asm00.System.Int32], []).CallVirtual("Next", null, $asm01.simpleray.RayTracer.random, 255),
$sig.get(6394, $asm00.System.Int32, [$asm00.System.Int32], []).CallVirtual("Next", null, $asm01.simpleray.RayTracer.random, 255)
).MemberwiseClone();
var s = new $asm01.simpleray.Sphere(new $asm01.simpleray.Vector3f(x, y, z), $asm01.simpleray.RayTracer.random.NextDouble(), c);
$asm01.simpleray.RayTracer.objects.Add(s);
}
var floor = new $asm01.simpleray.Plane(new $asm01.simpleray.Vector3f(0, 1, 0), -10, $asm05.System.Drawing.Color.Aquamarine);
$asm01.simpleray.RayTracer.objects.Add(floor);
$asm01.simpleray.RayTracer.lights.Add(new $asm01.simpleray.Light(new $asm01.simpleray.Vector3f(2, 0, 0)));
$asm01.simpleray.RayTracer.lights.Add(new $asm01.simpleray.Light(new $asm01.simpleray.Vector3f(0, 10, 7.5)));
$asm01.simpleray.RayTracer.pixelWidth = (($asm01.simpleray.RayTracer.screenBottomRightPos.x - $asm01.simpleray.RayTracer.screenTopLeftPos.x) / 640);
$asm01.simpleray.RayTracer.pixelHeight = (($asm01.simpleray.RayTracer.screenTopLeftPos.y - $asm01.simpleray.RayTracer.screenBottomRightPos.y) / 480);
$asm00.System.Console.WriteLine("Rendering...\n");
$asm00.System.Console.WriteLine("|0%---100%|");
$asm01.simpleray.RayTracer.RenderRow(canvas, 48, 0);
$sig.get(6527, null, [$asm00.System.String], []).CallVirtual("Save", null, canvas, "output.png");
}
);
$.Method({Static:true , Public:false}, "RenderPixel",
$sig.get(6118, $asm05.TypeRef("System.Drawing.Color"), [$.Int32, $.Int32], []),
function RenderPixel (x, y) {
var eyeToPixelDir = $asm01.simpleray.Vector3f.op_Subtraction(new $asm01.simpleray.Vector3f(($asm01.simpleray.RayTracer.screenTopLeftPos.x + (x * $asm01.simpleray.RayTracer.pixelWidth)), ($asm01.simpleray.RayTracer.screenTopLeftPos.y - (y * $asm01.simpleray.RayTracer.pixelHeight)), 0), $asm01.simpleray.RayTracer.eyePos);
eyeToPixelDir.Normalise();
var ray = new $asm01.simpleray.Ray($asm01.simpleray.RayTracer.eyePos, eyeToPixelDir);
return $asm01.simpleray.RayTracer.Trace(ray, 0);
}
);
$.Method({Static:true , Public:false}, "RenderRow",
$sig.get(6110, null, [
$asm05.TypeRef("System.Drawing.Bitmap"), $.Int32,
$.Int32
], []),
function RenderRow (canvas, dotPeriod, y) {
var $l$gc__DisplayClass = new $asm01.simpleray.RayTracer_$l$gc__DisplayClass1();
$l$gc__DisplayClass.canvas = canvas;
$l$gc__DisplayClass.dotPeriod = dotPeriod;
$l$gc__DisplayClass.y = y;
if ($l$gc__DisplayClass.y >= 480) {
return;
}
if (!($l$gc__DisplayClass.y % $l$gc__DisplayClass.dotPeriod)) {
$asm00.System.Console.Write("*");
}
for (var x = 0; x < 640; ++x) {
var c = $asm01.simpleray.RayTracer.RenderPixel(x, $l$gc__DisplayClass.y).MemberwiseClone();
$l$gc__DisplayClass.canvas.SetPixel(x, $l$gc__DisplayClass.y, c.MemberwiseClone());
}
setTimeout(function () {
$asm01.simpleray.RayTracer.RenderRow(this.canvas, this.dotPeriod, (this.y + 1));
}.bind($l$gc__DisplayClass), 0);
}
);
$.Method({Static:true , Public:false}, "Trace",
$sig.get(6119, $asm05.TypeRef("System.Drawing.Color"), [$asm01.TypeRef("simpleray.Ray"), $.Int32], []),
function Trace ($ray, traceDepth) {
var ray = new JSIL.Variable($ray);
$asm01.simpleray.RayTracer.CheckIntersection(/* ref */ ray);
if (!((ray.value.closestHitDistance < 1000) && (ray.value.closestHitObject !== null))) {
return $asm01.simpleray.RayTracer.BG_COLOR;
}
var r = (0.15000000596046448 * ray.value.closestHitObject.color.R);
var g = (0.15000000596046448 * ray.value.closestHitObject.color.G);
var b = (0.15000000596046448 * ray.value.closestHitObject.color.B);
var surfaceNormal = ray.value.closestHitObject.GetSurfaceNormalAtPoint(ray.value.hitPoint);
var viewerDir = $asm01.simpleray.Vector3f.op_UnaryNegation(ray.value.direction);
var enumerator = $asm01.simpleray.RayTracer.lights.GetEnumerator().MemberwiseClone();
var shadowRay = new JSIL.Variable(null);
try {
while (enumerator.MoveNext()) {
var light = enumerator.Current;
var lightDir = new $asm01.simpleray.Vector3f(0, 0, 0);
lightDir = $asm01.simpleray.Vector3f.op_Subtraction(light.position, ray.value.hitPoint);
var lightDistance = lightDir.Magnitude();
lightDir.Normalise();
shadowRay.value = new $asm01.simpleray.Ray($asm01.simpleray.Vector3f.op_Addition(ray.value.hitPoint, $asm01.simpleray.Vector3f.op_Multiply(lightDir, 9.9999997473787516E-05)), lightDir);
shadowRay.value.closestHitDistance = lightDistance;
$asm01.simpleray.RayTracer.CheckIntersection(/* ref */ shadowRay);
if (shadowRay.value.closestHitObject === null) {
var cosLightAngleWithNormal = surfaceNormal.Dot(lightDir);
if (cosLightAngleWithNormal > 0) {
r += ((0.5 * cosLightAngleWithNormal) * ray.value.closestHitObject.color.R);
g += ((0.5 * cosLightAngleWithNormal) * ray.value.closestHitObject.color.G);
b += ((0.5 * cosLightAngleWithNormal) * ray.value.closestHitObject.color.B);
var lightReflectionDir = $asm01.simpleray.Vector3f.op_Subtraction($asm01.simpleray.Vector3f.op_Multiply(surfaceNormal, (cosLightAngleWithNormal * 2)), lightDir);
var specularFactor = viewerDir.Dot(lightReflectionDir);
if (specularFactor > 0) {
specularFactor = (2 * JSIL.Cast(Math.pow(specularFactor, 50), $asm00.System.Single));
r += (specularFactor * ray.value.closestHitObject.color.R);
g += (specularFactor * ray.value.closestHitObject.color.G);
b += (specularFactor * ray.value.closestHitObject.color.B);
}
}
}
}
} finally {
enumerator.IDisposable_Dispose();
}
if (traceDepth < 3) {
var reflectedDir = ray.value.direction.ReflectIn(surfaceNormal);
var reflectionRay = new $asm01.simpleray.Ray($asm01.simpleray.Vector3f.op_Addition(ray.value.hitPoint, $asm01.simpleray.Vector3f.op_Multiply(reflectedDir, 9.9999997473787516E-05)), reflectedDir);
var reflectionCol = $asm01.simpleray.RayTracer.Trace(reflectionRay, (traceDepth + 1)).MemberwiseClone();
r += (0.5 * reflectionCol.R);
g += (0.5 * reflectionCol.G);
b += (0.5 * reflectionCol.B);
}
if (r > 255) {
r = 255;
}
if (g > 255) {
g = 255;
}
if (b > 255) {
b = 255;
}
return $sig.get(540, $asm05.System.Drawing.Color, [
$asm00.System.Int32, $asm00.System.Int32,
$asm00.System.Int32, $asm00.System.Int32
], []).CallStatic($asm05.System.Drawing.Color, "FromArgb", null,
255,
Math.floor(r),
Math.floor(g),
Math.floor(b)
);
}
);
$.Field({Static:true , Public:false}, "BG_COLOR", $asm05.TypeRef("System.Drawing.Color"), null);
$.Constant({Static:true , Public:false}, "CANVAS_HEIGHT", 480);
$.Constant({Static:true , Public:false}, "CANVAS_WIDTH", 640);
$.Field({Static:true , Public:false}, "eyePos", $asm01.TypeRef("simpleray.Vector3f"), function ($) {
return null;
});
$.Field({Static:true , Public:false}, "lights", $asm00.TypeRef("System.Collections.Generic.List`1", [$asm01.TypeRef("simpleray.Light")]), function ($) {
return null;
});
$.Constant({Static:true , Public:false}, "MATERIAL_DIFFUSE_COEFFICIENT", 0.5);
$.Constant({Static:true , Public:false}, "MATERIAL_REFLECTION_COEFFICIENT", 0.5);
$.Constant({Static:true , Public:false}, "MATERIAL_SPECULAR_COEFFICIENT", 2);
$.Constant({Static:true , Public:false}, "MATERIAL_SPECULAR_POWER", 50);
$.Constant({Static:true , Public:false}, "MAX_DEPTH", 3);
$.Field({Static:true , Public:false}, "objects", $asm00.TypeRef("System.Collections.Generic.List`1", [$asm01.TypeRef("simpleray.RTObject")]), function ($) {
return null;
});
$.Constant({Static:true , Public:false}, "PI", 3.1415927410125732);
$.Constant({Static:true , Public:false}, "PI_OVER_2", 1.5707963705062866);
$.Constant({Static:true , Public:false}, "PI_X_2", 6.2831854820251465);
$.Field({Static:true , Public:false}, "pixelHeight", $.Single, function ($) {
return 0;
});
$.Field({Static:true , Public:false}, "pixelWidth", $.Single, function ($) {
return 0;
});
$.Field({Static:true , Public:false}, "random", $asm00.TypeRef("System.Random"), function ($) {
return null;
});
$.Field({Static:true , Public:false}, "screenBottomRightPos", $asm01.TypeRef("simpleray.Vector3f"), function ($) {
return null;
});
$.Field({Static:true , Public:false}, "screenTopLeftPos", $asm01.TypeRef("simpleray.Vector3f"), function ($) {
return null;
});
$.Constant({Static:true , Public:false}, "TINY", 9.9999997473787516E-05);
$.Method({Static:true , Public:false}, ".cctor",
$sig.get(6122, null, [], []),
function _cctor () {
$asm01.simpleray.RayTracer.BG_COLOR = new $asm05.System.Drawing.Color();
$asm01.simpleray.RayTracer.BG_COLOR = $asm05.System.Drawing.Color.BlueViolet.MemberwiseClone();
$asm01.simpleray.RayTracer.eyePos = new $asm01.simpleray.Vector3f(0, 0, -5);
$asm01.simpleray.RayTracer.screenTopLeftPos = new $asm01.simpleray.Vector3f(-6, 4, 0);
$asm01.simpleray.RayTracer.screenBottomRightPos = new $asm01.simpleray.Vector3f(6, -4, 0);
}
);
});
JSIL.MakeClass($asm00.TypeRef("System.Object"), "simpleray.RayTracer/<>c__DisplayClass1", false, [], function ($) {
$.Method({Static:false, Public:true }, ".ctor",
$sig.get(14181, null, [], []),
function _ctor () {
}
);
$.Field({Static:false, Public:true }, "canvas", $asm05.TypeRef("System.Drawing.Bitmap"), function ($) {
return null;
});
$.Field({Static:false, Public:true }, "dotPeriod", $.Int32, function ($) {
return 0;
});
$.Field({Static:false, Public:true }, "y", $.Int32, function ($) {
return 0;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment