void SaveBlendshapesForEyelidPosition(ref EyelidPositionBlendshape[] blendshapesForPosition, Object rootObject, string positionName) { SkinnedMeshRenderer[] skinnedMeshRenderers = (rootObject as MonoBehaviour).GetComponentsInChildren<SkinnedMeshRenderer>(); List<EyelidPositionBlendshape> blendshapeForPositionList = new List<EyelidPositionBlendshape>(); if ( skinnedMeshRenderers.Length != blendshapesConfigs.Length ) { Debug.LogError("The saved data for open eyelids is invalid. Please reset to open eyelids and resave 'Eyes open, looking straight'."); isEyelidBlendshapeDefaultSet = false; isEyelidBlendshapeClosedSet = false; isEyelidBlendshapeLookDownSet = false; isEyelidBlendshapeLookUpSet = false; } else { for ( int i=0; i<skinnedMeshRenderers.Length; i++ ) { SkinnedMeshRenderer skinnedMeshRenderer = skinnedMeshRenderers[i]; BlendshapesConfig blendshapesConfig = blendshapesConfigs[i]; if ( skinnedMeshRenderer != blendshapesConfig.skinnedMeshRenderer || skinnedMeshRenderer.sharedMesh.blendShapeCount != blendshapesConfig.blendShapeCount ) { Debug.LogError("The saved data for open eyelids is invalid. Please reset to open eyelids and resave 'Eyes open, looking straight'."); isEyelidBlendshapeDefaultSet = false; isEyelidBlendshapeClosedSet = false; isEyelidBlendshapeLookDownSet = false; isEyelidBlendshapeLookUpSet = false; } else { for ( int j=0; j<blendshapesConfig.blendShapeCount; j++ ) { const float kEpsilon = 0.01f; if ( Mathf.Abs(blendshapesConfig.blendshapeWeights[j] - skinnedMeshRenderer.GetBlendShapeWeight(j)) >= kEpsilon ) { EyelidPositionBlendshape eyePositionBlendshape = new EyelidPositionBlendshape { skinnedMeshRenderer = skinnedMeshRenderer, index = j, defaultWeight = blendshapesConfig.blendshapeWeights[j], positionWeight = skinnedMeshRenderer.GetBlendShapeWeight(j) }; blendshapeForPositionList.Add( eyePositionBlendshape ); } } } } blendshapesForPosition = blendshapeForPositionList.ToArray(); Debug.Log("Found " + blendshapesForPosition.Length + " blend shapes for " + positionName + ":"); foreach ( EyelidPositionBlendshape blendshapeForPosition in blendshapesForPosition ) Debug.Log(blendshapeForPosition.skinnedMeshRenderer.name + " --> " + blendshapeForPosition.skinnedMeshRenderer.sharedMesh.GetBlendShapeName( blendshapeForPosition.index) + " open: " + blendshapeForPosition.defaultWeight + " closed: " + blendshapeForPosition.positionWeight); } }
void LerpBlendshapeConfig( EyelidPositionBlendshape[] blendshapes, float lerpValue, bool relativeToCurrentValueIfUsedInOtherConfig=false ) { foreach ( EyelidPositionBlendshape blendShape in blendshapes ) blendShape.skinnedMeshRenderer .SetBlendShapeWeight( blendShape.index, Mathf.Lerp( (blendShape.isUsedInEalierConfig && relativeToCurrentValueIfUsedInOtherConfig) ? blendShape.skinnedMeshRenderer.GetBlendShapeWeight(blendShape.index) : blendShape.defaultWeight, blendShape.positionWeight, lerpValue) ); }
void ResetBlendshapeConfig( EyelidPositionBlendshape[] blendshapes ) { if ( blendshapes == null ) return; foreach ( EyelidPositionBlendshape blendShape in blendshapes ) blendShape.skinnedMeshRenderer.SetBlendShapeWeight( blendShape.index, blendShape.defaultWeight ); }
public void Import(ControlDataForExport import, Transform startXform) { eyeControl = import.eyeControl; eyelidBoneMode = import.eyelidBoneMode; leftEye = Utils.GetTransformFromPath(startXform, import.leftEyePath); rightEye = Utils.GetTransformFromPath(startXform, import.rightEyePath); maxEyeUpBoneAngle = import.maxEyeUpBoneAngle; maxEyeDownBoneAngle = import.maxEyeDownBoneAngle; maxEyeUpEyeballAngle = import.maxEyeUpEyeballAngle; maxEyeDownEyeballAngle = import.maxEyeDownEyeballAngle; isEyeBallDefaultSet = import.isEyeBallDefaultSet; isEyeBoneDefaultSet = import.isEyeBoneDefaultSet; isEyeBallLookUpSet = import.isEyeBallLookUpSet; isEyeBoneLookUpSet = import.isEyeBoneLookUpSet; isEyeBallLookDownSet = import.isEyeBallLookDownSet; isEyeBoneLookDownSet = import.isEyeBoneLookDownSet; eyelidControl = import.eyelidControl; eyelidsFollowEyesVertically = import.eyelidsFollowEyesVertically; upperEyeLidLeft = Utils.GetTransformFromPath(startXform, import.upperEyeLidLeftPath); upperEyeLidRight = Utils.GetTransformFromPath(startXform, import.upperEyeLidRightPath); lowerEyeLidLeft = Utils.GetTransformFromPath(startXform, import.lowerEyeLidLeftPath); lowerEyeLidRight = Utils.GetTransformFromPath(startXform, import.lowerEyeLidRightPath); isEyelidBonesDefaultSet = import.isEyelidBonesDefaultSet; isEyelidBonesClosedSet = import.isEyelidBonesClosedSet; isEyelidBonesLookUpSet = import.isEyelidBonesLookUpSet; isEyelidBonesLookDownSet = import.isEyelidBonesLookDownSet; eyeWidenOrSquint = import.eyeWidenOrSquint; isEyelidBlendshapeDefaultSet = import.isEyelidBlendshapeDefaultSet; isEyelidBlendshapeClosedSet = import.isEyelidBlendshapeClosedSet; isEyelidBlendshapeLookUpSet = import.isEyelidBlendshapeLookUpSet; isEyelidBlendshapeLookDownSet = import.isEyelidBlendshapeLookDownSet; leftBoneEyeRotationLimiter.Import(import.leftBoneEyeRotationLimiter, startXform); rightBoneEyeRotationLimiter.Import(import.rightBoneEyeRotationLimiter, startXform); leftEyeballEyeRotationLimiter.Import(import.leftEyeballEyeRotationLimiter, startXform); rightEyeballEyeRotationLimiter.Import(import.rightEyeballEyeRotationLimiter, startXform); upperLeftLimiter.Import(import.upperLeftLimiter, startXform); upperRightLimiter.Import(import.upperRightLimiter, startXform); lowerLeftLimiter.Import(import.lowerLeftLimiter, startXform); lowerRightLimiter.Import(import.lowerRightLimiter, startXform); if ( import.blendshapesForBlinking != null ) { blendshapesForBlinking = new EyelidPositionBlendshape[import.blendshapesForBlinking.Length]; for ( int i=0; i<import.blendshapesForBlinking.Length; i++ ) { EyelidPositionBlendshape eyelidPositionBlendshape = new EyelidPositionBlendshape(); eyelidPositionBlendshape.Import(import.blendshapesForBlinking[i], startXform); blendshapesForBlinking[i] = eyelidPositionBlendshape; } } if ( import.blendshapesForLookingUp != null ) { blendshapesForLookingUp = new EyelidPositionBlendshape[import.blendshapesForLookingUp.Length]; for ( int i=0; i<import.blendshapesForLookingUp.Length; i++ ) { EyelidPositionBlendshape eyelidPositionBlendshape = new EyelidPositionBlendshape(); eyelidPositionBlendshape.Import(import.blendshapesForLookingUp[i], startXform); blendshapesForLookingUp[i] = eyelidPositionBlendshape; } } if ( import.blendshapesForLookingDown != null ) { blendshapesForLookingDown = new EyelidPositionBlendshape[import.blendshapesForLookingDown.Length]; for ( int i=0; i<import.blendshapesForLookingDown.Length; i++ ) { EyelidPositionBlendshape eyelidPositionBlendshape = new EyelidPositionBlendshape(); eyelidPositionBlendshape.Import(import.blendshapesForLookingDown[i], startXform); blendshapesForLookingDown[i] = eyelidPositionBlendshape; } } bool canImportBlendshapeConfigs = false; SkinnedMeshRenderer[] skinnedMeshRenderers = startXform.GetComponentsInChildren<SkinnedMeshRenderer>(); if (import.blendshapesConfigs != null) if ( skinnedMeshRenderers.Length == import.blendshapesConfigs.Length ) { canImportBlendshapeConfigs = true; foreach (BlendshapesConfigForExport blendshapeConfigImport in import.blendshapesConfigs) if (false == BlendshapesConfig.CanImport(blendshapeConfigImport, startXform)) { canImportBlendshapeConfigs = false; break; } } if ( canImportBlendshapeConfigs ) { blendshapesConfigs = new BlendshapesConfig[import.blendshapesConfigs.Length]; for ( int i=0; i<import.blendshapesConfigs.Length; i++ ) { BlendshapesConfig blendshapesConfig = new BlendshapesConfig(); blendshapesConfig.Import(startXform, import.blendshapesConfigs[i]); blendshapesConfigs[i] = blendshapesConfig; } } else blendshapesConfigs = null; }