Get it on Google Play


Wm뮤 :: 'Unity/shader' 카테고리의 글 목록 (2 Page)

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

Recent Comment

Archive


2022. 1. 10. 16:17

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

2022. 1. 7. 11:29

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

2021. 12. 29. 23:34 Unity/shader

 

 

 

https://www.shadertoy.com/view/4djSRW

 

Shadertoy

 

www.shadertoy.com

더보기

hashOld12의 패턴

float hashOld12(float2 p)
{
    return sin(dot(p, float2(100, 100)));
}

 

 

 

hash12의 패턴

float hash12(float2 p)
{
    float3 p3 = sin(float3(p.xyx) );
    p3 += dot(p3, p3.yzx );
    return sin((p3.x + p3.y) * p3.z);
}

편향랜덤이라서 안 쓰는게 좋을듯

 

 

 

 

 

 

 

 

 

noise(floatM|halfM|min10floatM|min16floatM)

'Unity > shader' 카테고리의 다른 글

유니티 Compute Shader  (0) 2022.01.07
유니티 코드없이 Invisible 사용하기 (마스크 거꾸로 사용)  (0) 2021.08.17
유니티 쉐이더 기초  (0) 2021.04.17
posted by 모카쨩
2021. 8. 17. 16:35 Unity/shader

마테리얼을 다음처럼 설정하고

 

 

0,1,2

 

 

 

6,1,0

 

 InvisibleTarget에 스프라이트를 넣고 Invisible로 지우면 된다

Invisible이 먼저 와야한다

Stencil ID는 서로 동일해야한다

 

 

 

 

 

스텐실

 

 

'Unity > shader' 카테고리의 다른 글

랜덤 쉐이더  (0) 2021.12.29
유니티 쉐이더 기초  (0) 2021.04.17
유니티 쉐이더 게임 관련  (0) 2021.04.17
posted by 모카쨩
2021. 4. 17. 19:41 Unity/shader

유니티 셰이더는 HLSL임

 

 

 

실수형

 

float

-고정밀 

-32비트

 

half

-중정밀

-16비트

-범위 : -60,000 ~ +60,000

-정밀도 : 십진숫자 약 3개

 

fixed

-저정밀

-11비트

-범위 : -2.0 ~ +2.0

-정밀도 : 1/256

 

 

 

정수형

int

 

 

 

fixed4 타입 선언

 

fixed4 col = fixed4(0,0,0,0);

 

 

전부 잘 작동

for

break

switch

continue

 

 

 

 

 

 

 

 

Fixed Function Shader 샘플

Shader "Ahzkwid/UnlitCullFont" {
    Properties {
        _MainTex ("MainTex", 2D) = "white" {}
        _Color ("Main Color", Color) = (1,1,1,0)
        _Stencil_Ref ("Stencil_Ref", int) = 1
    }
    SubShader {
        Tags { 
        "RenderType"="Opaque" 
        "Queue"="Geometry+3000"
        }

        Cull front
        //ZTest Always

        Pass{
        	blend srcalpha oneminussrcalpha
	        SetTexture [_MainTex] 
	        {
	        	ConstantColor [_Color]
	        	//Combine texture
	        	//Combine texture lerp(texture) constant
                //Combine texture* primary DOUBLE, texture* constant
                Combine texture* primary
                Combine previous* constant
	        }

        }

    } 
}

 

 

 

 

 

posted by 모카쨩
2021. 4. 17. 19:19 Unity/shader

 

 

 


#define _BoardSize 8.0

int2 GetPiecePos(float2 _pos)
{
    return int2(min(_BoardSize-1, _pos.x * _BoardSize), min(_BoardSize-1, _pos.y * _BoardSize));
}
fixed2 GetUvPos(int2 _pos)
{
   return fixed2((half)_pos.x / _BoardSize + (0.5 / _BoardSize), (half)_pos.y / _BoardSize + (0.5 / _BoardSize));
}


int2 piecePos = GetPiecePos(i.uv);
fixed2 uvPos = GetUvPos(piecePos);
col.r = uvPos.x * 0.1;
col.g = uvPos.y * 0.1;
col.a = 1;
return col;

결과

 

 

 

#define _BoardSize 8.0

int2 GetPiecePos(float2 _pos)
{
    return int2(min(_BoardSize-1, _pos.x * _BoardSize), min(_BoardSize-1, _pos.y * _BoardSize));
}
            
