private void OnSegmentationRender(ScriptableRenderContext context, HDCamera hdCamera)
        {
            var cmd = CommandBufferPool.Get();

            SensorPassRenderer.Render(context, cmd, hdCamera, renderTarget, passId, SimulatorManager.Instance.SkySegmentationColor);

            var clearKernel = cs.FindKernel("Clear");

            cmd.SetComputeVectorParam(cs, Properties.TexSize, new Vector4(Width, Height, 1f / Width, 1f / Height));
            cmd.SetComputeBufferParam(cs, clearKernel, Properties.MinX, minX);
            cmd.SetComputeBufferParam(cs, clearKernel, Properties.MaxX, maxX);
            cmd.SetComputeBufferParam(cs, clearKernel, Properties.MinY, minY);
            cmd.SetComputeBufferParam(cs, clearKernel, Properties.MaxY, maxY);
            cmd.DispatchCompute(cs, clearKernel, 4, 1, 1);

            var detectKernel = cs.FindKernel("Detect");

            cmd.SetComputeTextureParam(cs, detectKernel, Properties.Input, renderTarget.ColorHandle);
            cmd.SetComputeVectorParam(cs, Properties.TexSize, new Vector4(Width, Height, 1f / Width, 1f / Height));
            cmd.SetComputeBufferParam(cs, detectKernel, Properties.MinX, minX);
            cmd.SetComputeBufferParam(cs, detectKernel, Properties.MaxX, maxX);
            cmd.SetComputeBufferParam(cs, detectKernel, Properties.MinY, minY);
            cmd.SetComputeBufferParam(cs, detectKernel, Properties.MaxY, maxY);
            cmd.DispatchCompute(cs, detectKernel, HDRPUtilities.GetGroupSize(Width, 8), HDRPUtilities.GetGroupSize(Height, 8), 1);

            context.ExecuteCommandBuffer(cmd);
            cmd.Clear();
            CommandBufferPool.Release(cmd);
        }
示例#2
0
        private void CustomRender(ScriptableRenderContext context, HDCamera hd)
        {
            var cmd = CommandBufferPool.Get();

            void RenderPointCloud(CubemapFace face)
            {
                PointCloudManager.RenderLidar(context, cmd, hd, renderTarget.ColorHandle, renderTarget.DepthHandle, face);
            }

            RenderMarker.Begin();
            SensorPassRenderer.Render(context, cmd, hd, renderTarget, passId, Color.clear, RenderPointCloud);
            RenderMarker.End();

            ComputeMarker.Begin();
            var kernel = cs.FindKernel(Compensated ? "CubeComputeComp" : "CubeCompute");

            cmd.SetComputeTextureParam(cs, kernel, Properties.InputCubemapTexture, renderTarget.ColorHandle);
            cmd.SetComputeBufferParam(cs, kernel, Properties.Output, PointCloudBuffer);
            cmd.SetComputeBufferParam(cs, kernel, Properties.LatitudeAngles, LatitudeAnglesBuffer);
            cmd.SetComputeIntParam(cs, Properties.LaserCount, LaserCount);
            cmd.SetComputeIntParam(cs, Properties.MeasuresPerRotation, UsedMeasurementsPerRotation);
            cmd.SetComputeFloatParam(cs, Properties.AntialiasingThreshold, AntialiasingThreshold);
            cmd.SetComputeVectorParam(cs, Properties.Origin, SensorCamera.transform.position);
            cmd.SetComputeMatrixParam(cs, Properties.RotMatrix, Matrix4x4.Rotate(transform.rotation));
            cmd.SetComputeMatrixParam(cs, Properties.Transform, transform.worldToLocalMatrix);
            cmd.SetComputeVectorParam(cs, Properties.PackedVec, new Vector4(MaxDistance, DeltaLongitudeAngle, MinDistance, StartLongitudeOffset));
            cmd.DispatchCompute(cs, kernel, HDRPUtilities.GetGroupSize(UsedMeasurementsPerRotation, 8), HDRPUtilities.GetGroupSize(LaserCount, 8), 1);
            ComputeMarker.End();

            context.ExecuteCommandBuffer(cmd);
            cmd.Clear();
            CommandBufferPool.Release(cmd);
        }
        protected override void SensorUpdate()
        {
            Camera.Render();
            SegmentationCamera.Render();

            minX.GetData(minXarr);
            maxX.GetData(maxXarr);
            minY.GetData(minYarr);
            maxY.GetData(maxYarr);

            DetectedObjects.Clear();

            // 0 is reserved for clear color, 255 for non-agent segmentation
            for (var i = 1; i < 255; ++i)
            {
                if (minXarr[i] == Width - 1 || maxXarr[i] == 0)
                {
                    continue;
                }

                var detected = GetDetectedObject(i);
                if (detected != null)
                {
                    DetectedObjects.Add(detected);
                }
            }

            MaxTracked = Math.Max(MaxTracked, DetectedObjects.Count);
            if (Bridge != null && Bridge.Status == Status.Connected)
            {
                Publish(new Detected2DObjectData()
                {
                    Frame    = Frame,
                    Sequence = seqId++,
                    Time     = SimulatorManager.Instance.CurrentTime,
                    Data     = DetectedObjects.ToArray(),
                });
            }

            foreach (var obj in DetectedObjects)
            {
                var min = obj.Position - obj.Scale / 2;
                var max = obj.Position + obj.Scale / 2;

                var color = string.Equals(obj.Label, "Pedestrian") ? Color.yellow : Color.green;
                AAWireBoxes.DrawBox(min, max, color);
            }

            var cmd = CommandBufferPool.Get();

            Camera.Render();
            AAWireBoxes.Draw(cmd);
            AAWireBoxes.Clear();
            HDRPUtilities.ExecuteAndClearCommandBuffer(cmd);
            CommandBufferPool.Release(cmd);
        }
