2 using System.Collections.Generic;
3 using System.Diagnostics;
7 using System.Runtime.InteropServices;
8 using System.Threading.Tasks;
9 using System.Xml.Serialization;
10 using SiliconStudio.Paradox;
11 using SiliconStudio.Paradox.Effects;
12 using SiliconStudio.Paradox.Effects.Modules;
13 using SiliconStudio.Paradox.Engine;
14 using SiliconStudio.Paradox.Games;
15 using SiliconStudio.Paradox.Games.Mathematics;
16 using SiliconStudio.Paradox.Games.MicroThreading;
17 using SiliconStudio.Paradox.Configuration;
18 using SiliconStudio.Paradox.Input;
31 bool isModifyingPosition =
false;
32 bool isModifyingFov =
false;
33 Vector3 fixedPosition = Vector3.Zero;
34 Matrix fixedFreeMatrix = Matrix.Identity;
35 bool viewChanged =
true;
39 private Stopwatch clock =
new Stopwatch();
40 private bool flyingAround;
55 public float Speed {
get; set; }
66 [XmlAttribute(
"flyingAround")]
69 [XmlAttribute(
"speed")]
70 public float Speed {
get; set; }
75 bool autoswitch =
false;
79 var cameras = Context.EntityManager.Entities
80 .Where(x => x.ContainsKey(CameraComponent.Key))
81 .Select(x => x.Get(CameraComponent.Key)).ToArray();
83 if (engineContext.InputManager.IsKeyPressed(
Keys.F8))
85 autoswitch = !autoswitch;
98 await TaskEx.Delay((index == 1) ? 50000 : 10000);
116 var config = AppConfig.GetConfiguration<
Config>(
"CameraScript");
117 flyingAround = config.FlyingAround;
127 var zFar = 4000000.0f;
128 if (Context.RenderContext.IsZReverse)
134 camera =
new Camera(
new Vector3(200, 0, 200),
new Vector3(0, 0, 200), 1.2f,
RenderContext.Width,
RenderContext.Height, (
float)
RenderContext.Width /
RenderContext.Height, zNear, zFar);
135 camera.Mode = CameraMode.Free;
138 Speed = config.Speed;
161 var st =
new Stopwatch();
165 var viewParameters = Context.RenderContext.RenderPassPlugins.OfType<
MainPlugin>().FirstOrDefault().ViewParameters;
169 var lastTime = DateTime.UtcNow;
170 var pauseTime =
false;
173 await Scheduler.Current.NextFrame();
175 var mousePosition = Context.InputManager.MousePosition;
177 if (Context.InputManager.IsMouseButtonPressed(
MouseButton.Right))
179 camera.Mode = CameraMode.Free;
180 isModifyingPosition =
true;
188 fixedFreeMatrix = camera.WorldToCamera;
192 fixedPosition = camera.Position;
195 if (Context.InputManager.IsMouseButtonDown(
MouseButton.Right))
197 var deltaX = mousePosition.X - previousX;
198 var deltaY = mousePosition.Y - previousY;
199 if (isModifyingPosition)
202 yaw += deltaX *
Speed / 1000.0f;
203 pitch += deltaY *
Speed / 1000.0f;
207 camera.Position = (
Vector3)
Vector3.Transform(fixedPosition, Matrix.RotationX(pitch) * Matrix.RotationZ(yaw));
208 Console.WriteLine(camera.Position);
212 camera.WorldToCamera = Camera.YawPitchRoll(camera.Position, fixedFreeMatrix, yaw, -pitch, roll);
219 else if (isModifyingFov)
221 camera.FieldOfView += deltaX / 128.0f;
222 camera.FieldOfView = Math.Max(Math.Min(camera.FieldOfView, (float)Math.PI * 0.9f), 0.01f);
227 if (Context.InputManager.IsMouseButtonReleased(
MouseButton.Right))
229 isModifyingFov =
false;
230 isModifyingPosition =
false;
234 camera.Mode = CameraMode.Target;
238 previousX = mousePosition.X;
239 previousY = mousePosition.Y;
241 if (Context.InputManager.IsKeyDown(
Keys.LeftAlt) && Context.InputManager.IsKeyPressed(Keys.Enter))
242 Context.RenderContext.GraphicsDevice.IsFullScreen = !Context.RenderContext.GraphicsDevice.IsFullScreen;
244 if (Context.InputManager.IsKeyPressed(
Keys.F2))
245 pauseTime = !pauseTime;
247 var currentTime = DateTime.UtcNow;
250 Context.CurrentTime += currentTime - lastTime;
251 viewParameters.Set(GlobalKeys.Time, (float)Context.CurrentTime.TotalSeconds);
252 var timeStep = (float)(currentTime - lastTime).TotalMilliseconds;
253 viewParameters.Set(GlobalKeys.TimeStep, timeStep);
257 Context.RenderContext.IsPaused = pauseTime;
258 lastTime = currentTime;
260 if (Context.InputManager.IsKeyPressed(
Keys.Space))
263 var cameras = Context.EntityManager.Entities
264 .Where(x => x.ContainsKey(CameraComponent.Key))
265 .Select(x => x.Get(CameraComponent.Key)).ToArray();
270 TrackingCamera = (index < cameras.Length) ? cameras[index] : null;
283 Matrix projection, worldToCamera;
286 TrackingCamera.NearPlane = camera.NearClipPlane;
287 TrackingCamera.FarPlane = camera.FarClipPlane;
289 TrackingCamera.Calculate(out projection, out worldToCamera);
291 viewParameters.Set(TransformationKeys.View, worldToCamera);
292 viewParameters.Set(TransformationKeys.Projection, projection);
295 viewParameters.Set(CameraKeys.NearClipPlane, camera.NearClipPlane);
296 viewParameters.Set(CameraKeys.FarClipPlane, camera.FarClipPlane);
297 viewParameters.Set(CameraKeys.FieldOfView, TrackingCamera.VerticalFieldOfView);
300 viewParameters.Set(CameraKeys.Aspect, TrackingCamera.AspectRatio);
301 viewParameters.Set(CameraKeys.FocusDistance, TrackingCamera.FocusDistance);
307 var localPosition =
new Vector3(0, 0, 0);
309 if (Context.InputManager.IsKeyDown(
Keys.Left) || Context.InputManager.IsKeyDown(Keys.A))
311 localPosition.X -= 1.0f;
315 if (Context.InputManager.IsKeyDown(
Keys.Right) || Context.InputManager.IsKeyDown(Keys.D))
317 localPosition.X += 1.0f;
321 if (Context.InputManager.IsKeyDown(
Keys.Up) || Context.InputManager.IsKeyDown(Keys.W))
323 localPosition.Y -= 1.0f;
326 if (Context.InputManager.IsKeyDown(
Keys.Down) || Context.InputManager.IsKeyDown(Keys.S))
328 localPosition.Y += 1.0f;
332 if (Context.InputManager.IsKeyDown(
Keys.R))
337 if (Context.InputManager.IsKeyDown(
Keys.T))
342 var moveSpeedFactor = Context.InputManager.IsKeyDown(Keys.LeftShift) || Context.InputManager.IsKeyDown(
Keys.RightShift) ? 0.1f : 1.0f;
344 localPosition.Normalize();
345 localPosition *= (float)clock.ElapsedTicks * 1000.0f *
MoveSpeed * moveSpeedFactor / Stopwatch.Frequency;
347 localVelocity = localVelocity * 0.9f + localPosition * 0.1f;
349 if (localVelocity.Length() >
MoveSpeed * 0.001f)
353 camera.Position = camera.Position + localVelocity;
357 var leftRightVector = Vector3.Cross(destVector, Vector3.UnitZ);
358 leftRightVector.Normalize();
360 var newPosition = camera.Position - destVector * localVelocity.Y;
361 newPosition = newPosition - leftRightVector * localVelocity.X;
362 camera.Position = newPosition;
374 if (Context.InputManager.IsKeyPressed(
Keys.F))
376 camera.FieldOfView -= 0.1f;
380 if (Context.InputManager.IsKeyPressed(
Keys.G))
382 camera.FieldOfView += 0.1f;
388 viewParameters.Set(TransformationKeys.View, camera.WorldToCamera);
389 viewParameters.Set(TransformationKeys.Projection, camera.Projection);
390 viewParameters.Set(CameraKeys.NearClipPlane, camera.NearClipPlane);
391 viewParameters.Set(CameraKeys.FarClipPlane, camera.FarClipPlane);
392 viewParameters.Set(CameraKeys.FieldOfView, camera.FieldOfView);
395 viewParameters.Set(CameraKeys.Aspect, camera.Aspect);
396 viewParameters.Set(CameraKeys.FocusDistance, 0.0f);
401 if (Context.InputManager.IsKeyPressed(
Keys.I))
403 var worldToCamera = camera.WorldToCamera;
404 var target = (
Vector3)worldToCamera.Column3;
405 Console.WriteLine(
"camera.Position = new R32G32B32_Float({0}f,{1}f,{2}f); camera.Target = camera.Position + new R32G32B32_Float({3}f, {4}f, {5}f);", camera.
Position.X, camera.
Position.Y, camera.
Position.Z, target.X, target.Y, target.Z);
float Height
Gets the height of the camera.
SiliconStudio.Paradox.Games.Mathematics.Vector2 Vector2
Matrix WorldToCamera
Gets or sets the world to camera matrix.
float Width
Gets the width of the camera.
SiliconStudio.Core.IServiceRegistry IServiceRegistry
override async Task Execute()
async Task AutoswitchCamera(EngineContext engineContext)
System.Threading.Tasks.Task Task
CameraMode Mode
Gets or sets the mode of this camera.
Describes the camera projection and view.
Thread-local storage context used during rendering.
Plugin used for the main rendering view.
Vector3 Position
Gets or sets the position.
CameraComponent TrackingCamera
SiliconStudio.Core.Mathematics.Vector3 Vector3
CameraScript(IServiceRegistry registry)
SiliconStudio.Paradox.Input.Keys Keys