Exemple #1
0
        public GameObject ComputeNow(RGLODQuadTreeNode node, RGDataSource data, float density)
        {
            try
            {
                List <RGChunk> chunks = node.GetChunks(data);

                if (chunks == null)
                {
                    return(null);
                }

                foreach (RGChunk ck in chunks)
                {
                    foreach (KeyValuePair <int, RedGrassInstance> kvp in ck.grasses)
                    {
                        if (kvp.Value.Layer == 0)
                        {
                            _grasses.Add(kvp.Value);
                        }
                        else
                        {
                            _triGrasses.Add(kvp.Value);
                        }

                        if (ck.HGrass.ContainsKey(kvp.Key))
                        {
                            foreach (RedGrassInstance rgi in ck.HGrass[kvp.Key])
                            {
                                if (rgi.Layer == 0)
                                {
                                    _grasses.Add(rgi);
                                }
                                else
                                {
                                    _triGrasses.Add(rgi);
                                }
                            }
                        }
                    }
                }

                RedGrassMeshComputer.ComputeMesh(_grasses, _triGrasses, density);
                _grasses.Clear();
                _triGrasses.Clear();

                // Get Billboard output struct
                OutputGroup opg = new OutputGroup();

                int billboardCnt = RedGrassMeshComputer.s_Output.BillboardCount;
                int offsetCnt    = 0;

                while (billboardCnt > 0)
                {
                    int       cnt      = Mathf.Min(billboardCnt, mEvni.MeshQuadMaxCount);
                    Vector3[] verts    = new Vector3[cnt * 4];
                    Vector3[] norms    = new Vector3[cnt * 4];
                    Vector2[] uvs      = new Vector2[cnt * 4];
                    Vector2[] uv2s     = new Vector2[cnt * 4];
                    Color32[] colors32 = new Color32[cnt * 4];
                    int[]     indices  = new int[cnt * 6];

                    System.Array.Copy(RedGrassMeshComputer.s_Output.Verts, offsetCnt * 4, verts, 0, cnt * 4);
                    System.Array.Copy(RedGrassMeshComputer.s_Output.Norms, offsetCnt * 4, norms, 0, cnt * 4);
                    System.Array.Copy(RedGrassMeshComputer.s_Output.UVs, offsetCnt * 4, uvs, 0, cnt * 4);
                    System.Array.Copy(RedGrassMeshComputer.s_Output.UV2s, offsetCnt * 4, uv2s, 0, cnt * 4);
                    System.Array.Copy(RedGrassMeshComputer.s_Output.Color32s, offsetCnt * 4, colors32, 0, cnt * 4);
                    System.Array.Copy(RedGrassMeshComputer.s_Output.Indices, indices, cnt * 6);

                    opg.billboard.mVerts.Add(verts);
                    opg.billboard.mNorms.Add(norms);
                    opg.billboard.mUVs.Add(uvs);
                    opg.billboard.mUV2s.Add(uv2s);
                    opg.billboard.mColors32.Add(colors32);
                    opg.billboard.mIndices.Add(indices);

                    billboardCnt = billboardCnt - mEvni.MeshQuadMaxCount;
                    offsetCnt   += mEvni.MeshQuadMaxCount;
                }

                // Get Tri-Angel output struct
                int triCnt = RedGrassMeshComputer.s_Output.TriquadCount;
                offsetCnt = RedGrassMeshComputer.s_Output.BillboardCount;

                while (triCnt > 0)
                {
                    int cnt = Mathf.Min(triCnt, mEvni.MeshQuadMaxCount);

                    Vector3[] verts    = new Vector3[cnt * 4];
                    Vector3[] norms    = new Vector3[cnt * 4];
                    Vector2[] uvs      = new Vector2[cnt * 4];
                    Vector2[] uv2s     = new Vector2[cnt * 4];
                    Color32[] colors32 = new Color32[cnt * 4];
                    int[]     indices  = new int[cnt * 6];

                    System.Array.Copy(RedGrassMeshComputer.s_Output.Verts, offsetCnt * 4, verts, 0, cnt * 4);
                    System.Array.Copy(RedGrassMeshComputer.s_Output.Norms, offsetCnt * 4, norms, 0, cnt * 4);
                    System.Array.Copy(RedGrassMeshComputer.s_Output.UVs, offsetCnt * 4, uvs, 0, cnt * 4);
                    System.Array.Copy(RedGrassMeshComputer.s_Output.UV2s, offsetCnt * 4, uv2s, 0, cnt * 4);
                    System.Array.Copy(RedGrassMeshComputer.s_Output.Color32s, offsetCnt * 4, colors32, 0, cnt * 4);
                    System.Array.Copy(RedGrassMeshComputer.s_Output.Indices, indices, cnt * 6);

                    opg.tri.mVerts.Add(verts);
                    opg.tri.mNorms.Add(norms);
                    opg.tri.mUVs.Add(uvs);
                    opg.tri.mUV2s.Add(uv2s);
                    opg.tri.mColors32.Add(colors32);
                    opg.tri.mIndices.Add(indices);

                    triCnt     = triCnt - mEvni.MeshQuadMaxCount;
                    offsetCnt += mEvni.MeshQuadMaxCount;
                }

                string     desc = "Grass Batch [" + node.xIndex.ToString() + ", " + node.zIndex.ToString() + "] _ LOD [" + node.LOD + "]";
                GameObject go   = CreateBatchGos(desc, opg.billboard, opg.tri);
                node.gameObject = go;

                if (go != null)
                {
                    go.transform.parent        = transform;
                    go.transform.localPosition = Vector3.zero;

                    if (go.activeSelf != node.visible)
                    {
                        go.SetActive(node.visible);
                    }
                }

                return(go);
            }
            catch
            {
                return(null);
            }
        }
