@@ -275,6 +275,169 @@ pub fn resize(
275275 }
276276}
277277
278+ pub fn mode_3d (
279+ In ( entity) : In < Entity > ,
280+ mut projections : Query < & mut Projection > ,
281+ mut transforms : Query < & mut Transform > ,
282+ sizes : Query < & SurfaceSize > ,
283+ ) -> Result < ( ) > {
284+ let SurfaceSize ( width, height) = sizes
285+ . get ( entity)
286+ . map_err ( |_| ProcessingError :: GraphicsNotFound ) ?;
287+
288+ let width = * width as f32 ;
289+ let height = * height as f32 ;
290+
291+ let fov = std:: f32:: consts:: PI / 3.0 ; // 60 degrees
292+ let aspect = width / height;
293+ let camera_z = ( height / 2.0 ) / ( fov / 2.0 ) . tan ( ) ;
294+ let near = camera_z / 10.0 ;
295+ let far = camera_z * 10.0 ;
296+
297+ let mut projection = projections
298+ . get_mut ( entity)
299+ . map_err ( |_| ProcessingError :: GraphicsNotFound ) ?;
300+
301+ * projection = Projection :: Perspective ( PerspectiveProjection {
302+ fov,
303+ aspect_ratio : aspect,
304+ near,
305+ far,
306+ } ) ;
307+
308+ let mut transform = transforms
309+ . get_mut ( entity)
310+ . map_err ( |_| ProcessingError :: GraphicsNotFound ) ?;
311+
312+ * transform = Transform :: from_xyz ( 0.0 , 0.0 , camera_z) . looking_at ( Vec3 :: ZERO , Vec3 :: Y ) ;
313+
314+ Ok ( ( ) )
315+ }
316+
317+ pub fn mode_2d (
318+ In ( entity) : In < Entity > ,
319+ mut projections : Query < & mut Projection > ,
320+ mut transforms : Query < & mut Transform > ,
321+ sizes : Query < & SurfaceSize > ,
322+ ) -> Result < ( ) > {
323+ let SurfaceSize ( width, height) = sizes
324+ . get ( entity)
325+ . map_err ( |_| ProcessingError :: GraphicsNotFound ) ?;
326+
327+ let mut projection = projections
328+ . get_mut ( entity)
329+ . map_err ( |_| ProcessingError :: GraphicsNotFound ) ?;
330+
331+ * projection = Projection :: custom ( ProcessingProjection {
332+ width : * width as f32 ,
333+ height : * height as f32 ,
334+ near : 0.0 ,
335+ far : 1000.0 ,
336+ } ) ;
337+
338+ let mut transform = transforms
339+ . get_mut ( entity)
340+ . map_err ( |_| ProcessingError :: GraphicsNotFound ) ?;
341+
342+ * transform = Transform :: from_xyz ( 0.0 , 0.0 , 999.9 ) ;
343+
344+ Ok ( ( ) )
345+ }
346+
347+ pub fn camera_position (
348+ In ( ( entity, x, y, z) ) : In < ( Entity , f32 , f32 , f32 ) > ,
349+ mut transforms : Query < & mut Transform > ,
350+ ) -> Result < ( ) > {
351+ let mut transform = transforms
352+ . get_mut ( entity)
353+ . map_err ( |_| ProcessingError :: GraphicsNotFound ) ?;
354+
355+ transform. translation = Vec3 :: new ( x, y, z) ;
356+
357+ Ok ( ( ) )
358+ }
359+
360+ pub fn camera_look_at (
361+ In ( ( entity, target_x, target_y, target_z) ) : In < ( Entity , f32 , f32 , f32 ) > ,
362+ mut transforms : Query < & mut Transform > ,
363+ ) -> Result < ( ) > {
364+ let mut transform = transforms
365+ . get_mut ( entity)
366+ . map_err ( |_| ProcessingError :: GraphicsNotFound ) ?;
367+
368+ // TODO: allow specifying up vector?
369+ // does anyone actually use anything other than Vec3::Y here?
370+ let target = Vec3 :: new ( target_x, target_y, target_z) ;
371+ transform. look_at ( target, Vec3 :: Y ) ;
372+
373+ Ok ( ( ) )
374+ }
375+
376+ pub fn perspective (
377+ In ( (
378+ entity,
379+ PerspectiveProjection {
380+ fov,
381+ aspect_ratio,
382+ near,
383+ far,
384+ } ,
385+ ) ) : In < ( Entity , PerspectiveProjection ) > ,
386+ mut projections : Query < & mut Projection > ,
387+ ) -> Result < ( ) > {
388+ let mut projection = projections
389+ . get_mut ( entity)
390+ . map_err ( |_| ProcessingError :: GraphicsNotFound ) ?;
391+
392+ * projection = Projection :: Perspective ( PerspectiveProjection {
393+ fov,
394+ aspect_ratio,
395+ near,
396+ far,
397+ } ) ;
398+
399+ Ok ( ( ) )
400+ }
401+
402+ pub struct OrthoArgs {
403+ pub left : f32 ,
404+ pub right : f32 ,
405+ pub bottom : f32 ,
406+ pub top : f32 ,
407+ pub near : f32 ,
408+ pub far : f32 ,
409+ }
410+
411+ pub fn ortho (
412+ In ( (
413+ entity,
414+ OrthoArgs {
415+ left,
416+ right,
417+ bottom,
418+ top,
419+ near,
420+ far,
421+ } ,
422+ ) ) : In < ( Entity , OrthoArgs ) > ,
423+ mut projections : Query < & mut Projection > ,
424+ ) -> Result < ( ) > {
425+ let mut projection = projections
426+ . get_mut ( entity)
427+ . map_err ( |_| ProcessingError :: GraphicsNotFound ) ?;
428+
429+ // we need a custom projection to support processing's coordinate system
430+ // but this is in effect an orthographic projection with the given bounds
431+ * projection = Projection :: custom ( ProcessingProjection {
432+ width : right - left,
433+ height : top - bottom,
434+ near,
435+ far,
436+ } ) ;
437+
438+ Ok ( ( ) )
439+ }
440+
278441pub fn destroy (
279442 In ( entity) : In < Entity > ,
280443 mut commands : Commands ,
0 commit comments