Get it on Google Play


Wm뮤 :: 'Unity/C#' 카테고리의 글 목록

블로그 이미지
가끔 그림그리거나 3D모델링하거나
취미로 로봇만드는
퇴직한 전자과 게임프로그래머
2020.3.48f1 , 2022.3.6f1 주로 사용
모카쨩
@Ahzkwid

Recent Comment

Archive


'Unity/C#'에 해당되는 글 63건

  1. 2024.04.18 AutomaticShadowDistance
  2. 2024.03.19 c# Dictionary(딕셔너리) 관련
  3. 2023.12.19 자주 쓰는 DateTime 코드 모음
  4. 2023.09.09 Path 연산
  5. 2022.03.06 csv 사용법
  6. 2022.02.12 Windows DLL경로
  7. 2022.02.10 유니티 폴리곤 처리
  8. 2022.01.22 유니티 메시 관련
2024. 4. 18. 15:40 Unity/C#

 

 

 

https://ahzkwid.booth.pm/items/5632873

 

Automatic Shadow Distance - Wmup - BOOTH

ワールド用です DirectionalLightの影の長さをFPSに応じて自動的に変更するツールです。 動きがきれいじゃないので、キャラクターシャドウ専用に使用するのをお勧めします - Code - https://gist.githu

ahzkwid.booth.pm

여기서 다운 가능

 

 

 

 

 

using UdonSharp;
using UnityEngine;
using UnityEngine.UI;
using VRC.SDKBase;
using VRC.Udon;

[UdonBehaviourSyncMode(BehaviourSyncMode.None)]
public class AutomaticShadowDistance : UdonSharpBehaviour
{
    public Light directionalLight;
    public float frameCheckTimeCycle = 3f;
    int playerLayerIndex = 9;
    public Vector2 targetFps = new Vector2(30, 60);
    public Vector2 defaultShadowDistance = new Vector2(10, 80);
    public Vector2 playerShadowDistance = new Vector2(1,15);

    float tPre = 2;

    void UpdateShadowDistance(float t)
    {
        var defaultShadowDistanceMin = defaultShadowDistance.x;
        var defaultShadowDistanceMax = defaultShadowDistance.y;
        var playerShadowDistanceMin = playerShadowDistance.x;
        var playerShadowDistanceMax = playerShadowDistance.y;


        var targetDistanceDefault = Mathf.Lerp(defaultShadowDistanceMin, defaultShadowDistanceMax, t);
        var targetDistancePlayer = Mathf.Lerp(playerShadowDistanceMin, playerShadowDistanceMax, t);
        
        {
            //QualitySettings.shadowDistance = targetDistanceDefault;
        }
        {
            var ShadowCullDistances = new float[32];
            //ShadowCullDistances[0] = Mathf.Lerp(ShadowCullDistances[0], targetDistanceDefault, 0.1f);
            ShadowCullDistances[0] = targetDistanceDefault;
            ShadowCullDistances[11] = ShadowCullDistances[0];
            ShadowCullDistances[13] = ShadowCullDistances[0];
            ShadowCullDistances[14] = ShadowCullDistances[0];
            ShadowCullDistances[playerLayerIndex] = targetDistancePlayer;
            ShadowCullDistances[10] = ShadowCullDistances[playerLayerIndex];
            ShadowCullDistances[18] = ShadowCullDistances[playerLayerIndex];
            directionalLight.layerShadowCullDistances = ShadowCullDistances;

            //Camera.main.layerCullDistances = ShadowCullDistances;
        }
    }

    LightShadows shadowType = LightShadows.Soft;
    void Start()
    {
        //if (directionalLight==null)
        //{
        //    /*
        //    var lights = FindObjectsOfType<Light>();
        //    foreach (var light in lights)
        //    {
        //        if (light.type==LightType.Directional)
        //        {
        //            directionalLight = light;
        //        }
        //    }
        //    */
        //    /*
        //    var light= FindObjectOfType<Light>();
        //    if (light.type == LightType.Directional)
        //    {
        //        directionalLight = light;
        //    }
        //    */
        //}
        if (directionalLight.shadows != LightShadows.None)
        {
            shadowType = directionalLight.shadows;
        }

        UpdateShadowDistance(0);

        lastCheckTime = 0;
    }
    float lastCheckTime = 0;
    int frameCount = 0;

