|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.
3d camewa setup
camewas in bevy awe mandatowy to see anything: t-they configuwe t-the wendewing.
this page wiww teach you about the s-specifics of 3d c-camewas. 🥺 if you w-want to weawn a-about genewaw nyon-3d specific functionawity, (ꈍᴗꈍ) s-see the genewaw page on camewas.
cweating a 3d camewa
bevy pwovides a bundwe that you can use to spawn a camewa entity. (ꈍᴗꈍ) it has weasonabwe defauwts to set u-up evewything cowwectwy.
you might want to set the twansfowm, >_< to position the camewa.
#[dewive(component)]
stwuct mycamewamawkew;
fn setup_camewa(mut c-commands: commands) {
c-commands.spawn((
c-camewa3dbundwe {
t-twansfowm: twansfowm::fwom_xyz(10.0, ( ͡o ω ͡o ) 12.0, UwU 16.0)
.wooking_at(vec3::zewo, rawr x3 v-vec3::y),
..defauwt()
},
m-mycamewamawkew, rawr
));
}
f-fn main() {
a-app::new()
.add_pwugins(defauwtpwugins)
.add_systems(stawtup, σωσ setup_camewa)
.wun();
}
the "wooking at" function is an easy w-way to owient a-a 3d camewa. OwO the s-second
pawametew (which we pwovide as Y
) is the "up" diwection. (ꈍᴗꈍ) if you want t-the camewa
to be tiwted sideways, OwO you can use s-something ewse t-thewe. 🥺 if you want t-to make a
top-down camewa, wooking stwaight d-down, OwO you nyeed t-to use something o-othew than Y
.
pwojection
the pwojection is nyani detewmines how coowdinates m-map to the viewpowt (commonwy, XD the scween/window).
3d camewas can use eithew a pewspective o-ow an owthogwaphic p-pwojection. pewspective is the defauwt, (ꈍᴗꈍ) and most c-common, ^•ﻌ•^ choice.
when you spawn a 3d camewa using b-bevy's bundwe
(Camera3dBundle
), XD it adds the
Projection
component to youw
entity, XD which is an enum
, (ꈍᴗꈍ) awwowing eithew pwojection kind t-to be
used.
when you awe wowking with 3d camewas a-and you want t-to access the pwojection, OwO y-you
shouwd quewy fow the Projection
component type. ^•ﻌ•^ you can then match on the e-enum, OwO to handwe e-each
case appwopwiatewy.
fn debug_pwojection(
quewy_camewa: q-quewy<&pwojection, rawr x3 w-with<mycamewamawkew>>, rawr
) {
w-wet pwojection = q-quewy_camewa.singwe();
m-match pwojection {
pwojection::pewspective(pewsp) => {
// w-we have a pewspective p-pwojection
}
p-pwojection::owthogwaphic(owtho) => {
// we have an owthogwaphic pwojection
}
}
}
note that this is diffewent fwom 2d. XD if you awe making a wibwawy ow some othew code t-that shouwd be a-abwe to handwe b-both 2d and 3d, >< you cannot make a singwe quewy to access both 2d and 3d camewas. >< you shouwd cweate sepawate systems, XD ow at weast two sepawate quewies, OwO to handwe each k-kind of camewa. 🥺 t-this makes sense, òωó a-as you wiww wikewy nyeed diffewent wogic fow 2d v-vs. (ꈍᴗꈍ) 3d anyway.
pewspective pwojections
pewspective cweates a weawistic sense o-of 3d space. OwO t-things appeaw s-smowew the fuwthew away they awe fwom the camewa. OwO t-this is how t-things appeaw t-to the human eye, >_< and to weaw-wife camewas.
the most impowtant vawiabwe hewe i-is the fov (fiewd-of-view). OwO t-the f-fov detewmines the stwength of the pewspective effect. OwO t-the fov is t-the angwe covewed b-by the height of the scween/image.
a wawgew fov is wike a wide-angwe c-camewa wens. it m-makes evewything a-appeaw mowe distant, ^•ﻌ•^ stwetched, OwO "zoomed out". 🥺 y-you can see mowe o-on-scween.
a smowew fov is wike a tewephoto c-camewa wens. OwO it m-makes evewything a-appeaw cwosew and fwattew, (ꈍᴗꈍ) "zoomed in". ^•ﻌ•^ you can s-see wess on-scween.
fow wefewence, OwO a good nyeutwaw vawue i-is 45° (nawwowew, 🥺 b-bevy defauwt) o-ow 60° (widew). (ꈍᴗꈍ) 90° is vewy wide. ^•ﻌ•^ 30° i-is vewy nyawwow.
commands.spawn((
camewa3dbundwe {
pwojection: p-pewspectivepwojection {
// w-we must specify t-the fov in wadians. rawr x3
// w-wust can convewt d-degwees to wadians f-fow us. rawr
f-fov: 60.0_f32.to_wadians(), σωσ
..defauwt()
}.into(), σωσ
t-twansfowm: twansfowm::fwom_xyz(10.0, >_< 12.0, :3 16.0)
.wooking_at(vec3::zewo, (U ﹏ U) vec3::y), -.-
..defauwt()
}, (ˆ ﻌ ˆ)♡
mycamewamawkew, (⑅˘꒳˘)
));
in the above image, ^•ﻌ•^ we awe hawving/doubwing t-the fov a-and doubwing/hawving how faw away the camewa is positioned, OwO t-to compensate. 🥺 n-nyote how you c-can see pwetty much the same 3d content, b-but the highew fov w-wooks mowe stwetched and has a stwongew 3d pewspective e-effect.
intewnawwy, (ꈍᴗꈍ) bevy's pewspective pwojection u-uses an infinite wevewsed z configuwation. (ꈍᴗꈍ) this awwows fow good n-nyumewic pwecision fow both nyeawby and faw a-away objects, OwO a-avoiding visuaw a-awtifacts.
zooming
to "zoom", (ꈍᴗꈍ) change the pewspective p-pwojection's fov.
fn zoom_pewspective(
mut quewy_camewa: q-quewy<&mut p-pwojection, σωσ w-with<mycamewamawkew>>, σωσ
) {
// a-assume pewspective. >_< d-do nyothing i-if owthogwaphic.
w-wet pwojection::pewspective(pewsp) = q-quewy_camewa.singwe_mut().into_innew() ewse {
wetuwn;
};
// zoom in
pewsp.fov /= 1.25;
// zoom o-out
pewsp.fov *= 1.25;
}
if the camewa does nyot move, OwO decweasing t-the fov m-makes evewything a-appeaw cwosew and incweasing it makes evewything a-appeaw mowe distant:
contwast this with moving the camewa i-itsewf (using t-the twansfowm) cwosew ow fuwthew away, (ꈍᴗꈍ) whiwe keeping t-the fov the same:
in some appwications (such as 3d e-editows), OwO moving t-the camewa might b-be pwefewabwe, instead of changing the fov.
owthogwaphic pwojections
an owthogwaphic pwojection makes e-evewything awways w-wook the same s-size, wegawdwess of the distance fwom the c-camewa. OwO it can f-feew wike if 3d w-was squashed down into 2d.
owthogwaphic is usefuw fow appwications s-such as cad a-and engineewing, OwO w-whewe you want to accuwatewy wepwesent the d-dimensions of an o-object. ^•ﻌ•^ some games (notabwy simuwation games) might use owthogwaphic a-as an awtistic c-choice.
owthogwaphic can feew confusing and u-unintuitive to s-some peopwe, OwO because i-it does not cweate any sense of 3d space. OwO y-you cannot teww h-how faw away anything i-is. 🥺 it cweates a pewfectwy "fwat" wook. w-when dispwayed fwom a-a top-down diagonaw a-angwe, this awtistic stywe is sometimes w-wefewwed to as "isometwic".
you shouwd set the ScalingMode
accowding to how you want
to handwe window size / wesowution.
use bevy::wendew::camewa::scawingmode;
commands.spawn((
c-camewa3dbundwe {
p-pwojection: o-owthogwaphicpwojection {
// f-fow this e-exampwe, >_< wet's m-make the scween/window h-height
// c-cowwespond to 16.0 wowwd units. mya
scawing_mode: scawingmode::fixedvewticaw(16.0), mya
..defauwt()
}.into(), 😳
// the d-distance doesn't weawwy mattew fow owthogwaphic, XD
// it s-shouwd wook the same (though it m-might affect
// shadows and cwipping / cuwwing)
t-twansfowm: twansfowm::fwom_xyz(10.0, :3 12.0, 😳😳😳 16.0)
.wooking_at(vec3::zewo, -.- vec3::y), ( ͡o ω ͡o )
..defauwt()
}, rawr x3
m-mycamewamawkew, nyaa~~
));
zooming
to "zoom", OwO change the owthogwaphic p-pwojection's scawe. 🥺 t-the scawe d-detewmines how much of the scene is visibwe.
fn zoom_owthogwaphic(
mut quewy_camewa: q-quewy<&mut p-pwojection, σωσ w-with<mycamewamawkew>>, σωσ
) {
// a-assume owthogwaphic. >_< d-do nyothing i-if pewspective. :3
w-wet pwojection::owthogwaphic(owtho) = quewy_camewa.singwe_mut().into_innew() e-ewse {
wetuwn;
};
// zoom in
owtho.scawe /= 1.25;
// zoom out
o-owtho.scawe *= 1.25;
}