Skip to content

Commit

Permalink
fix sun
Browse files Browse the repository at this point in the history
  • Loading branch information
PearCoding committed Oct 22, 2024
1 parent 8eb388a commit 7ed97bd
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 19 deletions.
5 changes: 3 additions & 2 deletions scenes/sun.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"technique": {
"type": "path",
"max_depth": 64
"max_depth": 64,
"nee": true
},
"camera": {
"type": "perspective",
Expand Down Expand Up @@ -43,6 +44,6 @@
{"name":"Pillar","shape":"Pillar", "bsdf":"mat-Pillar", "transform": [ 0.25,0,0,0, 0,0.5,0,-1, 0,0,0.25,0, 0,0,0,1 ]}
],
"lights": [
{"type":"directional", "name":"Sun", "radius": 100, "direction": [1, -0.25, 0]}
{"type":"sun", "name":"Sun", "direction": [1, -0.25, 0]}
]
}
2 changes: 1 addition & 1 deletion src/artic/light/perez.art
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ fn @make_perez_light_from_model(id: i32, scene_bbox: BBox, sun_dir: Vec3, tint_c
color_add(if has_sun && model.clearness > 1 { color_mulf(sun_color, math_builtins::fabs(sin_altitude) / flt_pi) } else { color_builtins::black },
color_mulf(zenith, normfactor)));

let actual_sun = if model.clearness > 1 { color_mulf(sun_color, sun_factor) } else { color_builtins::black };
let actual_sun = if has_sun && model.clearness > 1 { color_mulf(sun_color, sun_factor) } else { color_builtins::black };

make_perez_light_raw(id, scene_bbox, sun_dir, sky_color, actual_ground, math_builtins::cos(rad(flt_sun_radius_deg / 2)), actual_sun, model.params, has_ground, has_sun, transform)

Expand Down
18 changes: 7 additions & 11 deletions src/artic/light/sun.art
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ static flt_sun_radius_deg = 0.533:f32; // Sun solid angle seen from earth given
fn @sun_cos_angle_from_radius(radius: f32) = 1 / math_builtins::sqrt(radius * radius + 1); // cos(atan(R))
fn @sun_radius_from_cos_angle(cos_angle: f32) = math_builtins::sqrt(1 - cos_angle * cos_angle) / cos_angle; // tan(acos(C))

fn @make_sun_light(id: i32, sun_dir: Vec3, scene_bbox: BBox, cos_sun_angle: f32, color: Color, handle_as_delta: bool) -> Light {
fn @make_sun_light(id: i32, sun_dir: Vec3 /* Scene to Light */, scene_bbox: BBox, cos_sun_angle: f32, color: Color, handle_as_delta: bool) -> Light {
let scene_radius = bbox_radius(scene_bbox) * 1.01;
let frame = make_orthonormal_mat3x3(sun_dir);
let sun_radius = sun_radius_from_cos_angle(cos_sun_angle);
let sun_area = flt_pi * sun_radius * sun_radius;

let dir_pdf = uniform_cone_pdf(cos_sun_angle);

fn @check_if_hit(gdir: Vec3) = mat3x3_left_mul(frame, gdir).z <= cos_sun_angle;
fn @check_if_hit(gdir: Vec3) = vec3_dot(sun_dir, gdir) >= cos_sun_angle;

Light {
id = id,
Expand All @@ -26,18 +26,14 @@ fn @make_sun_light(id: i32, sun_dir: Vec3, scene_bbox: BBox, cos_sun_angle: f32,
let (pos, pos_pdf) = env_sample_pos(rnd, ndir, bbox_center(scene_bbox), scene_radius);
make_emission_sample(pos, vec3_neg(ndir), color_mulf(color, safe_div(1, pos_pdf * sun_area * sample.pdf)), pos_pdf, sun_area * sample.pdf, sample.dir.z)
},
emission = @ |ctx| {
if handle_as_delta {
color_builtins::black
emission = @ |ctx| {
if !handle_as_delta && check_if_hit(ctx.ray.dir) {
color
} else {
if check_if_hit(ctx.ray.dir) {
color
} else {
color_builtins::black
}
color_builtins::black
}
},
pdf_direct = @ |ray, _| {
pdf_direct = @ |ray, _| {
if handle_as_delta {
make_delta_pdf()
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/light/PerezLight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ void PerezLight::serialize(const SerializationInput& input) const
<< ", " << LoaderUtils::inlineSceneBBox(input.Tree.context());

if (mLight->hasProperty("direction"))
input.Stream << ", " << input.Tree.getInline("direction");
input.Stream << ", vec3_normalize(" << input.Tree.getInline("direction") << ")";
else
input.Stream << ", " << LoaderUtils::inlineVector(mSunDirection);
input.Stream << ", vec3_normalize(" << LoaderUtils::inlineVector(mSunDirection) << ")";

input.Stream << ", " << input.Tree.getInline("color")
<< ", " << input.Tree.getInline("ground");
Expand All @@ -93,7 +93,7 @@ void PerezLight::serialize(const SerializationInput& input) const
<< ", " << (mHasSun ? "true" : "false");

if (mLight->hasProperty("up")) {
input.Stream << ", make_cie_sky_transform(" << input.Tree.getInline("up") << ")";
input.Stream << ", make_cie_sky_transform(vec3_normalize(" << input.Tree.getInline("up") << "))";
} else {
const Matrix3f trans = mLight->property("transform").getTransform().linear().transpose().inverse();
input.Stream << ", " << LoaderUtils::inlineMatrix(trans);
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/light/SunLight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ void SunLight::serialize(const SerializationInput& input) const
<< " let light_" << light_id << " = make_sun_light(" << input.ID;

if (mLight->hasProperty("direction"))
input.Stream << ", " << input.Tree.getInline("direction");
input.Stream << ", vec3_normalize(" << input.Tree.getInline("direction") << ")";
else
input.Stream << ", " << LoaderUtils::inlineVector(mDirection);
input.Stream << ", vec3_normalize(" << LoaderUtils::inlineVector(mDirection) << ")";

input.Stream << ", " << LoaderUtils::inlineSceneBBox(input.Tree.context());

Expand Down

0 comments on commit 7ed97bd

Please sign in to comment.