    public Toggle toggle;
    public Slider slider;
    public GameObject lockPanel;

    public bool alwaysUseShdow = true;
    bool CheckT(float t)
    {
        return ((Mathf.Abs(t - tPre) > 0.1f)
            || ((t == 0) && (tPre < 0.1f))
            || ((t == 1) && (tPre > 0.9f)));
    }
    void Update()
    {
        var useShadowOff = !alwaysUseShdow;

        var toggleManual = toggle;
        lockPanel.SetActive(toggleManual.isOn == false);



        var useShadowOffvalue = 0.1f;
        if (toggleManual.isOn)
        {
            if (slider.value > useShadowOffvalue)
            {
                var t = (slider.value - useShadowOffvalue) * (1f / (1f- useShadowOffvalue));
                if (CheckT(t))
                {
                    UpdateShadowDistance(t);
                    tPre = t;
                }
                if (useShadowOff)
                {
                    if (directionalLight.shadows == LightShadows.None)
                    {
                        directionalLight.shadows = shadowType;
                    }
                }
            }
            else
            {
                if (useShadowOff)
                {
                    if (directionalLight.shadows != LightShadows.None)
                    {
                        directionalLight.shadows = LightShadows.None;
                    }
                }
            }
        }
        else
        {


            frameCount++;

            if (Time.time - lastCheckTime >= frameCheckTimeCycle)
            {
                var fps = 0f;
                if (Time.deltaTime > 0)
                {
                    fps = 1f / Time.deltaTime;
                }
                if (frameCheckTimeCycle > 0.1f)
                {
                    fps = frameCount / frameCheckTimeCycle;
                }


                var min = targetFps.x;
                var max = targetFps.y;
                var t = (Mathf.Clamp(fps, min, max) - min) / (max - min);

                /*
                if ((Mathf.Abs(t - tPre) > 0.1f)
                    || ((t == 0) && (tPre < 0.1f))
                    || ((t == 1) && (tPre > 0.9f)))
                */
                if (CheckT(t))
                {

                    UpdateShadowDistance(t);
                    tPre = t;

                }


                if (Time.time > 10)
                {
                    if (useShadowOff)
                    {
                        if (directionalLight.shadows == LightShadows.None)
                        {
                            if (t > 0.8f)
                            {
                                directionalLight.shadows = shadowType;
                            }
                        }
                        else
                        {
                            if (fps < min / 2)
                            {
                                directionalLight.shadows = LightShadows.None;
                            }
                        }
                    }
                }
                if (directionalLight.shadows == LightShadows.None)
                {
                    slider.value = 0f;
                }
                else
                {
                    slider.value = useShadowOffvalue + t * (1f- useShadowOffvalue);
                }


                frameCount = 0;
                lastCheckTime = Time.time;
            }
        }
    }
}

'Unity > C#' 카테고리의 다른 글

c# Dictionary(딕셔너리) 관련  (0) 2024.03.19
자주 쓰는 DateTime 코드 모음  (0) 2023.12.19
Path 연산  (0) 2023.09.09
posted by 모카쨩
2024. 3. 19. 01:48 Unity/C#

 

ClassList to Dictionary

아래의 경우에는 중복되는 userCode의 경우 price를 전부 병합하여 생성한다

왜냐면 Dictionary는 동일키생성을 허용하지 않기 때문이다

var dictionary = classList.GroupBy(x => x.userCode).ToDictionary(x=>x.First().userCode, x=>x.Sum(x=>x.price));

 

 

 

Dictionary Sort

//오름차순 정렬
dictionary=dictionary.OrderBy(x => x.Value).ToDictionary(x=>x.Key, x => x.Value); //오름차순 정렬
 
//내림차순 정렬
dictionary=dictionary.OrderByDescending(x => x.Value).ToDictionary(x=>x.Key, x => x.Value); //내림차순 정렬

 

 

슬라이스

{
    //슬라이스(길이만큼 자르기)
    var length = 3;
    dictionary = dictionary.Take(length).ToDictionary(x => x.Key, x => x.Value);
}

 

 

