Пример #1
0
    //Membuat titik sampel beserta nilai density-nya
    //Creates sample points along its density value
    public void GenerateDensity()
    {
        Debug.ClearDeveloperConsole();
        totalTimeElapsed   = 0;
        timeDuration       = 0;
        timeStart          = 0;
        generatedMeshCount = numberOfGeneratedMeshObject.x * numberOfGeneratedMeshObject.y * numberOfGeneratedMeshObject.z;

        InitGeneratedMesh();
        Vector3 offset = Vector3.zero;
        int     index;

        //Buffer for sample points. Value for second parameter is based on Vector4 size in byte
        _pointsBuffer = new ComputeBuffer(_voxel.totalVertex, 16);
        //Buffer for line data. Value for second parameter is based on LineProperties struct's size in byte
        _linePropBuffer = new ComputeBuffer(_lineGenerator.line.Count, 52);
        //Buffer for line's node data. Value for second parameter is based on NodeProperties struct's size in byte
        _nodePositionBuffer = new ComputeBuffer(_lineGenerator.node.Count, 20);

        numThreadGroup.x = Mathf.CeilToInt(_voxel.numVertex.x / numThreads) + 1;
        numThreadGroup.y = Mathf.CeilToInt(_voxel.numVertex.y / numThreads) + 1;
        numThreadGroup.z = Mathf.CeilToInt(_voxel.numVertex.z / numThreads) + 1;

        for (int k = 0; k < numberOfGeneratedMeshObject.z; k++)
        {
            for (int j = 0; j < numberOfGeneratedMeshObject.y; j++)
            {
                for (int i = 0; i < numberOfGeneratedMeshObject.x; i++)
                {
                    timeStart = (float)System.DateTime.Now.Second + ((float)System.DateTime.Now.Millisecond / 1000);

                    offset.x = i * _voxel.boundaryWorldPos.x;
                    offset.y = j * _voxel.boundaryWorldPos.y;
                    offset.z = k * _voxel.boundaryWorldPos.z;
                    index    = i + (k * numberOfGeneratedMeshObject.x) + (j * (numberOfGeneratedMeshObject.x * numberOfGeneratedMeshObject.z));
                    SetGeneralInputs(_densityShader);

                    _linePropBuffer.SetData(_lineGenerator.GetLineProps());
                    _nodePositionBuffer.SetData(_lineGenerator.GetNodes());

                    _densityShader.SetBuffer(0, "nodeProps", _nodePositionBuffer);
                    _densityShader.SetBuffer(0, "lineProps", _linePropBuffer);
                    _densityShader.SetBool("isWorldSpaceNoise", isWorldSpaceNoise);
                    _densityShader.SetFloat("noiseIntensity", noiseIntensity);
                    _densityShader.SetFloat("noiseScale", noiseScale);
                    _densityShader.SetFloat("smoothingFactor", smoothingFactor);
                    _densityShader.SetVector("offset", offset);
                    _densityShader.Dispatch(0, numThreadGroup.x, numThreadGroup.y, numThreadGroup.z);
                    //_densityShader.Dispatch(0,_voxel.numVoxel.x+1,_voxel.numVoxel.y+1,_voxel.numVoxel.z+1);

                    _pointsDensity = new Vector4[_voxel.totalVertex];
                    _pointsBuffer.GetData(_pointsDensity);

                    //generatedMeshObjectsHolder.transform.GetChild(index).GetComponent<GeneratedMeshProperties>().Initiate();
                    GenerateMesh(index);
                }
            }
        }
        //Debug.Log("Total time elapsed: "+ totalTimeElapsed);
        if (showGeneratedVertexInfo)
        {
            ShowVoidVertex();
        }
        ReleaseBuffers();
    }