* Render environment editor: rotation, intensity, backdrop colour and ground through render.environment.set * Render environment editor: rotation ring in the view with one undo step per drag * Render environment: Source, orientation, backdrop, ground and atmosphere sections * Keep segmented controls inside their allocated rows * fix: compile legacy environment controls only for tests
This commit is contained in:
@@ -7,6 +7,59 @@ use solidcraft_render::lighting::{
|
||||
environment::{EnvironmentResource, EnvironmentSource, SkyGradient},
|
||||
};
|
||||
use std::sync::Arc;
|
||||
|
||||
pub(super) const CONTRACT: crate::schema::TypedContract<crate::schema::render::EnvironmentParams> = crate::schema::TypedContract::new();
|
||||
|
||||
pub(super) fn set(s: &mut crate::Session, params: &serde_json::Value) -> Result<serde_json::Value> {
|
||||
use solidcraft_doc::render_settings::{RenderJob, SkyEnvironment};
|
||||
let command = "render.environment.set";
|
||||
let input: crate::schema::render::EnvironmentParams = serde_json::from_value(params.clone()).map_err(|e| bad(command, e.to_string()))?;
|
||||
if input.rotation_deg.is_none() && input.intensity.is_none() {
|
||||
return Err(bad(command, "rotation_deg or intensity is required"));
|
||||
}
|
||||
let rotation = input
|
||||
.rotation_deg
|
||||
.map(|degrees| {
|
||||
if !degrees.is_finite() || degrees.abs() > 1e6 {
|
||||
Err(bad(command, "rotation must be finite degrees within +/-1000000"))
|
||||
} else {
|
||||
Ok(((degrees + 180.0).rem_euclid(360.0) - 180.0).to_radians())
|
||||
}
|
||||
})
|
||||
.transpose()?;
|
||||
let mut setup = super::patched_settings(s, &input.name, &input.set, command)?;
|
||||
let (angle, intensity, maximum) = match &mut setup.job {
|
||||
Some(RenderJob::CpuRaytrace { options }) => {
|
||||
if let Some(hdr) = &mut options.hdr {
|
||||
(Some(&mut hdr.rotation_rad), &mut hdr.intensity, 64.0)
|
||||
} else if let Some(environment) = &mut options.environment {
|
||||
(None, &mut environment.intensity, 64.0)
|
||||
} else {
|
||||
return Err(bad(command, "choose an environment before editing it"));
|
||||
}
|
||||
}
|
||||
Some(RenderJob::GpuPath { options, .. }) => {
|
||||
if let Some(hdr) = &mut options.hdr {
|
||||
(Some(&mut hdr.rotation_rad), &mut hdr.intensity, 100.0)
|
||||
} else if let Some(SkyEnvironment::Sky { rotation_rad, intensity, .. }) = &mut options.environment {
|
||||
(Some(rotation_rad), intensity, 100.0)
|
||||
} else {
|
||||
return Err(bad(command, "choose an environment before editing it"));
|
||||
}
|
||||
}
|
||||
_ => return Err(bad(command, "environment editing requires a typed raytrace setup")),
|
||||
};
|
||||
if let Some(value) = input.intensity {
|
||||
if !value.is_finite() || !(0.0..=maximum).contains(&value) {
|
||||
return Err(bad(command, format!("intensity must be finite and within 0..{maximum}")));
|
||||
}
|
||||
*intensity = value;
|
||||
}
|
||||
if let Some(value) = rotation {
|
||||
*angle.ok_or_else(|| bad(command, "a constant environment has no rotation"))? = value;
|
||||
}
|
||||
super::set_settings(s, &serde_json::json!({"name":input.name,"set":setup}))
|
||||
}
|
||||
fn unit() -> f64 {
|
||||
1.0
|
||||
}
|
||||
|
||||
@@ -4,6 +4,89 @@ fn save(s: &mut Session, patch: Value) {
|
||||
s.execute("render.settings.set", &json!({"name":"Studio", "set":patch})).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn environment_editor_rotation_wraps_and_each_edit_is_one_undo() {
|
||||
let mut s = Session::default();
|
||||
save(&mut s, json!({"job":{"method":"gpu_path","options":{"environment":{"kind":"sky"}}}}));
|
||||
for (degrees, expected) in [(215.0, -145.0), (540.0, -180.0), (-541.0, 179.0), (1e6, -80.0), (-1e6, 80.0)] {
|
||||
let before = s.doc.clone();
|
||||
let undo = s.undo.len();
|
||||
let result = s.execute("render.environment.set", &json!({"name":"Studio","rotation_deg":degrees})).unwrap();
|
||||
let angle = result["setup"]["job"]["options"]["environment"]["rotation_rad"].as_f64().unwrap().to_degrees();
|
||||
assert!((-180.0..180.0).contains(&angle));
|
||||
assert!((angle - expected).abs() < 1e-9);
|
||||
assert_eq!(s.undo.len(), undo + 1);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(s.doc, before);
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
}
|
||||
let undo = s.undo.len();
|
||||
s.execute("render.environment.set", &json!({"name":"Studio","intensity":2.5})).unwrap();
|
||||
assert_eq!(s.undo.len(), undo + 1);
|
||||
s.execute("render.environment.set", &json!({"name":"Studio","intensity":2.5})).unwrap();
|
||||
assert_eq!(s.undo.len(), undo + 1, "unchanged values do not add history");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn environment_editor_hostile_inputs_refuse_atomically() {
|
||||
let mut s = Session::default();
|
||||
save(&mut s, json!({"job":{"method":"gpu_path","options":{"environment":{"kind":"sky"}}}}));
|
||||
let before = s.doc.clone();
|
||||
let history = (s.revision, s.undo.len(), s.redo.len());
|
||||
for extra in [
|
||||
json!({"rotation_deg":1e6+1.0}),
|
||||
json!({"rotation_deg":-1e6-1.0}),
|
||||
json!({"rotation_deg":1e300}),
|
||||
json!({"rotation_deg":"NaN"}),
|
||||
json!({"rotation_deg":null}),
|
||||
json!({"intensity":-0.01}),
|
||||
json!({"intensity":100.01}),
|
||||
json!({"intensity":1e300}),
|
||||
json!({"intensity":"Infinity"}),
|
||||
json!({"rotation_deg":30,"intensity":101}),
|
||||
json!({"unexpected":true}),
|
||||
json!({}),
|
||||
] {
|
||||
let mut request = json!({"name":"Studio"});
|
||||
request.as_object_mut().unwrap().extend(extra.as_object().unwrap().clone());
|
||||
assert!(s.execute("render.environment.set", &request).is_err(), "{request}");
|
||||
assert!(Arc::ptr_eq(&before, &s.doc));
|
||||
assert_eq!(history, (s.revision, s.undo.len(), s.redo.len()));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn environment_editor_preserves_source_and_refuses_absent_or_incompatible_environment() {
|
||||
let mut s = Session::default();
|
||||
save(&mut s, json!({"job":{"method":"cpu_raytrace","options":{"environment":{"color":[0.2,0.3,0.4],"intensity":1}}}}));
|
||||
let before = s.doc.clone();
|
||||
for request in [json!({"rotation_deg":30}), json!({"intensity":64.01})] {
|
||||
let mut params = json!({"name":"Studio"});
|
||||
params.as_object_mut().unwrap().extend(request.as_object().unwrap().clone());
|
||||
assert!(s.execute("render.environment.set", ¶ms).is_err());
|
||||
assert!(Arc::ptr_eq(&before, &s.doc));
|
||||
}
|
||||
let reply = s.execute("render.environment.set", &json!({"name":"Studio","intensity":64})).unwrap();
|
||||
assert_eq!(reply["setup"]["job"]["options"]["environment"]["color"], json!([0.2, 0.3, 0.4]));
|
||||
for job in [json!({"method":"raster"}), json!({"method":"gpu_path"}), json!({"method":"cpu_raytrace"})] {
|
||||
let before = s.doc.clone();
|
||||
assert!(s.execute("render.environment.set", &json!({"name":"Studio","set":{"job":job},"intensity":2})).is_err());
|
||||
assert!(Arc::ptr_eq(&before, &s.doc));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn environment_editor_command_has_schemas_and_strict_validation() {
|
||||
let command = crate::find_command("render.environment.set").unwrap();
|
||||
assert!(command.contract.is_some());
|
||||
assert!(command.output.is_some());
|
||||
let mut s = Session { param_check: crate::schema::ParamCheck::Strict, ..Default::default() };
|
||||
save(&mut s, json!({"job":{"method":"gpu_path","options":{"environment":{"kind":"sky"}}}}));
|
||||
let reply = s.execute("render.environment.set", &json!({"name":"Studio","intensity":2})).unwrap();
|
||||
assert_eq!(reply["setup"]["job"]["options"]["environment"]["intensity"], 2.0);
|
||||
assert!(s.execute("render.environment.set", &json!({"name":"Studio","intensity":2,"unknown":true})).is_err());
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[test]
|
||||
fn embedded_hdr_capture_is_offline_atomic_deduplicated_and_undoable_after_source_removal() {
|
||||
|
||||
Reference in New Issue
Block a user