Skip to content
This repository was archived by the owner on May 26, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,39 @@ MigrationBackup/

# Ionide (cross platform F# VS Code tools) working folder
.ionide/


# Common IntelliJ Platform excludes

# User specific
**/.idea/**/workspace.xml
**/.idea/**/tasks.xml
**/.idea/shelf/*
**/.idea/dictionaries
**/.idea/httpRequests/

# Sensitive or high-churn files
**/.idea/**/dataSources/
**/.idea/**/dataSources.ids
**/.idea/**/dataSources.xml
**/.idea/**/dataSources.local.xml
**/.idea/**/sqlDataSources.xml
**/.idea/**/dynamic.xml

# Rider
# Rider auto-generates .iml files, and contentModel.xml
**/.idea/**/*.iml
**/.idea/**/contentModel.xml
**/.idea/**/modules.xml

*.suo
*.user
.vs/
[Bb]in/
[Oo]bj/
_UpgradeReport_Files/
[Pp]ackages/

Thumbs.db
Desktop.ini
.DS_Store
38 changes: 1 addition & 37 deletions Graphics/Shared/CharaController.cs
Original file line number Diff line number Diff line change
@@ -1,49 +1,13 @@
using KKAPI;
using KKAPI.Chara;
using System.Collections;
using AIChara;
using UnityEngine;

namespace Graphics
{
// TODO: Optimize Character Focus Controller... Somehow adding male is killing fps!?
internal class CharaController : CharaCustomFunctionController
{
protected override void OnCardBeingSaved(GameMode currentGameMode)
{
//no action
}

protected override void OnReload(GameMode currentGameMode)
{
StartCoroutine(LoadColliders(currentGameMode));
}

private IEnumerator LoadColliders(GameMode currentGameMode)
{
if (GameMode.Maker == currentGameMode || GameMode.Studio == currentGameMode)
ChaControl.LoadHitObject();

yield return new WaitUntil(() => null != ChaControl.objHitBody && null != ChaControl.objHitHead);
ForceColliderUpdate(false);
}

internal void ForceColliderUpdate(bool force)
{
GameObject[] hitObjects = { ChaControl.objHitBody, ChaControl.objHitHead };
foreach (GameObject hitObject in hitObjects)
{
if (ReferenceEquals(null, hitObject))
return;

SkinnedCollisionHelper[] componentsInChildren = hitObject.GetComponentsInChildren<SkinnedCollisionHelper>(true);
foreach (SkinnedCollisionHelper skinnedCollisionHelper in componentsInChildren)
{
skinnedCollisionHelper.gameObject.SetActive(true);
skinnedCollisionHelper.forceUpdate = force;
skinnedCollisionHelper.updateOncePerFrame = !force;
}
}
}
}
}
}
185 changes: 44 additions & 141 deletions Graphics/Shared/FocusPuller.cs
Original file line number Diff line number Diff line change
@@ -1,79 +1,56 @@
using UnityEngine;
using System.Collections;
using KKAPI;
using Studio;
using UnityEngine;

