Exemplo n.º 1
0
    void OnPreCullEvent(Camera camera)
    {
#if UNITY_EDITOR
        if (camera.cameraType == CameraType.SceneView)
        {
            // シーンビューのカメラはチェック
            if (this.drawInSceneView == false)
            {
                return;
            }
        }
#endif
        RenderPath path;

        // カリングマスクをチェック
        if ((Camera.current.cullingMask & (1 << gameObject.layer)) == 0)
        {
            if (renderPaths.ContainsKey(camera))
            {
                // レンダーパスが存在すればコマンドバッファを解除
                path = renderPaths[camera];
                path.Dispose();
                renderPaths.Remove(camera);
            }
            return;
        }

        if (renderPaths.ContainsKey(camera))
        {
            // レンダーパスが有れば使う
            path = renderPaths[camera];
        }
        else
        {
            // 無ければレンダーパスを作成
            path = new RenderPath(camera, cameraEvent, renderPaths.Count);
            path.Init(this.enableDistortion);
            renderPaths.Add(camera, path);
        }

        if (!path.IsValid())
        {
            path.Dispose();
            path.Init(this.enableDistortion);
        }

        // 歪みテクスチャをセット
        if (path.renderTexture)
        {
            Plugin.EffekseerSetBackGroundTexture(path.renderId, path.renderTexture.GetNativeTexturePtr());
        }

        // ビュー関連の行列を更新
        Plugin.EffekseerSetProjectionMatrix(path.renderId, Utility.Matrix2Array(
                                                GL.GetGPUProjectionMatrix(camera.projectionMatrix, false)));
        Plugin.EffekseerSetCameraMatrix(path.renderId, Utility.Matrix2Array(
                                            camera.worldToCameraMatrix));
    }
