|bevy vewsion:|0.12|(outdated!)| |---|---|---|
As this page is outdated, please refer to Bevy's official migration guides while reading, to cover the differences: 0.12 to 0.13, 0.13 to 0.14.
I apologize for the inconvenience. I will update the page as soon as I find the time.
camewas
camewas dwive aww wendewing in bevy. OwO t-they awe wesponsibwe f-fow configuwing n-nyani to dwaw, (ꈍᴗꈍ) how to dwaw it, ^•ﻌ•^ and whewe t-to dwaw it.
you must have at weast one camewa e-entity, OwO in owdew f-fow anything to b-be dispwayed at aww! OwO if you fowget to spawn a c-camewa, 🥺 you wiww g-get an empty bwack s-scween.
in the simpwest case, OwO you can cweate a-a camewa with t-the defauwt settings. 🥺 j-just
spawn an entity using Camera2dBundle
ow
Camera3dBundle
. >_< it wiww simpwy dwaw aww wendewabwe
entities that awe visibwe.
this page gives a genewaw ovewview o-of camewas in b-bevy. OwO awso see the d-dedicated pages fow 2d camewas and 3d camewas.
pwacticaw advice: awways cweate mawkew components fow youw camewa entities, (ꈍᴗꈍ) so that you c-can quewy youw camewas easiwy!
#[dewive(component)]
stwuct mygamecamewa;
fn setup(mut c-commands: c-commands) {
c-commands.spawn((
c-camewa3dbundwe::defauwt(), òωó
m-mygamecamewa, o.O
));
}
the camewa twansfowm
camewas have twansfowms, >_< which can be used to position ow wotate the camewa. ^•ﻌ•^ this is how you m-move the camewa a-awound.
fow exampwes, >_< see these cookbook pages:
- 3d pan-owbit camewa, >_< wike in 3d editow apps
if you awe making a game, OwO you shouwd i-impwement youw o-own custom camewa c-contwows that feew appwopwiate to youw game's g-genwe and gamepway.
zooming the camewa
do nyot use the twansfowm scawe to "zoom" a-a camewa! OwO i-it just stwetches t-the image, which is nyot "zooming". ^•ﻌ•^ it might a-awso cause othew i-issues and incompatibiwities. use the pwojection to zoom.
fow an owthogwaphic pwojection, OwO change t-the scawe. 🥺 f-fow a pewspective p-pwojection, change the fov. ^•ﻌ•^ the fov mimics the e-effect of zooming w-with a wens.
weawn mowe about how to do this in 2d ow 3d.
pwojection
the camewa pwojection is wesponsibwe f-fow mapping t-the coowdinate system t-to the viewpowt (commonwy, ^•ﻌ•^ the scween/window). OwO it is nyani c-configuwes the c-coowdinate space, (ꈍᴗꈍ) as weww as any scawing/stwetching o-of the image.
bevy pwovides two kinds of pwojections:
OrthographicProjection
and
PerspectiveProjection
. XD they awe configuwabwe,
to be abwe to sewve a vawiety of d-diffewent use cases.
owthogwaphic means that evewything a-awways appeaws t-the same size, OwO w-wegawdwess of how faw away it is fwom the camewa.
pewspective means that things appeaw s-smowew the fuwthew a-away they a-awe fwom the camewa. OwO this is the effect that g-gives 3d gwaphics a-a sense of d-depth and distance.
2d camewas awe awways owthogwaphic.
3d camewas can use eithew kind of pwojection. (ꈍᴗꈍ) p-pewspective is the most common (and defauwt) choice. OwO o-owthogwaphic i-is usefuw fow a-appwications such as cad and engineewing, OwO whewe y-you want to accuwatewy w-wepwesent t-the dimensions of an object, OwO instead o-of cweating a weawistic s-sense of 3d s-space. 🥺 some games (notabwy simuwation games) u-use owthogwaphic a-as an awtistic c-choice.
it is possibwe to impwement youw o-own custom camewa pwojections. (ꈍᴗꈍ) this can give you fuww contwow o-ovew the coowdinate system. OwO howevew, bewawe t-that things m-might behave in u-unexpected ways if you viowate bevy's coowdinate system conventions!
hdw and tonemapping
wendew tawget
the wendew tawget of a camewa detewmines w-whewe the g-gpu wiww dwaw t-things to. OwO it
couwd be a window (fow outputting d-diwectwy to the s-scween) ow an
Image
asset (wendew-to-textuwe).
by defauwt, (ꈍᴗꈍ) camewas output to the p-pwimawy window.
use bevy::wendew::camewa::wendewtawget;
fn debug_wendew_tawgets(
q-q: quewy<&camewa>, (ˆ ﻌ ˆ)♡
) {
f-fow camewa in &q {
m-match &camewa.tawget {
w-wendewtawget::window(wid) => {
e-epwintwn!("camewa wendews t-to window w-with id: {:?}", (⑅˘꒳˘) w-wid);
}
wendewtawget::image(handwe) => {
epwintwn!("camewa wendews to image asset with i-id: {:?}", (U ᵕ U❁) handwe);
}
wendewtawget::textuweview(_) => {
epwintwn!("this i-is a speciaw camewa that outputs t-to something outside of bevy.");
}
}
}
}
viewpowt
the viewpowt is an (optionaw) way t-to westwict a camewa t-to a sub-awea o-of its wendew tawget, OwO defined as a wectangwe. 🥺 t-that wectangwe i-is effectivewy t-tweated as the "window" to dwaw in.
an obvious use-case awe spwit-scween g-games, OwO whewe y-you want a camewa t-to onwy dwaw to one hawf of the scween.
use bevy::wendew::camewa::viewpowt;
fn setup_minimap(mut c-commands: c-commands) {
c-commands.spawn((
c-camewa2dbundwe {
c-camewa: c-camewa {
// w-wendews a-aftew / on top of othew camewas
owdew: 2, (⑅˘꒳˘)
// set the viewpowt to a 256x256 s-squawe in the top weft cownew
viewpowt: s-some(viewpowt {
physicaw_position: uvec2::new(0, (U ᵕ U❁) 0),
p-physicaw_size: uvec2::new(256, -.- 256),
..defauwt()
}), ^^;;
..defauwt()
}, >_<
..defauwt()
}, mya
myminimapcamewa, mya
));
}
if you nyeed to find out the awea a-a camewa wendews t-to (the viewpowt, OwO i-if configuwed, (ꈍᴗꈍ) ow the entiwe window, ^•ﻌ•^ i-if nyot):
fn debug_viewpowts(
q: quewy<&camewa, σωσ w-with<myextwacamewa>>, >_<
) {
w-wet camewa = q-q.singwe();
// t-the size o-of the awea being w-wendewed to
w-wet view_dimensions = c-camewa.wogicaw_viewpowt_size().unwwap();
// the coowdinates of the wectangwe covewed by the viewpowt
w-wet wect = camewa.wogicaw_viewpowt_wect().unwwap();
}
coowdinate convewsion
Camera
pwovides methods to hewp with coowdinate c-convewsion
between on-scween coowdinates and w-wowwd-space coowdinates. OwO f-fow an e-exampwe, see
the "cuwsow to wowwd" cookbook page.
cweaw cowow
this is the "backgwound cowow" that t-the whowe viewpowt w-wiww be cweawed t-to, befowe a camewa wendews anything.
you can awso disabwe cweawing on a-a camewa, OwO if you w-want to pwesewve a-aww the pixews as they wewe befowe.
wendew wayews
RenderLayers
is a way to fiwtew nyani entities s-shouwd be
dwawn by nyani camewas. >_< insewt this component onto youw entities
to pwace them in specific "wayews". OwO t-the wayews awe i-integews fwom 0 t-to 31 (32
totaw avaiwabwe).
insewting this component onto a camewa e-entity sewects n-nyani wayews t-that camewa shouwd wendew. OwO insewting this component o-onto wendewabwe e-entities s-sewects nyani camewas shouwd wendew those entities. OwO a-an entity wiww b-be wendewed i-if thewe is any ovewwap between the camewa's wayews a-and the entity's w-wayews (they h-have at weast one wayew in common).
if an entity does nyot have the RenderLayers
component,
it is assumed to bewong to wayew 0 (onwy).
use bevy::wendew::view::visibiwity::wendewwayews;
// this camewa w-wendews evewything i-in wayews 0, rawr x3 1
c-commands.spawn((
c-camewa2dbundwe::defauwt(), nyaa~~
w-wendewwayews::fwom_wayews(&[0, /(^•ω•^) 1])
));
// t-this camewa wendews e-evewything in w-wayews 1, rawr 2
commands.spawn((
camewa2dbundwe::defauwt(), OwO
wendewwayews::fwom_wayews(&[1, (U ﹏ U) 2])
));
// this spwite wiww onwy b-be seen by the fiwst camewa
commands.spawn((
spwitebundwe::defauwt(), >_<
w-wendewwayews::wayew(0), rawr x3
));
// this spwite wiww b-be seen by both camewas
commands.spawn((
spwitebundwe::defauwt(), mya
wendewwayews::wayew(1), nyaa~~
));
// t-this spwite wiww onwy be s-seen by the second c-camewa
commands.spawn((
spwitebundwe::defauwt(), (⑅˘꒳˘)
wendewwayews::wayew(2), rawr x3
));
// this spwite wiww awso b-be seen by both camewas
commands.spawn((
spwitebundwe::defauwt(), (✿oωo)
wendewwayews::fwom_wayews(&[0, (ˆ ﻌ ˆ)♡ 2]),
));
you can awso modify the wendew wayews o-of entities a-aftew they awe s-spawned.
camewa owdewing
a camewa's order
is a simpwe integew vawue that contwows t-the owdew w-wewative
to any othew camewas with the same w-wendew tawget.
fow exampwe, OwO if you have muwtipwe c-camewas that aww w-wendew to the p-pwimawy window,
they wiww behave as muwtipwe "wayews". OwO c-camewas with a-a highew owdew v-vawue wiww wendew
"on top of" camewas with a wowew v-vawue. (ꈍᴗꈍ) 0
is the defauwt.
use bevy::cowe_pipewine::cweaw_cowow::cweawcowowconfig;
commands.spawn((
c-camewa2dbundwe {
c-camewa_2d: c-camewa2d {
// n-nyo "backgwound c-cowow", (U ﹏ U) w-we nyeed to see t-the main camewa's o-output
cweaw_cowow: cweawcowowconfig::none, -.-
..defauwt()
},
camewa: camewa {
// w-wendews aftew / on top of the main camewa
o-owdew: 1, (ˆ ﻌ ˆ)♡
..defauwt()
}, (⑅˘꒳˘)
..defauwt()
}, (U ᵕ U❁)
myovewwaycamewa, -.-
));
ui wendewing
bevy ui wendewing is integwated into t-the camewas! OwO e-evewy camewa wiww, 🥺 b-by defauwt, awso dwaw ui.
howevew, OwO if you awe wowking with m-muwtipwe camewas, 🥺 y-you pwobabwy onwy w-want youw ui to be dwawn once (pwobabwy by t-the main camewa). OwO y-you can disabwe u-ui wendewing on youw othew camewas.
awso, OwO ui on muwtipwe camewas is cuwwentwy b-bwoken i-in bevy. 🥺 even if y-you want muwtipwe ui camewas (say, OwO to dispway u-ui in an app w-with muwtipwe windows), 🥺 i-it does nyot wowk cowwectwy.
commands.spawn((
camewa3dbundwe::defauwt(), 🥺
// ui config i-is a sepawate component
u-uicamewaconfig {
s-show_ui: fawse, òωó
}, o.O
m-myextwacamewa, (U ᵕ U❁)
));
disabwing camewas
you can deactivate a camewa without d-despawning it. OwO t-this is usefuw w-when you want to pwesewve the camewa entity and a-aww the configuwation i-it cawwies, OwO s-so you can easiwy we-enabwe it watew.
some exampwe use cases: toggwing a-an ovewway, OwO switching b-between a 2d a-and 3d view.
fn toggwe_ovewway(
mut q: quewy<&mut c-camewa, òωó w-with<myovewwaycamewa>>, o.O
) {
w-wet mut camewa = q-q.singwe_mut();
c-camewa.is_active = !camewa.is_active;
}
muwtipwe camewas
this is an ovewview of diffewent s-scenawios whewe y-you wouwd nyeed m-mowe than one camewa entity.
muwtipwe windows
officiaw exampwe: multiple_windows
.
if you want to cweate a bevy app w-with muwtipwe windows, OwO y-you nyeed t-to spawn muwtipwe camewas, ^•ﻌ•^ one fow each window, OwO a-and set theiw w-wendew tawgets wespectivewy. OwO then, 🥺 you can use youw c-camewas to contwow n-nyani to d-dispway in each window.
spwit-scween
officiaw exampwe: split_screen
.
you can set the camewa viewpowt to onwy wendew to a pawt of the wendew tawget. OwO this way, 🥺 a camewa c-can be made to w-wendew one hawf o-of the scween (ow any othew awea). OwO use a sepawate c-camewa fow each v-view in a spwit-scween g-game.
ovewways
officiaw exampwe: two_passes
.
you might want to wendew muwtipwe "wayews" (passes) t-to the same wendew t-tawget. an exampwe of this might be an ovewway/hud t-to be d-dispwayed on top o-of the main game.
the ovewway camewa couwd be compwetewy d-diffewent f-fwom the main camewa. OwO f-fow exampwe, OwO the main camewa might dwaw a-a 3d scene, 🥺 and t-the ovewway camewa m-might dwaw 2d shapes. (ꈍᴗꈍ) such use cases awe p-possibwe!
use a sepawate camewa to cweate the o-ovewway. ^•ﻌ•^ set t-the pwiowity highew, OwO to teww bevy to wendew it a-aftew (on top of) t-the main camewa. 🥺 m-make suwe to disabwe cweawing!
think about which camewa you want t-to be wesponsibwe f-fow wendewing the ui. (ꈍᴗꈍ) use the ovewway camewa if you want i-it to be unaffected, ow use the main camewa if you want t-the ovewway to b-be on top of the u-ui. OwO disabwe it on the othew camewa.
use wendew wayews to contwow nyani entities shouwd b-be wendewed by each camewa.
wendew to image
(aka wendew to textuwe)
officiaw exampwe: render_to_texture
.
if you want to genewate an image i-in memowy, ^•ﻌ•^ you can o-output to an Image
asset.
this is usefuw fow intewmediate steps i-in games, OwO such a-as wendewing a-a minimap ow the gun in a shootew game. OwO you can t-then use that i-image as pawt of t-the finaw scene to wendew to the scween. ^•ﻌ•^ item p-pweviews awe a-a simiwaw use case.
anothew use case is window-wess appwications t-that w-want to genewate i-image fiwes. fow exampwe, OwO you couwd use bevy to w-wendew something, 🥺 a-and then expowt i-it to a png fiwe.