// Utility scripts for the post processing stack
// https://github.com/keijiro/PostProcessingUtilities
namespace Graphics
{
public class FocusPuller : MonoBehaviour
{
private float _velocity;
internal static float MinSpeed = 1f;
internal static float MaxSpeed = 12f;

private GameObject _customTarget;
private GameObject _focusTargetGameObject;
private float _speed = 6f;
private Vector3 lastDoFPoint;
private bool initialFrame;
private GameObject _ftgo;
private Transform _target;
#if DEBUG
private LineDrawer lineDrawer;
#endif
private float _velocity;

internal LayerMask HitLayer { get; set; }
internal float MaxDistance { get; set; }
internal Vector3 TargetFocusPoint { get; private set; }
internal Vector3 TargetPosition { get => null != _target ? _target.position : Vector3.zero; }
internal Vector3 TransformPosition { get => null != _target ? transform.position : Vector3.zero; }
internal Vector3 HitPoint { get; private set; }
internal Vector3 RayOrigin { get; private set; }
internal Vector3 TargetPosition => null != _target ? _target.position : Vector3.zero;

internal float Speed
{
get => _speed;
set => _speed = Mathf.Max(MinSpeed, value);
}
internal static float MinSpeed = 1f;
internal static float MaxSpeed = 12f;
internal static string ColliderLayer = "H_MetaBallB";
internal static string DefaultLayer = "Default";

private void Awake()
{
_ftgo = new GameObject("FocusTarget");
_target = _ftgo.transform;
HitLayer = CullingMaskExtensions.LayerCullingShow(Graphics.Instance.CameraSettings.MainCamera.cullingMask, ColliderLayer);
HitLayer = CullingMaskExtensions.LayerCullingHide(HitLayer, DefaultLayer);
_focusTargetGameObject = new GameObject("FocusTarget");
_target = _focusTargetGameObject.transform;
MaxDistance = Graphics.Instance.CameraSettings.MainCamera.farClipPlane;
#if DEBUG
lineDrawer = new LineDrawer();
#endif
}

private void OnEnable()
{
initialFrame = true;
}

private void OnValidate()
{
Speed = _speed;
StartCoroutine(FindTarget());
}

private void OnPreRender()
{
if (_target == null) return;

if (initialFrame)
if (_target == null)
{
initialFrame = false;
Focus(new Vector3(Screen.width / 2, Screen.height / 2));
}
else
{
Focus(Input.mousePosition);
return;
}

Focus();

// Retrieve the current value.
float d1 = Graphics.Instance.PostProcessingSettings.FocalDistance;
#if DEBUG
lineDrawer.DrawLineInGameView(transform.position, _target.position, Color.green);
#endif

// Calculate the depth of the focus point.
float d2 = Vector3.Dot(_target.position - transform.position, transform.forward);
if (d2 < 0.1f)
Expand All @@ -83,122 +60,48 @@ private void OnPreRender()

// Damped-spring interpolation.
float dt = Time.deltaTime;
float n1 = _velocity - (d1 - d2) * Speed * Speed * dt;
float n2 = 1 + Speed * dt;
float n1 = _velocity - ((d1 - d2) * Speed * Speed * dt);
float n2 = 1 + (Speed * dt);
_velocity = n1 / (n2 * n2);
float d = d1 + _velocity * dt;
float d = d1 + (_velocity * dt);

// Apply the result.
Graphics.Instance.PostProcessingSettings.FocalDistance = d;
}

private void Focus(Vector3 PointOnScreen)
private void OnValidate()
{
TargetFocusPoint = PointOnScreen;
// our ray
var ray = transform.GetComponent<Camera>().ScreenPointToRay(PointOnScreen);
RayOrigin = ray.origin;
#if DEBUG
lineDrawer.DrawLineInGameView(ray.origin, ray.direction * 100, Color.yellow);
#endif
if (Physics.Raycast(ray, out RaycastHit hit, MaxDistance, HitLayer))
{
HitPoint = hit.point;
#if DEBUG
lineDrawer.DrawLineInGameView(ray.origin, hit.point, Color.green);
#endif
// do we have a new point?
if (lastDoFPoint == hit.point)
{
return;
}
Speed = _speed;
}

_target.position = hit.point;
// asign the last hit
lastDoFPoint = hit.point;
}
/*
* debug - Do any of the rays hit?
* RaycastHit[] results = new RaycastHit[10];

if (0 < Physics.RaycastNonAlloc(ray, results, MaxDistance))
private IEnumerator FindTarget()
{
while (isActiveAndEnabled)
{
foreach (var result in results)
yield return new WaitForSecondsRealtime(.5f);
_customTarget = null;
foreach (ObjectCtrlInfo info in Singleton<Studio.Studio>.Instance.dicObjectCtrl.Values)
{
// Check for null since some array spots might be
if (result.collider != null)
if (!(info is OCIFolder folder))
{
Graphics.Instance.Log.Log(BepInEx.Logging.LogLevel.All, "hit " + result.collider.gameObject.name);
continue;
}
}
}
else
{
Graphics.Instance.Log.Log(BepInEx.Logging.LogLevel.All, "didn't hit");
}
*/
}
#if DEBUG
public struct LineDrawer
{
private LineRenderer lineRenderer;
private float lineSize;

public LineDrawer(float lineSize = 0.2f)
{
GameObject lineObj = new GameObject("LineObj");
lineRenderer = lineObj.AddComponent<LineRenderer>();
//Particles/Additive
lineRenderer.material = new Material(Shader.Find("Hidden/Internal-Colored"));

this.lineSize = lineSize;
}

private void init(float lineSize = 0.2f)
{
if (lineRenderer == null)
{
GameObject lineObj = new GameObject("LineObj");
lineRenderer = lineObj.AddComponent<LineRenderer>();
//Particles/Additive
lineRenderer.material = new Material(Shader.Find("Hidden/Internal-Colored"));

this.lineSize = lineSize;
}
}

//Draws lines through the provided vertices
public void DrawLineInGameView(Vector3 start, Vector3 end, Color color)
{
if (lineRenderer == null)
{
init(0.2f);
if (folder.name.Equals("DOF Target") && !ReferenceEquals(folder.objectItem, null) && folder.objectItem.activeInHierarchy)
{
_customTarget = folder.objectItem;
}
}

//Set color
lineRenderer.startColor = color;
lineRenderer.endColor = color;

//Set width
lineRenderer.startWidth = lineSize;
lineRenderer.endWidth = lineSize;

//Set line count which is 2
lineRenderer.positionCount = 2;

//Set the postion of both two lines
lineRenderer.SetPosition(0, start);
lineRenderer.SetPosition(1, end);
}
}

public void Destroy()
private void Focus()
{
if (KoikatuAPI.GetCurrentGameMode() == GameMode.Studio)
{
if (lineRenderer != null)
{
UnityEngine.Object.Destroy(lineRenderer.gameObject);
}
// ReSharper disable once MergeConditionalExpression
_target.position = _customTarget != null ? _customTarget.transform.position : Singleton<Studio.Studio>.Instance.cameraCtrl.targetPos;
}
}
#endif
}
}
5 changes: 0 additions & 5 deletions Graphics/Shared/Inspector/PostProcessingInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,14 +341,9 @@ private static void PostProcessVolumeSettings(PostProcessingSettings settings, P

if (showAdvanced)
{
SelectionMask("Focus culling mask", focusPuller.HitLayer, mask => focusPuller.HitLayer = mask);
GUI.enabled = false;
Label("Max Distance", focusPuller.MaxDistance.ToString());
Dimension("Target Focus Point", focusPuller.TargetFocusPoint);
Dimension("Target Position", focusPuller.TargetPosition);
Dimension("Transform Position", focusPuller.TransformPosition);
Dimension("Hit Point", focusPuller.HitPoint);
Dimension("Ray Origin", focusPuller.RayOrigin);
GUI.enabled = true;
}
}
Expand Down