1use kurbo::Point;
2use pax_engine::*;
3use pax_runtime::api::{
4 Color, Depth, Layer, LightShape, Numeric, SceneAmbientLight, SceneLight, SceneLighting, Size,
5 Vector3,
6};
7use pax_runtime::{
8 BaseInstance, ExpandedNode, InstanceFlags, InstanceNode, InstantiationArgs, RuntimeContext,
9};
10use std::rc::Rc;
11
12use crate::common::canvas_surface_transform;
13
14#[pax]
19#[engine_import_path("pax_engine")]
20#[primitive("pax_std::drawing::lighting::LightFrameInstance")]
21pub struct LightFrame {}
22
23pub struct LightFrameInstance {
24 base: BaseInstance,
25}
26
27impl InstanceNode for LightFrameInstance {
28 fn instantiate(args: InstantiationArgs) -> Rc<Self>
29 where
30 Self: Sized,
31 {
32 Rc::new(Self {
33 base: BaseInstance::new(
34 args,
35 InstanceFlags {
36 invisible_to_slot: false,
37 invisible_to_raycasting: true,
38 layer: Layer::DontCare,
39 is_component: false,
40 is_slot: false,
41 },
42 ),
43 })
44 }
45
46 fn establishes_light_frame(&self) -> bool {
47 true
48 }
49
50 fn resolve_debug(
51 &self,
52 f: &mut std::fmt::Formatter,
53 _expanded_node: Option<&ExpandedNode>,
54 ) -> std::fmt::Result {
55 f.debug_struct("LightFrame").finish()
56 }
57
58 fn base(&self) -> &BaseInstance {
59 &self.base
60 }
61}
62
63#[pax]
65#[engine_import_path("pax_engine")]
66#[custom(Default)]
67#[primitive("pax_std::drawing::lighting::LightSourceInstance")]
68pub struct LightSource {
69 pub color: Property<Color>,
71 pub intensity: Property<f64>,
73 pub radius: Property<Size>,
75 pub z: Property<Depth>,
77 pub shape: Property<LightShape>,
79 pub direction: Property<Vector3>,
81 pub enabled: Property<bool>,
83}
84
85impl Default for LightSource {
86 fn default() -> Self {
87 Self {
88 color: Property::new(Color::WHITE),
89 intensity: Property::new(1.0),
90 radius: Property::new(Size::Pixels(Numeric::F64(240.0))),
91 z: Property::new(Depth(Numeric::F64(200.0))),
92 shape: Property::new(LightShape::Point),
93 direction: Property::new(Vector3::new(0.0, 0.0, -1.0)),
94 enabled: Property::new(true),
95 }
96 }
97}
98
99pub struct LightSourceInstance {
100 base: BaseInstance,
101}
102
103impl InstanceNode for LightSourceInstance {
104 fn instantiate(args: InstantiationArgs) -> Rc<Self>
105 where
106 Self: Sized,
107 {
108 Rc::new(Self {
109 base: BaseInstance::new(
110 args,
111 InstanceFlags {
112 invisible_to_slot: false,
113 invisible_to_raycasting: true,
114 layer: Layer::Canvas,
115 is_component: false,
116 is_slot: false,
117 },
118 ),
119 })
120 }
121
122 fn handle_mount(
123 self: Rc<Self>,
124 expanded_node: &Rc<ExpandedNode>,
125 context: &Rc<RuntimeContext>,
126 ) {
127 let tab = expanded_node.transform_and_bounds.clone();
128 let (color, intensity, radius, z, shape, direction, enabled) = expanded_node
129 .with_properties_unwrapped(|properties: &mut LightSource| {
130 (
131 properties.color.clone(),
132 properties.intensity.clone(),
133 properties.radius.clone(),
134 properties.z.clone(),
135 properties.shape.clone(),
136 properties.direction.clone(),
137 properties.enabled.clone(),
138 )
139 });
140
141 let deps = &[
142 tab.untyped(),
143 color.untyped(),
144 intensity.untyped(),
145 radius.untyped(),
146 z.untyped(),
147 shape.untyped(),
148 direction.untyped(),
149 enabled.untyped(),
150 ];
151 let cloned_expanded_node = expanded_node.clone();
152 let cloned_context = context.clone();
153
154 expanded_node
155 .changed_listener
156 .replace_with(Property::computed(
157 move || {
158 cloned_context.set_all_canvases_dirty();
159 cloned_context
160 .set_canvas_dirty(cloned_expanded_node.occlusion.get().render_layer_id);
161 },
162 deps,
163 ));
164 }
165
166 fn resolve_scene_light(
167 &self,
168 expanded_node: &ExpandedNode,
169 context: &RuntimeContext,
170 ) -> Option<SceneLight> {
171 expanded_node.with_properties_unwrapped(|properties: &mut LightSource| {
172 if !properties.enabled.get() {
173 return None;
174 }
175
176 let tab = expanded_node.transform_and_bounds.get();
177 let center = Point::new(tab.bounds.0 * 0.5, tab.bounds.1 * 0.5);
178 let point = canvas_surface_transform(expanded_node, context) * center;
179 Some(SceneLight {
180 shape: properties.shape.get(),
181 position: Vector3::new(point.x, point.y, properties.z.get().to_float()),
182 direction: properties.direction.get(),
183 color: properties.color.get(),
184 intensity: properties.intensity.get().max(0.0),
185 radius: properties.radius.get().expect_pixels().to_float().max(0.0),
186 enabled: true,
187 })
188 })
189 }
190
191 fn resolve_debug(
192 &self,
193 f: &mut std::fmt::Formatter,
194 _expanded_node: Option<&ExpandedNode>,
195 ) -> std::fmt::Result {
196 f.debug_struct("LightSource").finish()
197 }
198
199 fn base(&self) -> &BaseInstance {
200 &self.base
201 }
202}
203
204#[pax]
206#[engine_import_path("pax_engine")]
207#[custom(Default)]
208#[primitive("pax_std::drawing::lighting::AmbientLightInstance")]
209pub struct AmbientLight {
210 pub color: Property<Color>,
212 pub intensity: Property<f64>,
214 pub enabled: Property<bool>,
216}
217
218impl Default for AmbientLight {
219 fn default() -> Self {
220 Self {
221 color: Property::new(Color::WHITE),
222 intensity: Property::new(SceneLighting::DEFAULT_AMBIENT_INTENSITY),
223 enabled: Property::new(true),
224 }
225 }
226}
227
228pub struct AmbientLightInstance {
229 base: BaseInstance,
230}
231
232impl InstanceNode for AmbientLightInstance {
233 fn instantiate(args: InstantiationArgs) -> Rc<Self>
234 where
235 Self: Sized,
236 {
237 Rc::new(Self {
238 base: BaseInstance::new(
239 args,
240 InstanceFlags {
241 invisible_to_slot: false,
242 invisible_to_raycasting: true,
243 layer: Layer::Canvas,
244 is_component: false,
245 is_slot: false,
246 },
247 ),
248 })
249 }
250
251 fn handle_mount(
252 self: Rc<Self>,
253 expanded_node: &Rc<ExpandedNode>,
254 context: &Rc<RuntimeContext>,
255 ) {
256 let (color, intensity, enabled) =
257 expanded_node.with_properties_unwrapped(|properties: &mut AmbientLight| {
258 (
259 properties.color.clone(),
260 properties.intensity.clone(),
261 properties.enabled.clone(),
262 )
263 });
264
265 let deps = &[color.untyped(), intensity.untyped(), enabled.untyped()];
266 let cloned_expanded_node = expanded_node.clone();
267 let cloned_context = context.clone();
268
269 expanded_node
270 .changed_listener
271 .replace_with(Property::computed(
272 move || {
273 cloned_context.set_all_canvases_dirty();
274 cloned_context
275 .set_canvas_dirty(cloned_expanded_node.occlusion.get().render_layer_id);
276 },
277 deps,
278 ));
279 }
280
281 fn resolve_scene_ambient_light(
282 &self,
283 expanded_node: &ExpandedNode,
284 _context: &RuntimeContext,
285 ) -> Option<SceneAmbientLight> {
286 expanded_node.with_properties_unwrapped(|properties: &mut AmbientLight| {
287 properties.enabled.get().then(|| SceneAmbientLight {
288 color: properties.color.get(),
289 intensity: properties.intensity.get().max(0.0),
290 })
291 })
292 }
293
294 fn resolve_debug(
295 &self,
296 f: &mut std::fmt::Formatter,
297 _expanded_node: Option<&ExpandedNode>,
298 ) -> std::fmt::Result {
299 f.debug_struct("AmbientLight").finish()
300 }
301
302 fn base(&self) -> &BaseInstance {
303 &self.base
304 }
305}