중복제거

단 Dictionary는 원래 중복 Key를 허용하지 않기 때문에 Value만 가능하다

dictionary = dictionary.GroupBy(x => x.Value).Select(x => x.First()).ToDictionary(x => x.Key, x => x.Value); //중복제거

'Unity > C#' 카테고리의 다른 글

AutomaticShadowDistance  (0) 2024.04.18
자주 쓰는 DateTime 코드 모음  (0) 2023.12.19
Path 연산  (0) 2023.09.09
posted by 모카쨩
2023. 12. 19. 14:19 Unity/C#

현재시간

long second= System.DateTime.Now.Ticks / 10000000 % 60;
long minute= System.DateTime.Now.Ticks / 10000000/60 % 60;
long hour= System.DateTime.Now.Ticks / 10000000/60/60 % 24;
System.DateTime.NowGetHour(System.DateTime.Now) //24시는 0으로 표기되고 23시는 23시로 나온다
DateTime Date = DateTime.UtcNow.AddYears(-10) //10년전


System.DateTime.Now
//결과:2021-06-08 PM 6:00:44



//Unix시간, 유닉스 타임 스탬프 (UnixTimeStamp)
//DateTime버전 길지만 변환이 쉬움
DateTime date=DateTime.Now;
(Int32)(date.Subtract(new DateTime(1970, 1, 1))).TotalSeconds
//결과: 2021-1-16 (6:12:31 GMT) => 1610788351

//역변환
DateTime date= new DateTime(1970, 1, 1).AddSeconds(unixTime);
//결과: 2021-1-16 (6:12:31 GMT) => 1610788351

//DateTimeOffset버전 짧지만 변환이 어려움
DateTimeOffset.Now.ToUnixTimeSeconds();



var time = System.DateTime.Now;
Debug.Log($"<b>[{time.Hour.ToString("D2")}:{time.Minute.ToString("D2")}:{time.Second.ToString("D2")}.{time.Millisecond.ToString("D3")}]</b>");
//굵은 글씨로 현재시간 시분초(소숫점 포함) 표기
//결과 : [18:17:30.428]

 

 

ticks를 datetime으로

long ticks = System.DateTime.UtcNow.Ticks;
System.DateTime dateTime = new System.DateTime(ticks);
Debug.Log(dateTime);

 

dateTime to int

기준일로부터 68년간 유효하다 (최대 69년)

//68년간 유효
long offsetYears = System.DateTime.MinValue.AddYears(1953).Ticks;
Debug.Log($"offsetYears: {offsetYears}");

long ticks = System.DateTime.Now.Ticks;
Debug.Log($"ticks: {ticks}");

int ticksInt = (int)((ticks- offsetYears)/ 10000000);
Debug.Log($"ticksInt: {ticksInt}");

long intToTicks = (long)ticksInt * 10000000+ offsetYears;
Debug.Log($"intToTicks: {intToTicks}");

System.DateTime dateTime = new System.DateTime(intToTicks);
Debug.Log($"dateTime: {dateTime}");

 

 

datetime 문자열 변환

var dateTimeString = dateTime.ToString();
dateTime= System.DateTime.Parse(dateTimeString);

 

 

DateTime To TimeSpan

원문 : https://stackoverflow.com/questions/17959440/convert-datetime-to-timespan

var timeSpan = System.TimeSpan.FromTicks(dateTime.Ticks);

 

 

DateTime To TotalDays

var timeSpan = System.TimeSpan.FromTicks(dateTime.Ticks);
var totalDays = timeSpan.TotalDays;

 

 

 

DateTime 덧셈


dateTime = dateTime.AddTicks(dateTime2.Ticks);

 

 

 

 

DateTime을 0~1 float로 변환

//오래될수록 1에 가까워짐

//샘플코드는 최대 한달

//7일전 = 0.2258064

//15일전 = 0.483871

//31일전 = 1

var lastUtcTime = System.DateTime.UtcNow.AddDays(-7); //7일전 접속

