메뉴 건너뛰기

조회 수 25 추천 수 0 댓글 0


Honeycam 2022-09-02 12-31-51.gif

 

 

스파인 텍스쳐에 낙서하기.

 

 

 

 

 

 

image.png

 

스파인 API 구조를 보면 뭐 대략 이렇게 돼 있는데

이번에 중요한 부분은 가운데쯤 있는 Skin과 Attachments이다.

그 중에 어태치먼트가 스킨의 실제 텍스쳐 정도의 역할을 하는 것으로 보이는데

그 텍스쳐를 제어해서 스파인에 런타임으로 텍스쳐를 수정하는 것이 목표.

=동물의 숲에서 옷에 직접 도트질하기 Spine 버전.

 

 

 

image.png

 

예시에서 테두리 쳐진 부분 것이 Attachment이고

그것의 텍스쳐에서 필요한 부분만 복사해서 새로운 텍스쳐를 하나 만들어내고

새 텍스쳐를 수정해서 꾸민다음 저 Attachment에 끼우겠다는 심보.

 

Attachment가 약간 포토샵의 레이어 같은 느낌?

Skin은 레이어 모아둔 폴더 느낌?

 

 

 

        public Texture2D GetTextureFormSlotAttachment(Slot slot) {
            Texture originalTex = slot.Attachment.GetMaterial().mainTexture;
            MeshAttachment _att = slot.Attachment as MeshAttachment;
            TextureRegion attTexRegion = _att.Region;
            AtlasRegion attAtlasRegion = attTexRegion as AtlasRegion;

            Debug.Log(string.Format("u: {0}, u2: {1}, v: {2}, v2: {3}", attTexRegion.u, attTexRegion.u2, attTexRegion.v, attTexRegion.v2));
            Debug.Log(string.Format("w: {0}, h: {1}, OW: {2}, OH: {3}", attTexRegion.width, attTexRegion.height, attTexRegion.OriginalWidth, attTexRegion.OriginalHeight));
            Debug.Log(string.Format("{0}, {1}, {2}, {3}", originalTex.width * attTexRegion.u, originalTex.width * attTexRegion.u2, originalTex.height * attTexRegion.v, originalTex.height * attTexRegion.v2));

            Texture2D newTex = new Texture2D(attTexRegion.width, attTexRegion.height, TextureFormat.RGBA32, false);
            Graphics.CopyTexture(originalTex, 0, 0, (int)(originalTex.width * attTexRegion.u), (int)(originalTex.height * attTexRegion.v2), attTexRegion.width, attTexRegion.height,
                newTex, 0, 0, 0, 0);

            if (attAtlasRegion.degrees != 0) {
                Color32[] originalPixels;
                Color32[] rotatedPixels;
                int w = newTex.width;
                int h = newTex.height;
                int iRotated, iOriginal;
                switch (attAtlasRegion.degrees) {
                    case 90:
                        originalPixels = newTex.GetPixels32();
                        rotatedPixels = new Color32[originalPixels.Length];

                        for (int j = 0; j < h; ++j) {
                            for (int i = 0; i < w; ++i) {
                                iRotated = (i + 1) * h - j - 1;
                                iOriginal = originalPixels.Length - 1 - (j * w + i);
                                rotatedPixels[iRotated] = originalPixels[iOriginal];
                            }
                        }

                        newTex = new Texture2D(h, w);
                        newTex.SetPixels32(rotatedPixels);
                        newTex.Apply();
                        break;
                    case 180:
                        originalPixels = newTex.GetPixels32();
                        System.Array.Reverse(originalPixels, 0, originalPixels.Length);

                        newTex.SetPixels32(originalPixels);
                        newTex.Apply();
                        break;
                    case 270:
                        originalPixels = newTex.GetPixels32();
                        rotatedPixels = new Color32[originalPixels.Length];

                        for (int j = 0; j < h; ++j) {
                            for (int i = 0; i < w; ++i) {
                                iRotated = (i + 1) * h - j - 1;
                                iOriginal = j * w + i;
                                rotatedPixels[iRotated] = originalPixels[iOriginal];
                            }
                        }

                        newTex = new Texture2D(h, w);
                        newTex.SetPixels32(rotatedPixels);
                        newTex.Apply();
                        break;
                    default:

                        break;
                }
            }

            newTex.filterMode = FilterMode.Point;
            SpineSlotPainter.instance.SetImage(newTex); //이건 UI 스크립트에 텍스쳐 등록하고 표시하는 기능
            return newTex;
        }

 

여기까지가 Slot에서 현재 텍스쳐를 복사해서 Texture2D로 반환하는 함수이다.

slot에 Attachment의 Material가 쓰는 mainTexture를 가져오고, (=여러 부위가 섞인 통짜 텍스쳐[Atlas]임)

거기서 TextureRegion, AtlasRegion을 어찌어찌 다뤄서 Atlas 안에 실제로 사용하는 부분만 잘라낸 후

