'전체보기'에 해당되는 글 1012건
- 2022.02.16 C# 환경변수 설정
- 2022.02.12 유니티 텍스처 관련
- 2022.02.12 Windows DLL경로
- 2022.02.11 유니티 안드로이드 동영상 관련자료
- 2022.02.10 유니티 폴리곤 처리
- 2022.02.08 유니티 pb stl 사용법
- 2022.02.07 인생 중간결산 (1)
- 2022.02.02 서브스탠스 페인터 커스텀 마테리얼
SetPixel
public Texture2D texture2D;
void Start()
{
texture2D = new Texture2D(512,1);
for (int i = 0; i < 512; i++)
{
texture2D.SetPixel(i,0, new Color(1, (float)i / 512, 0, 0.25f));
}
texture2D.Apply();
}
SetPixels
public Texture2D texture2D;
void Start()
{
var colors = new Color[512];
for (int i = 0; i < 512; i++)
{
colors[i] = new Color(1, (float)i / 512, 0, 0.25f);
}
texture2D = new Texture2D(512, 1);
texture2D.SetPixels(0, 0, texture2D.width, texture2D.height, colors);
texture2D.Apply();
}
픽셀 불러오기
var pixels = tex.GetPixels(0, 0, tex.width, tex.height);
for (int x = 0; x < tex.width; x++)
{
for (int y = 0; y < pixels.Length / tex.width; y++)
{
var index = y * tex.width + x;
var pixel = pixels[index];
}
}
렌더텍스처 픽셀 가져오기
원문코드는 이곳에서 가져왔다 https://docs.unity3d.com/kr/530/ScriptReference/RenderTexture-active.html
static public Texture2D GetRTPixels(RenderTexture rt)
{
// Remember currently active render texture
RenderTexture currentActiveRT = RenderTexture.active;
// Set the supplied RenderTexture as the active one
RenderTexture.active = rt;
// Create a new Texture2D and read the RenderTexture image into it
Texture2D tex = new Texture2D(rt.width, rt.height);
tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
// Restorie previously active render texture
RenderTexture.active = currentActiveRT;
return tex;
}
위 코드 사용 예제
var texture = GetRTPixels(renderTexture);
var pixels = texture.GetPixels(0, 0, texture.width, texture.height, miplevel : 0);
Texture2D 빈 가장자리 제거
static public Texture2D TrimTexture(Texture2D texture)
{
return TrimTexture(texture, Vector2.zero);
}
static public Texture2D TrimTexture(Texture2D texture,Vector2 padding)
{
var rect = new Rect(0, 0, texture.width, texture.height);
{
var pixels = texture.GetPixels(0, 0, texture.width, texture.height);
//x
for (int x = 0; x < texture.width; x++)
{
for (int y = 0; y < pixels.Length / texture.width; y++)
{
var index = x + y * texture.width;
var pixel = pixels[index];
if (pixel.a > 0)
{
rect.x = x;
break;
}
}
if (rect.x != 0)
{
break;
}
}
//y
for (int y = 0; y < pixels.Length / texture.width; y++)
{
for (int x = 0; x < texture.width; x++)
{
var index = x + y * texture.width;
var pixel = pixels[index];
if (pixel.a > 0)
{
rect.y = y;
break;
}
}
if (rect.y != 0)
{
break;
}
}
//width
for (int x = texture.width - 1; x >= 0; x--)
{
for (int y = pixels.Length / texture.width - 1; y >= 0; y--)
{
var index = x + y * texture.width;
var pixel = pixels[index];
if (pixel.a > 0)
{
rect.width = x - rect.x;
break;
}
}
if (rect.width != texture.width)
{
break;
}
}
//height
for (int y = pixels.Length / texture.width - 1; y >= 0; y--)
{
for (int x = texture.width - 1; x >= 0; x--)
{
var index = x + y * texture.width;
var pixel = pixels[index];
if (pixel.a > 0)
{
rect.height = y - rect.y;
break;
}
}
if (rect.height != texture.height)
{
break;
}
}
}
{
var pixels = texture.GetPixels((int)rect.x, (int)rect.y, (int)rect.width, (int)rect.height);
var trimTexture = new Texture2D((int)rect.width+ (int)padding.x*2, (int)rect.height + (int)padding.y * 2);
var defaultPixels = new Color[trimTexture.width * trimTexture.height];
System.Array.ConvertAll(defaultPixels,x=>new Color(0,0,0,0));
trimTexture.SetPixels(0, 0, trimTexture.width, trimTexture.height, defaultPixels);
trimTexture.SetPixels((int)padding.x, (int)padding.y, (int)rect.width, (int)rect.height, pixels);
trimTexture.Apply();
return trimTexture;
}
}
RGB만 바꾸기
만들고 보니 쓸모가 없어졌네
/// <summary>
/// rgb만 바꾸고 a는 그냥 놔둠
/// </summary>
/// <param name="texture"></param>
/// <param name="color"></param>
static public void ChangeRGB(Texture2D texture,Color color)
{
var pixels = texture.GetPixels(0, 0, texture.width, texture.height);
for (int x = 0; x < texture.width; x++)
{
for (int y = 0; y < pixels.Length / texture.width; y++)
{
var index = x + y * texture.width;
pixels[index].r = color.r;
pixels[index].g = color.g;
pixels[index].b = color.b;
}
}
texture.SetPixels(0, 0, texture.width, texture.height, pixels);
}
'Unity' 카테고리의 다른 글
유니티 IOS 인터페이스 (0) | 2022.02.25 |
---|---|
유니티 안드로이드 동영상 관련자료 (0) | 2022.02.11 |
유니티 pb stl 사용법 (0) | 2022.02.08 |
내가 이 세계에서 머물면서 알아낸것들이 있다
-우리들과 돌멩이는 동등하다는것
우리가 소위 영혼이라고 부르는것은 돌멩이에도 개미에게도 감시카메라에게도 존재한다. 즉 개체로서 분리될수 있다면 뭐든지 성립한다. 우리와 돌멩이의 차이점은 겨우 기억과 연산의 유무뿐이다. 감시카메라는 자기자신을 인지하지 못하는 어린아이와 같다
-우리에게 있어 시간의 흐름이란 기억의 변화이다.
물리법칙에서의 시간과 우리의 시간은 동등하지 않다. 즉 우리가 자기자신을 인식하는것 자체가 시간의 흐름이라는 것이다. 때문에 수면등의 이유로 기억의 변화가 생기지 않으면 중간시간은 존재하지 않는다.
-우주는 무한하나 유한하다
우주는 모든가능성을 시험하지 않는다.
예를들면 1+1을 3이라고 하지 않으며 느닷없이 태양을 0.1초만에 은하바깥으로 이동시키거나 하지 않는다. 어디까지나 특정 법칙하에서만 모든 가능성을 시험한다.
-매우 거대한 숫자 하나는 우주와 동등하다
거대숫자에서 무작위와 법칙 두가지 다 얻을수 있기 때문이다.
이것을 응용해서 우리는 의사난수를 만들어 사용하고 있다.
-평행세계는 존재할것이다(가정)
위와 동일하다 우리우주가 거대숫자라면 또 다른 숫자또한 우주가 될수있기 때문이다
'과학기술' 카테고리의 다른 글
내 몸의 위험을 알리는 신호 첫번째 -탈수증상- (0) | 2015.11.07 |
---|---|
(심장)늙어서 하는 운동은 오히려 악영향을 끼칠수도 있다 (0) | 2015.09.20 |
면역거부 없는 장기이식기술 개발 성공 (0) | 2013.06.24 |
'서브스탠스 페인터' 카테고리의 다른 글
서브스탠스 페인터 랜덤타일링 (0) | 2023.01.12 |
---|---|
서브스탠스 페인터 익스포트 (0) | 2022.01.22 |
서브스탠스 체크무늬 (0) | 2022.01.22 |