Exemplo n.º 1
0
    protected override void OnUpdate()
    {
        var t = (float)Time.ElapsedTime;

        Entities.ForEach((ref DynamicBuffer <BlendShapeWeight> weights, in AnimateBlendShape data) =>
        {
            var frequency     = data.Frequency;
            var phase         = data.PhaseShift;
            var interpolation = 0.5f * (math.sin(k_Two_Pi * frequency * (t + phase)) + 1f);
            var weight        = math.lerp(data.From, data.To, interpolation);

            for (int i = 0; i < weights.Length; ++i)
            {
                weights[i] = new BlendShapeWeight {
                    Value = weight
                };
            }
        }).ScheduleParallel();
    }
Exemplo n.º 2
0
    protected override void OnUpdate()
    {
        var et = (float)Time.ElapsedTime;

        Entities.ForEach((
                             ref DynamicBuffer <BlendShapeWeight> weights,
                             in UpdateBlendWeightData bwData
                             ) =>
        {
            var weightValue = Mathf.Abs(Mathf.Sin(et * bwData.Value) * 100f);
            for (int index = 0; index < weights.Length; ++index)
            {
                weights[index] = new BlendShapeWeight {
                    Value = weightValue
                };
            }
        }
                         ).ScheduleParallel();
    }
Exemplo n.º 3
0
        private static void ConvertBlendShapeData(GameObjectConversionSystem gsys,
                                                  UnityEngine.SkinnedMeshRenderer uSkinnedMeshRenderer, Entity meshRendererEntity)
        {
            Mesh uMesh = uSkinnedMeshRenderer.sharedMesh;

            if (uMesh.blendShapeCount <= 0)
            {
                return;
            }
            DynamicBuffer <BlendShapeWeight> blendShapeWeightsBuffer =
                gsys.DstEntityManager.AddBuffer <BlendShapeWeight>(meshRendererEntity);

            for (int shapeIndex = 0; shapeIndex < uMesh.blendShapeCount; shapeIndex++)
            {
                string blendShapeName = uMesh.GetBlendShapeName(shapeIndex);
                float  curWeight      = uSkinnedMeshRenderer.GetBlendShapeWeight(shapeIndex);

                BlendShapeWeight blendShapeWeight = new BlendShapeWeight();
                blendShapeWeight.NameHash  = BlendShapeChannel.GetNameHash(blendShapeName);
                blendShapeWeight.CurWeight = curWeight;
                blendShapeWeightsBuffer.Add(blendShapeWeight);
            }
        }
Exemplo n.º 4
0
        protected override void OnUpdate()
        {
            var materials = new List <Material>(10);
            var context   = new RenderMeshConversionContext(DstEntityManager, this)
            {
                AttachToPrimaryEntityForSingleMaterial = false,
            };

            Entities.ForEach((SkinnedMeshRenderer meshRenderer) =>
            {
                meshRenderer.GetSharedMaterials(materials);
                foreach (var material in materials)
                {
                    if (material == null)
                    {
                        continue;
                    }

                    var supportsSkinning = material.HasProperty(s_SkinMatrixIndexProperty) || material.HasProperty(s_ComputeMeshIndexProperty);
                    if (!supportsSkinning)
                    {
                        string errorMsg = "";
                        errorMsg       += $"Shader [{material.shader.name}] on [{meshRenderer.name}] does not support skinning. This can result in incorrect rendering.{System.Environment.NewLine}";
                        errorMsg       += $"Please see documentation for Linear Blend Skinning Node and Compute Deformation Node in Shader Graph.{System.Environment.NewLine}";
                        Debug.LogWarning(errorMsg, meshRenderer);
                    }
                }

                var mesh           = meshRenderer.sharedMesh;
                var root           = meshRenderer.rootBone ? meshRenderer.rootBone : meshRenderer.transform;
                var hasSkinning    = mesh == null ? false : mesh.boneWeights.Length > 0 && mesh.bindposes.Length > 0;
                var hasBlendShapes = mesh == null ? false : mesh.blendShapeCount > 0;
                var deformedEntity = GetPrimaryEntity(meshRenderer);

                // Convert Renderers as normal MeshRenderers.
                // No need to process light maps as skinned objects are never light mapped.
                context.Convert(meshRenderer, mesh, materials, root);

                foreach (var rendererEntity in GetEntities(meshRenderer))
                {
                    if (DstEntityManager.HasComponent <RenderMesh>(rendererEntity))
                    {
                        // Add relevant deformation tags to converted render entities and link them to the DeformedEntity.
#if ENABLE_COMPUTE_DEFORMATIONS
                        DstEntityManager.AddComponentData(rendererEntity, new DeformedMeshIndex());
#endif
                        DstEntityManager.AddComponentData(rendererEntity, new DeformedEntity {
                            Value = deformedEntity
                        });

                        DstEntityManager.AddComponentData(rendererEntity, new RenderBounds {
                            Value = meshRenderer.localBounds.ToAABB()
                        });

                        if (hasSkinning)
                        {
                            DstEntityManager.AddComponent <SkinningTag>(rendererEntity);
                        }

                        if (hasBlendShapes)
                        {
                            DstEntityManager.AddComponent <BlendShapeTag>(rendererEntity);
                        }
                    }
                }

                // Fill the blend shape weights.
                if (hasBlendShapes && !DstEntityManager.HasComponent <BlendShapeWeight>(deformedEntity))
                {
                    DstEntityManager.AddBuffer <BlendShapeWeight>(deformedEntity);
                    var weights = DstEntityManager.GetBuffer <BlendShapeWeight>(deformedEntity);
                    weights.ResizeUninitialized(meshRenderer.sharedMesh.blendShapeCount);

                    for (int i = 0; i < weights.Length; ++i)
                    {
                        weights[i] = new BlendShapeWeight {
                            Value = meshRenderer.GetBlendShapeWeight(i)
                        };
                    }
                }

                // Fill the skin matrices with bindpose skin matrices.
                if (hasSkinning && !DstEntityManager.HasComponent <SkinMatrix>(deformedEntity))
                {
                    var bones         = meshRenderer.bones;
                    var rootMatrixInv = root.localToWorldMatrix.inverse;

                    DstEntityManager.AddBuffer <SkinMatrix>(deformedEntity);
                    var skinMatrices = DstEntityManager.GetBuffer <SkinMatrix>(deformedEntity);
                    skinMatrices.ResizeUninitialized(bones.Length);

                    for (int i = 0; i < bones.Length; ++i)
                    {
                        var bindPose         = meshRenderer.sharedMesh.bindposes[i];
                        var boneMatRootSpace = math.mul(rootMatrixInv, bones[i].localToWorldMatrix);
                        var skinMatRootSpace = math.mul(boneMatRootSpace, bindPose);
                        skinMatrices[i]      = new SkinMatrix {
                            Value = new float3x4(skinMatRootSpace.c0.xyz, skinMatRootSpace.c1.xyz, skinMatRootSpace.c2.xyz, skinMatRootSpace.c3.xyz)
                        };
                    }
                }
            });

            context.EndConversion();
        }
