// alias is to use for attachment, it's usually a bone name public void AddMeshVertex(string prefabName, Instancing.LodInfo[] lodInfo, Transform[] bones, List <Matrix4x4> bindPose, int bonePerVertex, string alias = null) { UnityEngine.Profiling.Profiler.BeginSample("AddMeshVertex()"); for (int x = 0; x != lodInfo.Length; ++x) { Instancing.LodInfo lod = lodInfo[x]; for (int i = 0; i != lod.skinnedMeshRenderer.Length; ++i) { Mesh m = lod.skinnedMeshRenderer[i].sharedMesh; if (m == null) { continue; } int nameCode = lod.skinnedMeshRenderer[i].name.GetHashCode(); VertexCache cache = null; //if the nameCode have been add to the dict, load it. if (vertexCachePool.TryGetValue(nameCode, out cache)) { lod.vertexCacheList[i] = cache; continue; } //if not, create a vertexCache and add it to the pool. VertexCache vertexCache = CreateVertexCache(prefabName, nameCode, 0, m); //仅仅是创建,包括PackageList、InstanceData都还为空。 vertexCache.bindPose = bindPose.ToArray(); //刚刚得到的该prefab的所有sharedmesh所用到的bone的bindpose都存进去。 lod.vertexCacheList[i] = vertexCache; //与MeshRender数一一对应。 SetupVertexCache(vertexCache, lod.skinnedMeshRenderer[i], bones, bonePerVertex); } //for those non-skinned mesh for (int i = 0, j = lod.skinnedMeshRenderer.Length; i != lod.meshRenderer.Length; ++i, ++j) { Mesh m = lod.meshFilter[i].sharedMesh; if (m == null) { continue; } int renderName = lod.meshRenderer[i].name.GetHashCode(); int aliasName = (alias != null ? alias.GetHashCode() : 0); VertexCache cache = null; if (vertexCachePool.TryGetValue(renderName + aliasName, out cache)) { lod.vertexCacheList[j] = cache; continue; } VertexCache vertexCache = CreateVertexCache(prefabName, renderName, aliasName, m); if (bindPose != null) { vertexCache.bindPose = bindPose.ToArray(); } lod.vertexCacheList[lod.skinnedMeshRenderer.Length + i] = vertexCache; SetupVertexCache(vertexCache, lod.meshRenderer[i], m, bones, bonePerVertex); } } UnityEngine.Profiling.Profiler.EndSample(); }
private void generateBoneMatrixData() { Vector3 cameraPosition = cameraTransform.position; for (int i = 0; i != _InstancingData.Count; ++i) //对列表中每个对象做Instancing。 { Instancing instance = _InstancingData[i]; //if (!instance.IsPlaying()) // continue; //if (instance.aniIndex < 0 && instance.parentInstance == null) // continue; instance.UpdateAnimation(); //if (!instance.visible) // continue; //here only one Lod //instance.UpdateLod(cameraPosition); //Instancing.LodInfo lod = instance.lodInfo[instance.lodLevel]; Instancing.LodInfo lod = instance.lodInfo[0]; int aniTextureIndex = -1; //if (instance.parentInstance != null) // aniTextureIndex = instance.parentInstance.aniTextureIndex; //else aniTextureIndex = instance.aniTextureIndex; for (int j = 0; j != lod.vertexCacheList.Length; ++j) { VertexCache cache = lod.vertexCacheList[j] as VertexCache; int packageIndex = cache.runtimePackageIndex[aniTextureIndex]; Debug.Assert(packageIndex < cache.packageList[aniTextureIndex].Count); VertexCache.InstancingPackage package = cache.packageList[aniTextureIndex][packageIndex]; if (package.instancingCount + 1 > instancingPackageSize) { ++cache.runtimePackageIndex[aniTextureIndex]; packageIndex = cache.runtimePackageIndex[aniTextureIndex]; if (packageIndex >= cache.packageList[aniTextureIndex].Count) { VertexCache.InstancingPackage newPackage = CreatePackage(cache.instanceData, cache.mesh, cache.materials, aniTextureIndex); cache.packageList[aniTextureIndex].Add(newPackage); PreparePackageMaterial(newPackage, cache, aniTextureIndex); newPackage.instancingCount = 1; } } else { ++package.instancingCount; } } VertexCache vertexCache = lod.vertexCacheList[0]; InstanceData data = vertexCache.instanceData; int index = vertexCache.runtimePackageIndex[aniTextureIndex]; VertexCache.InstancingPackage pkg = vertexCache.packageList[aniTextureIndex][index]; int count = pkg.instancingCount - 1; if (count >= 0) { Matrix4x4 worldMat = instance.worldTransform.localToWorldMatrix; Matrix4x4[] arrayMat = data.WorldMatrix[aniTextureIndex][index]; arrayMat[count].m00 = worldMat.m00; arrayMat[count].m01 = worldMat.m01; arrayMat[count].m02 = worldMat.m02; arrayMat[count].m03 = worldMat.m03; arrayMat[count].m10 = worldMat.m10; arrayMat[count].m11 = worldMat.m11; arrayMat[count].m12 = worldMat.m12; arrayMat[count].m13 = worldMat.m13; arrayMat[count].m20 = worldMat.m20; arrayMat[count].m21 = worldMat.m21; arrayMat[count].m22 = worldMat.m22; arrayMat[count].m23 = worldMat.m23; arrayMat[count].m30 = worldMat.m30; arrayMat[count].m31 = worldMat.m31; arrayMat[count].m32 = worldMat.m32; arrayMat[count].m33 = worldMat.m33; float frameIndex = 0, preFrameIndex = -1; //if (instance.parentInstance != null) //{ // frameIndex = instance.parentInstance.aniInfo[instance.parentInstance.aniIndex].animationIndex + // instance.parentInstance.curFrame; // if (instance.parentInstance.preAniIndex >= 0) // preFrameIndex = // instance.parentInstance.aniInfo[instance.parentInstance.preAniIndex].animationIndex + // instance.parentInstance.preAniFrame; //} //else //{ //frameIndex = instance.aniInfo[instance.aniIndex].animationIndex + instance.curFrame; //if (instance.preAniIndex >= 0) // preFrameIndex = instance.aniInfo[instance.preAniIndex].animationIndex + instance.preAniFrame; frameIndex = instance.curFrame; if (instance.preAniIndex >= 0) { preFrameIndex = instance.preAniFrame; } //} data.FrameIndex[aniTextureIndex][index][count] = frameIndex; data.PreFrameIndex[aniTextureIndex][index][count] = preFrameIndex; data.TransitionProgress[aniTextureIndex][index][count] = instance.transitionProgress; } } }