Пример #1
0
        public void InitializeInputArrays(NativeArray <BoundingVolumeHierarchy.PointAndIndex> points, NativeArray <Aabb> aabbs, NativeArray <CollisionFilter> filters)
        {
            var random = new Mathematics.Random(1234);

            const float posRange       = 1000f;
            const float radiusRangeMin = 1f;
            const float radiusRangeMax = 10f;

            for (int i = 0; i < points.Length; i++)
            {
                float2 pos;
                pos.x     = random.NextFloat(-posRange, posRange);
                pos.y     = random.NextFloat(-posRange, posRange);
                points[i] = new BoundingVolumeHierarchy.PointAndIndex {
                    Position = pos, Index = i
                };

                var radius = new float2(random.NextFloat(radiusRangeMin, radiusRangeMax));
                aabbs[i] = new Aabb {
                    Min = pos - radius, Max = pos + radius
                };

                filters[i] = CollisionFilter.Default;
            }
        }
Пример #2
0
        public void InitializeInputWithCopyArrays(NativeArray <BoundingVolumeHierarchy.PointAndIndex> points, NativeArray <Aabb> aabbs, NativeArray <CollisionFilter> filters)
        {
            var random = new Mathematics.Random(1234);

            const float posRange       = 1000f;
            const float radiusRangeMin = 1f;
            const float radiusRangeMax = 10f;

            for (int i = 0; i < points.Length; i++)
            {
                var pos = random.NextFloat2(-posRange, posRange);
                points[i] = new BoundingVolumeHierarchy.PointAndIndex {
                    Position = pos, Index = i
                };

                var radius = new float2(random.NextFloat(radiusRangeMin, radiusRangeMax));
                aabbs[i] = new Aabb {
                    Min = pos - radius, Max = pos + radius
                };

                points[i + 1] = new BoundingVolumeHierarchy.PointAndIndex {
                    Position = pos, Index = i + 1
                };

                aabbs[i + 1] = new Aabb {
                    Min = pos - radius, Max = pos + radius
                };

                filters[i] = new CollisionFilter
                {
                    GroupIndex   = 0,
                    BelongsTo    = random.NextUInt(0, 16),
                    CollidesWith = random.NextUInt(0, 16)
                };

                filters[i + 1] = new CollisionFilter
                {
                    GroupIndex   = 0,
                    BelongsTo    = random.NextUInt(0, 16),
                    CollidesWith = random.NextUInt(0, 16)
                };

                i++;
            }
        }
Пример #3
0
        unsafe public void Render()
        {
            if (m_DebugRenderMode == DebugRenderMode.None)
            {
                return;
            }

            if (m_DebugRenderMode == DebugRenderMode.Mesh)
            {
                var OcclusionMesh = GetComponentTypeHandle <OcclusionMesh>();

                var material = new Material(Shader.Find("Hidden/OcclusionDebug"));

                var layout = new[]
                {
                    new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32, 4)
                };

                var chunks = m_Occluders.CreateArchetypeChunkArray(Allocator.TempJob);

                Mathematics.Random rnd = new Mathematics.Random(1);
                for (int i = 0; i < chunks.Length; ++i)
                {
                    var chunk  = chunks[i];
                    var meshes = chunk.GetNativeArray(OcclusionMesh);
                    for (int k = 0; k < meshes.Length; k++)
                    {
                        var m = meshes[k];

                        Mesh mesh = new Mesh();
                        mesh.SetVertexBufferParams(m.vertexCount, layout);

                        var verts = (float4 *)m.transformedVertexData.GetUnsafePtr();
                        var tris  = (int *)m.indexData.GetUnsafePtr();

                        var outVerts = new NativeArray <Vector4>(m.vertexCount, Allocator.Temp);
                        for (int vtx = 0; vtx < m.vertexCount; vtx++, ++verts)
                        {
                            outVerts[vtx] = new Vector4(verts->x, verts->y, verts->z, verts->w);
                        }
                        mesh.SetVertexBufferData(outVerts, 0, 0, m.vertexCount);
                        outVerts.Dispose();

                        var outTris = new int[m.indexCount];
                        for (int idx = 0; idx < m.indexCount; idx++, ++tris)
                        {
                            outTris[idx] = *tris;
                        }
                        mesh.SetIndices(outTris, MeshTopology.Triangles, 0);

                        mesh.name = $"Debug Occluder {i}:{k}";

                        // the vertices are already in screenspace and perspective projected
                        mesh.bounds = new Bounds(Vector3.zero, new Vector3(10000, 10000, 1000));

                        var color = Color.HSVToRGB(rnd.NextFloat(), 1.0f, 1.0f);
                        material.SetColor("_Color", color);

                        material.SetPass(0);
                        Graphics.DrawMeshNow(mesh, Matrix4x4.identity);
                    }
                }

                chunks.Dispose();
            }
            else if (m_DepthTexture != null)
            {
                var  material = new Material(Shader.Find("Hidden/OcclusionShowDepth"));
                Mesh quad     = GetQuadMesh();


                if (m_DebugRenderMode == DebugRenderMode.Bounds)
                {
                    material.EnableKeyword("BOUNDS_ONLY");
                }
                else if (m_DebugRenderMode == DebugRenderMode.Test)
                {
                    material.EnableKeyword("DEPTH_WITH_TEST");
                }

                material.SetTexture("_Depth", m_DepthTexture);
                material.SetTexture("_Test", m_TestTexture);
                material.SetTexture("_Bounds", m_BoundsTexture);
                material.SetPass(0);
                Graphics.DrawMeshNow(quad, Matrix4x4.identity);
            }
        }