/// <summary>
        /// Safely triggers <see cref="OnDidImportModelTexture"/>
        /// </summary>
        /// <param name="importer">Importer.</param>
        /// <param name="model">Imported model.</param>
        /// <param name="texture">Imported texture.</param>
        internal static void SendModelTextureImportEvent(CubismModel3JsonImporter importer, CubismModel model, Texture2D texture)
        {
            if (OnDidImportTexture == null)
            {
                return;
            }


            OnDidImportTexture(importer, model, texture);
        }
        /// <summary>
        /// Safely triggers <see cref="OnDidImportModel"/>.
        /// </summary>
        /// <param name="importer">Importer.</param>
        /// <param name="model">Imported model.</param>
        internal static void SendModelImportEvent(CubismModel3JsonImporter importer, CubismModel model)
        {
            if (OnDidImportModel == null)
            {
                return;
            }


            OnDidImportModel(importer, model);
        }
        /// <summary>
        /// Makes sure textures used by Cubism models have the <see cref="TextureImporter.alphaIsTransparency"/> option enabled.
        /// </summary>
        /// <param name="importer">Importer.</param>
        /// <param name="model">Imported model.</param>
        /// <param name="texture">Imported texture.</param>
        private static void BuiltinTextureImportHandler(CubismModel3JsonImporter importer, CubismModel model, Texture2D texture)
        {
            var textureImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(texture)) as TextureImporter;


            // Return early if texture already seems to be set up.
            if (textureImporter.alphaIsTransparency)
            {
                return;
            }


            // Set up texture importing.
            textureImporter.alphaIsTransparency = true;
            textureImporter.textureType         = TextureImporterType.Default;


            EditorUtility.SetDirty(texture);
        }
