Esempio n. 1
0
    /// <summary>
    /// Process meshFilters in Unity's main thread, as we are required to by Unity. At least we've rigged it as a
    /// coroutine! Right? OK I know I really wish we could have used mesh data in a thread but properties die as well.
    /// </summary>
    /// <returns>IEnumartor aka Coroutine</returns>
    /// <remarks>
    /// For the sake of the demo we are going to need to roll over the "Target" to find all the
    /// meshes that we need to look at, but in theory you could do this without having to load the
    /// object by simply having raw mesh data, or any other means of accessing it.
    /// </remarks>
    public IEnumerator PreProcess()
    {
        _combinerStartTime = Time.time;
        // Create a new MeshCombiner (we dont want any old data kicking around)
        _meshCombiner = new Hydrogen.Threading.Jobs.MeshCombiner();

        // Yes We Hate This - There Are Better Implementations
        MeshFilter[] meshFilters = TargetMeshes.GetComponentsInChildren <MeshFilter> ();

        // Loop through all of our mesh filters and add them to the combiner to be combined.
        for (int x = 0; x < meshFilters.Length; x++)
        {
            if (meshFilters [x].gameObject.activeSelf)
            {
                _meshCombiner.AddMesh(meshFilters [x],
                                      meshFilters [x].GetComponent <Renderer>(),
                                      meshFilters [x].transform.localToWorldMatrix);
            }

            // We implemented this as a balance point to try and break some of the processing up.
            // If we were to yield every pass it was taking to long to do nothing.
            if (x > 0 && x % ThrottleRate == 0)
            {
                yield return(new WaitForEndOfFrame());
            }
        }

        // Start the threaded love
        if (_meshCombiner.MeshInputCount > 0)
        {
            _meshCombiner.Combine(ThreadCallback);
        }
        yield return(new WaitForEndOfFrame());
    }