Exemple #2
0
        public void ProcessReqs(Queue <RGRegion> reqs)
        {
            while (true)
            {
                RGRegion region = null;
                lock (reqs)
                {
                    if (mReqs.Count == 0)
                    {
                        break;
                    }

                    region = reqs.Dequeue();
                }

                OutpuRegion opr = new OutpuRegion();

                foreach (RGLODQuadTreeNode node in region.nodes)
                {
                    List <RGChunk> chunks = node.GetChunks(mData);

                    if (chunks == null)
                    {
                        continue;
                    }

                    foreach (RGChunk ck in chunks)
                    {
                        foreach (KeyValuePair <int, RedGrassInstance> kvp in ck.grasses)
                        {
                            if (kvp.Value.Layer == 0)
                            {
                                _grasses.Add(kvp.Value);
                            }
                            else
                            {
                                _triGrasses.Add(kvp.Value);
                            }

                            if (ck.HGrass.ContainsKey(kvp.Key))
                            {
                                foreach (RedGrassInstance rgi in ck.HGrass[kvp.Key])
                                {
                                    if (rgi.Layer == 0)
                                    {
                                        _grasses.Add(rgi);
                                    }
                                    else
                                    {
                                        _triGrasses.Add(rgi);
                                    }
                                }
                            }
                        }
                    }

                    if (node.LOD >= mEvni.LODDensities.Length)
                    {
                        node.LOD = mEvni.LODDensities.Length - 1;
                    }
                    if (node.LOD < 0)
                    {
                        node.LOD = 0;
                    }
                    RedGrassMeshComputer.ComputeMesh(_grasses, _triGrasses, mEvni.Density * mEvni.LODDensities[node.LOD]);

                    _grasses.Clear();
                    _triGrasses.Clear();

                    // Get Billboard output struct
                    OutputGroup opg = new OutputGroup();
                    opg.node = node;


                    int billboardCnt = RedGrassMeshComputer.s_Output.BillboardCount;
                    int offsetCnt    = 0;

                    while (billboardCnt > 0)
                    {
                        int       cnt      = Mathf.Min(billboardCnt, mEvni.MeshQuadMaxCount);
                        Vector3[] verts    = new Vector3 [cnt * 4];
                        Vector3[] norms    = new Vector3 [cnt * 4];
                        Vector2[] uvs      = new Vector2 [cnt * 4];
                        Vector2[] uv2s     = new Vector2 [cnt * 4];
                        Color32[] colors32 = new Color32 [cnt * 4];
                        int[]     indices  = new int [cnt * 6];

                        System.Array.Copy(RedGrassMeshComputer.s_Output.Verts, offsetCnt * 4, verts, 0, cnt * 4);
                        System.Array.Copy(RedGrassMeshComputer.s_Output.Norms, offsetCnt * 4, norms, 0, cnt * 4);
                        System.Array.Copy(RedGrassMeshComputer.s_Output.UVs, offsetCnt * 4, uvs, 0, cnt * 4);
                        System.Array.Copy(RedGrassMeshComputer.s_Output.UV2s, offsetCnt * 4, uv2s, 0, cnt * 4);
                        System.Array.Copy(RedGrassMeshComputer.s_Output.Color32s, offsetCnt * 4, colors32, 0, cnt * 4);
                        System.Array.Copy(RedGrassMeshComputer.s_Output.Indices, indices, cnt * 6);

                        opg.billboard.mVerts.Add(verts);
                        opg.billboard.mNorms.Add(norms);
                        opg.billboard.mUVs.Add(uvs);
                        opg.billboard.mUV2s.Add(uv2s);
                        opg.billboard.mColors32.Add(colors32);
                        opg.billboard.mIndices.Add(indices);

                        billboardCnt = billboardCnt - mEvni.MeshQuadMaxCount;
                        offsetCnt   += mEvni.MeshQuadMaxCount;
                    }

                    // Get Tri-Angel output struct
                    int triCnt = RedGrassMeshComputer.s_Output.TriquadCount;
                    offsetCnt = RedGrassMeshComputer.s_Output.BillboardCount;

                    while (triCnt > 0)
                    {
                        int cnt = Mathf.Min(triCnt, mEvni.MeshQuadMaxCount);

                        Vector3[] verts    = new Vector3 [cnt * 4];
                        Vector3[] norms    = new Vector3 [cnt * 4];
                        Vector2[] uvs      = new Vector2 [cnt * 4];
                        Vector2[] uv2s     = new Vector2 [cnt * 4];
                        Color32[] colors32 = new Color32 [cnt * 4];
                        int[]     indices  = new int [cnt * 6];

                        System.Array.Copy(RedGrassMeshComputer.s_Output.Verts, offsetCnt * 4, verts, 0, cnt * 4);
                        System.Array.Copy(RedGrassMeshComputer.s_Output.Norms, offsetCnt * 4, norms, 0, cnt * 4);
                        System.Array.Copy(RedGrassMeshComputer.s_Output.UVs, offsetCnt * 4, uvs, 0, cnt * 4);
                        System.Array.Copy(RedGrassMeshComputer.s_Output.UV2s, offsetCnt * 4, uv2s, 0, cnt * 4);
                        System.Array.Copy(RedGrassMeshComputer.s_Output.Color32s, offsetCnt * 4, colors32, 0, cnt * 4);
                        System.Array.Copy(RedGrassMeshComputer.s_Output.Indices, indices, cnt * 6);

                        opg.tri.mVerts.Add(verts);
                        opg.tri.mNorms.Add(norms);
                        opg.tri.mUVs.Add(uvs);
                        opg.tri.mUV2s.Add(uv2s);
                        opg.tri.mColors32.Add(colors32);
                        opg.tri.mIndices.Add(indices);

                        triCnt     = triCnt - mEvni.MeshQuadMaxCount;
                        offsetCnt += mEvni.MeshQuadMaxCount;
                    }

                    opr.groups.Add(opg);
                    opr.oldGos = region.oldGos;
                }

                lock (mOutputs)
                {
                    mOutputs.Add(opr);
                }
            }
        }