Exemple #1
0
        void ReleaseInstance()
        {
            TextureWriter.twGuardBegin();
            aepAPI.aepGuardBegin();

            // release ports & params
            aepAPI.aepDestroyLayer(m_img_src);
            m_img_src = default(aepAPI.aepLayer);

            aepAPI.aepEraseDeferredCall(m_otp_render); m_otp_render   = 0;
            TextureWriter.twEraseDeferredCall(m_tw_read); m_tw_read   = 0;
            TextureWriter.twEraseDeferredCall(m_tw_write); m_tw_write = 0;

            m_params = null;

            // release instance
            aepAPI.aepDestroyInstance(m_inst);
            m_inst.ptr = IntPtr.Zero;

            aepAPI.aepGuardEnd();
            TextureWriter.twGuardEnd();

            if (m_began)
            {
                aepAPI.aepEndSequence(m_inst);
                m_began = false;
            }
        }
Exemple #2
0
        void ReleaseInstance()
        {
            TextureWriter.twGuardBegin();
            otpAPI.otpGuardBegin();

            // release ports & params
            otpAPI.otpDestroyImage(m_img_src);
            m_img_src = default(otpAPI.otpImage);

            otpAPI.otpEraseDeferredCall(m_otp_render); m_otp_render   = 0;
            TextureWriter.twEraseDeferredCall(m_tw_read); m_tw_read   = 0;
            TextureWriter.twEraseDeferredCall(m_tw_write); m_tw_write = 0;
            if (m_ports != null)
            {
                foreach (var port in m_ports)
                {
                    port.Release();
                }
            }

            m_ports  = null;
            m_params = null;

            // release instance
            otpAPI.otpDestroyInstance(m_inst);
            m_inst.ptr = IntPtr.Zero;

            otpAPI.otpGuardEnd();
            TextureWriter.twGuardEnd();
        }
Exemple #3
0
        void UpdateInputImages(Texture rt_src)
        {
            if (rt_src != null)
            {
                // copy rt_src content to memory
                if (!m_img_src)
                {
                    m_img_src = otpAPI.otpCreateImage();
                }
                otpAPI.otpResizeImage(m_img_src, rt_src.width, rt_src.height);

                var src_data = default(otpAPI.otpImageData);
                otpAPI.otpGetImageData(m_img_src, ref src_data);
                m_tw_read = TextureWriter.ReadDeferred(
                    src_data.data, src_data.width * src_data.height, TextureWriter.twPixelFormat.RGBAu8, rt_src, m_tw_read);
                GL.IssuePluginEvent(GetTWEvent(), m_tw_read);
            }


            foreach (var port in m_ports)
            {
                if (port.input != null)
                {
                    var     image = port.GetImage();
                    Texture input = port.GetUncompressedInput();
                    otpAPI.otpResizeImage(image, input.width, input.height);

                    var idata = default(otpAPI.otpImageData);
                    otpAPI.otpGetImageData(image, ref idata);
                    port.tw_read = TextureWriter.ReadDeferred(
                        idata.data, idata.width * idata.height, TextureWriter.twPixelFormat.RGBAu8, input, port.tw_read);
                    GL.IssuePluginEvent(GetTWEvent(), port.tw_read);
                }
            }
        }
Exemple #4
0
        void UpdateInputImages(Texture rt_src)
        {
            if (rt_src != null)
            {
                // copy rt_src content to memory
                if (!m_img_src)
                {
                    m_img_src = aepAPI.aepCreateLayer();
                }
                aepAPI.aepResizeLayer(m_img_src, rt_src.width, rt_src.height);

                var src_data = default(aepAPI.aepLayerData);
                aepAPI.aepGetLayerData(m_img_src, ref src_data);
                m_tw_read = TextureWriter.ReadDeferred(
                    src_data.pixels, src_data.width * src_data.height, TextureWriter.twPixelFormat.RGBAu8, rt_src, m_tw_read);
                GL.IssuePluginEvent(GetTWEvent(), m_tw_read);
            }
        }
