|
| 1 | +## ⚠️ **Experimental:** Do not use in production! ⚠️ |
| 2 | + |
| 3 | +# Impeller Scene |
| 4 | + |
| 5 | +Impeller Scene is an experimental realtime 3D renderer powered by Impeller's |
| 6 | +render layer with the following design priorities: |
| 7 | + |
| 8 | +* Ease of use. |
| 9 | +* Suitability for mobile. |
| 10 | +* Common case scalability. |
| 11 | + |
| 12 | +The aim is to create a familiar and flexible scene graph capable of building |
| 13 | +complex dynamic scenes for games and beyond. |
| 14 | + |
| 15 | +## Example |
| 16 | + |
| 17 | +```cpp |
| 18 | +std::shared_ptr<impeller::Context> context = |
| 19 | + /* Create the backend-specific Impeller context */; |
| 20 | + |
| 21 | +auto allocator = context->GetResourceAllocator(); |
| 22 | + |
| 23 | +/// Load resources. |
| 24 | + |
| 25 | +auto dash_gltf = impeller::scene::LoadGLTF(allocator, "models/dash.glb"); |
| 26 | +auto environment_hdri = |
| 27 | + impeller::scene::LoadHDRI(allocator, "environment/table_mountain.hdr"); |
| 28 | + |
| 29 | +/// Construct a scene. |
| 30 | + |
| 31 | +auto scene = impeller::scene::Scene(context); |
| 32 | + |
| 33 | +scene.Add(dash_gltf.scene); |
| 34 | + |
| 35 | +auto& dash_player = dash_gltf.scene.CreateAnimationPlayer(); |
| 36 | +auto& walk_action = dash_player.CreateClipAction(dash_gltf.GetClip("Walk")); |
| 37 | +walk_action.SetLoop(impeller::scene::AnimationAction::kLoopForever); |
| 38 | +walk_action.SetWeight(0.7f); |
| 39 | +walk_action.Seek(0.0f); |
| 40 | +walk_action.Play(); |
| 41 | +auto& run_action = dash_player.CreateClipAction(dash_gltf.GetClip("Run")); |
| 42 | +run_action.SetLoop(impeller::scene::AnimationAction::kLoopForever); |
| 43 | +run_action.SetWeight(0.3f); |
| 44 | +run_action.Play(); |
| 45 | + |
| 46 | +scene.Add( |
| 47 | + impeller::scene::DirectionalLight( |
| 48 | + /* color */ impeller::Color::AntiqueWhite(), |
| 49 | + /* intensity */ 5, |
| 50 | + /* direction */ {2, 3, 4})); |
| 51 | + |
| 52 | +impeller::scene::StaticMeshEntity sphere_entity; |
| 53 | +sphere_entity.SetGlobalTransform( |
| 54 | + Matrix::MakeRotationEuler({kPiOver4, kPiOver4, 0})); |
| 55 | +sphere_entity.SetCullingMode(impeller::scene::CullingMode::kFrustum); |
| 56 | + |
| 57 | +std::unique_ptr<impeller::scene::SphereGeometry> sphere = |
| 58 | + impeller::scene::Geometry::MakeSphere(allocator, /* radius */ 2); |
| 59 | + |
| 60 | +sphere_entity.SetGeometry(sphere); |
| 61 | + |
| 62 | +auto material = impeller::scene::Material::MakeStandard(); |
| 63 | +material.SetAlbedo(impeller::Color::Red()); |
| 64 | +material.SetRoughness(0.4); |
| 65 | +material.SetMetallic(0.2); |
| 66 | +// Common properties shared by all materials. |
| 67 | +material.SetEnvironmentMap(environment_hdri); |
| 68 | +material.SetFlatShaded(true); |
| 69 | +material.SetBlendConfig({ |
| 70 | + impeller::BlendOperation::kAdd, // color_op |
| 71 | + impeller::BlendFactor::kOne, // source_color_factor |
| 72 | + impeller::BlendFactor::kOneMinusSourceAlpha, // destination_color_factor |
| 73 | + impeller::BlendOperation::kAdd, // alpha_op |
| 74 | + impeller::BlendFactor::kOne, // source_alpha_factor |
| 75 | + impeller::BlendFactor::kOneMinusSourceAlpha, // destination_alpha_factor |
| 76 | +}); |
| 77 | +material.SetStencilConfig({ |
| 78 | + impeller::StencilOperation::kIncrementClamp, // operation |
| 79 | + impeller::CompareFunction::kAlways, // compare |
| 80 | +}); |
| 81 | + |
| 82 | +sphere_entity->SetMaterials({material}); |
| 83 | + |
| 84 | + |
| 85 | +impeller::scene::StaticMeshEntity cube_entity; |
| 86 | +cube_entity.GetGeometry( |
| 87 | + impeller::scene::Geometry::MakeCube(allocator, {4, 4, 4})); |
| 88 | +cube_entity.SetMaterials({material}); |
| 89 | + |
| 90 | +cube_entity.SetLocalTransform(Matrix::MakeTranslation({4, 0, 0})); |
| 91 | + |
| 92 | +sphere_entity->Add(sube_entity); |
| 93 | +scene.Add(sphere_entity); |
| 94 | + |
| 95 | +/// Post processing. |
| 96 | + |
| 97 | +auto dof = impeller::scene::PostProcessingEffect::MakeBokeh( |
| 98 | + /* aperture_size */ 0.2, |
| 99 | + /* focus_plane_distance */ 50); |
| 100 | +scene.SetPostProcessing({dof}); |
| 101 | + |
| 102 | +/// Render the scene. |
| 103 | + |
| 104 | +auto renderer = impeller::Renderer(context); |
| 105 | + |
| 106 | +while(true) { |
| 107 | + std::unique_ptr<impeller::Surface> surface = /* Wrap the window surface */; |
| 108 | + |
| 109 | + renderer->Render(surface, [&scene](RenderTarget& render_target) { |
| 110 | + /// Render a perspective view. |
| 111 | + |
| 112 | + auto camera = |
| 113 | + impeller::Camera::MakePerspective( |
| 114 | + /* fov */ kPiOver4, |
| 115 | + /* position */ {50, -30, 50}) |
| 116 | + .LookAt( |
| 117 | + /* target */ impeller::Vector3::Zero, |
| 118 | + /* up */ {0, -1, 0}); |
| 119 | + |
| 120 | + scene.Render(render_target, camera); |
| 121 | + |
| 122 | + /// Render an overhead view on the bottom right corner of the screen. |
| 123 | + |
| 124 | + auto size = render_target.GetRenderTargetSize(); |
| 125 | + auto minimap_camera = |
| 126 | + impeller::Camera::MakeOrthographic( |
| 127 | + /* view */ Rect::MakeLTRB(-100, -100, 100, 100), |
| 128 | + /* position */ {0, -50, 0}) |
| 129 | + .LookAt( |
| 130 | + /* target */ impeller::Vector3::Zero, |
| 131 | + /* up */ {0, 0, 1}) |
| 132 | + .WithViewport(IRect::MakeXYWH(size.width / 4, size.height / 4, |
| 133 | + size.height / 5, size.height / 5)); |
| 134 | + |
| 135 | + scene.Render(render_target, minimap_camera); |
| 136 | + |
| 137 | + return true; |
| 138 | + }); |
| 139 | +} |
| 140 | +``` |
0 commit comments