Skip to content

Commit 3fff0cd

Browse files
committed
feat:尝试拼成大图
1 parent bd14924 commit 3fff0cd

25 files changed

+4037
-1405
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[Oo]bj/
44
[Bb]uild/
55
[Bb]uilds/
6+
[Ll]ogs/
67
Assets/AssetStoreTools*
78

89
# Visual Studio cache directory

Assets/GifForUnity/GenBig.compute

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Each #kernel tells which function to compile; you can have many kernels
2+
#pragma kernel CSMain
3+
4+
//struct ColorData {
5+
// RWBuffer<int> buffer;
6+
//};
7+
8+
//RWStructuredBuffer<ColorData> InData;
9+
10+
// Create a RenderTexture with enableRandomWrite flag and set it
11+
// with cs.SetTexture
12+
13+
14+
[numthreads(8,8,1)]
15+
void CSMain (uint3 id : SV_DispatchThreadID)
16+
{
17+
// TODO: insert actual code here!
18+
}

Assets/GifForUnity/GenBig.compute.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/GifForUnity/Scripts/Editor.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using UnityEditor;
5+
6+
[CustomEditor(typeof(GifAssetImporter))]
7+
public class CustomInspectorGUI : Editor
8+
{
9+
public override void OnInspectorGUI()
10+
{
11+
base.OnInspectorGUI();
12+
GUI.TextField(new Rect(Vector2.zero, new Vector2(100, 20)), "Hello");
13+
}
14+
}

Assets/GifForUnity/Scripts/Editor/CustomInspectorGUI.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using UnityEditor;
5+
using System.IO;
6+
7+
public class GifAssetImporter : AssetImporter
8+
{
9+
public List<Texture2D> texs;
10+
11+
public void SetData(string path)
12+
{
13+
var fs = new FileStream(path, FileMode.Open, FileAccess.Read);
14+
if (fs == null)
15+
Debug.LogError("Open file error!");
16+
17+
int length = (int)fs.Length;
18+
byte[] gifData = new byte[length];
19+
fs.Read(gifData, 0, length);
20+
21+
GifForUnity.GifDecoder decoder = new GifForUnity.GifDecoder(gifData);
22+
decoder.ProcessData();
23+
24+
texs = decoder.resultTexs;
25+
}
26+
}

Assets/GifForUnity/Scripts/Editor/GifAssetImporter.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEditor;
4+
using UnityEngine;
5+
6+
public class GifAssetPostprocessor : AssetPostprocessor
7+
{
8+
//void OnPreprocessAsset()
9+
//{
10+
// Debug.Log("pre load");
11+
//}
12+
13+
14+
void OnPreprocessTexture()
15+
{
16+
if (assetPath.EndsWith(".gif"))
17+
{
18+
var gifImporter = (GifAssetImporter)assetImporter;//AssetImporter.GetAtPath(assetPath) as GifAssetImporter;
19+
gifImporter.SetData(assetPath);
20+
}
21+
}
22+
}

Assets/GifForUnity/Scripts/Editor/GifAssetPostprocessor.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/GifForUnity/Scripts/GifDecoder.cs

+44-44
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,18 @@ public GifDecoder(byte[] gifData)
7373

7474
#region CurrentImageData
7575

76-
private int _imageLeft; // 该帧图像绘制起点的左值(x)
77-
private int _imageTop; // 该帧图像绘制起点的顶值(y)
78-
private int _imageWidth; // 该帧图像绘制的宽度
79-
private int _imageHeight; // 该帧图像绘制的高度
76+
private int _imageLeft; // 该帧图像绘制起点的左值(x)
77+
private int _imageTop; // 该帧图像绘制起点的顶值(y)
78+
private int _imageWidth; // 该帧图像绘制的宽度
79+
private int _imageHeight; // 该帧图像绘制的高度
8080

8181
#endregion
8282

8383
#region Data for LZWDecompress
8484

85-
private int[] _indices = new int[4096]; // 编译表 ,存储了实际code在codes中的下标
86-
private ushort[] _codes = new ushort[128 * 1024]; // 颜色表,存储实际的code
87-
private uint[] _curBlock = new uint[64]; // 当前Image sub block 的数据 最多 256/4
85+
private int[] _indices = new int[4096]; // 编译表 ,存储了实际code在codes中的下标
86+
private ushort[] _codes = new ushort[128 * 1024]; // 颜色表,存储实际的code
87+
private uint[] _curBlock = new uint[64]; // 当前Image sub block 的数据 最多 256/4
8888

