在 UWP(Universal Windows Platform)应用中加载和显示 3D 模型可以使用 Windows.UI.Xaml.Controls
命名空间中的 Model3D
和 Viewport3D
控件。以下是一个基本的示例,展示如何在 UWP 应用中加载和显示 3D 模型。
将你的 3D 模型文件(例如 .obj
、.fbx
等)添加到项目中。你可以将文件放在项目的 Assets
文件夹中。
在 MainPage.xaml
中添加 SwapChainPanel
和 Viewport3D
控件,用于显示 3D 模型。
<Page
x:Class="YourNamespace.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:YourNamespace"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<SwapChainPanel x:Name="swapChainPanel" />
</Grid>
</Page>
在 MainPage.xaml.cs
中编写代码,加载和显示 3D 模型。
using System;
using System.Numerics;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.UI.Composition;
using Microsoft.Graphics.Canvas.UI.Xaml;
using Windows.UI.Composition;
using Windows.UI.Xaml.Hosting;
namespace YourNamespace
{
public sealed partial class MainPage : Page
{
private Compositor _compositor;
private ContainerVisual _root;
private CompositionTarget _target;
private CanvasDevice _device;
private CompositionGraphicsDevice _compositionGraphicsDevice;
private CanvasSwapChain _swapChain;
public MainPage()
{
this.InitializeComponent();
this.Loaded += MainPage_Loaded;
}
private void MainPage_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
_compositor = Window.Current.Compositor;
_root = _compositor.CreateContainerVisual();
_target = ElementCompositionPreview.GetElementVisual(swapChainPanel);
_target.Children.InsertAtTop(_root);
_device = CanvasDevice.GetSharedDevice();
_compositionGraphicsDevice = CanvasComposition.CreateCompositionGraphicsDevice(_compositor, _device);
_swapChain = new CanvasSwapChain(_device, (float)swapChainPanel.ActualWidth, (float)swapChainPanel.ActualHeight, 96);
var swapChainBrush = _compositor.CreateSurfaceBrush(CanvasComposition.CreateCompositionSurfaceForSwapChain(_compositor, _swapChain));
var spriteVisual = _compositor.CreateSpriteVisual();
spriteVisual.Brush = swapChainBrush;
spriteVisual.Size = new Vector2((float)swapChainPanel.ActualWidth, (float)swapChainPanel.ActualHeight);
_root.Children.InsertAtTop(spriteVisual);
Load3DModel();
}
private async void Load3DModel()
{
// 加载 3D 模型
var loader = new Windows.ApplicationModel.Resources.Core.ResourceLoader();
var uri = new Uri(loader.GetString("ms-appx:///Assets/YourModel.fbx"));
var storageFile = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);
var device = CanvasDevice.GetSharedDevice();
var model = await CanvasModel.LoadAsync(device, storageFile);
// 渲染 3D 模型
using (var ds = _swapChain.CreateDrawingSession(Windows.UI.Colors.Transparent))
{
ds.Clear(Windows.UI.Colors.Transparent);
ds.DrawModel(model, new Vector3(0, 0, 0), new Vector3(1, 1, 1), Quaternion.Identity);
}
_swapChain.Present();
}
}
}
.fbx
)。领取专属 10元无门槛券
手把手带您无忧上云