浏览代码

Demo3: Rewrite of front page demo to use the new library.

Vicente Penades 5 年之前
父节点
当前提交
253abd1dea

+ 8 - 9
src/Demo1/Demo1.csproj

@@ -1,4 +1,4 @@
-<Project Sdk="Microsoft.NET.Sdk">
+<Project Sdk="Microsoft.NET.Sdk">
   <PropertyGroup>
     <OutputType>WinExe</OutputType>
     <TargetFramework>netcoreapp3.1</TargetFramework>
@@ -9,16 +9,10 @@
     <ApplicationManifest>app.manifest</ApplicationManifest>
     <ApplicationIcon>Icon.ico</ApplicationIcon>
   </PropertyGroup>
-  <ItemGroup>
-    <None Remove="Content\WaterBottle.glb" />
+  <ItemGroup>    
     <None Remove="Icon.ico" />
     <None Remove="Icon.bmp" />
-  </ItemGroup>
-  <ItemGroup>
-    <Content Include="Content\WaterBottle.glb">
-      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
-    </Content>
-  </ItemGroup>
+  </ItemGroup>  
   <ItemGroup>
     <EmbeddedResource Include="Icon.ico" />
     <EmbeddedResource Include="Icon.bmp" />
@@ -33,4 +27,9 @@
   <ItemGroup>
     <ProjectReference Include="..\MonoGame.Framework.Graphics.GLTF\MonoGame.Framework.Pipeline.GLTF.csproj" />
   </ItemGroup>
+
+  <ItemGroup>
+    <Content Include="Content\WaterBottle.glb" CopyToOutputDirectory="PreserveNewest" />
+  </ItemGroup>
+  
 </Project>

+ 8 - 13
src/Demo2/Demo2.csproj

@@ -1,4 +1,4 @@
-<Project Sdk="Microsoft.NET.Sdk">
+<Project Sdk="Microsoft.NET.Sdk">
   <PropertyGroup>
     <OutputType>WinExe</OutputType>
     <TargetFramework>netcoreapp3.1</TargetFramework>
@@ -9,20 +9,10 @@
     <ApplicationManifest>app.manifest</ApplicationManifest>
     <ApplicationIcon>Icon.ico</ApplicationIcon>
   </PropertyGroup>
-  <ItemGroup>
-    <None Remove="Content\CesiumMan.glb" />
-    <None Remove="Content\WaterBottle.glb" />
+  <ItemGroup>    
     <None Remove="Icon.ico" />
     <None Remove="Icon.bmp" />
-  </ItemGroup>
-  <ItemGroup>
-    <Content Include="Content\CesiumMan.glb">
-      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
-    </Content>
-    <Content Include="Content\WaterBottle.glb">
-      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
-    </Content>
-  </ItemGroup>
+  </ItemGroup>  
   <ItemGroup>
     <EmbeddedResource Include="Icon.ico" />
     <EmbeddedResource Include="Icon.bmp" />
@@ -37,4 +27,9 @@
   <ItemGroup>
     <ProjectReference Include="..\MonoGame.Framework.Graphics.GLTF\MonoGame.Framework.Pipeline.GLTF.csproj" />
   </ItemGroup>
+
+  <ItemGroup>
+    <Content Include="Content\*.glb" CopyToOutputDirectory="PreserveNewest" />
+  </ItemGroup>
+  
 </Project>

二进制
src/Demo3/Content/Avocado.glb


二进制
src/Demo3/Content/BrainStem.glb


二进制
src/Demo3/Content/CesiumMan.glb


二进制
src/Demo3/Content/haunted_house.glb


二进制
src/Demo3/Content/shark.glb


+ 35 - 0
src/Demo3/Demo3.csproj