示例#4
0
        void CustomRender(ScriptableRenderContext context, HDCamera hd)
        {
            var cmd = CommandBufferPool.Get();

            SensorPassRenderer.Render(context, cmd, hd, renderTarget, passId, SimulatorManager.Instance.SkySegmentationColor);
            var kernel = computeUtils.FindKernel("FillAlphaXR");

            cmd.SetComputeTextureParam(computeUtils, kernel, Properties.Output, renderTarget.ColorHandle);
            cmd.SetComputeVectorParam(computeUtils, Properties.TexSize, new Vector4(Width, Height, 1f / Width, 1f / Height));
            cmd.DispatchCompute(computeUtils, kernel, HDRPUtilities.GetGroupSize(Width, 8), HDRPUtilities.GetGroupSize(Height, 8), 1);
            context.ExecuteCommandBuffer(cmd);
            cmd.Clear();
            CommandBufferPool.Release(cmd);
        }
示例#5
0
        /// <summary>
        /// Initialization method
        /// </summary>
        private void Initialize()
        {
            if (initialized)
            {
                return;
            }

            //Increase shadows distance
            var shadows =
                volume.profile.components.Find(component => component is HDShadowSettings) as HDShadowSettings;

            if (shadows != null)
            {
                shadows.maxShadowDistance.value *= QualityDistanceMultiplier;
            }

            QualitySettings.renderPipeline = vseHdrpSettings;
            HDRPUtilities.ReinitializeRenderPipeline();

            initialized = true;
        }
示例#6
0
        protected void RenderCamera()
        {
            var cmd = CommandBufferPool.Get();
            var hd  = HDCamera.GetOrCreate(SensorCamera);

            if (renderTarget.IsCube && !HDAdditionalCameraData.hasCustomRender)
            {
                // HDRP renders cubemap as multiple separate images, each with different exposure.
                // Locking exposure will force it to use the same value for all faces, removing inconsistencies.
                hd.LockExposure();
                SensorCamera.stereoSeparation = 0f;
                SensorCamera.RenderToCubemap(renderTarget, faceMask, Camera.MonoOrStereoscopicEye.Left);
                hd.UnlockExposure();
            }
            else
            {
                SensorCamera.Render();
            }

            if (Distorted)
            {
                if (Fisheye)
                {
                    LensDistortion.UnifiedProjectionDistort(cmd, renderTarget, DistortedHandle);
                }
                else
                {
                    LensDistortion.PlumbBobDistort(cmd, renderTarget, DistortedHandle);
                }

                cmd.SetGlobalVector(ScreenSizeProperty, new Vector4(Width, Height, 1.0f / Width, 1.0f / Height));
                var ctx = new PostProcessPassContext(cmd, hd, DistortedHandle);
                SimulatorManager.Instance.Sensors.PostProcessSystem.RenderLateForSensor(ctx, this);
            }

            FinalRenderTarget.BlitTo2D(cmd, hd);
            HDRPUtilities.ExecuteAndClearCommandBuffer(cmd);
            CommandBufferPool.Release(cmd);
        }