회전이 들어갔다면 정방향으로 다시 텍스쳐를 회전시키기까지 한 뒤 Texture2D로 보낸다.

 

 

 

 

 

 

 

        public Material CreateMeshAttachmentByTexture(Slot slot, Texture2D texture, int slotIndex) {
            if (slot == null) return null;
            MeshAttachment oldAtt = slot.Attachment as MeshAttachment;
            if (oldAtt == null || texture == null) return null;

            MeshAttachment att = new MeshAttachment(oldAtt.Name);
            att.Region = CreateRegion(texture);
            att.Path = oldAtt.Path;

            att.Bones = oldAtt.Bones;
            att.Edges = oldAtt.Edges;
            att.Triangles = oldAtt.Triangles;
            att.Vertices = oldAtt.Vertices;
            att.WorldVerticesLength = oldAtt.WorldVerticesLength;
            att.HullLength = oldAtt.HullLength;
            //att.RegionRotate = false;

            att.Region.u = 0f;
            att.Region.v = 1f;
            att.Region.u2 = 1f;
            att.Region.v2 = 0f;
            att.RegionUVs = oldAtt.RegionUVs;

            att.UpdateRegion();

            Material mat = new Material(Shader.Find("Sprites/Default")); //여기서 Material 적절히 바꿔줘야함
            mat.mainTexture = texture;
            (att.Region as AtlasRegion).page.rendererObject = mat;

            slot.Attachment = att;
        }

 

 

이제 스파인의 해당 Slot에 위에서 추출했던 Texture2D를 끼워주면 되는데

위의 API 구조에 보이는 attachment 중에서 MeshAttachment를 새로 만들어서,

그것을 원래 Slot의 attachment 자리에 끼워주면 된다.

(대충 슬롯의 텍스쳐를 바꿔치기 한다는 뜻)

 

단, Spine Material을 사용하지 않으므로 적절한 매터리얼을 끼워줘야

빛이나 그림자가 다른 부위와 이질감 있게 동작하지 않을 것이다.

 

 

 

이제 Texture2D에  SetPixel을 하던 어떻게든 해서 변화를 주면 스파인에 즉시 반영된다.

투명색 칠하기도 된다.

 

 

 

 

 

해달라해서 해봤는데 생각보단 쉽게됨.


사진 및 파일 첨부

여기에 파일을 끌어 놓거나 왼쪽의 버튼을 클릭하세요.

파일 용량 제한 : 0MB (허용 확장자 : *.*)

0개 첨부 됨 ( / )

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
47 그외 게임 메이커 스튜디오 C# dll 라이브러리 만들 때 .NET 프레임워크 버전 문제 file 조루나 2024.05.03 3
46 게임 애드몹 Code 3 no fill 에러 원인 중 한 가지 file 조루나 2024.01.16 18
45 게임 유니티 특) 9 Slice랑 Filled 동시에 안 됨. 조루나 2023.11.29 10
44 게임 메쉬로 Progress circle 그리기 file 조루나 2023.11.23 10
43 그외 남이 만든 게 안 돌아가서 뜯어고친 C# 셀레니움 크롬 드라이버 자동 업데이트 조루나 2023.11.23 10
42 게임 우효, 이런 좋은 버그리포트 도구가 있었다니 Sentry 1 file 조루나 2023.05.08 43
41 게임 Unity 빌드 전 대화상자 출력 조루나 2023.04.20 10
40 게임 유니티 모바일에서 VideoPlayer로 영상 재생하면 자꾸 멈춘다. 조루나 2023.02.23 33
39 게임 세 가지 셀 셰이딩 에셋의 단순 프레임 비교 file 조루나 2023.02.03 43
38 게임 유니티 transform 참조의 문제 file 조루나 2022.11.10 63
37 게임 에셋 Super Tilemap Editor가 바닥면 Collider를 만들어주지 않아 file 조루나 2022.10.27 10
36 게임 미소녀 뱀파이어 서바이버즈를 만들자! - 캐릭터 몸에 마법 문신 넣기 file 조루나 2022.10.06 98
35 게임 Unity 2020에서 Spine 3.6 Runtime이 AtlasAsset을 제대로 Import하지 못 할 때 file 조루나 2022.09.20 19
» 게임 스파인 텍스쳐 런타임 색칠하기 스파인 텍스쳐에 낙서하기. 스파인 API 구조를 보면 뭐 대략 이렇게 돼 있는데 이번에 중요한 부분은 가운데쯤 있는 Skin과 Attachments이다. 그 중에 어태치먼트... file 조루나 2022.09.02 25
33 비주얼 블렌더 맛보기 1 file 조루나 2022.07.21 70
32 게임 파티클 메쉬와 Optimize Mesh Data의 저주 file 조루나 2022.07.14 20
31 게임 유니티 2D(2.5D) 타일 에셋 - Super Tilemap Editor file 조루나 2022.07.06 25
30 게임 유니티 컬러 스페이스와 스파인 HSL 색 변경의 역습 file 조루나 2022.07.06 126
29 게임 앱 이름에 따라 프레임이 다르게 나온다... file 조루나 2022.06.21 15
28 게임 유니티 스파인 슬롯에 HSL 색상 변경(=포토샵 Colorize) 3 file 조루나 2022.05.13 170
Board Pagination Prev 1 2 3 Next
/ 3