Exemple #4
0
        /// <summary>
        /// Create pose motions.
        /// </summary>
        /// <param name="sender">Event source.</param>
        /// <param name="model">Imported model.</param>
        private static void OnModelImport(CubismModel3JsonImporter sender, CubismModel model)
        {
            var shouldImportAsOriginalWorkflow = CubismUnityEditorMenu.ShouldImportAsOriginalWorkflow;
            var pose3Json = sender.Model3Json.Pose3Json;

            // Fail silently...
            if (!shouldImportAsOriginalWorkflow || pose3Json == null)
            {
                return;
            }

            var assetsDirectoryPath = Application.dataPath.Replace("Assets", "");
            var assetPath           = sender.AssetPath.Replace(assetsDirectoryPath, "");
            var fileReferences      = sender.Model3Json.FileReferences;

            // Create pose animation clip
            var motions = new List <CubismModel3Json.SerializableMotion>();

            if (fileReferences.Motions.GroupNames != null)
            {
                for (var i = 0; i < fileReferences.Motions.GroupNames.Length; i++)
                {
                    motions.AddRange(fileReferences.Motions.Motions[i]);
                }
            }


            for (var i = 0; i < motions.Count; ++i)
            {
                var motionPath = Path.GetDirectoryName(assetPath) + "/" + motions[i].File;
                var jsonString = string.IsNullOrEmpty(motionPath)
                                    ? null
                                    : File.ReadAllText(motionPath);

                if (jsonString == null)
                {
                    continue;
                }

                var directoryPath = Path.GetDirectoryName(assetPath) + "/";
                var motion3Json   = CubismMotion3Json.LoadFrom(jsonString);

                var animationClipPath = directoryPath + motions[i].File.Replace(".motion3.json", ".anim");
                var newAnimationClip  = motion3Json.ToAnimationClip(shouldImportAsOriginalWorkflow, false, true, pose3Json);
                var oldAnimationClip  = AssetDatabase.LoadAssetAtPath <AnimationClip>(animationClipPath);

                // Create animation clip.
                if (oldAnimationClip == null)
                {
                    AssetDatabase.CreateAsset(newAnimationClip, animationClipPath);
                    oldAnimationClip = newAnimationClip;
                }
                // Update animation clip.
                else
                {
                    EditorUtility.CopySerialized(newAnimationClip, oldAnimationClip);
                    EditorUtility.SetDirty(oldAnimationClip);
                    newAnimationClip = oldAnimationClip;
                }

                // Add animation event
                {
                    var instanceId = newAnimationClip.GetInstanceID();

                    var sourceAnimationEvents = AnimationUtility.GetAnimationEvents(newAnimationClip);
                    var index = -1;

                    for (var j = 0; j < sourceAnimationEvents.Length; ++j)
                    {
                        if (sourceAnimationEvents[j].functionName != "InstanceId")
                        {
                            continue;
                        }

                        index = j;
                        break;
                    }

                    if (index == -1)
                    {
                        index = sourceAnimationEvents.Length;
                        Array.Resize(ref sourceAnimationEvents, sourceAnimationEvents.Length + 1);
                        sourceAnimationEvents[sourceAnimationEvents.Length - 1] = new AnimationEvent();
                    }

                    sourceAnimationEvents[index].time           = 0;
                    sourceAnimationEvents[index].functionName   = "InstanceId";
                    sourceAnimationEvents[index].intParameter   = instanceId;
                    sourceAnimationEvents[index].messageOptions = SendMessageOptions.DontRequireReceiver;

                    AnimationUtility.SetAnimationEvents(newAnimationClip, sourceAnimationEvents);
                }


                // update fade motion data.
                var fadeMotionPath = directoryPath + motions[i].File.Replace(".motion3.json", ".fade.asset");
                var fadeMotion     = AssetDatabase.LoadAssetAtPath <CubismFadeMotionData>(fadeMotionPath);

                if (fadeMotion == null)
                {
                    fadeMotion = CubismFadeMotionData.CreateInstance(motion3Json, fadeMotionPath.Replace(directoryPath, ""),
                                                                     newAnimationClip.length, shouldImportAsOriginalWorkflow, true);

                    AssetDatabase.CreateAsset(fadeMotion, fadeMotionPath);

                    EditorUtility.SetDirty(fadeMotion);

                    // Fade用モーションの参照をリストに追加
                    var directoryName      = Path.GetDirectoryName(fadeMotionPath).ToString();
                    var modelDir           = Path.GetDirectoryName(directoryName).ToString();
                    var modelName          = Path.GetFileName(modelDir).ToString();
                    var fadeMotionListPath = Path.GetDirectoryName(directoryName).ToString() + "/" + modelName + ".fadeMotionList.asset";
                    var fadeMotions        = AssetDatabase.LoadAssetAtPath <CubismFadeMotionList>(fadeMotionListPath);

                    // 参照リスト作成
                    if (fadeMotions == null)
                    {
                        fadeMotions = ScriptableObject.CreateInstance <CubismFadeMotionList>();
                        fadeMotions.MotionInstanceIds       = new int[0];
                        fadeMotions.CubismFadeMotionObjects = new CubismFadeMotionData[0];
                        AssetDatabase.CreateAsset(fadeMotions, fadeMotionListPath);
                    }

                    var instanceId  = oldAnimationClip.GetInstanceID();
                    var motionIndex = Array.IndexOf(fadeMotions.MotionInstanceIds, instanceId);
                    if (motionIndex != -1)
                    {
                        fadeMotions.CubismFadeMotionObjects[motionIndex] = fadeMotion;
                    }
                    else
                    {
                        motionIndex = fadeMotions.MotionInstanceIds.Length;

                        Array.Resize(ref fadeMotions.MotionInstanceIds, motionIndex + 1);
                        fadeMotions.MotionInstanceIds[motionIndex] = instanceId;

                        Array.Resize(ref fadeMotions.CubismFadeMotionObjects, motionIndex + 1);
                        fadeMotions.CubismFadeMotionObjects[motionIndex] = fadeMotion;
                    }

                    EditorUtility.SetDirty(fadeMotions);
                }

                for (var curveIndex = 0; curveIndex < motion3Json.Curves.Length; ++curveIndex)
                {
                    var curve = motion3Json.Curves[curveIndex];

                    if (curve.Target == "PartOpacity")
                    {
                        if (pose3Json.FadeInTime == 0.0f)
                        {
                            fadeMotion.ParameterIds[curveIndex]          = curve.Id;
                            fadeMotion.ParameterFadeInTimes[curveIndex]  = pose3Json.FadeInTime;
                            fadeMotion.ParameterFadeOutTimes[curveIndex] = (curve.FadeOutTime < 0.0f) ? -1.0f : curve.FadeOutTime;
                            fadeMotion.ParameterCurves[curveIndex]       = new AnimationCurve(CubismMotion3Json.ConvertCurveSegmentsToKeyframes(curve.Segments));
                        }
                        else
                        {
                            fadeMotion.ParameterIds[curveIndex]          = curve.Id;
                            fadeMotion.ParameterFadeInTimes[curveIndex]  = pose3Json.FadeInTime;
                            fadeMotion.ParameterFadeOutTimes[curveIndex] = (curve.FadeOutTime < 0.0f) ? -1.0f : curve.FadeOutTime;
                            fadeMotion.ParameterCurves[curveIndex]       = CubismMotion3Json.ConvertSteppedCurveToLinerCurver(curve, pose3Json.FadeInTime);
                        }
                    }
                }

                EditorUtility.SetDirty(fadeMotion);
            }

            InitializePosePart(model.Parts, pose3Json.Groups);
        }