示例#7
0
        private static void ReinitializeRenderPipeline()
        {
            // NOTE: This is a workaround for Vulkan. Even if HDRP is reinitialized, lighting data and depth buffers
            //       on render targets (even ones created afterwards) will be corrupted. Reloading scene before
            //       forcefully reinitializing HDRP will refresh both lighting and depth data appropriately.
            //       This happens automatically for scene bundles, but is required for prefab ones.
            //       If this is not called for scene bundles, however, command line execution from async method will
            //       not create render pipeline at all when using Vulkan and crash with invalid memory access
            // Last tested on Unity 2019.3.15f1 and HDRP 7.3.1

            const string loaderScenePath = "Assets/Scenes/LoaderScene.unity";
            var          openScenePaths  = new List <string>();
            var          activeScenePath = SceneManager.GetActiveScene().path;

            for (var i = 0; i < EditorSceneManager.loadedSceneCount; ++i)
            {
                openScenePaths.Add(SceneManager.GetSceneAt(i).path);
            }

            EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);

            var mainScenePath = string.IsNullOrEmpty(activeScenePath) ? loaderScenePath : activeScenePath;

            EditorSceneManager.OpenScene(mainScenePath, OpenSceneMode.Single);
            foreach (var scenePath in openScenePaths)
            {
                if (string.Equals(scenePath, activeScenePath) || string.IsNullOrEmpty(scenePath))
                {
                    continue;
                }

                EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Additive);
            }

            HDRPUtilities.ReinitializeRenderPipeline();
        }
示例#8
0
        /// <summary>
        /// Deinitialization method
        /// </summary>
        /// <param name="reinitializeRenderPipeline">Should reinitialize render pipeline after changes</param>
        private void Deinitialize(bool reinitializeRenderPipeline)
        {
            if (!initialized)
            {
                return;
            }

            //Revert shadows distance
            var shadows =
                volume.profile.components.Find(component => component is HDShadowSettings) as HDShadowSettings;

            if (shadows != null)
            {
                shadows.maxShadowDistance.value /= QualityDistanceMultiplier;
            }

            QualitySettings.renderPipeline = defaultHdrpSettings;
            if (reinitializeRenderPipeline)
            {
                HDRPUtilities.ReinitializeRenderPipeline();
            }

            initialized = false;
        }
示例#9
0
        public bool Save(string path)
        {
            int rotationCount = Mathf.CeilToInt(360.0f / HorizontalAngleLimit);

            float minAngle = 360.0f / CurrentMeasurementsPerRotation;
            int   count    = (int)(HorizontalAngleLimit / minAngle);

            float angle = HorizontalAngleLimit / 2.0f;

            var active = new ReadRequest[rotationCount];
            var cmd    = CommandBufferPool.Get();

            try
            {
                for (int i = 0; i < rotationCount; i++)
                {
                    var rotation = Quaternion.AngleAxis(angle, Vector3.up);
                    SensorCamera.transform.localRotation = rotation;

                    BeginReadRequest(count, ref active[i]);

                    angle += HorizontalAngleLimit;
                    if (angle >= 360.0f)
                    {
                        angle -= 360.0f;
                    }
                }

                for (int i = 0; i < rotationCount; i++)
                {
                    EndReadRequest(cmd, active[i]);
                }
            }
            finally
            {
                HDRPUtilities.ExecuteAndClearCommandBuffer(cmd);
                CommandBufferPool.Release(cmd);
                Array.ForEach(active, req => AvailableRenderTextures.Push(req.TextureSet));
            }

            PointCloudBuffer.GetData(Points);

            var worldToLocal = LidarTransform;

            if (Compensated)
            {
                worldToLocal = worldToLocal * transform.worldToLocalMatrix;
            }

            try
            {
                using (var writer = new PcdWriter(path))
                {
                    for (int p = 0; p < Points.Length; p++)
                    {
                        var point = Points[p];
                        if (point != Vector4.zero)
                        {
                            writer.Write(worldToLocal.MultiplyPoint3x4(point), point.w);
                        }
                    }
                    ;
                }

                return(true);
            }
            catch (Exception ex)
            {
                Debug.LogException(ex);
                return(false);
            }
        }
