/// <summary>
            /// Add <see cref="MasksMaskedsPair"/> to the list.
            /// </summary>
            public void Add(CubismDrawable masked, CubismDrawable[] masks)
            {
                // Try to add masked to existing mask compound.
                for (var i = 0; i < Entries.Count; ++i)
                {
                    var match = (Entries[i].Masks.Length == masks.Length);


                    if (!match)
                    {
                        continue;
                    }


                    for (var j = 0; j < Entries[i].Masks.Length; ++j)
                    {
                        if (Entries[i].Masks[j] != masks[j].GetComponent <CubismRenderer>())
                        {
                            match = false;


                            break;
                        }
                    }


                    if (!match)
                    {
                        continue;
                    }


                    Entries[i].Maskeds.Add(masked.GetComponent <CubismRenderer>());


                    return;
                }


                // Create new pair.
                var renderers = new CubismRenderer[masks.Length];


                for (var i = 0; i < masks.Length; ++i)
                {
                    renderers[i] = masks[i].GetComponent <CubismRenderer>();
                }


                Entries.Add(new MasksMaskedsPair
                {
                    Masks   = renderers,
                    Maskeds = new List <CubismRenderer>()
                    {
                        masked.GetComponent <CubismRenderer>()
                    }
                });
            }
Esempio n. 2
0
        /// <summary>
        /// Builtin <see cref="Material"/> picker.
        /// </summary>
        /// <param name="sender">Event source.</param>
        /// <param name="drawable">Drawable to map to.</param>
        /// <returns>Mapped texture.</returns>
        public static Material MaterialPicker(CubismModel3Json sender, CubismDrawable drawable)
        {
            if (drawable.IsDoubleSided)
            {
                if (drawable.BlendAdditive)
                {
                    return((drawable.IsMasked)
                        ? (drawable.IsInverted) ? CubismBuiltinMaterials.UnlitAdditiveMaskedInverted :
                           CubismBuiltinMaterials.UnlitAdditiveMasked
                        : CubismBuiltinMaterials.UnlitAdditive);
                }


                if (drawable.MultiplyBlend)
                {
                    return((drawable.IsMasked)
                        ? (drawable.IsInverted) ? CubismBuiltinMaterials.UnlitMultiplyMaskedInverted :
                           CubismBuiltinMaterials.UnlitMultiplyMasked
                        : CubismBuiltinMaterials.UnlitMultiply);
                }


                return((drawable.IsMasked)
                    ? (drawable.IsInverted) ? CubismBuiltinMaterials.UnlitMaskedInverted :
                       CubismBuiltinMaterials.UnlitMasked
                    : CubismBuiltinMaterials.Unlit);
            }

            if (drawable.BlendAdditive)
            {
                return((drawable.IsMasked)
                    ? (drawable.IsInverted) ? CubismBuiltinMaterials.UnlitAdditiveMaskedInvertedCulling :
                       CubismBuiltinMaterials.UnlitAdditiveMaskedCulling
                    : CubismBuiltinMaterials.UnlitAdditiveCulling);
            }


            if (drawable.MultiplyBlend)
            {
                return((drawable.IsMasked)
                    ? (drawable.IsInverted) ? CubismBuiltinMaterials.UnlitMultiplyMaskedInvertedCulling :
                       CubismBuiltinMaterials.UnlitMultiplyMaskedCulling
                    : CubismBuiltinMaterials.UnlitMultiplyCulling);
            }


            return((drawable.IsMasked)
                ? (drawable.IsInverted) ? CubismBuiltinMaterials.UnlitMaskedInvertedCulling :
                   CubismBuiltinMaterials.UnlitMaskedCulling
                : CubismBuiltinMaterials.UnlitCulling);
        }
        protected override void DrawMask(CubismDrawable drawable, Matrix4 clippingMatrix)
        {
            var shader = shaders[0];

            if (drawable.ConstantFlags.HasFlag(ConstantDrawableFlags.IsDoubleSided))
            {
                GL.Enable(EnableCap.CullFace);
            }
            else
            {
                GL.Disable(EnableCap.CullFace);
            }

            shader.Bind();

            shader.GetUniform <int>("s_texture0").Value = 0;

            shader.GetUniform <Vector4>("u_channelFlag").Value = new Vector4(1.0f, 0.0f, 0.0f, 0.0f);
            shader.GetUniform <Matrix4>("u_clipMatrix").Value  = clippingMatrix;
            shader.GetUniform <Vector4>("u_baseColor").Value   = new Vector4(-1.0f, -1.0f, 1.0f, 1.0f);

            GL.EnableVertexAttribArray(0);

            fixed(float *pinnedVertexBuffer = drawable.Vertices)
            GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, sizeof(float) * 2, (IntPtr)pinnedVertexBuffer);

            GL.EnableVertexAttribArray(1);

            fixed(float *pinnedUVBuffer = drawable.TextureCoordinates)
            GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, sizeof(float) * 2, (IntPtr)pinnedUVBuffer);

            fixed(short *pinnedIndexBuffer = drawable.Indices)
            GL.DrawElements(PrimitiveType.Triangles, drawable.Indices.Length, DrawElementsType.UnsignedShort, (IntPtr)pinnedIndexBuffer);

            shader.Unbind();
        }
        /// <summary>
        /// カスタマイズされたマテリアルを適用する
        /// </summary>
        /// <param name="drawable"></param>
        /// <returns></returns>
        private static Material CustomizeMaterial(CubismDrawable drawable)
        {
            Material material = null;

            var cubismRenderer = drawable.gameObject.GetComponent <CubismRenderer>();

            var split = drawable.Id.Split('_');

            if (split.Length >= 2)
            {
                var originalName = split[0] + split[1];
                var joinName     = string.Empty;
                switch (split[0])
                {
                //レンダーテクスチャ(パーティクル)
                //particle_部位名(_(スキニングしている場合記号))
                case "Particle":

                    var particleMaterialFolderPath = GetMaterialFolder();
                    joinName = particleMaterialFolderPath + "/" + originalName;
                    material = AssetDatabase.LoadAssetAtPath <Material>(joinName + ".mat");
                    CustomRenderTexture customRenderTexture = null;
                    if (material == null)
                    {
                        material = new Material(Shader.Find("Live2DVerification/CubismModelRenderTexture"));

                        AssetDatabase.CreateAsset(material, joinName + ".mat");
                        //TODO 空のテクスチャを作成

                        customRenderTexture = new CustomRenderTexture(cubismRenderer.MainTexture.width, cubismRenderer.MainTexture.height);
                        customRenderTexture.initializationMode = CustomRenderTextureUpdateMode.Realtime;
                        customRenderTexture.updateMode         = CustomRenderTextureUpdateMode.Realtime;
                        AssetDatabase.CreateAsset(customRenderTexture, joinName + "CustomRenderTexture.asset");

                        //TODO
                        material.SetTexture("RenderTex", customRenderTexture);

                        OrtegaLogger.Log("CustomMaterial setting complete." +
                                         $"setting target:{drawable.Id}" +
                                         $"create:{joinName}CustomRenderTexture.asset & {joinName}.mat");
                    }
                    else
                    {
                        customRenderTexture = AssetDatabase.LoadAssetAtPath <CustomRenderTexture>(joinName + "CustomRenderTexture.asset");
                    }

                    //live2d専用のマテリアル設定
                    material.SetInt("_SrcColor", 1);
                    material.SetInt("_DstColor", 10);
                    material.SetInt("_SrcAlpha", 1);
                    material.SetInt("_DstAlpha", 10);
                    //独自のテクスチャ設定
                    material.SetTexture("_RenderTex", customRenderTexture);

                    AssetDatabase.SaveAssets();
                    AssetDatabase.Refresh();
                    break;

                //流れるテクスチャ
                //flowTexture_部位名(_(スキニングしている場合記号))
                case "FlowTexture":

                    var flowTextureMaterialFolderPath = GetMaterialFolder();
                    joinName = flowTextureMaterialFolderPath + "/" + originalName;
                    material = AssetDatabase.LoadAssetAtPath <Material>(joinName + ".mat");
                    Texture2D subTexture  = null;
                    Texture2D flowTexture = null;
                    if (material == null)
                    {
                        material = new Material(Shader.Find("Live2DVerification/CubismModelFlowTexture"));
                        AssetDatabase.CreateAsset(material, joinName + ".mat");

                        //TODO 空のテクスチャを作成
                        subTexture = new Texture2D(cubismRenderer.MainTexture.width, cubismRenderer.MainTexture.height, TextureFormat.ARGB32, false, false);
                        var subTextureBytes = subTexture.EncodeToPNG();
                        File.WriteAllBytes(joinName + "SubTexture.png", subTextureBytes);

                        flowTexture = new Texture2D(cubismRenderer.MainTexture.width, cubismRenderer.MainTexture.height, TextureFormat.ARGB32, false, false);

                        var flowTextureBytes = flowTexture.EncodeToPNG();
                        File.WriteAllBytes(joinName + "FlowTexture.png", flowTextureBytes);

                        Debug.Log("CustomMaterial setting complete." +
                                  $"setting target:{drawable.Id}" +
                                  $"create:{joinName}SubTexture.png & {joinName}FlowTexture.png & {joinName}.mat");
                    }
                    else
                    {
                        subTexture  = AssetDatabase.LoadAssetAtPath <Texture2D>(joinName + "SubTexture.png");
                        flowTexture = AssetDatabase.LoadAssetAtPath <Texture2D>(joinName + "FlowTexture.png");
                    }

                    //live2d専用のマテリアル設定
                    material.SetInt("_SrcColor", 1);
                    material.SetInt("_DstColor", 10);
                    material.SetInt("_SrcAlpha", 1);
                    material.SetInt("_DstAlpha", 10);

                    //独自のテクスチャ設定
                    material.SetTexture("_SubTex", subTexture);
                    material.SetTexture("_FlowMap", flowTexture);

                    AssetDatabase.SaveAssets();
                    AssetDatabase.Refresh();
                    break;

                //新しいマテリアルが追加されたらどんどん追加記述していく。
                default:
                    OrtegaLogger.Log($"DefaultMaterial setting complete.setting target:{drawable.Id}");
                    break;
                }
            }

            return(material);
        }
 protected abstract void DrawMesh(CubismDrawable drawable, Texture texture, FrameBuffer clippingMask, Matrix4 drawMatrix);
 protected abstract void DrawMask(CubismDrawable drawable, Matrix4 clippingMatrix);
        protected override void DrawMesh(CubismDrawable drawable, Texture texture, FrameBuffer clippingMask, Matrix4 drawMatrix)
        {
            int offset = 1 + (clippingMask != null ? (drawable.ConstantFlags.HasFlag(ConstantDrawableFlags.IsInvertedMask) ? 2 : 1) : 0) + (UsePremultipliedAlpha ? 3 : 0);

            IShader            shader;
            BlendingParameters blendingParameters;

            if (drawable.ConstantFlags.HasFlag(ConstantDrawableFlags.BlendMultiplicative))
            {
                shader             = shaders[offset];
                blendingParameters = new BlendingParameters
                {
                    Source           = BlendingType.DstColor,
                    Destination      = BlendingType.OneMinusSrcAlpha,
                    SourceAlpha      = BlendingType.Zero,
                    DestinationAlpha = BlendingType.One,
                };
            }
            else if (drawable.ConstantFlags.HasFlag(ConstantDrawableFlags.BlendAdditive))
            {
                shader             = shaders[offset];
                blendingParameters = new BlendingParameters
                {
                    Source           = BlendingType.One,
                    Destination      = BlendingType.One,
                    SourceAlpha      = BlendingType.Zero,
                    DestinationAlpha = BlendingType.One,
                };
            }
            else
            {
                shader             = shaders[offset];
                blendingParameters = new BlendingParameters
                {
                    Source           = BlendingType.One,
                    Destination      = BlendingType.OneMinusSrcAlpha,
                    SourceAlpha      = BlendingType.One,
                    DestinationAlpha = BlendingType.OneMinusSrcAlpha,
                };
            }

            GLWrapper.SetBlend(blendingParameters);
            shader.Bind();

            if (clippingMask != null)
            {
                clippingMask.Texture.Bind(TextureUnit.Texture1);

                shader.GetUniform <int>("s_texture1").Value        = 1;
                shader.GetUniform <Matrix4>("u_clipMatrix").Value  = drawMatrix;
                shader.GetUniform <Vector4>("u_channelFlag").Value = new Vector4(1.0f, 0.0f, 0.0f, 0.0f);
            }
            else
            {
                GLWrapper.BindTexture(null, TextureUnit.Texture1);
            }

            texture.TextureGL.Bind();

            shader.GetUniform <int>("s_texture0").Value   = 0;
            shader.GetUniform <Matrix4>("u_matrix").Value = MvpMatrix;

            var color = new Vector4(Color.R, Color.G, Color.B, Color.A);

            color.W *= drawable.Opacity;

            if (UsePremultipliedAlpha)
            {
                color.X *= drawable.Opacity;
                color.Y *= drawable.Opacity;
                color.Z *= drawable.Opacity;
            }

            shader.GetUniform <Vector4>("u_baseColor").Value = color;

            GL.EnableVertexAttribArray(0);

            fixed(float *pinnedVertexBuffer = drawable.Vertices)
            GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, sizeof(float) * 2, (IntPtr)pinnedVertexBuffer);

            GL.EnableVertexAttribArray(1);

            fixed(float *pinnedUVBuffer = drawable.TextureCoordinates)
            GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, sizeof(float) * 2, (IntPtr)pinnedUVBuffer);

            fixed(short *pinnedIndexBuffer = drawable.Indices)
            GL.DrawElements(PrimitiveType.Triangles, drawable.Indices.Length, DrawElementsType.UnsignedShort, (IntPtr)pinnedIndexBuffer);

            shader.Unbind();
        }
