add mirror

This commit is contained in:
2023-06-01 20:58:30 +05:00
parent ab92899cb0
commit 59aa0639d0
72 changed files with 16053 additions and 504 deletions
+32 -2
View File
@@ -19,6 +19,12 @@ public class AnimationHandler : MonoBehaviour
_scales = new Dictionary<string, Vector3>();
_visualizer.OnDetectionStatusChanged += OnFaceDetected;
// transform.DOLocalRotate(new Vector3(0, -360f, 0), 15f, RotateMode.FastBeyond360)
// .SetLoops(-1, LoopType.Restart)
// .SetRelative()
// .SetEase(Ease.Linear);
foreach(var item in _objects)
{
int rotateDir = Random.Range(-4, 5);
@@ -42,7 +48,31 @@ public class AnimationHandler : MonoBehaviour
private void OnFaceDetected(bool isDetected)
{
foreach(var item in _objects)
item.DOScale(isDetected ? _scales[item.name] : Vector3.zero, Random.Range(.2f, .8f));
Sequence sq = DOTween.Sequence();
if(isDetected)
{
foreach(var item in _objects)
sq.Append(item.DOScale(_scales[item.name], Random.Range(.2f, .4f)));
sq.Play();
}
else
{
if(sq.IsPlaying()) sq.Kill();
foreach(var item in _objects)
item.DOScale(Vector3.zero, .2f);
}
}
private void ChangeScale(int index)
{
if (index == _objects.Length)
{
return;
}
_objects[index].DOScale(_scales[_objects[index].name], Random.Range(.2f, .4f))
.OnComplete(() => ChangeScale(++index));
}
}
+44
View File
@@ -0,0 +1,44 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Zenject;
using DG.Tweening;
using UltraFace;
public class MirrorAnimationHandler : MonoBehaviour
{
[Inject] private Visualizer _visualizer;
[SerializeField] private Material _material;
[SerializeField] private Color _baseColor;
private void Start() {
_material.color = Color.white;
}
public void Init()
{
_material.DOColor(_baseColor, 0);
_visualizer.OnDetectionStatusChanged += isDetected =>
{
if(isDetected)
{
Debug.Log("хуй");
transform.DOLocalRotate(Vector3.up * (360f - 35f), 1f)
.SetRelative()
.SetEase(Ease.InOutSine)
.SetLoops(1)
.OnComplete(() => _material.DOColor(Color.white, .5f));
}
else
{
transform.DOLocalRotate(Vector3.up * (360f + 35f), 1f)
.SetRelative()
.SetLoops(1)
.SetEase(Ease.InOutSine);
_material.DOColor(_baseColor, .5f);
}
};
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f7bbb20227be0d44ebf2f987b6c2c378
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
-2
View File
@@ -29,8 +29,6 @@ public class ImageCanvasHandler : MonoBehaviour
{
_canvasGroup.DOFade(0, .25f);
}
_rectTransform.DORotate(_rectTransform.eulerAngles + Vector3.up * 360, .5f);
}
private void OnDestroy()
+12 -3
View File
@@ -11,6 +11,8 @@ public class SettingsUI : MonoBehaviour
[Inject] private Visualizer _visualizer;
[Inject] private DetectionSetup _detectionSetup;
[SerializeField] private bool _initOnStart;
[SerializeField] private TMP_Text _faceCounterText;
[SerializeField] private TMP_Text _minFaceHeight;
[SerializeField] private TMP_Text _maxFaceHeight;
@@ -18,14 +20,21 @@ public class SettingsUI : MonoBehaviour
private void Start()
{
if(_initOnStart)
{
OnAcceptButtonClick(0.134f);
return;
}
_acceptButton.onClick.AddListener(() => OnAcceptButtonClick());
}
private void OnAcceptButtonClick()
private void OnAcceptButtonClick(float min = -1f)
{
_detectionSetup.SetMinFaceHeight(float.Parse(_maxFaceHeight.text));
Debug.LogWarning($"минимальная дистанция - {_maxFaceHeight.text}");
if(min == -1f) _detectionSetup.SetMinFaceHeight(float.Parse(_maxFaceHeight.text));
else _detectionSetup.SetMinFaceHeight(min);
_visualizer.Init();
FindAnyObjectByType<MirrorAnimationHandler>().Init();
Destroy(gameObject);
}
+63 -28
View File
@@ -5,6 +5,7 @@ using Klak.TestTools;
using System.Linq;
using System.Collections.Generic;
using Zenject;
using BodyPix;
namespace UltraFace
{
@@ -12,34 +13,50 @@ namespace UltraFace
{
#region Editable attributes
[SerializeField] bool _useBodyTracking;
[SerializeField] ImageSource _source = null;
[SerializeField, Range(0, 1)] float _threshold = 0.5f;
[SerializeField] ResourceSet _resources = null;
[SerializeField] ResourceSet _faceResources = null;
[SerializeField] Shader _visualizer = null;
[SerializeField] Texture2D _texture = null;
[SerializeField] UI.RawImage _previewUI = null;
[SerializeField] UI.RawImage _facePreviewUI = null;
FaceDetector _faceDetector;
Material _faceMaterial;
ComputeBuffer _drawArgs;
[Space(16)]
[SerializeField] Shader _bodyShader = null;
[SerializeField] RenderTexture _background = null;
[SerializeField] BodyPix.ResourceSet _bodyResources;
[SerializeField] UI.RawImage _bodyPreviewUI = null;
[SerializeField] float _bodyThreshold;
BodyDetector _bodyDetector;
Material _bodyMaterial;
[Inject] private DetectionSetup _detectionSetup;
public int faceCounter => _detector.Detections.Count();
public int faceCounter => _faceDetector.Detections.Count();
public float minFaceHeight
{
get
{
if(_detector.Detections.Count() == 0)
if(_faceDetector.Detections.Count() == 0)
return 0f;
else
return _detector.Detections.Min(face => face.y2 - face.y1);
return _faceDetector.Detections.Min(face => face.y2 - face.y1);
}
}
public float maxFaceHeight
{
get
{
if(_detector.Detections.Count() == 0)
if(_faceDetector.Detections.Count() == 0)
return 0f;
else
return _detector.Detections.Max(face => face.y2 - face.y1);
return _faceDetector.Detections.Max(face => face.y2 - face.y1);
}
}
@@ -49,23 +66,25 @@ namespace UltraFace
#endregion
#region Private objects
FaceDetector _detector;
Material _material;
ComputeBuffer _drawArgs;
#endregion
#region MonoBehaviour implementation
void Start()
{
_detector = new FaceDetector(_resources);
_material = new Material(_visualizer);
_faceDetector = new FaceDetector(_faceResources);
_faceMaterial = new Material(_visualizer);
_drawArgs = new ComputeBuffer(4, sizeof(uint), ComputeBufferType.IndirectArguments);
_drawArgs.SetData(new [] {6, 0, 0, 0});
_detectedCounter = new List<bool>();
_bodyDetector = new BodyDetector(_bodyResources, 320, 240);
_bodyMaterial = new Material(_bodyShader);
_bodyPreviewUI.material = _bodyMaterial;
if(!_useBodyTracking)
{
_facePreviewUI.gameObject.SetActive(true);
_bodyPreviewUI.gameObject.SetActive(false);
}
}
public void Init()
@@ -75,8 +94,18 @@ namespace UltraFace
void Update()
{
_detector.ProcessImage(_source.Texture, _threshold);
_previewUI.texture = _source.Texture;
if(_useBodyTracking)
{
_bodyDetector.ProcessImage(_source.Texture);
_bodyMaterial.SetTexture("_BgTexture", _background);
_bodyMaterial.SetTexture("_CameraTexture", _source.Texture);
_bodyMaterial.SetTexture("_MaskTexture", _bodyDetector.MaskTexture);
_bodyMaterial.SetFloat("_Threshold", _bodyThreshold);
_bodyPreviewUI.texture = _source.Texture;
}
_faceDetector.ProcessImage(_source.Texture, _threshold);
_facePreviewUI.texture = _source.Texture;
if(!_detectionSetup.isValidate) return;
@@ -89,15 +118,15 @@ namespace UltraFace
{
if(maxFaceHeight < _detectionSetup.minFaceHeight) return;
_isDetected = true;
OnDetectionStatusChanged?.Invoke(_isDetected);
Debug.LogWarning("лицо определено");
}
else if(_isDetected && trueCounter < 64)
{
_isDetected = false;
OnDetectionStatusChanged?.Invoke(_isDetected);
Debug.LogWarning("лицо потеряно");
}
OnDetectionStatusChanged?.Invoke(_isDetected);
_detectedCounter = new List<bool>();
}
@@ -109,19 +138,25 @@ namespace UltraFace
{
return;
_detector.SetIndirectDrawCount(_drawArgs);
_material.SetFloat("_Threshold", _threshold);
_material.SetTexture("_Texture", _texture);
_material.SetBuffer("_Detections", _detector.DetectionBuffer);
_material.SetPass(_texture == null ? 0 : 1);
_faceDetector.SetIndirectDrawCount(_drawArgs);
_faceMaterial.SetFloat("_Threshold", _threshold);
_faceMaterial.SetTexture("_Texture", _texture);
_faceMaterial.SetBuffer("_Detections", _faceDetector.DetectionBuffer);
_faceMaterial.SetPass(_texture == null ? 0 : 1);
Graphics.DrawProceduralIndirectNow(MeshTopology.Triangles, _drawArgs, 0);
}
void OnDestroy()
{
_detector?.Dispose();
Destroy(_material);
_faceDetector?.Dispose();
Destroy(_faceMaterial);
_drawArgs?.Dispose();
_bodyDetector?.Dispose();
_bodyDetector = null;
Destroy(_bodyMaterial);
_bodyMaterial = null;
}
#endregion