Exemplo n.º 1
0
        protected virtual void Initialize(Transform transform, AVolumeSettings settings)
        {
            _transform = transform;
            _settings  = settings;

            //always add a bounds border when initializing AVolume
            AVolumeSettings.AddBoundsBorder(ref settings);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets all Renderers parented to the VolumeBaker & Encapsulates them with the bounds (unless manual bounds is enabled)
        /// </summary>
        /// <param name="settings"></param>
        /// <param name="renderers"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        public bool GetChildRenderersAndEncapsulate(ref AVolumeSettings settings, ref List <Renderer> renderers, Transform target)
        {
            if (renderers == null)
            {
                return(false);
            }

            //get renderers in children
            Renderer[] mrs = target.GetComponentsInChildren <Renderer>();
            if (mrs == null || mrs.Length == 0)
            {
                return(false);
            }

            Bounds newBounds = useManualBounds ? bounds : new Bounds(target.position, Vector3.zero);
            bool   first     = true;

            foreach (var r in mrs)
            {
                if (!(r is MeshRenderer) && !(r is SkinnedMeshRenderer))
                {
                    continue;
                }
                //skip inactive renderers
                if (!r.gameObject.activeSelf)
                {
                    continue;
                }
                if (!r.enabled)
                {
                    continue;
                }

                if (!useManualBounds) //do not change bounds if manual bounds specified
                {
                    if (!fitToVertices)
                    {
                        newBounds.Encapsulate(r.bounds);
                    }
                    else
                    {
                        //iterate all vertices and encapsulate
                        Mesh mesh = null;
                        if (r is MeshRenderer)
                        {
                            MeshFilter mf = r.GetComponent <MeshFilter>();
                            if (mf != null && mf.sharedMesh != null)
                            {
                                mesh = mf.sharedMesh;
                            }
                        }
                        else
                        {
                            mesh = (r as SkinnedMeshRenderer).sharedMesh;
                        }

                        if (mesh != null)
                        {
                            Bounds b = EncapsulateVertices(mesh, r.transform.localToWorldMatrix);

                            if (first)
                            {
                                newBounds = b;
                            }
                            else
                            {
                                newBounds.Encapsulate(b);
                            }

                            first = false;
                        }
                    }
                }

                renderers.Add(r);
            }

            if (renderers.Count == 0)
            {
                return(false);
            }

            Debug.Log($"GetMeshRenderersInChildren newBounds: {newBounds.center}  Size: {newBounds.size}  Dimensions: {settings.Dimensions}");

            //assign new bounds
            //remove the world offset
            if (!useManualBounds)
            {
                newBounds = new Bounds(newBounds.center - target.position, newBounds.size);
            }

            settings = new AVolumeSettings(newBounds, settings.Dimensions)
            {
                UsePadding = !useManualBounds //no padding with manual bounds, sorry!
            };
            AVolumeSettings.AddBoundsBorder(ref settings);

            return(true);
        }
Exemplo n.º 3
0
        public static bool GetMeshRenderersInChildren(ref AVolumeSettings settings, ref List <Renderer> renderers, Transform target, bool fitToVertices)
        {
            if (renderers == null)
            {
                return(false);
            }

            //get renderers in children
            Renderer[] mrs = target.GetComponentsInChildren <Renderer>();
            if (mrs == null || mrs.Length == 0)
            {
                return(false);
            }

            Bounds newBounds = new Bounds(target.position, Vector3.zero);
            bool   first     = true;

            foreach (var r in mrs)
            {
                if (!(r is MeshRenderer) && !(r is SkinnedMeshRenderer))
                {
                    continue;
                }
                //skip inactive renderers
                if (!r.gameObject.activeSelf)
                {
                    continue;
                }
                if (!r.enabled)
                {
                    continue;
                }

                if (!fitToVertices)
                {
                    newBounds.Encapsulate(r.bounds);
                }
                else
                {
                    //iterate all vertices and encapsulate
                    Mesh mesh = null;
                    if (r is MeshRenderer)
                    {
                        MeshFilter mf = r.GetComponent <MeshFilter>();
                        if (mf != null && mf.sharedMesh != null)
                        {
                            mesh = mf.sharedMesh;
                        }
                    }
                    else
                    {
                        mesh = (r as SkinnedMeshRenderer).sharedMesh;
                    }

                    if (mesh != null)
                    {
                        Bounds b = EncapsulateVertices(mesh, r.transform.localToWorldMatrix);

                        if (first)
                        {
                            newBounds = b;
                        }
                        else
                        {
                            newBounds.Encapsulate(b);
                        }

                        first = false;
                    }
                }

                renderers.Add(r);
            }

            if (renderers.Count == 0)
            {
                return(false);
            }

            //assign new bounds
            //remove the world offset
            newBounds = new Bounds(newBounds.center - target.position, newBounds.size);

            settings = new AVolumeSettings(newBounds, settings.Dimensions);
            AVolumeSettings.AddBoundsBorder(ref settings);

            return(true);
        }