Skip to content

Commit

Permalink
Fix warnings in Rider.
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelbigos committed Apr 9, 2022
1 parent 56bffc8 commit fb14da7
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 166 deletions.
6 changes: 3 additions & 3 deletions Assets/Scenes/SDFDemo.unity
Original file line number Diff line number Diff line change
Expand Up @@ -811,8 +811,8 @@ MonoBehaviour:
SdfResolution: 32
FPS: 0
AOSamples: 8
AOKernelSize: 0.075
AOKernelSize: 0.05
MaxRaymarchSteps: 128
SunPos: {x: 0, y: 10, z: 0}
ShadowIntensity: 0.5
ShadowSoftness: 0.5
ShadowIntensity: 0.66
ShadowSoftness: 0.9
52 changes: 23 additions & 29 deletions Assets/Scripts/Boids/BoidController.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System;
using System.Collections.Generic;
using SDF;
using UImGui;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Serialization;
using Random = UnityEngine.Random;

namespace Boids
{
Expand All @@ -14,45 +11,44 @@ public class BoidController : MonoBehaviour
public struct Boid
{
public Vector3 Position;
public Vector3 Velocity;
public float Radius;
}

[SerializeField] private Vector3 _planetCentre = new Vector3(0.0f, 0.0f, 0.0f);
[SerializeField] private SDFRenderer _sdfRendererInstance;
[FormerlySerializedAs("_sdfGenInstance")] [SerializeField] private SDF.SDF _sdfInstance;
[SerializeField] private ComputeShader _boidPhysicsCompute;
[SerializeField] private DebugWindow _debugWindow;
[SerializeField] private Vector3 PlanetCentre = new Vector3(0.0f, 0.0f, 0.0f);
[SerializeField] private SDFRenderer SDFRendererInstance;
[SerializeField] private SDF.SDF SDFInstance;
[SerializeField] private ComputeShader BoidPhysicsCompute;
[SerializeField] private DebugWindow DebugWindow;

private readonly List<Boid> _boids = new List<Boid>();
private int _playerBoidID;

private void Start()
{
_boidPhysicsCompute.SetVector("_planetCentre", _planetCentre);
_boidPhysicsCompute.SetFloat("_gravity", 1.0f);
BoidPhysicsCompute.SetVector("_planetCentre", PlanetCentre);
BoidPhysicsCompute.SetFloat("_gravity", 1.0f);
}

private void PhysicsCompute()
{
int kernelID = _boidPhysicsCompute.FindKernel("Step");
_boidPhysicsCompute.GetKernelThreadGroupSizes(kernelID, out uint _, out uint _, out uint _);
int kernelID = BoidPhysicsCompute.FindKernel("Step");
BoidPhysicsCompute.GetKernelThreadGroupSizes(kernelID, out uint _, out uint _, out uint _);

_boidPhysicsCompute.SetInt("_numBoids", _boids.Count);
BoidPhysicsCompute.SetInt("_numBoids", _boids.Count);

ComputeBuffer boidBufferIn = new ComputeBuffer(_boids.Count, 7 * 4);
boidBufferIn.SetData(_boids);
_boidPhysicsCompute.SetBuffer(kernelID, "_boidBufferIn", boidBufferIn);
BoidPhysicsCompute.SetBuffer(kernelID, "_boidBufferIn", boidBufferIn);

ComputeBuffer boidBufferOut = new ComputeBuffer(_boids.Count, 7 * 4);
boidBufferOut.SetData(_boids);
_boidPhysicsCompute.SetBuffer(kernelID, "_boidBufferOut", boidBufferOut);
BoidPhysicsCompute.SetBuffer(kernelID, "_boidBufferOut", boidBufferOut);

_boidPhysicsCompute.SetFloat("_timeStep", Time.deltaTime);
_boidPhysicsCompute.SetFloat("_gravity", _debugWindow.Gravity);
_boidPhysicsCompute.SetTexture(kernelID, "_sdfTexIn", _sdfInstance.SDFTexture);
BoidPhysicsCompute.SetFloat("_timeStep", Time.deltaTime);
BoidPhysicsCompute.SetFloat("_gravity", DebugWindow.Gravity);
BoidPhysicsCompute.SetTexture(kernelID, "_sdfTexIn", SDFInstance.SDFTexture);

_boidPhysicsCompute.Dispatch(kernelID, _boids.Count, 1, 1);
BoidPhysicsCompute.Dispatch(kernelID, _boids.Count, 1, 1);

Boid[] boidArray = new Boid[_boids.Count];
boidBufferOut.GetData(boidArray);
Expand All @@ -68,25 +64,25 @@ private void PhysicsCompute()

private void Update()
{
_sdfInstance.SetSDFParams(_boidPhysicsCompute);
SDFInstance.SetSDFParams(BoidPhysicsCompute);

if (_boids.Count > 0)
{
PhysicsCompute();
}
_sdfRendererInstance.SetBoids(_boids);
SDFRendererInstance.SetBoids(_boids);

if (_debugWindow.Spawn)
if (DebugWindow.Spawn)
{
_debugWindow.Spawn = false;
DebugWindow.Spawn = false;
DoSpawn();
}

if (_debugWindow.Reset)
if (DebugWindow.Reset)
{
_debugWindow.Reset = false;
DebugWindow.Reset = false;
_boids.Clear();
_sdfInstance.Reset();
SDFInstance.Reset();
}
}

Expand All @@ -100,11 +96,9 @@ public void Spawn(InputAction.CallbackContext context)

public void DoSpawn()
{
float impulse = 0.25f;
Boid newBoid = new()
{
Position = new Vector4(0.0f, 1.5f, 0.0f),
Velocity = new Vector4(_debugWindow.Gravity * impulse * Random.Range(-1.0f, 1.0f), 0.0f, _debugWindow.Gravity * impulse * Random.Range(-1.0f, 1.0f)),
Radius = 0.066f
};

Expand Down
30 changes: 15 additions & 15 deletions Assets/Scripts/Camera/OrbitCameraController.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class OrbitCameraController : MonoBehaviour
{
[SerializeField] private Camera _camera;

public float MinDistance = 25.0f;
public float MaxDistance = 200.0f;
public Camera Camera;
public float ZoomSpeed = 1.0f;
public float ZoomDeceleration = 20.0f;
public float PanSpeed = 0.1f;
Expand All @@ -22,8 +17,14 @@ public class OrbitCameraController : MonoBehaviour
private Vector2 _velocity;
private float _zoomVelocity;
private bool _mouseDown;
private Transform _transform;

private void Awake()
{
_transform = transform;
}

void Update()
private void Update()
{
if (_mouseDown)
{
Expand All @@ -35,13 +36,14 @@ void Update()
_velocity = Vector2.Lerp(_velocity, new Vector2(0.0f, 0.0f), Time.deltaTime * Deceleration);

Vector3 newPos = transform.localPosition;
newPos += new Vector3(0.0f, 0.0f, _zoomVelocity * Mathf.Abs(transform.localPosition.z));
newPos += new Vector3(0.0f, 0.0f, _zoomVelocity * Mathf.Abs(_transform.localPosition.z));
newPos.z = Mathf.Clamp(newPos.z, -MaxZoom, -MinZoom);
transform.localPosition = newPos;
_transform.localPosition = newPos;
_zoomVelocity = Mathf.Lerp(_zoomVelocity, 0.0f, Time.deltaTime * ZoomDeceleration);

_camera.transform.position = transform.position;
_camera.transform.rotation = transform.rotation;

Transform camTransform = Camera.transform;
camTransform.position = _transform.position;
camTransform.rotation = _transform.rotation;
}

public void MouseMove(InputAction.CallbackContext context)
Expand All @@ -58,8 +60,6 @@ public void RightMouse(InputAction.CallbackContext context)
case InputActionPhase.Canceled:
_mouseDown = false;
break;
default:
break;
}
}

Expand All @@ -71,6 +71,6 @@ public void MouseWheel(InputAction.CallbackContext context)
value = Vector2.ClampMagnitude(value, 1.0f);
_zoomVelocity += value.y * ZoomSpeed;
}

}
}
32 changes: 13 additions & 19 deletions Assets/Scripts/Camera/WasdCameraController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