int2 LenPos(int len, int angle)
{

    int2 _pos = int2(0, 0);
    switch (angle)
    {
    case 0:
        _pos.x = len;
        _pos.y = 0;
        break;
    case 45:
        _pos.x = len;
        _pos.y = len;
        break;
    case 90:
        _pos.x = 0;
        _pos.y = len;
        break;
    case 135:
        _pos.x = -len;
        _pos.y = len;
        break;
    case 180:
        _pos.x = -len;
        _pos.y = 0;
        break;
    case 225:
        _pos.x = -len;
        _pos.y = -len;
        break;
    case 270:
        _pos.x = 0;
        _pos.y = -len;
        break;
    case 315:
        _pos.x = len;
        _pos.y = -len;
        break;
    }
    return _pos;
}



int2 piecePos = GetPiecePos(i.uv);

int angle = piecePos.y * 45;
col.r = 0.5+(float2)LenPos(piecePos.x, angle).x * 0.05;
col.g = 0.5 + (float2)LenPos(piecePos.x, angle).y * 0.05;
col.b = 0.5;
col.a = 1;
return col;

결과

아름다운 무지개가 나왔다

Hue가 어째서 360도의 각도를 지니는지 알수있는 부분

 


int2 LenPos(int len, int angle)
{

    int2 _pos = int2(0, 0);
    if (angle == 0)
    {
        _pos.x = len;
        _pos.y = 0;
    }
    else if (angle == 45)
    {
        _pos.x = len;
        _pos.y = len;
    }
    else if (angle == 90)
    {
        _pos.x = 0;
        _pos.y = len;
    }
    else if (angle == 135)
    {
        _pos.x = -len;
        _pos.y = len;
    }
    else if (angle == 180)
    {
        _pos.x = -len;
        _pos.y = 0;
    }
    else if (angle == 225)
    {
        _pos.x = -len;
        _pos.y = -len;
    }
    else if (angle == 270)
    {
        _pos.x = 0;
        _pos.y = -len;
    }
    else if (angle == 315)
    {
        _pos.x = len;
        _pos.y = -len;
    }
    return _pos;
}

if버전

 

 

 

 

 

'Unity > shader' 카테고리의 다른 글

유니티 쉐이더 기초  (0) 2021.04.17
모션블러 종류  (1) 2021.03.02
유니티 쉐이더 인스펙터  (0) 2021.02.21
posted by 모카쨩
2021. 3. 2. 00:02 Unity/shader

1.이전화면 프레임을 남기는 방식

=초당프레임을 충분히 올리지 않으면 잔상현상이 생김

따라서 처리량대비 성능이 안 좋음

 

2. 화면내에서의 각속도와 이동속도를 가져와서 쉐이더에서 블러처리를 적용

=위화감이 생기지만 1번에 비해 가벼움

 

3.이전 프레임과 비교하여 변형된 부분에 블러를 적용

=가볍고 빠르지만 현실적이진 않고 눈속임정도

 

 

'Unity > shader' 카테고리의 다른 글

유니티 쉐이더 게임 관련  (0) 2021.04.17
유니티 쉐이더 인스펙터  (0) 2021.02.21
스텐실  (0) 2021.01.20
posted by 모카쨩
2021. 2. 21. 19:33 Unity/shader
using UnityEngine;
using UnityEditor;

public class AhzkwidToonInspector : ShaderGUI
{
    public enum _RM
    {
        Opaque, Cutout, Transparent
    }
    public enum _BM
    {
        None, ext, Body, Hair, Cloth
    }
    public enum _CM
    {
        Back, None, Front
    }

