Esempio n. 1
0
        public virtual void SetCrossboardDataset(CrossboardDataset dataset, Material material)
        {
            var mesh = new Mesh();

            mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
            mesh.vertices    = dataset.POSITION;

            if (OpaqueCrossboard)
            {
                mesh.SetUVs(0, dataset.UV0List);
                mesh.SetUVs(1, dataset.UV1List);
                mesh.SetUVs(2, dataset.UV2List);
                mesh.SetUVs(3, dataset.UV3List);
            }
            else if (OpaqueCrossboardCompute)
            {
                mesh.SetUVs(0, dataset.UV0ListComp);
                mesh.SetUVs(1, dataset.UV1ListComp);
            }
            else
            {
                mesh.uv  = dataset.UV0;
                mesh.uv2 = dataset.UV1;
            }

            mesh.colors = dataset.COLOR;

            var n       = dataset.POSITION.Length;
            var indices = new int[n];

            for (var i = 0; i < n; ++i)
            {
                indices[i] = i;
            }

            mesh.SetIndices(indices, MeshTopology.Points, 0);


            //mesh.bounds = new Bounds(Vector3.zero, new Vector3(100f, 100f, 100f));

            var meshFilter = gameObject.GetComponent <MeshFilter>();

            if (meshFilter == null)
            {
                meshFilter = gameObject.AddComponent <MeshFilter>();
            }

            meshFilter.sharedMesh = mesh;

            var meshRenderer = gameObject.GetComponent <MeshRenderer>();

            if (meshRenderer == null)
            {
                meshRenderer = gameObject.AddComponent <MeshRenderer>();
            }

            meshRenderer.sharedMaterial = material ?? DefaultMaterial;
        }
        public override void SetCrossboardDataset(CrossboardDataset dataset, Material material)
        {
            var n = dataset.POSITION.Length;

            // instance data is 6 floats (position x,y,z, extents x,y,z)
            _inputBuffer = new ComputeBuffer(n, Marshal.SizeOf(typeof(instance_data)), ComputeBufferType.Default);

            // render data is 3 floats (position x,y,z)
            _outputBuffer = new ComputeBuffer(n, Marshal.SizeOf(typeof(instance_data)), ComputeBufferType.Append);

            // n instances
            var instanceDataArray = new instance_data[n];

            // copy data
            for (var i = 0; i < n; ++i)
            {
                instanceDataArray[i].Position = dataset.POSITION[i];
                instanceDataArray[i].Size     = dataset.UV0ListComp[i].x;
                instanceDataArray[i].Rotation = new Vector3(dataset.UV0ListComp[i].y, dataset.UV0ListComp[i].z, dataset.UV0ListComp[i].w);
                instanceDataArray[i].Color    = new Vector3(dataset.COLOR[i].r, dataset.COLOR[i].g, dataset.COLOR[i].b);

                instanceDataArray[i].Offset      = dataset.UV1ListComp[i].x;
                instanceDataArray[i].PlaneOffset = new Vector3(dataset.UV1ListComp[i].y, dataset.UV1ListComp[i].z, dataset.UV1ListComp[i].w);
            }

            // copy data to GPU buffer
            _inputBuffer.SetData(instanceDataArray);

            // create indirect argument buffer
            _argBuffer = new ComputeBuffer(4, Marshal.SizeOf(typeof(int)), ComputeBufferType.IndirectArguments);

            // clone the material
            // _material = Instantiate(material);// Material;// Instantiate(Material);
            if (_material)
            {
                Destroy(_material);
            }

            _material = material ?? DefaultMaterial;

            // get cull kernel
            _cullingKernel = _computeShader.FindKernel("CS_Cull");

            _computeShader.SetBuffer(_cullingKernel, Shader.PropertyToID("Input"), _inputBuffer);
            _computeShader.SetBuffer(_cullingKernel, Shader.PropertyToID("Output"), _outputBuffer);
            _computeShader.SetInt(Shader.PropertyToID("_count"), n);

            int[] args = new int[] { 0, 1, 0, 0 };
            _argBuffer.SetData(args);

            _material.SetBuffer("_buffer", _outputBuffer);
        }