private void CreateFromPrefab(GameObject go, Material replaceMaterial)
        {
            var lodGroup = go.GetComponent <LODGroup>();

            if (lodGroup != null)
            {
                var lods = lodGroup.GetLODs();

                _lodRatios    = new float[lods.Length];
                _lodRenderers = new LodRenderer[lods.Length];

                for (int i = 0; i < lods.Length; ++i)
                {
                    var lod = lods[i];

                    _lodRatios[i]    = lod.screenRelativeTransitionHeight;
                    _lodRenderers[i] = new LodRenderer
                    {
                        Renderers = new LodActualRenderer[lod.renderers.Length]
                    };

                    var singleLod = _lodRenderers[i];

                    for (int j = 0; j < lod.renderers.Length; ++j)
                    {
                        var meshFilter = lod.renderers[j].GetComponent <MeshFilter>();
                        // plain meshfiler&meshrenderer
                        if (meshFilter != null)
                        {
                            if (replaceMaterial == null)
                            {
                                singleLod.Renderers[j] = BuildRenderer(meshFilter.sharedMesh,
                                                                       lod.renderers[j].GetComponent <MeshRenderer>().sharedMaterials);
                            }
                            else
                            {
                                singleLod.Renderers[j] = BuildRenderer(meshFilter.sharedMesh,
                                                                       GetDefaultInstanceMaterial(meshFilter.sharedMesh.subMeshCount, null,
                                                                                                  replaceMaterial));
                            }
                        }
                        else
                        {
                            // Billboard Renderer&Billboard Asset
                        }
                    }
                }

                SetBoundingVolume(lods[0]);
                _lodSize = lodGroup.size;
            }
            else
            {
                var nonLodMesh      = go.GetComponent <MeshFilter>().sharedMesh;
                var nonLodMaterials = GetDefaultInstanceMaterial(nonLodMesh.subMeshCount,
                                                                 go.GetComponent <MeshRenderer>().sharedMaterials, replaceMaterial);

                CreateNonLodRenderer(nonLodMesh, nonLodMaterials);
            }
        }
        private void CreateNonLodRenderer(Mesh nonLodMesh, Material[] nonLodMaterials)
        {
            var renderer = BuildRenderer(nonLodMesh, nonLodMaterials);

            _lodRenderers    = new LodRenderer[1];
            _lodRenderers[0] = new LodRenderer
            {
                Renderers = new [] { renderer }
            };

            var bounds = renderer.Mesh.bounds;

            _sphereCenter = bounds.center;
            _sphereRadius = bounds.extents.magnitude;

            var size = bounds.size;

            _lodSize = Mathf.Max(size.x, Mathf.Max(size.y, size.z));
        }
Esempio n. 3
0
        public InstancingRenderer(DetailPrototype prototype)
        {
            Mesh nonLodMesh = null;

            Material[] nonLodMaterials = null;

            if (prototype.usePrototypeMesh)
            {
                var go       = prototype.prototype;
                var lodGroup = go.GetComponent <LODGroup>();
                if (lodGroup != null)
                {
                    var lods = lodGroup.GetLODs();

                    _lodRatios    = new float[lods.Length];
                    _lodRenderers = new LodRenderer[lods.Length];

                    for (int i = 0; i < lods.Length; ++i)
                    {
                        var lod = lods[i];

                        _lodRatios[i]    = lod.screenRelativeTransitionHeight;
                        _lodRenderers[i] = new LodRenderer
                        {
                            Renderers = new LodActualRenderer[lod.renderers.Length]
                        };

                        for (int j = 0; j < lod.renderers.Length; ++j)
                        {
                            var meshFilter = lod.renderers[j].GetComponent <MeshFilter>();
                            _lodRenderers[i].Renderers[j] = BuildRenderer(meshFilter.sharedMesh,
                                                                          GetDefaultInstanceMaterial(meshFilter.sharedMesh, null));
                        }
                    }

                    SetBoundingVolume(lods[0]);
                    _lodSize = lodGroup.size;
                }
                else
                {
                    nonLodMesh      = go.GetComponent <MeshFilter>().sharedMesh;
                    nonLodMaterials = GetDefaultInstanceMaterial(nonLodMesh, go.GetComponent <MeshRenderer>().sharedMaterials);
                }
            }
            else
            {
                nonLodMesh      = Constants.Rendering.QuadMesh;
                nonLodMaterials = new [] { Constants.Rendering.GetGrassMaterial() };

                foreach (var nonLodMaterial in nonLodMaterials)
                {
                    nonLodMaterial.mainTexture = prototype.prototypeTexture;
                }
            }

            if (_lodRenderers == null)
            {
                var renderer = BuildRenderer(nonLodMesh, nonLodMaterials);

                _lodRenderers    = new LodRenderer[1];
                _lodRenderers[0] = new LodRenderer
                {
                    Renderers = new [] { renderer }
                };

                var bounds = renderer.Mesh.bounds;
                _sphereCenter = bounds.center;
                _sphereRadius = bounds.extents.magnitude;

                var size = bounds.size;
                _lodSize = Mathf.Max(size.x, Mathf.Max(size.y, size.z));
            }
        }