@@ -0,0 +1,35 @@
+<Project Sdk="Microsoft.NET.Sdk">
+  <PropertyGroup>
+    <OutputType>WinExe</OutputType>
+    <TargetFramework>netcoreapp3.1</TargetFramework>
+    <PublishReadyToRun>false</PublishReadyToRun>
+    <TieredCompilation>false</TieredCompilation>
+  </PropertyGroup>
+  <PropertyGroup>
+    <ApplicationManifest>app.manifest</ApplicationManifest>
+    <ApplicationIcon>Icon.ico</ApplicationIcon>
+  </PropertyGroup>
+  <ItemGroup>
+    <None Remove="Icon.ico" />
+    <None Remove="Icon.bmp" />
+  </ItemGroup>
+  <ItemGroup>
+    <EmbeddedResource Include="Icon.ico" />
+    <EmbeddedResource Include="Icon.bmp" />
+  </ItemGroup>
+  <ItemGroup>
+    <TrimmerRootAssembly Include="Microsoft.Xna.Framework.Content.ContentTypeReader" Visible="false" />
+  </ItemGroup>
+  <ItemGroup>
+    <PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.0.1641" />
+    <PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.0.1641" />
+  </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="..\MonoGame.Framework.Graphics.GLTF\MonoGame.Framework.Pipeline.GLTF.csproj" />
+  </ItemGroup>
+  
+  <ItemGroup>
+    <Content Include="Content\*.glb" CopyToOutputDirectory="PreserveNewest" />
+  </ItemGroup>
+  
+</Project>

+ 192 - 0
src/Demo3/Game1.cs