示例#10
0
        public Vector4[] Capture()
        {
            Debug.Assert(Compensated); // points should be in world-space
            int rotationCount = Mathf.CeilToInt(360.0f / HorizontalAngleLimit);

            float minAngle = 360.0f / CurrentMeasurementsPerRotation;
            int   count    = (int)(HorizontalAngleLimit / minAngle);

            float angle = HorizontalAngleLimit / 2.0f;

            var textures = new Texture2D[rotationCount];

            var cmd = CommandBufferPool.Get();
            var rt  = RenderTexture.active;

            try
            {
                for (int i = 0; i < rotationCount; i++)
                {
                    var rotation = Quaternion.AngleAxis(angle, Vector3.up);
                    SensorCamera.transform.localRotation = rotation;

                    var req = new ReadRequest();
                    if (BeginReadRequest(count, ref req))
                    {
                        RenderTexture.active = req.TextureSet.ColorTexture;
                        Texture2D texture;
                        if (AvailableTextures.Count > 0)
                        {
                            texture = AvailableTextures.Pop();
                        }
                        else
                        {
                            texture = new Texture2D(RenderTextureWidth, RenderTextureHeight, TextureFormat.RGBA32, false, true);
                        }
                        texture.ReadPixels(new Rect(0, 0, RenderTextureWidth, RenderTextureHeight), 0, 0);
                        textures[i] = texture;
                        EndReadRequest(cmd, req);

                        AvailableRenderTextures.Push(req.TextureSet);
                    }

                    angle += HorizontalAngleLimit;
                    if (angle >= 360.0f)
                    {
                        angle -= 360.0f;
                    }
                }
            }
            finally
            {
                HDRPUtilities.ExecuteAndClearCommandBuffer(cmd);
                CommandBufferPool.Release(cmd);
                RenderTexture.active = rt;
                Array.ForEach(textures, AvailableTextures.Push);
            }

            PointCloudBuffer.GetData(Points);

            return(Points);
        }