8989
#endregion
9090

@@ -288,26 +288,26 @@ private void Interlace()
288288

289289
private void LZWDecompress(Color32[] colorTable,int width,int height)
290290
{
291-
// 图像数据使用一维数组存储
292-
int row = (height - _imageTop - 1) * width;// y值,gif从左上角开始,unity texture2d从左下角开始
293-
int col = _imageLeft; // x 的 最小值
294-
int rightEdge = _imageLeft + _imageWidth; // x的最大值
291+
// 图像数据使用一维数组存储
292+
int row = (height - _imageTop - 1) * width;// y值,gif从左上角开始,unity texture2d从左下角开始
293+
int col = _imageLeft; //x 的 最小值
294+
int rightEdge = _imageLeft + _imageWidth; // x的最大值
295295
//Debug.LogError(col + " " + rightEdge + " " + (height - _imageTop));
296-
int minTableSize = gifData[_byteIndex++]; // 最小/起始 颜色长度(单位为bit),该值范围为2-8
296+
int minTableSize = gifData[_byteIndex++]; // 最小/起始 颜色长度(单位为bit),该值范围为2-8
297297
//if (minTableSize > 11)
298298
// minTableSize = 11;
299299

300-
// 初始化编译表
300+
// 初始化编译表
301301

302-
int codeSize = minTableSize + 1;// 单个数字的初始的编码长度,最长不超过12位
303-
int nextSize = Pow2[codeSize];// 下一次编译表扩容时的最大容量
304-
int maxCodesLength = Pow2[minTableSize];// 当前编译表中的最大容量(不计下面两个)
305-
int clearCode = maxCodesLength;// 清除标记
306-
int endCode = maxCodesLength + 1;// 结束标记
302+
int codeSize = minTableSize + 1;// 单个数字的初始的编码长度,最长不超过12位
303+
int nextSize = Pow2[codeSize];// 下一次编译表扩容时的最大容量
304+
int maxCodesLength = Pow2[minTableSize];// 当前编译表中的最大容量(不计下面两个)
305+
int clearCode = maxCodesLength;// 清除标记
306+
int endCode = maxCodesLength + 1;// 结束标记
307307
//Debug.Log("endCode:" + endCode);
308308

309-
int codesEnd = 0;// 实际code的index
310-
int codesNum = maxCodesLength + 2;// 编译表长度
309+
int codesEnd = 0;// 实际code的index
310+
int codesNum = maxCodesLength + 2;// 编译表长度
311311
//Debug.Log("Start:" + codesNum);
312312
//Dictionary<uint, int[]> _dic = new Dictionary<uint, int[]>();
313313
//for (uint i = 0; i < maxCodesLength; i++)
@@ -318,41 +318,41 @@ private void LZWDecompress(Color32[] colorTable,int width,int height)
318318
for (ushort i = 0; i < codesNum; i++)
319319
{
320320
_indices[i] = codesEnd;
321-
_codes[codesEnd++] = 1;// 之后跟着的code个数
321+
_codes[codesEnd++] = 1;// 之后跟着的code个数
322322
_codes[codesEnd++] = i;// code
323323
}
324324
//Debug.Log("_codes end:"+codesEnd+" " + _codes[codesEnd-1]);
325-
//LZW解压缩 loop
325+
//LZW解压缩 loop
326326

327-
uint previousCode = NoCode;// 前缀
328-
// tip: gif存储颜色数字时采用可变编码(即舍弃高位无用的0),
329-
uint mask = (uint)(nextSize - 1);//用于提取数字,从低位开始
330-
uint shiftRegister = 0;//移位缓存器
327+
uint previousCode = NoCode;// 前缀
328+
// tip: gif存储颜色数字时采用可变编码(即舍弃高位无用的0),
329+
uint mask = (uint)(nextSize - 1);//用于提取数字,从低位开始
330+
uint shiftRegister = 0;//移位缓存器
331331

332-
int bitsAvailable = 0;// 当前在移位缓存器可读取的bit数
333-
int bytesAvailable = 0;// 当前Image sub block中剩余的byte数
332+
int bitsAvailable = 0;// 当前在移位缓存器可读取的bit数
333+
int bytesAvailable = 0;// 当前Image sub block中剩余的byte数
334334

335335
int blockPos = 0;
336336

337337
while (true)
338338
{
339-
// 读取数字
339+
// 读取数字
340340
uint curCode = shiftRegister & mask;
341341

342342
if (bitsAvailable >= codeSize)
343343
{
344-
// 如果剩余的bits可供读取
344+
// 如果剩余的bits可供读取
345345
bitsAvailable -= codeSize;
346346
shiftRegister >>= codeSize;
347347
}
348348
else
349349
{
350-
// 否则,此时curCode中存储了剩余的bits(实际数字的部分bits)
350+
// 否则,此时curCode中存储了剩余的bits(实际数字的部分bits)
351351
// reload shift register
352352

353353

354354
// if start of new block
355-
// 当前子块已用完,读取下一个子块
355+
// 当前子块已用完,读取下一个子块
356356
if (bytesAvailable <= 0)
357357
{
358358
// read blocksize
@@ -372,7 +372,7 @@ private void LZWDecompress(Color32[] colorTable,int width,int height)
372372
}
373373

374374
// load shift register
375-
// 刷新缓存器
375+
// 刷新缓存器
376376
shiftRegister = _curBlock[blockPos++];
377377
int newBits = bytesAvailable >= 4 ? 32 : bytesAvailable * 8;
378378
bytesAvailable -= 4;
@@ -381,17 +381,17 @@ private void LZWDecompress(Color32[] colorTable,int width,int height)
381381

382382
if (bitsAvailable > 0)
383383
{
384-
// 读取数字时,可能会跨越不同的ImageSubBlock(即可简单理解为ImageData初始是连续的数字流,为切成一个个子块,
385-
// 人为插入了子块的大小)
386-
// 若curCode中已存储部分bits
384+
// 读取数字时,可能会跨越不同的ImageSubBlock(即可简单理解为ImageData初始是连续的数字流,为切成一个个子块,
385+
// 人为插入了子块的大小)
386+
// 若curCode中已存储部分bits
387387
var bitsRemaining = codeSize - bitsAvailable;
388388
curCode |= (shiftRegister << bitsAvailable) & mask;
389389
shiftRegister >>= bitsRemaining;
390390
bitsAvailable = newBits - bitsRemaining;
391391
}
392392
else
393393
{
394-
// 此时curCode为0
394+
// 此时curCode为0
395395
curCode = shiftRegister & mask;
396396
shiftRegister >>= codeSize;
397397
bitsAvailable = newBits - codeSize;
@@ -422,7 +422,7 @@ private void LZWDecompress(Color32[] colorTable,int width,int height)
422422
break;
423423
}
424424

425-
// 处理数字大致流程如下
425+
// 处理数字大致流程如下
426426
// let CODE be the next code in the code stream
427427
// is CODE in the code table?
428428
// Yes:
@@ -436,8 +436,8 @@ private void LZWDecompress(Color32[] colorTable,int width,int height)
436436
// add { PREVCODE} +K to code table
437437
// set PREVCODE = CODE
438438

439-
// 比较可知,两种情况下,均要将{ PREVCODE} +K 填入编译表,只是k的来源不同
440-
// 均要PREVCODE = CODE,不同点在于“Yes”时只输出{ CODE}到output,而“No”时,还需将k输出到output
439+
// 比较可知,两种情况下,均要将{ PREVCODE} +K 填入编译表,只是k的来源不同
440+
// 均要PREVCODE = CODE,不同点在于“Yes”时只输出{ CODE}到output,而“No”时,还需将k输出到output
441441

442442
bool plusOne = false;
443443
int codePos = 0;
@@ -451,7 +451,7 @@ private void LZWDecompress(Color32[] colorTable,int width,int height)
451451
{
452452
// write previous code
453453
codePos = _indices[previousCode];
454-
plusOne = true;//标记需将k输出到output
454+
plusOne = true;//标记需将k输出到output
455455
}
456456
else
457457
{
@@ -636,16 +636,16 @@ private void ReadHeader()
636636
}
637637
}
638638

639-
[MethodImpl(MethodImplOptions.AggressiveInlining)] // 积极内联
639+
[MethodImpl(MethodImplOptions.AggressiveInlining)] // 积极内联
640640
private byte ReadByte()
641641
{
642642
return gifData[_byteIndex++];
643643
}
644644

645-
[MethodImpl(MethodImplOptions.AggressiveInlining)] // 积极内联
645+
[MethodImpl(MethodImplOptions.AggressiveInlining)] // 积极内联
646646
private ushort ReadUInt16()
647647
{
648-
// gif 默认小端,即数字以从低到高位存储
648+
// gif 默认小端,即数字以从低到高位存储
649649
return (ushort)(gifData[_byteIndex++] | gifData[_byteIndex++] << 8);
650650
}
651651

0 commit comments

Comments
 (0)