@@ -0,0 +1,192 @@
+using System;
+
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Graphics;
+using Microsoft.Xna.Framework.Input;
+
+namespace Demo3
+{
+    /// <summary>
+    /// This is the main type for your game.
+    /// </summary>
+    public class Game1 : Game
+    {
+        #region lifecycle
+
+        public Game1()
+        {
+            _Graphics = new GraphicsDeviceManager(this);
+            _Graphics.GraphicsProfile = GraphicsProfile.HiDef;
+
+            Content.RootDirectory = "Content";
+        }
+
+        protected override void Initialize()
+        {
+            // TODO: Add your initialization logic here
+
+            this.Window.Title = "SharpGLTF - MonoGame Scene";
+            this.Window.AllowUserResizing = true;
+            this.Window.AllowAltF4 = true;
+
+            base.Initialize();
+        }
+
+        protected override void Dispose(bool disposing)
+        {
+            base.Dispose(disposing);
+        }
+
+        #endregion
+
+        #region resources
+
+        private readonly GraphicsDeviceManager _Graphics;
+
+        // these are the actual hardware resources that represent every model's geometry.        
+
+        ModelTemplate _AvodadoTemplate;
+        ModelTemplate _BrainStemTemplate;
+        ModelTemplate _CesiumManTemplate;
+        ModelTemplate _HauntedHouseTemplate;
+        ModelTemplate _SharkTemplate;
+
+        #endregion
+
+        #region content loading
+
+        protected override void LoadContent()
+        {
+            ModelTemplate _load(string filePath)
+            {
+                return Microsoft.Xna.Framework.Content.Pipeline.Graphics.FormatGLTF.LoadModel(filePath, this.GraphicsDevice);
+            }            
+
+            _AvodadoTemplate = _load("Content\\Avocado.glb");
+            _BrainStemTemplate = _load("Content\\BrainStem.glb");
+            _CesiumManTemplate = _load("Content\\CesiumMan.glb");
+            _HauntedHouseTemplate = _load("Content\\haunted_house.glb");
+            _SharkTemplate = _load("Content\\shark.glb");
+        }
+
+        protected override void UnloadContent()
+        {
+            _AvodadoTemplate?.Dispose();
+            _AvodadoTemplate = null;
+
+            _BrainStemTemplate?.Dispose();
+            _BrainStemTemplate = null;
+
+            _CesiumManTemplate?.Dispose();
+            _CesiumManTemplate = null;
+
+            _HauntedHouseTemplate?.Dispose();
+            _HauntedHouseTemplate = null;
+
+            _SharkTemplate?.Dispose();
+            _SharkTemplate = null;
+        }
+
+        #endregion
+
+        #region game loop
+
+        private PBREnvironment _LightsAndFog = PBREnvironment.CreateDefault();
+
+        // these are the scene instances we create for every glTF model we want to render on screen.
+        // Instances are designed to be as lightweight as possible, so it should not be a problem to
+        // create as many of them as you need at runtime.
+        private ModelLayerInstance _HauntedHouse;
+        private ModelLayerInstance _BrainStem;
+        private ModelLayerInstance _Avocado;
+        private ModelLayerInstance _CesiumMan1;
+        private ModelLayerInstance _CesiumMan2;
+        private ModelLayerInstance _CesiumMan3;
+        private ModelLayerInstance _CesiumMan4;
+        private ModelLayerInstance _Shark;
+
+        protected override void Update(GameTime gameTime)
+        {
+            // For Mobile devices, this logic will close the Game when the Back button is pressed
+            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
+            {
+                Exit();
+            }
+
+            // create as many instances as we need from the templates
+
+            if (_Avocado == null) _Avocado = _AvodadoTemplate.DefaultLayer.CreateInstance();
+            if (_HauntedHouse == null) _HauntedHouse = _HauntedHouseTemplate.DefaultLayer.CreateInstance();
+            if (_BrainStem == null) _BrainStem = _BrainStemTemplate.DefaultLayer.CreateInstance();
+
+            if (_CesiumMan1 == null) _CesiumMan1 = _CesiumManTemplate.DefaultLayer.CreateInstance();
+            if (_CesiumMan2 == null) _CesiumMan2 = _CesiumManTemplate.DefaultLayer.CreateInstance();
+            if (_CesiumMan3 == null) _CesiumMan3 = _CesiumManTemplate.DefaultLayer.CreateInstance();
+            if (_CesiumMan4 == null) _CesiumMan4 = _CesiumManTemplate.DefaultLayer.CreateInstance();
+
+            if (_Shark == null) _Shark = _SharkTemplate.DefaultLayer.CreateInstance();
+
+            // animate each instance individually.
+
+            var animTime = (float)gameTime.TotalGameTime.TotalSeconds;
+
+            _Avocado.WorldMatrix = Matrix.CreateScale(30) * Matrix.CreateRotationY(animTime * 0.3f) * Matrix.CreateTranslation(-4, 4, 1);
+            _HauntedHouse.WorldMatrix = Matrix.CreateScale(20) * Matrix.CreateRotationY(1);
+
+            _BrainStem.WorldMatrix = Matrix.CreateTranslation(0, 0.5f, 8);
+            _BrainStem.Armature.SetAnimationFrame(0, 0.7f * animTime);
+
+            _CesiumMan1.WorldMatrix = Matrix.CreateTranslation(-3, 0, 5);
+            _CesiumMan1.Armature.SetAnimationFrame(0, 0.3f);
+
+            _CesiumMan2.WorldMatrix = Matrix.CreateTranslation(-2, 0, 5);
+            _CesiumMan2.Armature.SetAnimationFrame(0, 0.5f * animTime);
+
+            _CesiumMan3.WorldMatrix = Matrix.CreateTranslation(2, 0, 5);
+            _CesiumMan3.Armature.SetAnimationFrame(0, 1.0f * animTime);
+
+            _CesiumMan4.WorldMatrix = Matrix.CreateTranslation(3, 0, 5);
+            _CesiumMan4.Armature.SetAnimationFrame(0, 1.5f * animTime);
+
+            _Shark.WorldMatrix = Matrix.CreateTranslation(5, 3, -6);
+            _Shark.Armature.SetAnimationFrame(0, 1.0f * animTime);
+
+            base.Update(gameTime);
+        }
+
+
+        protected override void Draw(GameTime gameTime)
+        {
+            GraphicsDevice.Clear(Color.DarkSlateGray);
+
+            base.Draw(gameTime);
+
+            // setup drawing context
+
+            var animTime = (float)gameTime.TotalGameTime.TotalSeconds;
+
+            var lookAt = new Vector3(0, 2, 0);
+            var camPos = new Vector3((float)Math.Sin(animTime * 0.5f) * 2, 2, 12);
+            var camera = Matrix.CreateWorld(camPos, lookAt - camPos, Vector3.UnitY);
+
+            var ctx = new ModelDrawingContext(_Graphics.GraphicsDevice);
+
+            ctx.SetCamera(camera);
+
+            // draw all the instances.            
+
+            ctx.DrawSceneInstances
+                (
+
+                // environment lights and fog
+                _LightsAndFog,
+
+                // all model instances
+                _Avocado, _HauntedHouse, _BrainStem, _CesiumMan1, _CesiumMan2, _CesiumMan3, _CesiumMan4, _Shark
+
+                );
+        }
+
+        #endregion
+    }
+}

