Exemplo n.º 1
0
        unsafe void RenderInternal(CommandBuffer commandBuffer, byte[] computeBufferTemp, ComputeBuffer computeBuffer, MaterialPropCollection matPropCol, RenderTexture background)
        {
            var renderParameterCount = Plugin.GetUnityRenderParameterCount();

            // var vertexBufferSize = Plugin.GetUnityRenderVertexBufferCount();

            if (renderParameterCount > 0)
            {
                Plugin.UnityRenderParameter parameter = new Plugin.UnityRenderParameter();

                var vertexBufferCount = Plugin.GetUnityRenderVertexBufferCount();

                if (vertexBufferCount > 0)
                {
                    var vertexBuffer = Plugin.GetUnityRenderVertexBuffer();

                    System.Runtime.InteropServices.Marshal.Copy(vertexBuffer, computeBufferTemp, 0, vertexBufferCount);
                    computeBuffer.SetData(computeBufferTemp, 0, 0, vertexBufferCount);
                }

                for (int i = 0; i < renderParameterCount; i++)
                {
                    Plugin.GetUnityRenderParameter(ref parameter, i);

                    if (parameter.RenderMode == 1)
                    {
                        // Draw model
                        var infoBuffer      = Plugin.GetUnityRenderInfoBuffer();
                        var modelParameters = ((Plugin.UnityRenderModelParameter *)(((byte *)infoBuffer.ToPointer()) + parameter.VertexBufferOffset));

                        MaterialKey key = new MaterialKey();
                        key.Blend  = (AlphaBlendType)parameter.Blend;
                        key.ZTest  = parameter.ZTest > 0;
                        key.ZWrite = parameter.ZWrite > 0;

                        if (parameter.Culling == 0)
                        {
                            key.Cull = (int)UnityEngine.Rendering.CullMode.Back;
                        }
                        if (parameter.Culling == 1)
                        {
                            key.Cull = (int)UnityEngine.Rendering.CullMode.Front;
                        }
                        if (parameter.Culling == 2)
                        {
                            key.Cull = (int)UnityEngine.Rendering.CullMode.Off;
                        }


                        for (int mi = 0; mi < parameter.ElementCount; mi++)
                        {
                            var model = EffekseerSystem.GetCachedModel(parameter.ModelPtr);
                            if (model == null)
                            {
                                continue;
                            }

                            var prop = matPropCol.GetNext();

                            if (parameter.IsDistortingMode > 0)
                            {
                                var material = materialsModelDistortion.GetMaterial(ref key);

                                prop.SetBuffer("buf_vertex", model.VertexBuffer);
                                prop.SetBuffer("buf_index", model.IndexBuffer);
                                prop.SetMatrix("buf_matrix", modelParameters[mi].Matrix);
                                prop.SetVector("buf_uv", modelParameters[mi].UV);
                                prop.SetVector("buf_color", modelParameters[mi].VColor);
                                prop.SetFloat("buf_vertex_offset", model.vertexOffsets[modelParameters[mi].Time]);
                                prop.SetFloat("buf_index_offset", model.indexOffsets[modelParameters[mi].Time]);

                                var colorTexture = GetCachedTexture(parameter.TexturePtrs0, background);
                                if (parameter.TextureWrapTypes[0] == 0)
                                {
                                    colorTexture.wrapMode = TextureWrapMode.Repeat;
                                }
                                else
                                {
                                    colorTexture.wrapMode = TextureWrapMode.Clamp;
                                }

                                if (parameter.TextureFilterTypes[0] == 0)
                                {
                                    colorTexture.filterMode = FilterMode.Point;
                                }
                                else
                                {
                                    colorTexture.filterMode = FilterMode.Bilinear;
                                }

                                prop.SetTexture("_ColorTex", colorTexture);

                                if (background != null)
                                {
                                    prop.SetTexture("_BackTex", GetCachedTexture(parameter.TexturePtrs1, background));
                                    //Temp
                                    //prop.SetTexture("_BackTex", background);

                                    commandBuffer.DrawProcedural(new Matrix4x4(), material, 0, MeshTopology.Triangles, model.IndexCounts[0], 1, prop);
                                }
                            }
                            else
                            {
                                var material = materialsModel.GetMaterial(ref key);

                                prop.SetBuffer("buf_vertex", model.VertexBuffer);
                                prop.SetBuffer("buf_index", model.IndexBuffer);
                                prop.SetMatrix("buf_matrix", modelParameters[mi].Matrix);
                                prop.SetVector("buf_uv", modelParameters[mi].UV);
                                prop.SetVector("buf_color", modelParameters[mi].VColor);

                                int modelTime = modelParameters[mi].Time;
                                prop.SetFloat("buf_vertex_offset", model.vertexOffsets[modelTime]);
                                prop.SetFloat("buf_index_offset", model.indexOffsets[modelTime]);

                                var colorTexture = GetCachedTexture(parameter.TexturePtrs0, background);
                                if (parameter.TextureWrapTypes[0] == 0)
                                {
                                    colorTexture.wrapMode = TextureWrapMode.Repeat;
                                }
                                else
                                {
                                    colorTexture.wrapMode = TextureWrapMode.Clamp;
                                }

                                if (parameter.TextureFilterTypes[0] == 0)
                                {
                                    colorTexture.filterMode = FilterMode.Point;
                                }
                                else
                                {
                                    colorTexture.filterMode = FilterMode.Bilinear;
                                }

                                prop.SetTexture("_ColorTex", GetCachedTexture(parameter.TexturePtrs0, background));

                                commandBuffer.DrawProcedural(new Matrix4x4(), material, 0, MeshTopology.Triangles, model.IndexCounts[0], 1, prop);
                            }
                        }
                    }
                    else
                    {
                        var prop = matPropCol.GetNext();

                        MaterialKey key = new MaterialKey();
                        key.Blend  = (AlphaBlendType)parameter.Blend;
                        key.ZTest  = parameter.ZTest > 0;
                        key.ZWrite = parameter.ZWrite > 0;
                        key.Cull   = (int)UnityEngine.Rendering.CullMode.Off;


                        if (parameter.IsDistortingMode > 0)
                        {
                            var material = materialsDistortion.GetMaterial(ref key);

                            prop.SetFloat("buf_offset", parameter.VertexBufferOffset / VertexDistortionSize);
                            prop.SetBuffer("buf_vertex", computeBuffer);

                            var colorTexture = GetCachedTexture(parameter.TexturePtrs0, background);
                            if (parameter.TextureWrapTypes[0] == 0)
                            {
                                colorTexture.wrapMode = TextureWrapMode.Repeat;
                            }
                            else
                            {
                                colorTexture.wrapMode = TextureWrapMode.Clamp;
                            }

                            if (parameter.TextureFilterTypes[0] == 0)
                            {
                                colorTexture.filterMode = FilterMode.Point;
                            }
                            else
                            {
                                colorTexture.filterMode = FilterMode.Bilinear;
                            }

                            prop.SetTexture("_ColorTex", colorTexture);

                            if (background != null)
                            {
                                prop.SetTexture("_BackTex", GetCachedTexture(parameter.TexturePtrs1, background));
                                commandBuffer.DrawProcedural(new Matrix4x4(), material, 0, MeshTopology.Triangles, parameter.ElementCount * 2 * 3, 1, prop);
                            }
                        }
                        else
                        {
                            var material = materials.GetMaterial(ref key);

                            prop.SetFloat("buf_offset", parameter.VertexBufferOffset / VertexSize);
                            prop.SetBuffer("buf_vertex", computeBuffer);

                            var colorTexture = GetCachedTexture(parameter.TexturePtrs0, background);
                            if (parameter.TextureWrapTypes[0] == 0)
                            {
                                colorTexture.wrapMode = TextureWrapMode.Repeat;
                            }
                            else
                            {
                                colorTexture.wrapMode = TextureWrapMode.Clamp;
                            }

                            if (parameter.TextureFilterTypes[0] == 0)
                            {
                                colorTexture.filterMode = FilterMode.Point;
                            }
                            else
                            {
                                colorTexture.filterMode = FilterMode.Bilinear;
                            }

                            prop.SetTexture("_ColorTex", colorTexture);

                            commandBuffer.DrawProcedural(new Matrix4x4(), material, 0, MeshTopology.Triangles, parameter.ElementCount * 2 * 3, 1, prop);
                        }
                    }
                }
            }
        }