Esempio n. 8
0
 /// <summary>
 /// Builtin <see cref="Texture2D"/> picker.
 /// </summary>
 /// <param name="sender">Event source.</param>
 /// <param name="drawable">Drawable to map to.</param>
 /// <returns>Mapped texture.</returns>
 public static Texture2D TexturePicker(CubismModel3Json sender, CubismDrawable drawable)
 {
     return(sender.Textures[drawable.TextureIndex]);
 }
Esempio n. 9
0
        /// <summary>
        /// Builtin <see cref="Texture2D"/> picker.
        /// </summary>
        /// <param name="compressedLive2D">model file.</param>
        /// <param name="drawable">Drawable to map to.</param>
        /// <param name="modelTextures">Loaded Live2D model textures</param>
        /// <returns>Mapped texture.</returns>

        public static Texture2D TexturePicker(CompressedLive2D compressedLive2D, CubismDrawable drawable, Texture2D[] modelTextures)
        {
            var index = drawable.TextureIndex;

            return(modelTextures[index]);
        }
Esempio n. 10
0
 /// <summary>
 /// Builtin <see cref="Material"/> picker.
 /// </summary>
 /// <param name="sender">Event source.</param>
 /// <param name="drawable">Drawable to map to.</param>
 /// <returns>Mapped texture.</returns>
 public static Material MaterialPicker(CubismModel3Json sender, CubismDrawable drawable)
 {
     return(null);
 }