Expand All @@ -9,16 +7,18 @@ public class WasdCameraController : MonoBehaviour
public float LookSpeed = 0.5f;
public float Deceleration = 5.0f;

public Camera _Camera;
public Camera Camera;

private Vector3 _velocity;
private bool _mouseDown;
private Transform _transform;
private Transform _camTransform;

void Start()
{
_Camera.depthTextureMode = DepthTextureMode.Depth;
_Camera.transform.position = transform.position;
_Camera.transform.rotation = transform.rotation;
Camera.depthTextureMode = DepthTextureMode.Depth;
_camTransform.position = _transform.position;
_camTransform.rotation = _transform.rotation;
}

void Update()
Expand All @@ -27,8 +27,8 @@ void Update()

if (_mouseDown)
{
Vector3 foward = _Camera.transform.forward;
Vector3 right = _Camera.transform.right;
Vector3 foward = _camTransform.forward;
Vector3 right = _camTransform.right;

if (Keyboard.current.wKey.isPressed)
dir += foward;
Expand All @@ -49,23 +49,19 @@ void Update()
_velocity = Vector3.Lerp(_velocity, new Vector3(0.0f, 0.0f, 0.0f), Mathf.Min(Time.deltaTime, 1.0f / 60.0f) * Deceleration);

Vector3 newPos = transform.localPosition;
transform.localPosition = newPos + _velocity * Time.deltaTime * 120.0f;
_transform.localPosition = newPos + _velocity * Time.deltaTime * 120.0f;

if (_mouseDown)
{
Vector2 mouse = Pointer.current.delta.ReadValue();
transform.Rotate(new Vector3(0.0f, 1.0f, 0.0f), mouse.x * LookSpeed, Space.World);
transform.Rotate(transform.right, -mouse.y * LookSpeed, Space.World);
transform.Rotate(_transform.right, -mouse.y * LookSpeed, Space.World);
}

_Camera.transform.position = transform.position;
_Camera.transform.rotation = transform.rotation;
_camTransform.position = _transform.position;
_camTransform.rotation = _transform.rotation;
}

public void MouseMove(InputAction.CallbackContext context)
{
}


public void RightMouse(InputAction.CallbackContext context)
{
switch (context.phase)
Expand All @@ -76,8 +72,6 @@ public void RightMouse(InputAction.CallbackContext context)
case InputActionPhase.Canceled:
_mouseDown = false;
break;
default:
break;
}
}
}
Loading

0 comments on commit fb14da7

Please sign in to comment.