Exemple #5
0
 public void Release()
 {
     if (rt != null)
     {
         rt.Release();
         rt = null;
     }
     if (tw_read != 0)
     {
         TextureWriter.twEraseDeferredCall(tw_read);
         tw_read = 0;
     }
     if (image)
     {
         otpAPI.otpDestroyImage(image);
         image = default(otpAPI.otpImage);
     }
 }
        public void Flush()
        {
            if (m_mesh == null)
            {
                Debug.LogWarning("AlembicPointsRenderer: mesh is not assigned");
                return;
            }
            if (m_materials == null || m_materials.Length == 0 || (m_materials.Length == 1 && m_materials[0] == null))
            {
                Debug.LogWarning("AlembicPointsRenderer: material is not assigned");
                return;
            }

            var points         = GetComponent <AlembicPoints>();
            var abcData        = points.abcData;
            int max_instances  = points.abcPeakVertexCount;
            int instance_count = abcData.count;

            m_bounds.center  = mul(abcData.boundsCenter, m_trans_scale);
            m_bounds.extents = mul(abcData.boundsExtents, m_trans_scale);

            if (instance_count == 0)
            {
                return;
            }                                    // nothing to draw

            // update data texture
            if (m_texPositions == null || !m_texPositions.IsCreated())
            {
                int height = ceildiv(max_instances, TextureWidth);
                m_texPositions  = CreateDataTexture(TextureWidth, height, RenderTextureFormat.ARGBFloat);
                m_texVelocities = CreateDataTexture(TextureWidth, height, RenderTextureFormat.ARGBFloat);
                m_texIDs        = CreateDataTexture(TextureWidth, height, RenderTextureFormat.RFloat);
            }
            TextureWriter.Write(m_texPositions, abcData.positions, abcData.count, TextureWriter.tDataFormat.Float3);
            TextureWriter.Write(m_texIDs, abcData.ids, abcData.count, TextureWriter.tDataFormat.LInt);

            // update expanded mesh
            if (m_expanded_mesh != null)
            {
                // destroy existing expanded mesh if mesh is replaced
                if (m_expanded_mesh.name != m_mesh.name + "_expanded")
                {
                    m_expanded_mesh = null;
                }
            }
            if (m_expanded_mesh == null)
            {
                m_expanded_mesh = CreateExpandedMesh(m_mesh, max_instances, out m_instances_par_batch);
                m_expanded_mesh.UploadMeshData(true);
                m_expanded_mesh.name = m_mesh.name + "_expanded";
            }

            if (m_actual_materials == null)
            {
                m_actual_materials = new List <List <Material> >();
                while (m_actual_materials.Count < m_materials.Length)
                {
                    m_actual_materials.Add(new List <Material>());
                }
            }

            var trans = GetComponent <Transform>();

            m_expanded_mesh.bounds = m_bounds;
            m_count_rate           = Mathf.Max(m_count_rate, 0.0f);
            instance_count         = Mathf.Min((int)(instance_count * m_count_rate), (int)(max_instances * m_count_rate));
            int batch_count = ceildiv(instance_count, m_instances_par_batch);

            // clone materials if needed
            for (int i = 0; i < m_actual_materials.Count; ++i)
            {
                var materials = m_actual_materials[i];
                while (materials.Count < batch_count)
                {
                    Material m = CloneMaterial(m_materials[i], materials.Count);
                    materials.Add(m);
                }
            }

            // update materials
            var worldToLocalMatrix = trans.localToWorldMatrix;

            for (int i = 0; i < m_actual_materials.Count; ++i)
            {
                var materials = m_actual_materials[i];
                for (int j = 0; j < materials.Count; ++j)
                {
                    var m = materials[j];
                    m.SetInt("_BatchBegin", j * m_instances_par_batch);
                    m.SetTexture("_PositionBuffer", m_texPositions);
                    m.SetTexture("_VelocityBuffer", m_texVelocities);
                    m.SetTexture("_IDBuffer", m_texIDs);

                    m.SetInt("_NumInstances", instance_count);
                    m.SetVector("_CountRate", new Vector4(m_count_rate, 1.0f / m_count_rate, 0.0f, 0.0f));
                    m.SetVector("_ModelScale", m_model_scale);
                    m.SetVector("_TransScale", m_trans_scale);
                    m.SetMatrix("_Transform", worldToLocalMatrix);
                }
            }

            if (m_makeChildRenderers)
            {
                // create child renderers if needed
                if (m_child_renderers == null)
                {
                    m_child_renderers = new List <MeshRenderer>();
                }
                while (m_child_renderers.Count < m_materials.Length)
                {
                    m_child_renderers.Add(MakeChildRenderer(m_child_renderers.Count));
                }

                // assign materials to child renderers
                for (int i = 0; i < m_actual_materials.Count; ++i)
                {
                    var materials = m_actual_materials[i];
                    materials.RemoveRange(batch_count, materials.Count - batch_count);
                    m_child_renderers[i].sharedMaterials = materials.ToArray();
                }

                // update mesh in child renderers if m_mesh is replaced
                for (int i = 0; i < m_child_renderers.Count; ++i)
                {
                    var mesh_filter = m_child_renderers[i].GetComponent <MeshFilter>();
                    if (mesh_filter.sharedMesh != m_expanded_mesh)
                    {
                        mesh_filter.sharedMesh = m_expanded_mesh;
                    }
                }
            }
            else
            {
                // clear child renderers
                if (m_child_renderers != null)
                {
                    foreach (var child in m_child_renderers)
                    {
                        if (child != null)
                        {
                            DestroyImmediate(child.gameObject);
                        }
                    }
                    m_child_renderers = null;
                }

                // issue draw calls
                int       layer  = gameObject.layer;
                Matrix4x4 matrix = Matrix4x4.identity;
                m_actual_materials.ForEach(a =>
                {
                    for (int i = 0; i < batch_count; ++i)
                    {
                        Graphics.DrawMesh(m_expanded_mesh, matrix, a[i], layer, null, 0, null, m_cast_shadow, m_receive_shadow);
                    }
                });
            }
        }