long min = System.DateTime.MinValue.Ticks;
long max = System.DateTime.MinValue.AddMonths(1).Ticks; //최대 1달
long normalizeTime = System.Math.Max((System.DateTime.UtcNow - lastUtcTime).Ticks, min);
normalizeTime = System.Math.Min(normalizeTime, max);

Debug.Log("마지막 접속으로부터 {(float)(((double)normalizeTime) / max)}개월 지남");

 

 

 

 

한달중 얼마나 지나갔는지 0~1float로 변환

단 day 기준임

v1

더보기
float CalculateDayRatioOfMonth(System.DateTime datetime)
{
    var datetimeAddMonths1 = datetime.AddMonths(1);
    var dayMax = datetimeAddMonths1.AddDays(-datetimeAddMonths1.Day).Day;
    return datetime.Day / dayMax;
}

v2

float GetDayRatioOfMonth(System.DateTime datetime)
{
    return datetime.Day / (float)System.DateTime.DaysInMonth(datetime.Year, datetime.Month);
}

 

 

 

날짜부분만 추출

v1

var date=System.DateTime.Parse(krTime.ToShortDateString());

 

v2

var date = krTime.Date;

 

 

DateTime의 Lerp

ChatGPT가 짜줬다


System.DateTime LerpDateTime(System.DateTime start, System.DateTime end, float t)
{
    var ticksStart = start.Ticks;
    var ticksEnd = end.Ticks;
    var ticksLerp = (long)(ticksStart + (ticksEnd - ticksStart) * (double)t);
    return new System.DateTime(ticksLerp);
}

 

 

한국시간 반환

var krTime = System.DateTime.UtcNow.AddHours(9);

 

 

 

 

 

일몰일출시간 반환

한국기준이고 2022년 기준으로 만들었다

 

 

 

 

'Unity > C#' 카테고리의 다른 글

c# Dictionary(딕셔너리) 관련  (0) 2024.03.19
Path 연산  (0) 2023.09.09
csv 사용법  (0) 2022.03.06
posted by 모카쨩
2023. 9. 9. 19:09 Unity/C#

 

 

 

생각보다 쓸일이 많더라

한 점에서 Path까지의 최단지점

맨 위에건 AI가 짜주었다

    Vector3 ClosestPoint(Vector3 a, Vector3 b, Vector3 position)
    {
        Vector3 ab = b - a;
        Vector3 ap = position - a;
        float t = Vector3.Dot(ap, ab) / Vector3.Dot(ab, ab);
        t = Mathf.Clamp01(t);
        return a + ab * t;
    }
    Vector3 ClosestPoint(Vector3 position, List<Vector3> path)
    {
        var closestIndex = ClosestIndex(position, path);
        var point = ClosestPoint(path[closestIndex], path[closestIndex + 1], position);

        return point;
    }
    int ClosestIndex(Vector3 position, List<Vector3> path)
    {
        float minDistance = float.MaxValue;
        int closestIndex = 0;

        for (int i = 0; i < path.Count - 1; i++)
        {
            var point = ClosestPoint(path[i], path[i + 1], position);
            var distance = Vector3.Distance(point, position);

            if (distance < minDistance)
            {
                minDistance = distance;
                closestIndex = i;
            }
        }

        return closestIndex;
    }
    float ClosestLenth(Vector3 position, List<Vector3> path)
    {
        var closestIndex = ClosestIndex(position, path);
        var closestLenth = 0f;

        for (int i = 0; i < closestIndex; i++)
        {
            closestLenth += Vector3.Distance(path[i], path[i + 1]);
        }
        if (path.Count > 1)
        {
            var closestPoint = ClosestPoint(path[closestIndex], path[closestIndex + 1], position);
            closestLenth += Vector3.Distance(path[closestIndex], closestPoint);
        }

        return closestLenth;
    }

 

 

 

총 길이

    float TotalLength(List<Vector3> path)
    {
        float totalLength = 0;
        for (int i = 0; i < path.Count - 1; i++)
        {
            totalLength += Vector3.Distance(path[i], path[i + 1]);
        }
        return totalLength;
    }

 

 

Progress 변환

위에 두개가 있어야 쓸수 있다