Exemplo n.º 5
0
        protected override void OnUpdate()
        {
            var materials = new List <Material>(10);

            Entities.ForEach((SkinnedMeshRenderer meshRenderer) =>
            {
                meshRenderer.GetSharedMaterials(materials);
                var mesh           = meshRenderer.sharedMesh;
                var root           = meshRenderer.rootBone ? meshRenderer.rootBone : meshRenderer.transform;
                var hasSkinning    = mesh == null ? false : mesh.boneWeights.Length > 0 && mesh.bindposes.Length > 0;
                var hasBlendShapes = mesh == null ? false : mesh.blendShapeCount > 0;
                var deformedEntity = GetPrimaryEntity(meshRenderer);

                // Convert Renderers as normal MeshRenderers.
                MeshRendererConversion.Convert(DstEntityManager, this, k_AttachToPrimaryEntityForSingleMaterial, meshRenderer, mesh, materials, root, meshRenderer.localBounds.ToAABB());

                foreach (var rendererEntity in GetEntities(meshRenderer))
                {
                    if (DstEntityManager.HasComponent <RenderMesh>(rendererEntity))
                    {
                        // Add relevant deformation tags to converted render entities and link them to the DeformedEntity.
#if ENABLE_COMPUTE_DEFORMATIONS
                        DstEntityManager.AddComponentData(rendererEntity, new DeformedMeshIndex());
#endif
                        DstEntityManager.AddComponentData(rendererEntity, new DeformedEntity {
                            Value = deformedEntity
                        });

                        if (hasSkinning)
                        {
                            DstEntityManager.AddComponent <SkinningTag>(rendererEntity);
                        }

                        if (hasBlendShapes)
                        {
                            DstEntityManager.AddComponent <BlendShapeTag>(rendererEntity);
                        }
                    }
                }

                // Fill the blend shape weights.
                if (hasBlendShapes && !DstEntityManager.HasComponent <BlendShapeWeight>(deformedEntity))
                {
                    DstEntityManager.AddBuffer <BlendShapeWeight>(deformedEntity);
                    var weights = DstEntityManager.GetBuffer <BlendShapeWeight>(deformedEntity);
                    weights.ResizeUninitialized(meshRenderer.sharedMesh.blendShapeCount);

                    for (int i = 0; i < weights.Length; ++i)
                    {
                        weights[i] = new BlendShapeWeight {
                            Value = meshRenderer.GetBlendShapeWeight(i)
                        };
                    }
                }

                // Fill the skin matrices with bindpose skin matrices.
                if (hasSkinning && !DstEntityManager.HasComponent <SkinMatrix>(deformedEntity))
                {
                    var bones         = meshRenderer.bones;
                    var rootMatrixInv = root.localToWorldMatrix.inverse;

                    DstEntityManager.AddBuffer <SkinMatrix>(deformedEntity);
                    var skinMatrices = DstEntityManager.GetBuffer <SkinMatrix>(deformedEntity);
                    skinMatrices.ResizeUninitialized(bones.Length);

                    for (int i = 0; i < bones.Length; ++i)
                    {
                        var bindPose         = meshRenderer.sharedMesh.bindposes[i];
                        var boneMatRootSpace = math.mul(rootMatrixInv, bones[i].localToWorldMatrix);
                        var skinMatRootSpace = math.mul(boneMatRootSpace, bindPose);
                        skinMatrices[i]      = new SkinMatrix {
                            Value = new float3x4(skinMatRootSpace.c0.xyz, skinMatRootSpace.c1.xyz, skinMatRootSpace.c2.xyz, skinMatRootSpace.c3.xyz)
                        };
                    }
                }
            });
        }