Exemple #7
0
        void OnRenderImage(RenderTexture rt_src, RenderTexture rt_dst)
        {
            if (!m_inst && m_pluginPath != null)
            {
                m_inst = aepAPI.aepCreateInstance(LoadModule());
            }
            if (!m_inst)
            {
                Graphics.Blit(rt_src, rt_dst);
                return;
            }


            bool needs_blit         = rt_dst == null;
            bool resolution_changed = false;

            if (m_prev_width != rt_src.width || m_prev_height != rt_src.height)
            {
                m_prev_width       = rt_src.width;
                m_prev_height      = rt_src.height;
                resolution_changed = true;
            }

            // rt_dst is null if this AfterEffectsFx is last post effect and camera's render target is null
            // in this case, we must allocate temporary RenderTexture, write result to it, and Blit() to destination.
            if (rt_dst == null)
            {
                if (m_rt_dst != null && resolution_changed)
                {
                    m_rt_dst.Release();
                    m_rt_dst = null;
                }
                if (m_rt_dst == null)
                {
                    m_rt_dst = new RenderTexture(rt_src.width, rt_src.height, 0, rt_src.format);
                    m_rt_dst.Create();
                }
                rt_dst = m_rt_dst;
            }

            if (m_began && resolution_changed)
            {
                aepAPI.aepEndSequence(m_inst);
                m_began = false;
            }
            if (!m_began)
            {
                aepAPI.aepBeginSequence(m_inst, rt_src.width, rt_src.height);
                m_began = true;
            }

            UpdateInputImages(rt_src);
            ApplyParams();
            aepAPI.aepSetInput(m_inst, m_img_src);
            m_otp_render = aepAPI.aepRenderDeferred(m_inst, Time.time, m_otp_render);
            GL.IssuePluginEvent(GetAEPEvent(), m_otp_render);

            var dst_data = default(aepAPI.aepLayerData);

            aepAPI.aepGetLayerData(aepAPI.aepGetDstImage(m_inst), ref dst_data);

            if (dst_data.width == rt_dst.width && dst_data.height == rt_dst.height)
            {
                m_tw_write = TextureWriter.WriteDeferred(
                    rt_dst, dst_data.pixels, dst_data.width * dst_data.height, TextureWriter.twPixelFormat.RGBAu8, m_tw_write);
                GL.IssuePluginEvent(GetTWEvent(), m_tw_write);
            }
            else
            {
                // blit & resize if size of img_dst != size of rt_dst
                if (m_rt_tmp != null && (m_rt_tmp.width != dst_data.width || m_rt_tmp.height != dst_data.height))
                {
                    m_rt_tmp.Release();
                    m_rt_tmp = null;
                }
                if (m_rt_tmp == null)
                {
                    m_rt_tmp            = new RenderTexture(dst_data.width, dst_data.height, 0, RenderTextureFormat.ARGB32);
                    m_rt_tmp.filterMode = FilterMode.Bilinear;
                    m_rt_tmp.Create();
                }
                m_tw_write = TextureWriter.WriteDeferred(
                    m_rt_tmp, dst_data.pixels, dst_data.width * dst_data.height, TextureWriter.twPixelFormat.RGBAu8, m_tw_write);
                GL.IssuePluginEvent(GetTWEvent(), m_tw_write);
                Graphics.Blit(m_rt_tmp, rt_dst);
            }

            if (needs_blit)
            {
                Graphics.Blit(rt_dst, (RenderTexture)null);
            }
        }
Exemple #8
0
 IntPtr GetTWEvent()
 {
     return(TextureWriter.GetRenderEventFunc());
 }