- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
class RayTracer {
private maxDepth = 5;
private intersections(ray: Ray, scene: Scene) {
let closest = +Infinity;
let closestInter: Intersection = undefined;
for (let i in scene.things) {
let inter = scene.things[i].intersect(ray);
if (inter != null && inter.dist < closest) {
closestInter = inter;
closest = inter.dist;
}
}
return closestInter;
}
private testRay(ray: Ray, scene: Scene) {
let isect = this.intersections(ray, scene);
if (isect != null) {
return isect.dist;
} else {
return undefined;
}
}
private traceRay(ray: Ray, scene: Scene, depth: number): Color {
let isect = this.intersections(ray, scene);
if (isect === undefined) {
return Color.background;
} else {
return this.shade(isect, scene, depth);
}
}
private shade(isect: Intersection, scene: Scene, depth: number) {
let d = isect.ray.dir;
let pos = Vector.plus(Vector.times(isect.dist, d), isect.ray.start);
let normal = isect.thing.normal(pos);
let reflectDir = Vector.minus(d, Vector.times(2, Vector.times(Vector.dot(normal, d), normal)));
let naturalColor = Color.plus(Color.background,
this.getNaturalColor(isect.thing, pos, normal, reflectDir, scene));
let reflectedColor = (depth >= this.maxDepth) ? Color.grey : this.getReflectionColor(isect.thing, pos, normal, reflectDir, scene, depth);
return Color.plus(naturalColor, reflectedColor);
}
private getReflectionColor(thing: Thing, pos: Vector, normal: Vector, rd: Vector, scene: Scene, depth: number) {
return Color.scale(thing.surface.reflect(pos), this.traceRay({ start: pos, dir: rd }, scene, depth + 1));
}
private getNaturalColor(thing: Thing, pos: Vector, norm: Vector, rd: Vector, scene: Scene) {
const addLight = (col: Color, light: Light) => {
let ldis = Vector.minus(light.pos, pos);
let livec = Vector.norm(ldis);
let neatIsect = this.testRay({ start: pos, dir: livec }, scene);
let isInShadow = (neatIsect === undefined) ? false : (neatIsect <= Vector.mag(ldis));
if (isInShadow) {
return col;
} else {
let illum = Vector.dot(livec, norm);
let lcolor = (illum > 0) ? Color.scale(illum, light.color)
: Color.defaultColor;
let specular = Vector.dot(livec, Vector.norm(rd));
let scolor = (specular > 0) ? Color.scale(Math.pow(specular, thing.surface.roughness), light.color)
: Color.defaultColor;
return Color.plus(col, Color.plus(Color.times(thing.surface.diffuse(pos), lcolor),
Color.times(thing.surface.specular(pos), scolor)));
}
}
//return scene.lights.reduce(addLight, Color.defaultColor);
let resColor = Color.defaultColor;
for (const light of scene.lights) {
resColor = addLight(resColor, light);
}
return resColor;
}
render(scene: Scene, screenWidth: number, screenHeight: number) {
const getPoint = (x: number, y: number, camera: Camera) => {
const recenterX = (x: number) => (x - (screenWidth / 2.0)) / 2.0 / screenWidth;
const recenterY = (y: number) => - (y - (screenHeight / 2.0)) / 2.0 / screenHeight;
return Vector.norm(Vector.plus(camera.forward, Vector.plus(Vector.times(recenterX(x), camera.right), Vector.times(recenterY(y), camera.up))));
}
for (let y = 0; y < screenHeight; y++) {
for (let x = 0; x < screenWidth; x++) {
let color = this.traceRay({ start: scene.camera.pos, dir: getPoint(x, y, scene.camera) }, scene, 0);
let c = Color.toDrawingColor(color);
//ctx.fillStyle = "rgb(" + String(c.r) + ", " + String(c.g) + ", " + String(c.b) + ")";
//ctx.fillRect(x, y, x + 1, y + 1);
// next line eats stack (or memory?)
print(`<rect x="${x}" y="${y}" width="1" height="1" style="fill:rgb(${c.r},${c.g},${c.b});" />`);
}
}
}
}
function defaultScene(): Scene {
ASD_77 # 0
rotoeb # 0
ASD_77 # 0 ⇈
guest # 0 ⇈
guest # 0
ASD_77 # 0 ⇈
ASD_77 # 0
bormand # 0 ⇈
ASD_77 # 0 ⇈
guest # 0 ⇈
ASD_77 # 0
JloJle4Ka # 0 ⇈
guest # 0 ⇈
JloJle4Ka # 0 ⇈
guest # 0 ⇈
rotoeb # 0 ⇈
j123123 # 0 ⇈
kPHP от вконтакта
bormand # 0 ⇈
guest # 0 ⇈
Точнее на крестах, но на крестах очень олимпиадных
bormand # 0 ⇈