Exemplo n.º 2
0
        public void Render(Camera camera, RenderTargetProperty renderTargetProperty, CommandBuffer targetCommandBuffer)
        {
            var settings = EffekseerSettings.Instance;

#if UNITY_EDITOR
            if (camera.cameraType == CameraType.SceneView)
            {
                // check a camera in the scene view
                if (settings.drawInSceneView == false)
                {
                    return;
                }
            }
#endif

            // check a culling mask
            var mask = Effekseer.Plugin.EffekseerGetCameraCullingMaskToShowAllEffects();

            // don't need to update because doesn't exists and need not to render
            if ((camera.cullingMask & mask) == 0 && !renderPaths.ContainsKey(camera))
            {
                return;
            }

            // GC renderpaths
            bool hasDisposed = false;
            foreach (var path_ in renderPaths)
            {
                path_.Value.LifeTime--;
                if (path_.Value.LifeTime < 0)
                {
                    path_.Value.Dispose();
                    hasDisposed = true;
                }
            }

            // dispose renderpaths
            if (hasDisposed)
            {
                List <Camera> removed = new List <Camera>();
                foreach (var path_ in renderPaths)
                {
                    if (path_.Value.LifeTime >= 0)
                    {
                        continue;
                    }

                    removed.Add(path_.Key);
                    Plugin.EffekseerAddRemovingRenderPath(path_.Value.renderId);
                }

                foreach (var r in removed)
                {
                    renderPaths.Remove(r);
                }
            }

            RenderPath path;

            if (renderPaths.ContainsKey(camera))
            {
                path = renderPaths[camera];
            }
            else
            {
                // render path doesn't exists, create a render path
                while (true)
                {
                    bool found = false;
                    foreach (var kv in renderPaths)
                    {
                        if (kv.Value.renderId == nextRenderID)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (found)
                    {
                        nextRenderID++;
                    }
                    else
                    {
                        break;
                    }
                }

                path = new RenderPath(camera, cameraEvent, nextRenderID, targetCommandBuffer != null);
                var stereoRenderingType = (camera.stereoEnabled) ? StereoRendererUtil.GetStereoRenderingType() : StereoRendererUtil.StereoRenderingTypes.None;
                path.Init(EffekseerRendererUtils.IsDistortionEnabled, renderTargetProperty, stereoRenderingType);
                renderPaths.Add(camera, path);
                nextRenderID = (nextRenderID + 1) % EffekseerRendererUtils.RenderIDCount;
            }

            if (!path.IsValid(renderTargetProperty))
            {
                path.Dispose();
                var stereoRenderingType = (camera.stereoEnabled) ? StereoRendererUtil.GetStereoRenderingType() : StereoRendererUtil.StereoRenderingTypes.None;
                path.Init(EffekseerRendererUtils.IsDistortionEnabled, renderTargetProperty, stereoRenderingType);
            }

            path.LifeTime = 60;
            Plugin.EffekseerSetRenderingCameraCullingMask(path.renderId, camera.cullingMask);

            // effects shown don't exists
            if ((camera.cullingMask & mask) == 0)
            {
                // Because rendering thread is asynchronous
                SpecifyRenderingMatrix(camera, path);
                return;
            }

            if (path.isCommandBufferFromExternal)
            {
                path.AssignExternalCommandBuffer(targetCommandBuffer, renderTargetProperty);
            }

            // if LWRP
            if (renderTargetProperty != null)
            {
                // flip a rendertaget
                // Direct11 : OK (2019, LWRP 5.13)
                // Android(OpenGL) : OK (2019, LWRP 5.13)
                Plugin.EffekseerSetRenderSettings(path.renderId, true);
                Plugin.EffekseerSetIsBackgroundTextureFlipped(0);
            }
            else
            {
#if UNITY_SWITCH && !UNITY_EDITOR
                Plugin.EffekseerSetIsBackgroundTextureFlipped(1);
#else
                Plugin.EffekseerSetIsBackgroundTextureFlipped(0);
#endif
            }

            // assign a dinsotrion texture
            if (path.renderTexture != null)
            {
                Plugin.EffekseerSetBackGroundTexture(path.renderId, path.renderTexture.ptr);
            }

            SpecifyRenderingMatrix(camera, path);
        }
Exemplo n.º 3
0
        public void Render(Camera camera, int?dstID, RenderTargetIdentifier?dstIdentifier)
        {
            var settings = EffekseerSettings.Instance;

#if UNITY_EDITOR
            if (camera.cameraType == CameraType.SceneView)
            {
                // check a camera in the scene view
                if (settings.drawInSceneView == false)
                {
                    return;
                }
            }
#endif
            // check a culling mask
            var mask = Effekseer.Plugin.EffekseerGetCameraCullingMaskToShowAllEffects();

            if ((camera.cullingMask & mask) == 0)
            {
                if (renderPaths.ContainsKey(camera))
                {
                    var path_ = renderPaths[camera];
                    path_.Dispose();
                    renderPaths.Remove(camera);
                }
                return;
            }

            // GC renderpaths
            bool hasDisposed = false;
            foreach (var path_ in renderPaths)
            {
                path_.Value.LifeTime--;
                if (path_.Value.LifeTime < 0)
                {
                    path_.Value.Dispose();
                    hasDisposed = true;
                }
            }

            // dispose renderpaths
            if (hasDisposed)
            {
                List <Camera> removed = new List <Camera>();
                foreach (var path_ in renderPaths)
                {
                    if (path_.Value.LifeTime >= 0)
                    {
                        continue;
                    }

                    removed.Add(path_.Key);
                }

                foreach (var r in removed)
                {
                    renderPaths.Remove(r);
                }
            }

            RenderPath path;

            if (renderPaths.ContainsKey(camera))
            {
                path = renderPaths[camera];
            }
            else
            {
                // render path doesn't exists, create a render path
                path = new RenderPath(camera, cameraEvent, renderPaths.Count);
                path.Init(EffekseerRendererUtils.IsDistortionEnabled);
                renderPaths.Add(camera, path);
            }

            if (!path.IsValid())
            {
                path.Dispose();
                path.Init(EffekseerRendererUtils.IsDistortionEnabled);
            }

            path.Update();
            path.LifeTime = 60;
            Plugin.EffekseerSetRenderingCameraCullingMask(path.renderId, camera.cullingMask);

            // assign a dinsotrion texture
            if (path.renderTexture)
            {
                Plugin.EffekseerSetBackGroundTexture(path.renderId, path.renderTexture.GetNativeTexturePtr());
            }
            else
            {
                Plugin.EffekseerSetBackGroundTexture(path.renderId, IntPtr.Zero);
            }

            // update view matrixes
            Plugin.EffekseerSetProjectionMatrix(path.renderId, Utility.Matrix2Array(
                                                    GL.GetGPUProjectionMatrix(camera.projectionMatrix, false)));
            Plugin.EffekseerSetCameraMatrix(path.renderId, Utility.Matrix2Array(
                                                camera.worldToCameraMatrix));

            // Reset command buffer
            path.commandBuffer.Clear();
            path.materiaProps.Reset();

            // generate render events on this thread
            Plugin.EffekseerRenderBack(path.renderId);

            // if memory is lacked, reallocate memory
            while (Plugin.GetUnityRenderParameterCount() > 0 && Plugin.GetUnityRenderVertexBufferCount() > path.computeBufferTemp.Length)
            {
                path.ReallocateComputeBuffer();
            }

            RenderInternal(path.commandBuffer, path.computeBufferTemp, path.computeBufferBack, path.materiaProps, path.renderTexture);

            // Distortion
            if (EffekseerRendererUtils.IsDistortionEnabled &&
                (path.renderTexture != null || dstID.HasValue || dstIdentifier.HasValue))
            {
                // Add a blit command that copy to the distortion texture
                if (dstID.HasValue)
                {
                    path.commandBuffer.Blit(dstID.Value, path.renderTexture);
                    path.commandBuffer.SetRenderTarget(dstID.Value);
                }
                else if (dstIdentifier.HasValue)
                {
                    path.commandBuffer.Blit(dstIdentifier.Value, path.renderTexture);
                    path.commandBuffer.SetRenderTarget(dstIdentifier.Value);
                }
                else
                {
                    path.commandBuffer.Blit(BuiltinRenderTextureType.CameraTarget, path.renderTexture);
                    path.commandBuffer.SetRenderTarget(BuiltinRenderTextureType.CameraTarget);
                }
            }

            Plugin.EffekseerRenderFront(path.renderId);

            // if memory is lacked, reallocate memory
            while (Plugin.GetUnityRenderParameterCount() > 0 && Plugin.GetUnityRenderVertexBufferCount() > path.computeBufferTemp.Length)
            {
                path.ReallocateComputeBuffer();
            }

            RenderInternal(path.commandBuffer, path.computeBufferTemp, path.computeBufferFront, path.materiaProps, path.renderTexture);
        }
Exemplo n.º 4
0
        public void Render(Camera camera, int?dstID, RenderTargetIdentifier?dstIdentifier)
        {
            var settings = EffekseerSettings.Instance;

#if UNITY_EDITOR
            if (camera.cameraType == CameraType.SceneView)
            {
                // check a camera in the scene view
                if (settings.drawInSceneView == false)
                {
                    return;
                }
            }
#endif

            // check a culling mask
            var mask = Effekseer.Plugin.EffekseerGetCameraCullingMaskToShowAllEffects();

            if ((camera.cullingMask & mask) == 0)
            {
                if (renderPaths.ContainsKey(camera))
                {
                    var path_ = renderPaths[camera];
                    path_.Dispose();
                    renderPaths.Remove(camera);
                }
                return;
            }

            // GC renderpaths
            bool hasDisposed = false;
            foreach (var path_ in renderPaths)
            {
                path_.Value.LifeTime--;
                if (path_.Value.LifeTime < 0)
                {
                    path_.Value.Dispose();
                    hasDisposed = true;
                }
            }

            // dispose renderpaths
            if (hasDisposed)
            {
                List <Camera> removed = new List <Camera>();
                foreach (var path_ in renderPaths)
                {
                    if (path_.Value.LifeTime >= 0)
                    {
                        continue;
                    }

                    removed.Add(path_.Key);
                }

                foreach (var r in removed)
                {
                    renderPaths.Remove(r);
                }
            }

            RenderPath path;

            if (renderPaths.ContainsKey(camera))
            {
                path = renderPaths[camera];
            }
            else
            {
                // render path doesn't exists, create a render path
                path = new RenderPath(camera, cameraEvent, renderPaths.Count);
                var stereoRenderingType = (camera.stereoEnabled)? StereoRendererUtil.GetStereoRenderingType() : StereoRendererUtil.StereoRenderingTypes.None;
                path.Init(EffekseerRendererUtils.IsDistortionEnabled, dstID, dstIdentifier, stereoRenderingType);
                renderPaths.Add(camera, path);
            }

            if (!path.IsValid())
            {
                path.Dispose();
                var stereoRenderingType = (camera.stereoEnabled) ? StereoRendererUtil.GetStereoRenderingType() : StereoRendererUtil.StereoRenderingTypes.None;
                path.Init(EffekseerRendererUtils.IsDistortionEnabled, dstID, dstIdentifier, stereoRenderingType);
            }

            path.LifeTime = 60;
            Plugin.EffekseerSetRenderingCameraCullingMask(path.renderId, camera.cullingMask);

            // if LWRP
            if (dstID.HasValue || dstIdentifier.HasValue)
            {
                // flip a rendertaget
                // Direct11 : OK (2019, LWRP 5.13)
                // Android(OpenGL) : OK (2019, LWRP 5.13)
                Plugin.EffekseerSetRenderSettings(path.renderId, true);
                Plugin.EffekseerSetIsBackgroundTextureFlipped(0);
            }
            else
            {
                Plugin.EffekseerSetIsBackgroundTextureFlipped(0);
            }

            // assign a dinsotrion texture
            if (path.renderTexture)
            {
                Plugin.EffekseerSetBackGroundTexture(path.renderId, path.renderTexture.GetNativeTexturePtr());
            }

            // specify matrixes for stereo rendering
            if (camera.stereoEnabled)
            {
                var stereoRenderType = StereoRendererUtil.GetStereoRenderingType();
                if (stereoRenderType != StereoRendererUtil.StereoRenderingTypes.None)
                {
                    float[] camCenterMat = Utility.Matrix2Array(camera.worldToCameraMatrix);
                    float[] projMatL     = Utility.Matrix2Array(GL.GetGPUProjectionMatrix(camera.GetStereoProjectionMatrix(Camera.StereoscopicEye.Left), false));
                    float[] projMatR     = Utility.Matrix2Array(GL.GetGPUProjectionMatrix(camera.GetStereoProjectionMatrix(Camera.StereoscopicEye.Right), false));
                    float[] camMatL      = Utility.Matrix2Array(camera.GetStereoViewMatrix(Camera.StereoscopicEye.Left));
                    float[] camMatR      = Utility.Matrix2Array(camera.GetStereoViewMatrix(Camera.StereoscopicEye.Right));
                    Plugin.EffekseerSetStereoRenderingMatrix(path.renderId, (int)stereoRenderType, camCenterMat, projMatL, projMatR, camMatL, camMatR);
                }
            }
            else
            {
                // update view matrixes
                Plugin.EffekseerSetProjectionMatrix(path.renderId, Utility.Matrix2Array(
                                                        GL.GetGPUProjectionMatrix(camera.projectionMatrix, false)));
                Plugin.EffekseerSetCameraMatrix(path.renderId, Utility.Matrix2Array(
                                                    camera.worldToCameraMatrix));
            }
        }