나중에 생각해봤는데 ProgressToPosition보다 lerp라고 쓰는게 나을듯


    float GetProgress(Vector3 a, Vector3 b, Vector3 position)
    {
        var ab = b - a;
        var ap = position - a;
        var progress = Vector3.Dot(ap, ab) / Vector3.Dot(ab, ab);
        return Mathf.Clamp01(progress);
    }
    float GetProgress(Vector3 position, List<Vector3> path)
    {
        var progress = ClosestLenth(position, path) / TotalLength(path);
        return Mathf.Clamp01(progress);
    }
    Vector3 ProgressToPosition(Vector3 a, Vector3 b,float progress)
    {
        return a+ (b - a) * Mathf.Clamp01(progress);
        //rotation = Quaternion.LookRotation(ab); //이정돈 외부에서 처리하라고
    }
    (Vector3 position, Quaternion rotation) ProgressToPosition(float progress,List<Vector3> path)
    {
        progress = Mathf.Clamp01(progress);
        float totalLength = TotalLength(path);
        float currentLength = progress * totalLength;
        float accumulatedLength = 0;

        for (int i = 0; i < path.Count - 1; i++)
        {
            float segmentLength = Vector3.Distance(path[i], path[i + 1]);
            if (accumulatedLength + segmentLength > currentLength)
            {
                float t = (currentLength - accumulatedLength) / segmentLength;
                var position = Vector3.Lerp(path[i], path[i + 1], t);
                var rotation = transform.rotation;
                if (path.Count >= 2)
                {
                    rotation = Quaternion.LookRotation(path[i + 1] - path[i]);
                }
                return (position, rotation);
            }
            accumulatedLength += segmentLength;
        }
        {
            var position = path[path.Count - 1];
            var rotation = transform.rotation;
            if (path.Count >= 2)
            {
                rotation = Quaternion.LookRotation(path[path.Count - 1] - path[path.Count - 2]);
            }
            return (position, rotation);
        }
    }

'Unity > C#' 카테고리의 다른 글

자주 쓰는 DateTime 코드 모음  (0) 2023.12.19
csv 사용법  (0) 2022.03.06
Windows DLL경로  (0) 2022.02.12
posted by 모카쨩
2022. 3. 6. 13:22 Unity/C#

 

 

 

 

 

 

 

혹은 아래걸 사용

https://wmmu.tistory.com/entry/%EC%9C%A0%EB%8B%88%ED%8B%B0-csv-%EC%97%90%EC%85%8B

 

유니티 csv 에셋

https://assetstore.unity.com/packages/tools/integration/csv-serialize-135763 CSV Serialize | 기능 통합 | Unity Asset Store Use the CSV Serialize from VisualWorks on your next project. Find this integration tool & more on the Unity Asset Store. assetsto

wmmu.tistory.com

 

'Unity > C#' 카테고리의 다른 글

Path 연산  (0) 2023.09.09
Windows DLL경로  (0) 2022.02.12
유니티 폴리곤 처리  (0) 2022.02.10
posted by 모카쨩
2022. 2. 12. 00:15 Unity/C#

 

 

 

C:\Windows\Microsoft.NET\Framework\v4.0.30319

여기에 있다

 

System.Windows.Forms.dll도 있음

 

 

 

'Unity > C#' 카테고리의 다른 글

csv 사용법  (0) 2022.03.06
유니티 폴리곤 처리  (0) 2022.02.10
유니티 메시 관련  (0) 2022.01.22
posted by 모카쨩
2022. 2. 10. 15:33

보호되어 있는 글입니다.
내용을 보시려면 비밀번호를 입력하세요.

2022. 1. 22. 12:04 Unity/C#

 

 

중앙값

var position = Vector3.zero;
position.x = mesh.vertices.Average(x => x.x);
position.y = mesh.vertices.Average(x => x.y);
position.z = mesh.vertices.Average(x => x.z);

'Unity > C#' 카테고리의 다른 글

유니티 폴리곤 처리  (0) 2022.02.10
유니티 ScriptableObject  (0) 2021.12.16
유니티 트랜스 폼 (Transform)  (0) 2021.11.30
posted by 모카쨩

  • total
  • today
  • yesterday

Recent Post

저사양 유저용 블로그 진입