示例#11
0
        public virtual void Update()
        {
            if (LaserCount != CurrentLaserCount ||
                MeasurementsPerRotation != CurrentMeasurementsPerRotation ||
                FieldOfView != CurrentFieldOfView ||
                CenterAngle != CurrentCenterAngle ||
                MinDistance != CurrentMinDistance ||
                MaxDistance != CurrentMaxDistance ||
                !Enumerable.SequenceEqual(VerticalRayAngles, CurrentVerticalRayAngles))
            {
                if (MinDistance > 0 && MaxDistance > 0 && LaserCount > 0 && MeasurementsPerRotation >= (360.0f / HorizontalAngleLimit))
                {
                    Reset();
                }
            }

            UpdateMarker.Begin();

            var cmd = CommandBufferPool.Get();

            foreach (var req in Active)
            {
                if (!req.TextureSet.IsValid(RenderTextureWidth, RenderTextureHeight))
                {
                    // lost render texture, probably due to Unity window resize or smth
                    req.TextureSet.Release();
                }
                else
                {
                    EndReadRequest(cmd, req);
                    AvailableRenderTextures.Push(req.TextureSet);

                    if (req.Index + req.Count >= CurrentMeasurementsPerRotation)
                    {
                        SendMessage();
                    }
                }
            }

            HDRPUtilities.ExecuteAndClearCommandBuffer(cmd);
            CommandBufferPool.Release(cmd);

            Active.Clear();

            if (IgnoreNewRquests > 0)
            {
                IgnoreNewRquests -= Time.unscaledDeltaTime;
            }
            else
            {
                float minAngle = 360.0f / CurrentMeasurementsPerRotation;

                AngleDelta += Time.deltaTime * 360.0f * RotationFrequency;
                int count = Mathf.CeilToInt(HorizontalAngleLimit / minAngle);

                while (AngleDelta >= HorizontalAngleLimit)
                {
                    float angle    = AngleStart + HorizontalAngleLimit / 2.0f;
                    var   rotation = Quaternion.AngleAxis(angle, Vector3.up);
                    SensorCamera.transform.localRotation = rotation;
                    if (Top != null)
                    {
                        Top.transform.localRotation = rotation;
                    }

                    var req = new ReadRequest();
                    if (BeginReadRequest(count, ref req))
                    {
                        req.AngleStart = AngleStart;

                        DateTime dt        = DateTimeOffset.FromUnixTimeMilliseconds((long)(SimulatorManager.Instance.CurrentTime * 1000.0)).UtcDateTime;
                        DateTime startHour = new DateTime(dt.Year, dt.Month,
                                                          dt.Day, dt.Hour, 0, 0);
                        TimeSpan timeOverHour = dt - startHour;
                        req.TimeStamp = (uint)(timeOverHour.Ticks / (TimeSpan.TicksPerMillisecond) * 1000);

                        Active.Add(req);
                    }

                    AngleDelta -= HorizontalAngleLimit;
                    AngleStart += HorizontalAngleLimit;

                    if (AngleStart >= 360.0f)
                    {
                        AngleStart -= 360.0f;
                    }
                }
            }

            UpdateMarker.End();
        }
        public static SignedDistanceFieldData Generate(GameObject obj, ComputeShader cs, Vector3Int resolution, Bounds bounds, float step)
        {
            var width  = resolution.x;
            var height = resolution.y;
            var depth  = resolution.z;

            var meshFilters = obj.GetComponentsInChildren <MeshFilter>();

            var voxelRes = new[] { width, height, depth };

            var resVoxels = new bool[width][][];

            for (var i = 0; i < width; ++i)
            {
                resVoxels[i] = new bool[height][];
                for (var j = 0; j < height; ++j)
                {
                    resVoxels[i][j] = new bool[depth];
                }
            }

            foreach (var meshFilter in meshFilters)
            {
                Voxelize(meshFilter, bounds, resolution, step, resVoxels);
            }

            var count     = width * height * depth;
            var bufferCpu = new float[count];

            for (var x = 0; x < width; ++x)
            {
                for (var z = 0; z < depth; ++z)
                {
                    var filled = false;

                    for (var y = height - 1; y >= 0; --y)
                    {
                        var full = resVoxels[x][y][z] || filled;
                        if (!filled && full)
                        {
                            filled = true;
                        }

                        var pos = TreeUtility.Flatten(x, y, z, voxelRes);
                        bufferCpu[pos] = full ? 1f : 0f;
                    }
                }
            }

            var buffer = new ComputeBuffer(count, sizeof(float));

            buffer.SetData(bufferCpu);

            var texture = new RenderTexture(width, height, 0, RenderTextureFormat.R8, RenderTextureReadWrite.Default)
            {
                dimension         = TextureDimension.Tex3D,
                volumeDepth       = depth,
                enableRandomWrite = true,
                wrapMode          = TextureWrapMode.Clamp
            };

            texture.Create();

            var initializeKernel = cs.FindKernel("Initialize");

            cs.SetTexture(initializeKernel, "_Texture", texture);
            cs.SetBuffer(initializeKernel, "_VoxelBuffer", buffer);
            cs.SetVector("_TexSize", new Vector4(width, height, depth, 0f));
            cs.Dispatch(initializeKernel, HDRPUtilities.GetGroupSize(width, 8),
                        HDRPUtilities.GetGroupSize(height, 8), HDRPUtilities.GetGroupSize(depth, 8));

            buffer.Dispose();

            return(new SignedDistanceFieldData()
            {
                texture = texture,
                bounds = bounds,
                voxels = resVoxels,
                step = step
            });
        }