private VertexCache CreateVertexCache(string prefabName, int renderName, int alias, Mesh mesh) { VertexCache vertexCache = new VertexCache(); int cacheName = renderName + alias; vertexCachePool[cacheName] = vertexCache; vertexCache.nameCode = cacheName; vertexCache.mesh = mesh; vertexCache.boneTextureIndex = FindTexture_internal( prefabName); //因为Texture不是从AnimationInfo读进来的,而是作为BoneInfo读入,所以需要在这里通过prefabName将boneTexture和vertexCache连起来。 vertexCache.weight = new Vector4[mesh.vertexCount]; vertexCache.boneIndex = new Vector4[mesh.vertexCount]; int packageCount = 1; if (vertexCache.boneTextureIndex >= 0) //如果找到了boneTexture,那么packageCount装载为该prefab的BoneTexture数。 { BoneTextureInfo texture = _boneTextrueInfo[vertexCache.boneTextureIndex]; packageCount = texture.BoneTexture.Length; } //建立了packageList,也是空的。 vertexCache.packageList = new List <VertexCache.InstancingPackage> [packageCount]; for (int i = 0; i != vertexCache.packageList.Length; ++i) { vertexCache.packageList[i] = new List <VertexCache.InstancingPackage>(); } vertexCache.runtimePackageIndex = new int[packageCount]; InstanceData data = null; int instanceName = prefabName.GetHashCode() + alias; //这里往instanceDataPool里面加入了一个空的data,只有Instancename。,并且创建只进行到List<float[]>[]的List层次,往List里面注入float[]在CreatPackage里面完成。 if (!instanceDataPool.TryGetValue(instanceName, out data)) { data = new InstanceData(); data.WorldMatrix = new List <Matrix4x4[]> [packageCount]; data.FrameIndex = new List <float[]> [packageCount]; data.PreFrameIndex = new List <float[]> [packageCount]; data.TransitionProgress = new List <float[]> [packageCount]; for (int i = 0; i != packageCount; ++i) { data.WorldMatrix[i] = new List <Matrix4x4[]>(); data.FrameIndex[i] = new List <float[]>(); data.PreFrameIndex[i] = new List <float[]>(); data.TransitionProgress[i] = new List <float[]>(); } instanceDataPool.Add(instanceName, data); } vertexCache.instanceData = data; return(vertexCache); }
private int FindTexture_internal(string name) { for (int i = 0; i != _boneTextrueInfo.Count; ++i) { BoneTextureInfo texture = _boneTextrueInfo[i]; if (texture.Name == name) { return(i); } } return(-1); }
//给shader传入参数? public void PreparePackageMaterial(VertexCache.InstancingPackage package, VertexCache vertexCache, int aniTextureIndex) { if (vertexCache.boneTextureIndex < 0) { return; } for (int i = 0; i != package.subMeshCount; ++i) { BoneTextureInfo texture = _boneTextrueInfo[vertexCache.boneTextureIndex]; package.material[i].SetTexture("_boneTexture", texture.BoneTexture[aniTextureIndex]); package.material[i].SetInt("_boneTextureWidth", texture.BoneTexture[aniTextureIndex].width); package.material[i].SetInt("_boneTextureHeight", texture.BoneTexture[aniTextureIndex].height); package.material[i].SetInt("_boneTextureBlockWidth", texture.BlockWidth); package.material[i].SetInt("_boneTextureBlockHeight", texture.BlockHeight); } }
//private LoadBoneTexture(BinaryReader reader, string prefebName) private void LoadBoneTexture(BinaryReader reader) { TextureFormat format = TextureFormat.RGBAHalf; int count = reader.ReadInt32(); int blockWidth = reader.ReadInt32(); int blockHeight = reader.ReadInt32(); //byte[] input = new byte[100]; //input = reader.ReadBytes(100); int nameLength; char character; boneNameIndex = new List <string>(); for (int i = 0; i != blockHeight; ++i) { string boneName = ""; nameLength = reader.ReadByte(); for (int j = 0; j != nameLength; ++j) { character = reader.ReadChar(); boneName = boneName + character; } boneNameIndex.Add(boneName); nameLength = reader.ReadByte(); } BoneTextureInfo aniTexture = new BoneTextureInfo(); aniTexture.BoneTexture = new Texture2D[count]; aniTexture.Name = prefebName; aniTexture.BlockWidth = blockWidth; aniTexture.BlockHeight = blockHeight; for (int i = 0; i != count; ++i) { int textureWidth = reader.ReadInt32(); int textureHeight = reader.ReadInt32(); int byteLength = reader.ReadInt32(); byte[] b = new byte[byteLength]; b = reader.ReadBytes(byteLength); Texture2D texture = new Texture2D(textureWidth, textureHeight, format, false); texture.LoadRawTextureData(b); texture.filterMode = FilterMode.Point; texture.Apply(); aniTexture.BoneTexture[i] = texture; FileStream debugfile = File.Open("D:/Format/3DsInput.txt", FileMode.Create); StreamWriter debugwriter = new StreamWriter(debugfile); for (int x = 0; x < textureWidth; x += 4) { for (int y = 0; y < textureHeight; y++) { debugwriter.Write(string.Format( "{0},{1}:[{2};{3};{4};{5}]\r\n", x, y, texture.GetPixel(x, y).ToString(), texture.GetPixel(x + 1, y).ToString(), texture.GetPixel(x + 2, y).ToString(), texture.GetPixel(x + 3, y).ToString())); } } debugwriter.Close(); } _boneTextrueInfo.Add(aniTexture); }