public void SetProjectionTest() { PerspectiveProjection projection = new PerspectiveProjection(); projection.Set(4, 3, 2, 10); PerspectiveProjection projection2 = new PerspectiveProjection(); projection2.Set(4, 3); projection2.Near = 2; projection2.Far = 10; Projection projection3 = new PerspectiveProjection { Left = -2, Right = 2, Bottom = -1.5f, Top = 1.5f, Near = 2, Far = 10, }; Matrix44F expected = Matrix44F.CreatePerspective(4, 3, 2, 10); Assert.IsTrue(Matrix44F.AreNumericallyEqual(expected, projection)); Assert.IsTrue(Matrix44F.AreNumericallyEqual(expected, projection2)); Assert.IsTrue(Matrix44F.AreNumericallyEqual(expected, projection3.ToMatrix44F())); }
void Awake() { Application.runInBackground = true; properties = GetComponent <Properties>(); try { properties.load(location); } catch (NegativeRuntimeException e) { Debug.LogException(e); strategicExit(); } _negativeSpace = GetComponent <NegativeSpace>(); _projection = Camera.main.GetComponent <PerspectiveProjection>(); _surfaceRequestListener = GetComponent <SurfaceRequestListener>(); _surfaceRequestListener.StartReceive(); GetComponent <SurfaceRequest>().request(); _udpBodiesListener = GameObject.Find("BodiesManager").GetComponent <UdpBodiesListener>(); _bodies = GameObject.Find("BodiesManager").GetComponent <BodiesManager>(); _udpBodiesListener.startListening(int.Parse(properties.localSetupInfo.trackerBroadcastPort)); }
public void SetProjectionFieldOfViewTest() { PerspectiveProjection projection = new PerspectiveProjection(); projection.SetFieldOfView(MathHelper.ToRadians(60), 16.0f / 9.0f, 1, 10); PerspectiveProjection projection2 = new PerspectiveProjection(); projection2.SetFieldOfView(MathHelper.ToRadians(60), 16.0f / 9.0f); projection2.Near = 1; projection2.Far = 10; Projection projection3 = new PerspectiveProjection { Left = -2.0528009f / 2.0f, Right = 2.0528009f / 2.0f, Bottom = -1.1547005f / 2.0f, Top = 1.1547005f / 2.0f, Near = 1, Far = 10, }; Matrix44F expected = Matrix44F.CreatePerspectiveFieldOfView(MathHelper.ToRadians(60), 16.0f / 9.0f, 1, 10); Assert.IsTrue(Matrix44F.AreNumericallyEqual(expected, projection)); Assert.IsTrue(Matrix44F.AreNumericallyEqual(expected, projection2)); Assert.IsTrue(Matrix44F.AreNumericallyEqual(expected, projection3.ToMatrix44F())); }
public OcclusionCullingScreen(IServiceLocator services) : base(services) { _sceneNodes = new List <SceneNode>(); // Create new occlusion buffer with default settings. OcclusionBuffer = new OcclusionBuffer(GraphicsService); OcclusionBuffer.ProgressiveShadowCasterCulling = true; EnableCulling = true; // Create a second camera for rendering a top-down view of the scene. var topDownPerspective = new PerspectiveProjection(); topDownPerspective.SetFieldOfView(MathHelper.ToRadians(90), 1, 1, 512); _topDownCameraNode = new CameraNode(new Camera(topDownPerspective)); _topDownCameraNode.PoseWorld = new Pose(new Vector3F(-10, 120, -10)); _topDownCameraNode.LookAt(new Vector3F(-10, 0, -10), Vector3F.UnitZ); _sceneQuery = new CustomSceneQuery(); _debugRenderer = new DebugRenderer(GraphicsService, null, null); // The DigitalRune Profiler is used to measure execution times. Profiler.SetFormat("Occlusion.Render", 1e3f, "[ms]"); Profiler.SetFormat("Occlusion.Query", 1e3f, "[ms]"); }
public CharacterControllerSample(Microsoft.Xna.Framework.Game game) : base(game) { SampleFramework.IsMouseVisible = false; GraphicsScreen.ClearBackground = true; GraphicsScreen.BackgroundColor = Color.CornflowerBlue; // Create a camera. var projection = new PerspectiveProjection(); projection.SetFieldOfView( ConstantsF.PiOver4, GraphicsService.GraphicsDevice.Viewport.AspectRatio, 0.1f, 1000.0f); _cameraNode = new CameraNode(new Camera(projection)); GraphicsScreen.CameraNode = _cameraNode; // We use one collision domain that computes collision info for all game objects. _domain = new CollisionDomain(new CollisionDetection()); // Create collision objects for a test level. CharacterControllerLevel.Load(_domain); // Add collision filter: // The _domain contains a lot of collision objects for obstacles in the level. // We do not need to compute contacts between these static obstacles. To avoid // this, the CharacterControllerLevel puts all level collision objects into // the collision group 1. We add a broad phase collision filter which filters out // collision checks between objects of collision group 1. _domain.BroadPhase.Filter = new DelegatePairFilter <CollisionObject>( pair => { if (pair.First.CollisionGroup == 1 && pair.Second.CollisionGroup == 1) { return(false); } return(true); }); // Create character controller. _character = new CharacterController(_domain); _character.Position = new Vector3F(0, 0, 1); // Create the trigger volume. _triggerVolume = new CollisionObject( new GeometricObject(new SphereShape(3), new Pose(new Vector3F(-5, 0, 5)))) { // We do not want to compute detailed contact information (contact points, contact // normal vectors, etc.). We are only interested if the object touches another object or not. // Therefore, we set the collision object type to "trigger". Trigger objects are better for // performance than normal collision objects. Additionally, the character controller should // be able to walk through the trigger volume. The character controller treats objects as // solids if it finds contact information (contact positions with contact normal vectors). Type = CollisionObjectType.Trigger }; _domain.CollisionObjects.Add(_triggerVolume); }
internal void _LoadData() { var matrix = new Matrix <float> (3, 3); DistortionCeofs = new Array <float> (5); IsValid = Plugin.Intrinsic_getData(this, matrix, DistortionCeofs); matrix.Get(out Matrix3x3 matrix1); Projection = new PerspectiveProjection(matrix1, device.FrameSize); }
public void GetScreenSizeWithPerspective() { // Camera var projection = new PerspectiveProjection(); projection.SetFieldOfView(MathHelper.ToRadians(90), 2.0f / 1.0f, 1.0f, 100f); var camera = new Camera(projection); var cameraNode = new CameraNode(camera); cameraNode.PoseWorld = new Pose(new Vector3(123, 456, -789), Matrix.CreateRotation(new Vector3(1, -2, 3), MathHelper.ToRadians(75))); // 2:1 viewport var viewport = new Viewport(10, 10, 200, 100); // Test object var shape = new SphereShape(); var geometricObject = new GeometricObject(shape); // Empty sphere at camera position. shape.Radius = 0; geometricObject.Pose = cameraNode.PoseWorld; Vector2F screenSize = GraphicsHelper.GetScreenSize(cameraNode, viewport, geometricObject); Assert.AreEqual(0, screenSize.X); Assert.AreEqual(0, screenSize.Y); // Empty sphere centered at near plane. shape.Radius = 0; geometricObject.Pose = cameraNode.PoseWorld * new Pose(new Vector3(0.123f, -0.543f, -1)); screenSize = GraphicsHelper.GetScreenSize(cameraNode, viewport, geometricObject); Assert.AreEqual(0, screenSize.X); Assert.AreEqual(0, screenSize.Y); // Create sphere which as a bounding sphere of ~1 unit diameter: // Since the bounding sphere is based on the AABB, we need to make the // actual sphere a bit smaller. shape.Radius = 1 / (2 * (float)Math.Sqrt(3)) + Numeric.EpsilonF; // Sphere at camera position. geometricObject.Pose = cameraNode.PoseWorld; screenSize = GraphicsHelper.GetScreenSize(cameraNode, viewport, geometricObject); Assert.Greater(screenSize.X, 200); Assert.Greater(screenSize.Y, 100); // Sphere at near plane. geometricObject.Pose = cameraNode.PoseWorld * new Pose(new Vector3(0.123f, -0.543f, -1)); screenSize = GraphicsHelper.GetScreenSize(cameraNode, viewport, geometricObject); Assert.IsTrue(Numeric.AreEqual(screenSize.X, 50.0f, 10f)); Assert.IsTrue(Numeric.AreEqual(screenSize.Y, 50.0f, 10f)); // Double distance --> half size geometricObject.Pose = cameraNode.PoseWorld * new Pose(new Vector3(0.123f, -0.543f, -2)); screenSize = GraphicsHelper.GetScreenSize(cameraNode, viewport, geometricObject); Assert.IsTrue(Numeric.AreEqual(screenSize.X, 25.0f, 5f)); Assert.IsTrue(Numeric.AreEqual(screenSize.Y, 25.0f, 5f)); }
void Awake() { _perspectiveProjection = Camera.main.GetComponent <PerspectiveProjection>(); _leftCursorScript = leftCursor.GetComponent <NSCursor>(); _rightCursorScript = rightCursor.GetComponent <NSCursor>(); _NSObjects = new Dictionary <string, GameObject>(); _NSNetwork = this.gameObject.GetComponent <RPCNetwork>(); }
private void InitializeGraphicsScreen() { Debug.Assert(GraphicsScreens.Count == 0, "Reset graphics screens before calling InitializeGraphicsScreen()."); var services = Document.Editor.Services; var graphicsService = services.GetInstance <IGraphicsService>().ThrowIfMissing(); // Initialize graphics screens. var graphicsScreen = UseDeferredLighting ? (GraphicsScreen) new DeferredGraphicsScreen(services) : new BasicGraphicsScreen(services); GraphicsScreens.Add(graphicsScreen); GraphicsScreens.Add(new DebugGraphicsScreen(services)); // Add default lighting. var scene = UseDeferredLighting ? ((DeferredGraphicsScreen)graphicsScreen).Scene : ((BasicGraphicsScreen)graphicsScreen).Scene; GameHelper.AddLights(scene); // Add a ground plane (useful for orientation and to check model shadows). if (_groundModelNode == null) { var content = services.GetInstance <ContentManager>().ThrowIfMissing(); _groundModelNode = content.Load <ModelNode>("DigitalRune.Editor.Game/Models/Misc/GroundPlane/GroundPlane").Clone(); } _groundModelNode.IsEnabled = ShowGroundPlane; scene.Children.Add(_groundModelNode); // Add camera. if (CameraNode == null) { var projection = new PerspectiveProjection(); projection.SetFieldOfView( ConstantsF.PiOver4, graphicsService.GraphicsDevice.Viewport.AspectRatio, 0.1f, 10000.0f); CameraNode = new CameraNode(new Camera(projection)) { Name = "CameraPerspective", }; } if (UseDeferredLighting) { ((DeferredGraphicsScreen)graphicsScreen).ActiveCameraNode = CameraNode; } else { ((BasicGraphicsScreen)graphicsScreen).CameraNode = CameraNode; } }
private void Update() { var intrinsic = cameraService == null ? null : cameraService.Device?.intrinsic; var curProj = intrinsic == null || !intrinsic.IsValid ? null : intrinsic.Projection; if (_proj == curProj) { return; } _proj = curProj; _UpdateLines(); }
//-------------------------------------------------------------- #region Methods //-------------------------------------------------------------- protected override void OnLoad() { var graphicsService = _services.GetInstance <IGraphicsService>(); // Define camera projection. var projection = new PerspectiveProjection(); projection.SetFieldOfView( ConstantsF.PiOver4, graphicsService.GraphicsDevice.Viewport.AspectRatio, 0.1f, 1000.0f); // Create a camera node. CameraNode = new CameraNode(new Camera(projection)); }
public void SetProjectionOffCenterTest() { PerspectiveProjection projection = new PerspectiveProjection(); projection.SetOffCenter(0, 4, 1, 4, 2, 10); PerspectiveProjection projection2 = new PerspectiveProjection(); projection2.SetOffCenter(0, 4, 1, 4); projection2.Near = 2; projection2.Far = 10; Matrix44F expected = Matrix44F.CreatePerspectiveOffCenter(0, 4, 1, 4, 2, 10); Assert.IsTrue(Matrix44F.AreNumericallyEqual(expected, projection)); Assert.IsTrue(Matrix44F.AreNumericallyEqual(expected, projection2.ToMatrix44F())); }
private void CreateDisplay <T>(MenuSettings <T> settings) where T : UnityEngine.Component { //Load 3d model. UnityEngine.Object modelObj = Resources.Load(String.Format("Displays/{0}", displayModels[selectedModel])); GameObject obj = Instantiate(modelObj) as GameObject; obj.name = settings.name; DisplayObject disp = obj.AddComponent <DisplayObject>(); obj.tag = "VRDisplay"; // Create settings. DisplaySettings settingsObj = CreateInstance <DisplaySettings>(); AssetDatabase.CreateAsset(settingsObj, string.Format("Assets/VRSettings/Displays/{0}.asset", obj.name)); disp.settings = settingsObj; // Continue depending on display type. switch (dispType) { case Gimbl.DisplayType.Monitor: // Go through screens surfaces. MeshRenderer[] meshes = obj.GetComponentsInChildren <MeshRenderer>(); foreach (MeshRenderer mesh in meshes) { // Turn off collider. mesh.GetComponent <MeshCollider>().enabled = false; // Create camera. GameObject cam = new GameObject(String.Format("Camera: {0}", mesh.name)); cam.transform.SetParent(mesh.transform.parent); cam.transform.localPosition = new Vector3(0, 0, 0); //Setup projection script. Camera camComp = cam.AddComponent <Camera>(); camComp.nearClipPlane = 0.3f; camComp.targetDisplay = 8; camComp.clearFlags = CameraClearFlags.Skybox; camComp.backgroundColor = Color.black; PerspectiveProjection proj = cam.AddComponent <PerspectiveProjection>(); proj.projectionScreen = mesh.gameObject; proj.setNearClipPlane = false; mesh.enabled = false; } break; } Undo.RegisterCreatedObjectUndo(obj, "Create Display"); settings.selected = disp as T; settings.name = ""; }
public void SetProjectionTest() { Matrix projectionMatrix = Matrix.CreateOrthographicOffCenter(1, 4, 2, 5, 6, 11); OrthographicProjection orthographicProjection = new OrthographicProjection(); orthographicProjection.Set(projectionMatrix); CameraInstance cameraInstance = new CameraInstance(new Camera(orthographicProjection)); Assert.AreEqual(Vector3.Zero, cameraInstance.PoseWorld.Position); Assert.AreEqual(Matrix.Identity, cameraInstance.PoseWorld.Orientation); Assert.That(Numeric.AreEqual(3, cameraInstance.Camera.Projection.Width)); Assert.That(Numeric.AreEqual(3, cameraInstance.Camera.Projection.Height)); Assert.That(Numeric.AreEqual(1f, cameraInstance.Camera.Projection.AspectRatio)); Assert.That(Numeric.AreEqual(6, cameraInstance.Camera.Projection.Near)); Assert.That(Numeric.AreEqual(11, cameraInstance.Camera.Projection.Far)); Assert.That(Numeric.AreEqual(1, cameraInstance.Camera.Projection.Left)); Assert.That(Numeric.AreEqual(4, cameraInstance.Camera.Projection.Right)); Assert.That(Numeric.AreEqual(2, cameraInstance.Camera.Projection.Bottom)); Assert.That(Numeric.AreEqual(5, cameraInstance.Camera.Projection.Top)); Assert.That(Numeric.AreEqual(5, cameraInstance.Camera.Projection.Depth)); Assert.That(Matrix.AreNumericallyEqual(orthographicProjection, cameraInstance.Camera.Projection)); Assert.That(Matrix.AreNumericallyEqual(orthographicProjection.Inverse, cameraInstance.Camera.Projection.Inverse)); Assert.IsNotNull(cameraInstance.BoundingShape); PerspectiveProjection perspectiveProjection = new PerspectiveProjection(); perspectiveProjection.Inverse = Matrix.CreatePerspectiveOffCenter(1, 5, 2, 5, 1, 10).Inverse; cameraInstance = new CameraInstance(new Camera(perspectiveProjection)); Assert.AreEqual(Vector3.Zero, cameraInstance.PoseWorld.Position); Assert.AreEqual(Matrix.Identity, cameraInstance.PoseWorld.Orientation); Assert.That(Numeric.AreEqual(MathHelper.ToRadians(33.690067f), cameraInstance.Camera.Projection.FieldOfViewX)); Assert.That(Numeric.AreEqual(MathHelper.ToRadians(15.255119f), cameraInstance.Camera.Projection.FieldOfViewY)); Assert.That(Numeric.AreEqual(4, cameraInstance.Camera.Projection.Width)); Assert.That(Numeric.AreEqual(3, cameraInstance.Camera.Projection.Height)); Assert.That(Numeric.AreEqual(4.0f / 3.0f, cameraInstance.Camera.Projection.AspectRatio)); Assert.That(Numeric.AreEqual(1, cameraInstance.Camera.Projection.Left)); Assert.That(Numeric.AreEqual(5, cameraInstance.Camera.Projection.Right)); Assert.That(Numeric.AreEqual(2, cameraInstance.Camera.Projection.Bottom)); Assert.That(Numeric.AreEqual(5, cameraInstance.Camera.Projection.Top)); Assert.That(Numeric.AreEqual(1, cameraInstance.Camera.Projection.Near)); Assert.That(Numeric.AreEqual(10, cameraInstance.Camera.Projection.Far)); Assert.That(Numeric.AreEqual(9, cameraInstance.Camera.Projection.Depth)); Assert.IsNotNull(cameraInstance.BoundingShape); }
public void DeleteActor() { bool accept = EditorUtility.DisplayDialog(string.Format("Remove Actor {0}?", name), string.Format("Are you sure you want to delete Actor {0}?", name), "Delete", "Cancel"); if (accept) { // Not deleting scriptable object asset so delete it can be undone. TagLayerEditor.TagsAndLayers.RemoveLayer(name); // unparent attached displays. PerspectiveProjection cam = GetComponentInChildren <PerspectiveProjection>(); if (cam != null) { cam.transform.parent.transform.SetParent(null); } Undo.DestroyObjectImmediate(gameObject); } }
public WindowsPhoneSample(Microsoft.Xna.Framework.Game game) : base(game) { GraphicsScreen.ClearBackground = true; GraphicsScreen.BackgroundColor = Color.CornflowerBlue; // Set a fixed camera. var projection = new PerspectiveProjection(); projection.SetFieldOfView( MathHelper.ToRadians(30), GraphicsService.GraphicsDevice.Viewport.AspectRatio, 1f, 1000.0f); Vector3F cameraTarget = new Vector3F(0, 1, 0); Vector3F cameraPosition = new Vector3F(0, 12, 0); Vector3F cameraUpVector = new Vector3F(0, 0, -1); GraphicsScreen.CameraNode = new CameraNode(new Camera(projection)) { View = Matrix44F.CreateLookAt(cameraPosition, cameraTarget, cameraUpVector), }; // We use the accelerometer to control the camera view. The accelerometer registers every // little change, but we do not want a shaky camera. We can use a low-pass filter to smooth // the sensor signal. _lowPassFilter = new LowPassFilter(new Vector3F(0, -1, 0)) { TimeConstant = 0.15f, // Let's try a time constant of 0.15 seconds. // When increasing the time constant the camera becomes more stable, // but also slower. }; // Enable touch gestures _originalEnabledGestures = TouchPanel.EnabledGestures; TouchPanel.EnabledGestures = GestureType.Tap // Tap is used to drop new bodies. | GestureType.Flick // Flick creates an explosion. | GestureType.Hold // Hold to clear the scene. | GestureType.Pinch; // Pinch can be used to zoom in or out. InitializePhysics(); }
public void UnprojectTest() { Viewport viewport = new Viewport(0, 0, 640, 480); PerspectiveProjection projection = new PerspectiveProjection(); projection.SetFieldOfView(MathHelper.ToRadians(60), viewport.AspectRatio, 10, 1000); Matrix view = Matrix.CreateLookAt(new Vector3(0, 0, 0), new Vector3(0, 0, -1), Vector3.Up); Assert.IsTrue(Vector3.AreNumericallyEqual(new Vector3(0, 0, -10), viewport.Unproject(new Vector3(320, 240, 0), projection, view))); Assert.IsTrue(Vector3.AreNumericallyEqual(new Vector3(projection.Left, projection.Top, -10), viewport.Unproject(new Vector3(0, 0, 0), projection, view))); Assert.IsTrue(Vector3.AreNumericallyEqual(new Vector3(projection.Right, projection.Top, -10), viewport.Unproject(new Vector3(640, 0, 0), projection, view))); Assert.IsTrue(Vector3.AreNumericallyEqual(new Vector3(projection.Left, projection.Bottom, -10), viewport.Unproject(new Vector3(0, 480, 0), projection, view))); Assert.IsTrue(Vector3.AreNumericallyEqual(new Vector3(projection.Right, projection.Bottom, -10), viewport.Unproject(new Vector3(640, 480, 0), projection, view))); Vector3[] farCorners = new Vector3[4]; GraphicsHelper.GetFrustumFarCorners(projection, farCorners); Assert.IsTrue(Vector3.AreNumericallyEqual((Vector3)farCorners[0], viewport.Unproject(new Vector3(0, 0, 1), projection, view))); Assert.IsTrue(Vector3.AreNumericallyEqual((Vector3)farCorners[1], viewport.Unproject(new Vector3(640, 0, 1), projection, view))); Assert.IsTrue(Vector3.AreNumericallyEqual((Vector3)farCorners[2], viewport.Unproject(new Vector3(0, 480, 1), projection, view))); Assert.IsTrue(Vector3.AreNumericallyEqual((Vector3)farCorners[3], viewport.Unproject(new Vector3(640, 480, 1), projection, view))); }
private void TestClippedProjection() { RandomHelper.Random = new Random(1234567); var p = new PerspectiveProjection(); p.SetOffCenter(-0.1f, 0.2f, -0.1f, 0.1f, 0.1f, 1); _debugRenderer.DrawViewVolume(p.ViewVolume, new Pose(new Vector3(0, 2, 0)), Color.Red, true, false); p.NearClipPlane = new Plane(new Vector3(-0.1f, +0.1f, 1).Normalized, -0.4f); PlaneShape.MeshSize = 2; _debugRenderer.DrawShape(new PlaneShape(p.NearClipPlane.Value), new Pose(new Vector3(0, 2, 0)), Vector3.One, Color.Green, false, false); Matrix m = p.ToMatrix(); for (int i = 0; i < 100000; i++) { Aabb aabb = p.ViewVolume.GetAabb(Pose.Identity); aabb.Minimum -= new Vector3(1); aabb.Maximum += new Vector3(1); float x = RandomHelper.Random.NextFloat(aabb.Minimum.X, aabb.Maximum.X); float y = RandomHelper.Random.NextFloat(aabb.Minimum.Y, aabb.Maximum.Y); float z = RandomHelper.Random.NextFloat(aabb.Minimum.Z, aabb.Maximum.Z); //if (RandomHelper.Random.NextBool()) // x = 0; //else // y = 0; Vector4 c = m * new Vector4(x, y, z, 1); c /= c.W; Color color = Color.Orange; if (c.X < -1 || c.X > 1 || c.Y < -1 || c.Y > 1 || c.Z < 0 || c.Z > 1) { continue;// color = Color.Gray; } _debugRenderer.DrawPoint(new Vector3(x, y + 2, z), color, false); } }
void Awake() { Application.runInBackground = true; _surfaceRequestListener = GetComponent <SurfaceRequestListener>(); _properties = GetComponent <Properties>(); _log = GetComponent <VisualLog>(); _negativeSpace = GetComponent <NegativeSpace>(); _bodiesListener = GameObject.Find("BodiesManager").GetComponent <UdpBodiesListener>(); _bodies = GameObject.Find("BodiesManager").GetComponent <BodiesManager>(); _prespectiveProjection = Camera.main.GetComponent <PerspectiveProjection>(); _tracker = GameObject.Find("RavatarManager").GetComponent <Tracker>(); _tcpKinectListener = GameObject.Find("RavatarManager").GetComponent <TcpKinectListener>(); _workspace = GetComponent <RPCWorkspace>(); _properties.load(); ConfigLoaded = _properties.ConfigLoaded; if (!ConfigLoaded) { _log.Show = true; } else { _log.WriteLine(this, "Config Loaded"); _surfaceRequestListener.StartReceive(); } if (location == Location.A) { _workspace.InitServer(); } else { _workspace.InitClient(); } }
internal void _UpdateSize() => Projection = new PerspectiveProjection(Projection, device.FrameSize);
public void SetProjectionTest() { Matrix44F projectionMatrix = Matrix44F.CreateOrthographicOffCenter(1, 4, 2, 5, 6, 11); OrthographicProjection orthographicProjection = new OrthographicProjection(); orthographicProjection.Set(projectionMatrix); CameraInstance cameraInstance = new CameraInstance(new Camera(orthographicProjection)); Assert.AreEqual(Vector3F.Zero, cameraInstance.PoseWorld.Position); Assert.AreEqual(Matrix33F.Identity, cameraInstance.PoseWorld.Orientation); Assert.That(Numeric.AreEqual(3, cameraInstance.Camera.Projection.Width)); Assert.That(Numeric.AreEqual(3, cameraInstance.Camera.Projection.Height)); Assert.That(Numeric.AreEqual(1f, cameraInstance.Camera.Projection.AspectRatio)); Assert.That(Numeric.AreEqual(6, cameraInstance.Camera.Projection.Near)); Assert.That(Numeric.AreEqual(11, cameraInstance.Camera.Projection.Far)); Assert.That(Numeric.AreEqual(1, cameraInstance.Camera.Projection.Left)); Assert.That(Numeric.AreEqual(4, cameraInstance.Camera.Projection.Right)); Assert.That(Numeric.AreEqual(2, cameraInstance.Camera.Projection.Bottom)); Assert.That(Numeric.AreEqual(5, cameraInstance.Camera.Projection.Top)); Assert.That(Numeric.AreEqual(5, cameraInstance.Camera.Projection.Depth)); Assert.That(Matrix44F.AreNumericallyEqual(orthographicProjection, cameraInstance.Camera.Projection)); Assert.That(Matrix44F.AreNumericallyEqual(orthographicProjection.Inverse, cameraInstance.Camera.Projection.Inverse)); Assert.IsNotNull(cameraInstance.BoundingShape); PerspectiveProjection perspectiveProjection = new PerspectiveProjection(); perspectiveProjection.Inverse = Matrix44F.CreatePerspectiveOffCenter(1, 5, 2, 5, 1, 10).Inverse; cameraInstance = new CameraInstance(new Camera(perspectiveProjection)); Assert.AreEqual(Vector3F.Zero, cameraInstance.PoseWorld.Position); Assert.AreEqual(Matrix33F.Identity, cameraInstance.PoseWorld.Orientation); Assert.That(Numeric.AreEqual(MathHelper.ToRadians(33.690067f), cameraInstance.Camera.Projection.FieldOfViewX)); Assert.That(Numeric.AreEqual(MathHelper.ToRadians(15.255119f), cameraInstance.Camera.Projection.FieldOfViewY)); Assert.That(Numeric.AreEqual(4, cameraInstance.Camera.Projection.Width)); Assert.That(Numeric.AreEqual(3, cameraInstance.Camera.Projection.Height)); Assert.That(Numeric.AreEqual(4.0f / 3.0f, cameraInstance.Camera.Projection.AspectRatio)); Assert.That(Numeric.AreEqual(1, cameraInstance.Camera.Projection.Left)); Assert.That(Numeric.AreEqual(5, cameraInstance.Camera.Projection.Right)); Assert.That(Numeric.AreEqual(2, cameraInstance.Camera.Projection.Bottom)); Assert.That(Numeric.AreEqual(5, cameraInstance.Camera.Projection.Top)); Assert.That(Numeric.AreEqual(1, cameraInstance.Camera.Projection.Near)); Assert.That(Numeric.AreEqual(10, cameraInstance.Camera.Projection.Far)); Assert.That(Numeric.AreEqual(9, cameraInstance.Camera.Projection.Depth)); Assert.IsNotNull(cameraInstance.BoundingShape); }
private void Render(RenderContext context) { var originalRenderTarget = context.RenderTarget; var originalViewport = context.Viewport; var graphicsDevice = context.GraphicsService.GraphicsDevice; if (_updateCubeMap) { _updateCubeMap = false; _cloudMapRenderer.Render(_skyNodes, context); // Create a camera with 45° FOV for a single cube map face. var perspectiveProjection = new PerspectiveProjection(); perspectiveProjection.SetFieldOfView(ConstantsF.PiOver2, 1, 1, 100); context.CameraNode = new CameraNode(new Camera(perspectiveProjection)); var size = _skybox.Texture.Size; var hdrFormat = new RenderTargetFormat(size, size, false, SurfaceFormat.HdrBlendable, DepthFormat.None); var hdrTarget = context.GraphicsService.RenderTargetPool.Obtain2D(hdrFormat); var ldrFormat = new RenderTargetFormat(size, size, false, SurfaceFormat.Color, DepthFormat.None); var ldrTarget = context.GraphicsService.RenderTargetPool.Obtain2D(ldrFormat); var spriteBatch = GraphicsService.GetSpriteBatch(); for (int side = 0; side < 6; side++) { // Rotate camera to face the current cube map face. var cubeMapFace = (CubeMapFace)side; context.CameraNode.View = Matrix44F.CreateLookAt( new Vector3F(), GraphicsHelper.GetCubeMapForwardDirection(cubeMapFace), GraphicsHelper.GetCubeMapUpDirection(cubeMapFace)); // Render sky into HDR render target. graphicsDevice.SetRenderTarget(hdrTarget); context.RenderTarget = hdrTarget; context.Viewport = graphicsDevice.Viewport; graphicsDevice.Clear(Color.Black); _skyRenderer.Render(_skyNodes, context); graphicsDevice.BlendState = BlendState.Opaque; // Convert HDR to RGBM. context.SourceTexture = hdrTarget; context.RenderTarget = ldrTarget; _colorEncoder.Process(context); context.SourceTexture = null; // Copy RGBM texture into cube map face. graphicsDevice.SetRenderTarget((RenderTargetCube)_skybox.Texture, cubeMapFace); spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque, null, null, null); spriteBatch.Draw(ldrTarget, new Vector2(0, 0), Color.White); spriteBatch.End(); } context.GraphicsService.RenderTargetPool.Recycle(ldrTarget); context.GraphicsService.RenderTargetPool.Recycle(hdrTarget); } graphicsDevice.BlendState = BlendState.Opaque; graphicsDevice.DepthStencilState = DepthStencilState.Default; graphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise; context.CameraNode = _cameraObject.CameraNode; var tempFormat = new RenderTargetFormat(originalRenderTarget); tempFormat.SurfaceFormat = SurfaceFormat.HdrBlendable; var tempTarget = context.GraphicsService.RenderTargetPool.Obtain2D(tempFormat); graphicsDevice.SetRenderTarget(tempTarget); graphicsDevice.Viewport = originalViewport; context.RenderTarget = tempTarget; context.Viewport = originalViewport; _skyRenderer.Render(_skybox, context); context.SourceTexture = tempTarget; context.RenderTarget = originalRenderTarget; _hdrFilter.Process(context); context.SourceTexture = null; context.GraphicsService.RenderTargetPool.Recycle(tempTarget); RenderDebugInfo(context); context.CameraNode = null; }
public void UnprojectTest() { Viewport viewport = new Viewport(0, 0, 640, 480); PerspectiveProjection projection = new PerspectiveProjection(); projection.SetFieldOfView(MathHelper.ToRadians(60), viewport.AspectRatio, 10, 1000); Matrix44F view = Matrix44F.CreateLookAt(new Vector3F(0, 0, 0), new Vector3F(0, 0, -1), Vector3F.Up); Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(0, 0, -10), viewport.Unproject(new Vector3F(320, 240, 0), projection, view))); Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(projection.Left, projection.Top, -10), viewport.Unproject(new Vector3F(0, 0, 0), projection, view))); Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(projection.Right, projection.Top, -10), viewport.Unproject(new Vector3F(640, 0, 0), projection, view))); Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(projection.Left, projection.Bottom, -10), viewport.Unproject(new Vector3F(0, 480, 0), projection, view))); Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(projection.Right, projection.Bottom, -10), viewport.Unproject(new Vector3F(640, 480, 0), projection, view))); Vector3[] farCorners = new Vector3[4]; GraphicsHelper.GetFrustumFarCorners(projection, farCorners); Assert.IsTrue(Vector3F.AreNumericallyEqual((Vector3F)farCorners[0], viewport.Unproject(new Vector3F(0, 0, 1), projection, view))); Assert.IsTrue(Vector3F.AreNumericallyEqual((Vector3F)farCorners[1], viewport.Unproject(new Vector3F(640, 0, 1), projection, view))); Assert.IsTrue(Vector3F.AreNumericallyEqual((Vector3F)farCorners[2], viewport.Unproject(new Vector3F(0, 480, 1), projection, view))); Assert.IsTrue(Vector3F.AreNumericallyEqual((Vector3F)farCorners[3], viewport.Unproject(new Vector3F(640, 480, 1), projection, view))); }
private bool _cullingEnabled = true; // True to use frustum culling. False to disable frustum culling. public FrustumCullingSample(Microsoft.Xna.Framework.Game game) : base(game) { GraphicsScreen.ClearBackground = true; GraphicsScreen.BackgroundColor = Color.CornflowerBlue; // The top-down camera. var orthographicProjection = new OrthographicProjection(); orthographicProjection.Set( LevelSize * 1.1f * GraphicsService.GraphicsDevice.Viewport.AspectRatio, LevelSize * 1.1f, 1, 10000f); var topDownCamera = new Camera(orthographicProjection); _topDownCameraNode = new CameraNode(topDownCamera) { View = Matrix44F.CreateLookAt(new Vector3F(0, 1000, 0), new Vector3F(0, 0, 0), -Vector3F.UnitZ), }; // The perspective camera moving through the scene. var perspectiveProjection = new PerspectiveProjection(); perspectiveProjection.SetFieldOfView( MathHelper.ToRadians(45), GraphicsService.GraphicsDevice.Viewport.AspectRatio, 1, 500); var sceneCamera = new Camera(perspectiveProjection); _sceneCameraNode = new CameraNode(sceneCamera); // Initialize collision detection. // We use one collision domain that manages all objects. _domain = new CollisionDomain(new CollisionDetection()) { // We exchange the default broad phase with a DualPartition. The DualPartition // has special support for frustum culling. BroadPhase = new DualPartition <CollisionObject>(), }; // Create a lot of random objects and add them to the collision domain. RandomHelper.Random = new Random(12345); for (int i = 0; i < NumberOfObjects; i++) { // A real scene consists of a lot of complex objects such as characters, vehicles, // buildings, lights, etc. When doing frustum culling we need to test each objects against // the viewing frustum. If it intersects with the viewing frustum, the object is visible // from the camera's point of view. However, in practice we do not test the exact object // against the viewing frustum. Each objects is approximated by a simpler shape. In our // example, we assume that each object is approximated with an oriented bounding box. // (We could also use an other shape, such as a bounding sphere.) // Create a random box. Shape randomShape = new BoxShape(RandomHelper.Random.NextVector3F(1, 10)); // Create a random position. Vector3F randomPosition; randomPosition.X = RandomHelper.Random.NextFloat(-LevelSize / 2, LevelSize / 2); randomPosition.Y = RandomHelper.Random.NextFloat(0, 2); randomPosition.Z = RandomHelper.Random.NextFloat(-LevelSize / 2, LevelSize / 2); // Create a random orientation. QuaternionF randomOrientation = RandomHelper.Random.NextQuaternionF(); // Create object and add it to collision domain. var geometricObject = new GeometricObject(randomShape, new Pose(randomPosition, randomOrientation)); var collisionObject = new CollisionObject(geometricObject) { CollisionGroup = 0, }; _domain.CollisionObjects.Add(collisionObject); } // Per default, the collision domain computes collision between all objects. // In this sample we do not need this information and disable it with a collision // filter. // In a real application, we would use this collision information for rendering, // for example, to find out which lights overlap with which meshes, etc. var filter = new CollisionFilter(); // Disable collision between objects in collision group 0. filter.Set(0, 0, false); _domain.CollisionDetection.CollisionFilter = filter; // Start with the top-down camera. GraphicsScreen.CameraNode = _topDownCameraNode; // We will collect a few statistics for debugging. Profiler.SetFormat("NoCull", 1000, "Time in ms to submit DebugRenderer draw jobs without frustum culling."); Profiler.SetFormat("WithCull", 1000, "Time in ms to submit DebugRenderer draw jobs with frustum culling."); }
public void RandomizedMassSceneUpdate2() { const int NumberOfSceneNodes = 10; const int NumberOfSteps = 10000; const float WorldSize = 1000; var random = new Random(123457); var scene = new Scene(); scene.EnableMultithreading = false; var nodes = new TestSceneNode[NumberOfSceneNodes]; // Create random nodes. for (int i = 0; i < NumberOfSceneNodes; i++) { var node = new TestSceneNode(); nodes[i] = node; var position = random.NextVector3F(0, WorldSize); var orientation = random.NextQuaternionF(); node.PoseLocal = new Pose(position, orientation); float p = random.NextFloat(0, 100); if (p < 0.1f) node.Shape = Shape.Empty; else if (p < 0.2f) node.Shape = Shape.Infinite; else if (p < 0.6f) node.Shape = new BoxShape(random.NextVector3F(0, WorldSize)); else node.Shape = new SphereShape(random.NextFloat(0, WorldSize)); } var projection = new PerspectiveProjection(); projection.SetFieldOfView(0.8f, 1, WorldSize / 10000, WorldSize); var camera = new Camera(projection); var cameraNode = new CameraNode(camera); for (int updateIndex = 0; updateIndex < NumberOfSteps; updateIndex++) { int actionsPerFrame = random.NextInteger(0, 100); for (int i = 0; i < actionsPerFrame; i++) { var node = nodes[random.Next(0, NumberOfSceneNodes)]; const int numberOfActions = 100; int action = random.Next(0, numberOfActions); //scene.Validate(); if (action == 0) { // Add if (node.Parent == null) { scene.Children.Add(node); //scene.Validate(); } } else if (action == 1) { // Remove if (node.Parent != null) { node.Parent.Children.Remove(node); //scene.Validate(); } } else if (action == 2) { // Move var pose = node.PoseWorld; pose.Position = random.NextVector3F(0, WorldSize); node.PoseWorld = pose; //scene.Validate(); } else if (action == 3) { // Very small Move var pose = node.PoseWorld; const float maxDistance = WorldSize / 10000; pose.Position += random.NextVector3F(-maxDistance, maxDistance); node.PoseWorld = pose; //scene.Validate(); } else if (action == 4) { // Small Move var pose = node.PoseWorld; const float maxDistance = WorldSize / 100; pose.Position += random.NextVector3F(-maxDistance, maxDistance); node.PoseWorld = pose; //scene.Validate(); } else if (action == 5) { // Scale node.ScaleLocal = random.NextVector3F(0.0f, 10f); //scene.Validate(); } else if (action == 6) { // Rotate node.PoseWorld = new Pose(node.PoseWorld.Position, random.NextQuaternionF()); //scene.Validate(); } else if (action == 7) { // Query var query = scene.Query<CameraFrustumQuery>(cameraNode, null); //Debug.WriteLine("Camera queried nodes: " + query.SceneNodes.Count); //scene.Validate(); } else if (action == 8) { // Move camera. cameraNode.PoseWorld = new Pose(random.NextVector3F(0, WorldSize), random.NextQuaternionF()); //scene.Validate(); } else if (action == 9) { // Change shape. int type = random.NextInteger(0, 5); if (type == 0) node.Shape = new BoxShape(random.NextVector3F(0, WorldSize)); else if (type == 1) node.Shape = new SphereShape(random.NextFloat(0, WorldSize)); else if (type == 2) node.Shape = new BoxShape(new Vector3F(Single.MaxValue)); else if (type == 3) node.Shape = new BoxShape(new Vector3F(0)); else if (type == 4) node.Shape = Shape.Empty; //scene.Validate(); } else if (action == 10) { // Add to random parent. if (node.Parent == null) { var randomParent = nodes[random.NextInteger(0, NumberOfSceneNodes - 1)]; // Avoid loops: bool isLoop = node.GetSubtree().Contains(randomParent); if (!isLoop) { if (randomParent.Children == null) randomParent.Children = new SceneNodeCollection(); randomParent.Children.Add(node); } } } else if (action == 11) { if (node.Parent != null && node.Parent != scene) node.Parent.Children.Remove(node); } else if (action == 13) { if (random.NextInteger(0, 100) < 5) scene.Children.Clear(); } else if (action == 14) { if (random.NextInteger(0, 100) < 5) scene.Children = new SceneNodeCollection(); } } //Debug.WriteLine("Number of nodes in scene: " + scene.GetDescendants().Count()); //scene.Validate(); scene.Update(TimeSpan.FromSeconds(0.016666666f)); //scene.Validate(); } }
public void GetScreenSizeWithPerspective() { // Camera var projection = new PerspectiveProjection(); projection.SetFieldOfView(MathHelper.ToRadians(90), 2.0f / 1.0f, 1.0f, 100f); var camera = new Camera(projection); var cameraNode = new CameraNode(camera); cameraNode.PoseWorld = new Pose(new Vector3F(123, 456, -789), Matrix33F.CreateRotation(new Vector3F(1, -2, 3), MathHelper.ToRadians(75))); // 2:1 viewport var viewport = new Viewport(10, 10, 200, 100); // Test object var shape = new SphereShape(); var geometricObject = new GeometricObject(shape); // Empty sphere at camera position. shape.Radius = 0; geometricObject.Pose = cameraNode.PoseWorld; Vector2F screenSize = GraphicsHelper.GetScreenSize(cameraNode, viewport, geometricObject); Assert.AreEqual(0, screenSize.X); Assert.AreEqual(0, screenSize.Y); // Empty sphere centered at near plane. shape.Radius = 0; geometricObject.Pose = cameraNode.PoseWorld * new Pose(new Vector3F(0.123f, -0.543f, -1)); screenSize = GraphicsHelper.GetScreenSize(cameraNode, viewport, geometricObject); Assert.AreEqual(0, screenSize.X); Assert.AreEqual(0, screenSize.Y); // Create sphere which as a bounding sphere of ~1 unit diameter: // Since the bounding sphere is based on the AABB, we need to make the // actual sphere a bit smaller. shape.Radius = 1 / (2 * (float)Math.Sqrt(3)) + Numeric.EpsilonF; // Sphere at camera position. geometricObject.Pose = cameraNode.PoseWorld; screenSize = GraphicsHelper.GetScreenSize(cameraNode, viewport, geometricObject); Assert.Greater(screenSize.X, 200); Assert.Greater(screenSize.Y, 100); // Sphere at near plane. geometricObject.Pose = cameraNode.PoseWorld * new Pose(new Vector3F(0.123f, -0.543f, -1)); screenSize = GraphicsHelper.GetScreenSize(cameraNode, viewport, geometricObject); Assert.IsTrue(Numeric.AreEqual(screenSize.X, 50.0f, 10f)); Assert.IsTrue(Numeric.AreEqual(screenSize.Y, 50.0f, 10f)); // Double distance --> half size geometricObject.Pose = cameraNode.PoseWorld * new Pose(new Vector3F(0.123f, -0.543f, -2)); screenSize = GraphicsHelper.GetScreenSize(cameraNode, viewport, geometricObject); Assert.IsTrue(Numeric.AreEqual(screenSize.X, 25.0f, 5f)); Assert.IsTrue(Numeric.AreEqual(screenSize.Y, 25.0f, 5f)); }
//-------------------------------------------------------------- #region Methods //-------------------------------------------------------------- protected override void OnLoad() { // Get services. _inputService = _services.GetInstance <IInputService>(); _graphicsService = _services.GetInstance <IGraphicsService>(); _scene = _services.GetInstance <IScene>(); var content = _services.GetInstance <ContentManager>(); var gameObjectService = _services.GetInstance <IGameObjectService>(); // Get camera game object. _cameraObject = (CameraObject)gameObjectService.Objects["Camera"]; // Create SkyNodes. InitializeSky(content); // Create LightNodes InitializeLights(); // Optionally, the sky is captured into a cube map and the cube map is added // to the scene instead of all the sky nodes. if (_cacheSky) { // We use a SceneCaptureNode to create the cube map. // The cube map uses RGBM encoding to store HDR values. var renderToTexture = new RenderToTexture { Texture = new RenderTargetCube( _graphicsService.GraphicsDevice, #if XBOX 512, #else 1024, #endif true, SurfaceFormat.Color, DepthFormat.None), }; var projection = new PerspectiveProjection(); projection.SetFieldOfView(ConstantsF.PiOver2, 1, 1, 10); _sceneCaptureNode = new SceneCaptureNode(renderToTexture) { // Note: The scene is captured at the origin (0, 0, 0). CameraNode = new CameraNode(new Camera(projection)), }; SkyboxNode = new SkyboxNode { Encoding = ColorEncoding.Rgbm, Texture = (TextureCube)renderToTexture.Texture, }; _scene.Children.Add(SkyboxNode); } // The Ephemeris class computes the positions of the sun and the moon as seen // from any place on the earth. _ephemeris = new Ephemeris { // Seattle, Washington //Latitude = 47, //Longitude = 122, //Altitude = 100 // Vienna //Latitude = 48, //Longitude = -16, //Altitude = 0 // Equator Latitude = 0, Longitude = 0, Altitude = 0 }; // Update the positions of sky objects and the lights. UpdateSky(); // Create cube map. if (_cacheSky) { UpdateCubeMap(TimeSpan.Zero); } // Add GUI controls to the Options window. var sampleFramework = _services.GetInstance <SampleFramework>(); var optionsPanel = sampleFramework.AddOptions("Game Objects"); var panel = SampleHelper.AddGroupBox(optionsPanel, "DynamicSkyObject"); SampleHelper.AddSlider( panel, "Time", "F2", 0, 24, (float)Time.TimeOfDay.TotalHours, value => { var time = Time; time = time.Subtract(time.TimeOfDay).Add(TimeSpan.FromHours(value)); Time = time; }); SampleHelper.AddCheckBox( panel, "Enable ambient light", EnableAmbientLight, isChecked => EnableAmbientLight = isChecked); SampleHelper.AddSlider( panel, "Fog sample angle", "F2", 0, ConstantsF.PiOver2, FogSampleAngle, value => FogSampleAngle = value); SampleHelper.AddSlider( panel, "Fog saturation", "F2", 0, 1, FogSaturation, value => FogSaturation = value); //SampleHelper.AddSlider( // panel, // "Fog scattering symmetry R", // "F2", // 0, // 1, // FogScatteringSymmetry.X, // value => FogScatteringSymmetry = new Vector3F(value, FogScatteringSymmetry.Y, FogScatteringSymmetry.Z)); //SampleHelper.AddSlider( // panel, // "Fog scattering symmetry G", // "F2", // 0, // 1, // FogScatteringSymmetry.Y, // value => FogScatteringSymmetry = new Vector3F(FogScatteringSymmetry.X, value, FogScatteringSymmetry.Z)); //SampleHelper.AddSlider( // panel, // "Fog scattering symmetry B", // "F2", // 0, // 1, // FogScatteringSymmetry.Z, // value => FogScatteringSymmetry = new Vector3F(FogScatteringSymmetry.X, FogScatteringSymmetry.Y, value)); }
/// <summary> /// Renders the environment maps for the image-based lights. /// </summary> /// <remarks> /// This method uses the current DeferredGraphicsScreen to render new environment maps at /// runtime. The DeferredGraphicsScreen has a SceneCaptureRenderer which we can use to /// capture environment maps of the current scene. /// To capture new environment maps the flag _updateEnvironmentMaps must be set to true. /// When this flag is set, SceneCaptureNodes are added to the scene. When the graphics /// screen calls the SceneCaptureRenderer the next time, the new environment maps will be /// captured. /// The flag _updateEnvironmentMaps remains true until the new environment maps are available. /// This method checks the SceneCaptureNode.LastFrame property to check if new environment maps /// have been computed. Usually, the environment maps will be available in the next frame. /// (However, the XNA Game class can skip graphics rendering if the game is running slowly. /// Then we would have to wait more than 1 frame.) /// When environment maps are being rendered, the image-based lights are disabled to capture /// only the scene with ambient and directional lights. Dynamic objects are also disabled /// to capture only the static scene. /// </remarks> private void UpdateEnvironmentMaps() { if (!_updateEnvironmentMaps) { return; } // One-time initializations: if (_sceneCaptureNodes[0] == null) { // Create cube maps and scene capture nodes. // (Note: A cube map size of 256 is enough for surfaces with a specular power // in the range [0, 200000].) for (int i = 0; i < _sceneCaptureNodes.Length; i++) { var renderTargetCube = new RenderTargetCube( GraphicsService.GraphicsDevice, 256, true, SurfaceFormat.Color, DepthFormat.None); var renderToTexture = new RenderToTexture { Texture = renderTargetCube }; var projection = new PerspectiveProjection(); projection.SetFieldOfView(ConstantsF.PiOver2, 1, 1, 100); _sceneCaptureNodes[i] = new SceneCaptureNode(renderToTexture) { CameraNode = new CameraNode(new Camera(projection)) { PoseWorld = _lightNodes[i].PoseWorld, }, }; _imageBasedLights[i].Texture = renderTargetCube; } // We use a ColorEncoder to encode a HDR image in a normal Color texture. _colorEncoder = new ColorEncoder(GraphicsService) { SourceEncoding = ColorEncoding.Rgb, TargetEncoding = ColorEncoding.Rgbm, }; // The SceneCaptureRenderer has a render callback which defines what is rendered // into the scene capture render targets. _graphicsScreen.SceneCaptureRenderer.RenderCallback = context => { var graphicsDevice = GraphicsService.GraphicsDevice; var renderTargetPool = GraphicsService.RenderTargetPool; // Get scene nodes which are visible by the current camera. CustomSceneQuery sceneQuery = context.Scene.Query <CustomSceneQuery>(context.CameraNode, context); // The final image has to be rendered into this render target. var ldrTarget = context.RenderTarget; // Use an intermediate HDR render target with the same resolution as the final target. var format = new RenderTargetFormat(ldrTarget) { SurfaceFormat = SurfaceFormat.HdrBlendable, DepthStencilFormat = DepthFormat.Depth24Stencil8 }; var hdrTarget = renderTargetPool.Obtain2D(format); graphicsDevice.SetRenderTarget(hdrTarget); context.RenderTarget = hdrTarget; // Render scene (without post-processing, without lens flares, no debug rendering, no reticle). _graphicsScreen.RenderScene(sceneQuery, context, false, false, false, false); // Convert the HDR image to RGBM image. context.SourceTexture = hdrTarget; context.RenderTarget = ldrTarget; _colorEncoder.Process(context); context.SourceTexture = null; // Clean up. renderTargetPool.Recycle(hdrTarget); context.RenderTarget = ldrTarget; }; } if (_sceneCaptureNodes[0].Parent == null) { // Add the scene capture nodes to the scene. for (int i = 0; i < _sceneCaptureNodes.Length; i++) { _graphicsScreen.Scene.Children.Add(_sceneCaptureNodes[i]); } // Remember the old time stamp of the nodes. _oldEnvironmentMapTimeStamp = _sceneCaptureNodes[0].LastFrame; // Disable all lights except ambient and directional lights. // We do not capture the image-based lights or any other lights (e.g. point lights) // in the cube map. foreach (var lightNode in _graphicsScreen.Scene.GetDescendants().OfType <LightNode>()) { lightNode.IsEnabled = (lightNode.Light is AmbientLight) || (lightNode.Light is DirectionalLight); } // Disable dynamic objects. foreach (var node in _graphicsScreen.Scene.GetDescendants()) { if (node is MeshNode || node is LodGroupNode) { if (!node.IsStatic) { node.IsEnabled = false; } } } } else { // The scene capture nodes are part of the scene. Check if they have been // updated. if (_sceneCaptureNodes[0].LastFrame != _oldEnvironmentMapTimeStamp) { // We have new environment maps. Restore the normal scene. for (int i = 0; i < _sceneCaptureNodes.Length; i++) { _graphicsScreen.Scene.Children.Remove(_sceneCaptureNodes[i]); } _updateEnvironmentMaps = false; foreach (var lightNode in _graphicsScreen.Scene.GetDescendants().OfType <LightNode>()) { lightNode.IsEnabled = true; } foreach (var node in _graphicsScreen.Scene.GetDescendants()) { if (node is MeshNode || node is LodGroupNode) { if (!node.IsStatic) { node.IsEnabled = true; } } } } } }
void Start() { _humans = new Dictionary <string, Human>(); _projectionScript = Camera.main.GetComponent <PerspectiveProjection>(); }
public FacialAnimationSample(Microsoft.Xna.Framework.Game game) : base(game) { _graphicsScreen = new DeferredGraphicsScreen(Services) { DrawReticle = false }; GraphicsService.Screens.Insert(0, _graphicsScreen); Services.Register(typeof(DebugRenderer), null, _graphicsScreen.DebugRenderer); Services.Register(typeof(IScene), null, _graphicsScreen.Scene); // Add a game object which adds some GUI controls for the deferred graphics // screen to the Options window. GameObjectService.Objects.Add(new DeferredGraphicsOptionsObject(Services)); // Use a fixed camera. var projection = new PerspectiveProjection(); projection.SetFieldOfView( ConstantsF.PiOver4, GraphicsService.GraphicsDevice.Viewport.AspectRatio, 0.1f, 10); var cameraNode = new CameraNode(new Camera(projection)); cameraNode.LookAt(new Vector3F(0.15f, 0.15f, 0.5f), new Vector3F(0.1f, 0.15f, 0), Vector3F.Up); _graphicsScreen.Scene.Children.Add(cameraNode); _graphicsScreen.ActiveCameraNode = cameraNode; // Lighting setup: var keyLight = new LightNode(new Spotlight { DiffuseIntensity = 0.6f, SpecularIntensity = 0.4f }); keyLight.LookAt(new Vector3F(-2, 2, 2), new Vector3F(), Vector3F.Up); _graphicsScreen.Scene.Children.Add(keyLight); var backLight = new LightNode(new Spotlight { DiffuseIntensity = 0.3f, SpecularIntensity = 0.3f }); backLight.LookAt(new Vector3F(1, 0.5f, -2), new Vector3F(), Vector3F.Up); _graphicsScreen.Scene.Children.Add(backLight); var fillLight = new LightNode(new AmbientLight { HemisphericAttenuation = 1, Intensity = 0.1f }); _graphicsScreen.Scene.Children.Add(fillLight); // The scene does not have a proper background. That's why the exposure is a // bit off. --> Reduce the max exposure. var hdrFilter = _graphicsScreen.PostProcessors.OfType <HdrFilter>().First(); hdrFilter.MaxExposure = 6; // Load the customized "Sintel" model (original: Durian Open Movie Project - http://www.sintel.org/). var model = ContentManager.Load <ModelNode>("Sintel/Sintel-Head").Clone(); model.PoseWorld = new Pose(new Vector3F(0, 0, 0), Matrix33F.CreateRotationY(MathHelper.ToRadians(10)) * Matrix33F.CreateRotationX(-MathHelper.ToRadians(90))); _graphicsScreen.Scene.Children.Add(model); // The model consists of a root node and a mesh node. // ModelNode "Sintel-Head" // MeshNode "Sintel" _sintel = (MeshNode)model.Children[0]; // The model contains two skeletal animations: // - "MOUTH-open" is just a single frame. // - "Test" is a short animation (250 frames). // In the Options window, we will add a slider to move the jaw. // Slider.Value = 0 ... mouth closed (default) _mouthClosedPose = SkeletonPose.Create(_sintel.Mesh.Skeleton); // Slider.Value = 1 ... mouth open (copied from the "MOUTH-open" animation) SkeletonKeyFrameAnimation mouthOpen = _sintel.Mesh.Animations["MOUTH-open"]; _mouthOpenPose = SkeletonPose.Create(_sintel.Mesh.Skeleton); mouthOpen.GetValue(TimeSpan.Zero, ref _mouthOpenPose, ref _mouthOpenPose, ref _mouthOpenPose); // Turn the "Test" animation into an endless loop. _skeletalAnimation = new AnimationClip <SkeletonPose>(_sintel.Mesh.Animations["Test"]) { Duration = TimeSpan.MaxValue, LoopBehavior = LoopBehavior.Cycle }; // Mesh has several morph targets for facial animation, which are imported // automatically via the content pipeline. Unfortunately, the XNA content // pipeline cannot import morph target animations automatically. // In this demo, we will create a morph target animation in code. _morphingAnimation = CreateMorphingAnimation(); CreateGuiControls(); }
public SceneCaptureCubeSample(Microsoft.Xna.Framework.Game game) : base(game) { SampleFramework.IsMouseVisible = false; // Create a graphics screen. This screen has to call the SceneCaptureRenderer // to handle the SceneCaptureNodes! _graphicsScreen = new DeferredGraphicsScreen(Services) { DrawReticle = true }; GraphicsService.Screens.Insert(0, _graphicsScreen); GameObjectService.Objects.Add(new DeferredGraphicsOptionsObject(Services)); Services.Register(typeof(DebugRenderer), null, _graphicsScreen.DebugRenderer); Services.Register(typeof(IScene), null, _graphicsScreen.Scene); // Add gravity and damping to the physics Simulation. Simulation.ForceEffects.Add(new Gravity()); Simulation.ForceEffects.Add(new Damping()); // Add a custom game object which controls the camera. var cameraGameObject = new CameraObject(Services); GameObjectService.Objects.Add(cameraGameObject); _graphicsScreen.ActiveCameraNode = cameraGameObject.CameraNode; // More standard objects. GameObjectService.Objects.Add(new GrabObject(Services)); GameObjectService.Objects.Add(new ObjectCreatorObject(Services)); //GameObjectService.Objects.Add(new StaticSkyObject(Services)); GameObjectService.Objects.Add(new DynamicSkyObject(Services, true, false, true)); GameObjectService.Objects.Add(new GroundObject(Services)); GameObjectService.Objects.Add(new DudeObject(Services)); GameObjectService.Objects.Add(new DynamicObject(Services, 1)); GameObjectService.Objects.Add(new DynamicObject(Services, 2)); GameObjectService.Objects.Add(new DynamicObject(Services, 5)); GameObjectService.Objects.Add(new DynamicObject(Services, 6)); GameObjectService.Objects.Add(new DynamicObject(Services, 7)); GameObjectService.Objects.Add(new FogObject(Services) { AttachToCamera = true }); GameObjectService.Objects.Add(new LavaBallsObject(Services)); // Add a few palm trees. Random random = new Random(12345); for (int i = 0; i < 10; i++) { Vector3F position = new Vector3F(random.NextFloat(-3, -8), 0, random.NextFloat(0, -5)); Matrix33F orientation = Matrix33F.CreateRotationY(random.NextFloat(0, ConstantsF.TwoPi)); float scale = random.NextFloat(0.5f, 1.2f); GameObjectService.Objects.Add(new StaticObject(Services, "PalmTree/palm_tree", scale, new Pose(position, orientation))); } // Load the "Bubble" mesh and place it at a fixed position in the scene. var modelNode = ContentManager.Load <ModelNode>("Bubble/Bubble"); var meshNode = modelNode.GetDescendants().OfType <MeshNode>().First().Clone(); meshNode.PoseWorld = new Pose(new Vector3F(0, 1, 0)); _graphicsScreen.Scene.Children.Add(meshNode); // Surface of the mesh should reflect the scene in real-time. Reflections are // created using environment mapping: The scene is rendered into a cube map, // which is then applied to the mesh. // To render the scene into a cube map, we need to define a CameraNode and a // SceneCaptureNode: The CameraNode defines the point from where the scene is // captured. The SceneCaptureNode defines where and in which format the captured // image is needed. // Attach a camera to the center of the mesh. var projection = new PerspectiveProjection(); projection.SetFieldOfView(ConstantsF.PiOver2, 1, 0.1f, 20); var captureCameraNode = new CameraNode(new Camera(projection)); meshNode.Children = new SceneNodeCollection { captureCameraNode }; // Attach a SceneCaptureNode with a cube map render target to the mesh. var renderToTexture = new RenderToTexture { Texture = new RenderTargetCube( GraphicsService.GraphicsDevice, 256, true, SurfaceFormat.Color, DepthFormat.None), }; var sceneCaptureNode = new SceneCaptureNode(renderToTexture) { Shape = meshNode.Shape, CameraNode = captureCameraNode, }; meshNode.Children.Add(sceneCaptureNode); // The bubble model uses a special effect and is rendered in the "AlphaBlend" // render pass. Let's modify the effect parameters to use the created cube map // as the reflection map of the bubble. var effectBinding = meshNode.Mesh.Materials[0]["AlphaBlend"]; effectBinding.Set("ReflectionStrength", 0.5f); effectBinding.Set("RefractionStrength", 0.0f); effectBinding.Set("FresnelBias", 1.0f); effectBinding.Set("BlendMode", 1.0f); effectBinding.Set("Alpha", 1.0f); effectBinding.Set("CustomEnvironmentMap", (TextureCube)renderToTexture.Texture); }
public void RandomizedMassSceneUpdate2() { const int NumberOfSceneNodes = 10; const int NumberOfSteps = 10000; const float WorldSize = 1000; var random = new Random(123457); var scene = new Scene(); scene.EnableMultithreading = false; var nodes = new TestSceneNode[NumberOfSceneNodes]; // Create random nodes. for (int i = 0; i < NumberOfSceneNodes; i++) { var node = new TestSceneNode(); nodes[i] = node; var position = random.NextVector3F(0, WorldSize); var orientation = random.NextQuaternionF(); node.PoseLocal = new Pose(position, orientation); float p = random.NextFloat(0, 100); if (p < 0.1f) { node.Shape = Shape.Empty; } else if (p < 0.2f) { node.Shape = Shape.Infinite; } else if (p < 0.6f) { node.Shape = new BoxShape(random.NextVector3F(0, WorldSize)); } else { node.Shape = new SphereShape(random.NextFloat(0, WorldSize)); } } var projection = new PerspectiveProjection(); projection.SetFieldOfView(0.8f, 1, WorldSize / 10000, WorldSize); var camera = new Camera(projection); var cameraNode = new CameraNode(camera); for (int updateIndex = 0; updateIndex < NumberOfSteps; updateIndex++) { int actionsPerFrame = random.NextInteger(0, 100); for (int i = 0; i < actionsPerFrame; i++) { var node = nodes[random.Next(0, NumberOfSceneNodes)]; const int numberOfActions = 100; int action = random.Next(0, numberOfActions); //scene.Validate(); if (action == 0) { // Add if (node.Parent == null) { scene.Children.Add(node); //scene.Validate(); } } else if (action == 1) { // Remove if (node.Parent != null) { node.Parent.Children.Remove(node); //scene.Validate(); } } else if (action == 2) { // Move var pose = node.PoseWorld; pose.Position = random.NextVector3F(0, WorldSize); node.PoseWorld = pose; //scene.Validate(); } else if (action == 3) { // Very small Move var pose = node.PoseWorld; const float maxDistance = WorldSize / 10000; pose.Position += random.NextVector3F(-maxDistance, maxDistance); node.PoseWorld = pose; //scene.Validate(); } else if (action == 4) { // Small Move var pose = node.PoseWorld; const float maxDistance = WorldSize / 100; pose.Position += random.NextVector3F(-maxDistance, maxDistance); node.PoseWorld = pose; //scene.Validate(); } else if (action == 5) { // Scale node.ScaleLocal = random.NextVector3F(0.0f, 10f); //scene.Validate(); } else if (action == 6) { // Rotate node.PoseWorld = new Pose(node.PoseWorld.Position, random.NextQuaternionF()); //scene.Validate(); } else if (action == 7) { // Query var query = scene.Query <CameraFrustumQuery>(cameraNode, null); //Debug.WriteLine("Camera queried nodes: " + query.SceneNodes.Count); //scene.Validate(); } else if (action == 8) { // Move camera. cameraNode.PoseWorld = new Pose(random.NextVector3F(0, WorldSize), random.NextQuaternionF()); //scene.Validate(); } else if (action == 9) { // Change shape. int type = random.NextInteger(0, 5); if (type == 0) { node.Shape = new BoxShape(random.NextVector3F(0, WorldSize)); } else if (type == 1) { node.Shape = new SphereShape(random.NextFloat(0, WorldSize)); } else if (type == 2) { node.Shape = new BoxShape(new Vector3F(Single.MaxValue)); } else if (type == 3) { node.Shape = new BoxShape(new Vector3F(0)); } else if (type == 4) { node.Shape = Shape.Empty; } //scene.Validate(); } else if (action == 10) { // Add to random parent. if (node.Parent == null) { var randomParent = nodes[random.NextInteger(0, NumberOfSceneNodes - 1)]; // Avoid loops: bool isLoop = node.GetSubtree().Contains(randomParent); if (!isLoop) { if (randomParent.Children == null) { randomParent.Children = new SceneNodeCollection(); } randomParent.Children.Add(node); } } } else if (action == 11) { if (node.Parent != null && node.Parent != scene) { node.Parent.Children.Remove(node); } } else if (action == 13) { if (random.NextInteger(0, 100) < 5) { scene.Children.Clear(); } } else if (action == 14) { if (random.NextInteger(0, 100) < 5) { scene.Children = new SceneNodeCollection(); } } } //Debug.WriteLine("Number of nodes in scene: " + scene.GetDescendants().Count()); //scene.Validate(); scene.Update(TimeSpan.FromSeconds(0.016666666f)); //scene.Validate(); } }