    public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
    {
        MaterialProperty _RenderMode
        , _BlendMode

        , _MainTex
        , _ColorTex
        , _Color
        , _EmissionTex
        , _EmissionColor
        , _Hue
        , _Sat
        , _Val
        , _Contrast
        , _outline_width
        , _OutlineTex
        , _OutlineMask
        , _OutlineColor
        , _Metallic
        , _ColorRamp
        , _CullingMode
        , _OutlineSat;

        _RenderMode = FindProperty("_RenderMode", properties);
        _BlendMode = FindProperty("_BlendMode", properties);
        _MainTex = FindProperty("_MainTex", properties);
        _ColorTex = FindProperty("_ColorTex", properties);
		_Color = FindProperty("_Color", properties);
		_EmissionTex = FindProperty("_EmissionTex", properties);
		_EmissionColor = FindProperty("_EmissionColor", properties);
        _Hue = FindProperty("_Hue", properties);
        _Sat = FindProperty("_Sat", properties);
        _Val = FindProperty("_Val", properties);
        _Contrast = FindProperty("_Contrast", properties);
        _outline_width = FindProperty("_outline_width", properties);
        _OutlineTex = FindProperty("_OutlineTex", properties);
        _OutlineMask = FindProperty("_OutlineMask", properties);
        _OutlineColor = FindProperty("_OutlineColor", properties);
        _Metallic = FindProperty("_Metallic", properties);
        _ColorRamp = FindProperty("_ColorRamp", properties);
        _CullingMode = FindProperty("_CullingMode", properties);
        _OutlineSat = FindProperty("_OutlineSat", properties);
        

        // render the default gui

        Material targetMat = materialEditor.target as Material;

		// see if redify is set, and show a checkbox
		EditorGUI.BeginChangeCheck();
        {
            
            EditorGUIUtility.labelWidth = 0f;//컬러위치 
            targetMat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
            targetMat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
            //targetMat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
            materialEditor.ShaderProperty(_RenderMode, new GUIContent("Render Mode"));
            //materialEditor.ShaderProperty(_BlendMode, new GUIContent("Blend Mode", "Blend Mode"));
            GUILayout.Label("Main Maps", EditorStyles.boldLabel);
            materialEditor.TexturePropertySingleLine(new GUIContent("Main Texture", "Blend Color Texture"), _MainTex, _BlendMode);
            EditorGUI.indentLevel = 1; //얼마나 오른쪽으로 이동시킬지

            switch ((_BM)_BlendMode.floatValue)
            {
                case _BM.None:
                    break;
                case _BM.ext:
                    materialEditor.TexturePropertySingleLine(new GUIContent("Blend Color", "Blend Color Texture"), _ColorTex, _Color);
                    materialEditor.TexturePropertySingleLine(new GUIContent("Emission Color", "Emission Color Texture"), _EmissionTex, _EmissionColor);
                    break;
                case _BM.Body:
                    break;
                case _BM.Hair:
                    break;
                case _BM.Cloth:
                    break;
                default:
                    break;
            }
            switch ((_BM)_BlendMode.floatValue)
            {
                case _BM.None:
                    break;
                default:
                    materialEditor.ShaderProperty(_Hue, new GUIContent("Hue"));
                    materialEditor.ShaderProperty(_Sat, new GUIContent("Sat"));
                    materialEditor.ShaderProperty(_Val, new GUIContent("Val"));
                    break;
            }
            switch ((_BM)_BlendMode.floatValue)
            {
                case _BM.None:
                    break;
                case _BM.ext:
                    materialEditor.ShaderProperty(_Contrast, new GUIContent("Contrast"));
                    materialEditor.ShaderProperty(_Metallic, new GUIContent("Metallic"));
                    materialEditor.ShaderProperty(_ColorRamp, new GUIContent("ColorRamp"));
                    break;
                case _BM.Body:
                    break;
                case _BM.Hair:
                    break;
                case _BM.Cloth:
                    break;
                default:
                    break;
            }
            EditorGUI.indentLevel = 0;

            EditorGUILayout.Space();
            GUILayout.Label("Outline", EditorStyles.boldLabel);
            materialEditor.ShaderProperty(_outline_width, new GUIContent("Outline Width"));
            if(_outline_width.floatValue>0)
            {
                EditorGUI.indentLevel = 1; //얼마나 오른쪽으로 이동시킬지
                //materialEditor.TexturePropertySingleLine(new GUIContent("Outline Color", "Outline Color Texture"), _OutlineTex, _OutlineColor);
                //materialEditor.TexturePropertySingleLine(new GUIContent("Outline Color", "Outline Color Texture"), _OutlineColor);
                materialEditor.TexturePropertySingleLine(new GUIContent("Outline Mask", "Outline Mask Texture"), _OutlineMask);
                materialEditor.ShaderProperty(_OutlineSat, new GUIContent("Outline Sat"));


                EditorGUI.indentLevel = 0; //얼마나 오른쪽으로 이동시킬지
            }
            switch ((_BM)_BlendMode.floatValue)
            {
                case _BM.None:
                    break;
                case _BM.ext:
                    break;
                case _BM.Body:
                    break;
                case _BM.Hair:
                    break;
                case _BM.Cloth:
                    break;
                default:
                    break;
            }
            switch ((_RM)_RenderMode.floatValue)
            {
                case _RM.Opaque:
                    break;
                case _RM.Cutout:
                    break;
                case _RM.Transparent:
                    break;
                default:
                    break;
            }

            materialEditor.ShaderProperty(_CullingMode,new GUIContent("Culling Mode", "Outline Mask Texture"));
            switch ((_CM)_CullingMode.floatValue)
            {
                case _CM.Back:
                    break;
                case _CM.None:
                    break;
                case _CM.Front:
                    break;
                default:
                    break;
            }
            EditorGUILayout.Space();
            //materialEditor.RenderQueueField();
            //materialEditor.EnableInstancingField();
            //materialEditor.DoubleSidedGIField();
            //base.OnGUI(materialEditor, properties);
        }
	}
}

AhzkwidToonShader에 포함된 예제

 


public enum _RM
{
        Default, Overlay
}

public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
{

    //쉐이더 프로퍼티들을 불러옴
    MaterialProperty _RenderMode = FindProperty(nameof(_RenderMode), properties);
    MaterialProperty _MainTex = FindProperty(nameof(_MainTex), properties);
    
    
    
    //인스펙터 변경사항이 있는지 체크하는거, 필수는 아님
    EditorGUI.BeginChangeCheck();
    {
        //해당 변수의 값을 읽어옴
        switch ((_RM)_RenderMode.floatValue)
        {
            case _RM.Default:
                //행동1
                break;
            case _RM.Overlay:
                //행동2
                break;
            default:
                break;
        }

        //일반 인스펙터 (참조변수가 앞에 있는게 특징이다)
        materialEditor.ShaderProperty(_RenderRange, "Render Range");
        
        
        //텍스처 인스펙터 (GuIContent를 써서 설명도 같이 넣고 있다. 그냥 문자열로 넣어도 됨)
        materialEditor.TexturePropertySingleLine(new GUIContent("Main Texture", "Default Texture"), _MainTex);

    }
}

 

 

 

 

들여쓰기

EditorGUI.indentLevel = 1; //얼마나 오른쪽으로 이동시킬지
{
    var guiContent=new GUIContent("Main Texture(변수명)", "Default Texture(설명)");
    materialEditor.TexturePropertySingleLine(guiContent, _MainTex);
}
EditorGUI.indentLevel = 0; //얼마나 오른쪽으로 이동시킬지

 

 

 

헤더+들여쓰기

//헤더
GUILayout.Label("헤더", EditorStyles.boldLabel);
{
    //들여쓰기
    EditorGUI.indentLevel = 1; //얼마나 오른쪽으로 이동시킬지
    {
        materialEditor.ShaderProperty(_Value1, "인스펙터1");
        materialEditor.ShaderProperty(_Value2, "인스펙터2");
        materialEditor.ShaderProperty(_Value3, "인스펙터3");
        materialEditor.ShaderProperty(_Value4, "인스펙터4");
    }
    EditorGUI.indentLevel = 0;
}

 

 

렌더큐옵션

EditorGUILayout.Space();
EditorGUILayout.Space();
materialEditor.RenderQueueField();

 

 

어스밴스 옵션

EditorGUILayout.Space();
EditorGUILayout.Space();
GUILayout.Label("Advanced Oprions", EditorStyles.boldLabel);
{
    materialEditor.EnableInstancingField();
    materialEditor.DoubleSidedGIField();
}

 

 

 

 

 

쉐이더 코드에 다음과같이 넣어줘야 작동한다

FallBack은 필수는 아니고 쉐이더 미싱일때 작동하는거

SubShader
{
		FallBack "Diffuse" //기본마테리얼
		CustomEditor "AhzkwidToonInspector" //커스텀인스펙터
}

'Unity > shader' 카테고리의 다른 글

모션블러 종류  (1) 2021.03.02
스텐실  (0) 2021.01.20
자주쓰는 유니티 쉐이더 코드모음  (0) 2020.11.04
posted by 모카쨩

  • total
  • today
  • yesterday

Recent Post

저사양 유저용 블로그 진입