二进制
src/Demo3/Icon.bmp


二进制
src/Demo3/Icon.ico


+ 14 - 0
src/Demo3/Program.cs

@@ -0,0 +1,14 @@
+using System;
+
+namespace Demo3
+{
+    public static class Program
+    {
+        [STAThread]
+        static void Main()
+        {
+            using (var game = new Game1())
+                game.Run();
+        }
+    }
+}

+ 43 - 0
src/Demo3/app.manifest

@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="utf-8"?>
+<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
+  <assemblyIdentity version="1.0.0.0" name="Demo3"/>
+  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
+    <security>
+      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
+        <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
+      </requestedPrivileges>
+    </security>
+  </trustInfo>
+
+  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
+    <application>
+      <!-- A list of the Windows versions that this application has been tested on and is
+           is designed to work with. Uncomment the appropriate elements and Windows will 
+           automatically selected the most compatible environment. -->
+
+      <!-- Windows Vista -->
+      <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />
+
+      <!-- Windows 7 -->
+      <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />
+
+      <!-- Windows 8 -->
+      <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />
+
+      <!-- Windows 8.1 -->
+      <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />
+
+      <!-- Windows 10 -->
+      <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
+
+    </application>
+  </compatibility>
+
+  <application xmlns="urn:schemas-microsoft-com:asm.v3">
+    <windowsSettings>
+      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/pm</dpiAware>
+      <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">permonitorv2,permonitor</dpiAwareness>
+    </windowsSettings>
+  </application>
+
+</assembly>

+ 7 - 1
src/MonoGameScene.sln

@@ -17,7 +17,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MonoGame.Framework.Pipeline
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Demo1", "Demo1\Demo1.csproj", "{2C4B5022-8BA0-44CB-A612-995518C216D2}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demo2", "Demo2\Demo2.csproj", "{9B0C0AF4-F505-4EFA-A707-6AE469224A98}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Demo2", "Demo2\Demo2.csproj", "{9B0C0AF4-F505-4EFA-A707-6AE469224A98}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demo3", "Demo3\Demo3.csproj", "{C617C831-E727-411A-901B-0302C7CE5AA2}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -57,6 +59,10 @@ Global
 		{9B0C0AF4-F505-4EFA-A707-6AE469224A98}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{9B0C0AF4-F505-4EFA-A707-6AE469224A98}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{9B0C0AF4-F505-4EFA-A707-6AE469224A98}.Release|Any CPU.Build.0 = Release|Any CPU
+		{C617C831-E727-411A-901B-0302C7CE5AA2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{C617C831-E727-411A-901B-0302C7CE5AA2}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{C617C831-E727-411A-901B-0302C7CE5AA2}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{C617C831-E727-411A-901B-0302C7CE5AA2}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE