-
[게임엔진활용] 1-4강 쉐이더로 효과 만들기[DSU] 게임엔진활용 2026. 3. 12. 23:15728x90
여러가지 쉐이더를 만들어서 실시간으로 적용해봅니다.
RenderView.cs 파일에서 여러 material을 사용할 수 있도록 수정합니다.
텍스쳐를 회색, 모자이크, 눈에 오는 효과를 추가해보았습니다.
material에서 쉐이더로 넘어가는 값들은, 코드는 39 라인에 쉽게 확인할 수 있도록 매크로 묶어두었습니다.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859using UnityEngine;public class RenderView : MonoBehaviour{public MainCamera mc;public int materialIndex;Material[] material;float playbackTime;// Start is called once before the first execution of Update after the MonoBehaviour is createdvoid Start(){mc = FindFirstObjectByType<MainCamera>();//material = new Material(Shader.Find("Unlit/Texture"));// 기본 제공 쉐이더string[] strShader = new string[] { "std", "grey", "mozic", "snow" };// 직접 만든 쉐이더material = new Material[strShader.Length];for (int i = 0; i < strShader.Length; i++)material[i] = new Material(Shader.Find("Custom/" + strShader[i]));playbackTime = 0f;}// Update is called once per framevoid Update(){}void OnGUI(){if (mc != null && mc.rt != null){GL.Clear(true, true, Color.black);Texture tex = mc.rt;Material m = material[materialIndex];m.SetTexture("_BaseMap", tex);m.SetColor("_BaseColor", Color.white);#if true// 0~1. std/grey none// 2. mozicm.SetFloat("size", 10);// 3. snowm.SetFloat("iTime", playbackTime);playbackTime += Time.deltaTime;#endiffloat r0 = (float)Screen.width / tex.width;float r1 = (float)Screen.height / tex.height;float r = (r0 < r1 ? r0 : r1);int w = (int)(tex.width * r), h = (int)(tex.height * r);int x = (Screen.width - w) / 2, y = (Screen.height - h) / 2;Graphics.DrawTexture(new Rect(x, y, w, h), tex,new Rect(0, 0, 1, 1),0, 0, 0, 0, m);}}}cs std, grey, mozic, snow shader 파일입니다. 설명이 필요 없는 부분은 생략했습니다.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859Shader "Custom/std"//Shader "Custom/grey"//Shader "Custom/mozic"//Shader "Custom/snow"{Properties{// 생략}SubShader{Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }Pass{// 생략CBUFFER_START(UnityPerMaterial)half4 _BaseColor;float4 _BaseMap_ST;float4 _BaseMap_TexelSize;// 추가CBUFFER_ENDVaryings vert(Attributes IN){// 생략}half4 frag(Varyings IN) : SV_Target{#if 1 // stdhalf4 c = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv) * _BaseColor;#elif 1 // greyhalf4 c = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv);float grey = c.r * 0.3 + c.g * 0.59 + c.b * 0.11;c = float4(grey, grey, grey, _BaseColor.a);#elif 1 // mozicfloat2 uv = IN.uv;float sizeX = size;float sizeY = size;float px = sizeX * _BaseMap_TexelSize.x;float py = sizeY * _BaseMap_TexelSize.y;uv = float2(px * floor(uv.x/px), py * floor(uv.y/py));half4 c = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, uv) * _BaseColor;#else // snow는 길어서 수업시간#endif#ifndef UNITY_COLORSPACE_GAMMAc.rgb = LinearToSRGB(c.rgb);#endifreturn c;}ENDHLSL}}}cs 실행결과 입니다.

2강에서는 더블버퍼링 및 키입력에 대해 알아보겠습니다.
긴 글 읽어주셔서 감사합니다 :)
'[DSU] 게임엔진활용' 카테고리의 다른 글
[게임엔진활용] 2강 게임 개발 해상도(더블버퍼링, 뷰포트) (0) 2026.03.25 [게임엔진활용] 1-5강 쉐이더토이 적용 (0) 2026.03.16 [게임엔진활용] 1-3강 해상도2 및 쉐이더 (0) 2026.03.12 [게임엔진활용] 1-2강 해상도1 (0) 2026.03.12 [게임엔진활용] 1-1강 프로젝트 생성 및 키보드 (0) 2026.03.12

