Beispiel #1
0
        private void AddView(CullingCameraConfig view)
        {
            viewsProp.arraySize++;
            var newEntry = viewsProp.GetArrayElementAtIndex(viewsProp.arraySize - 1);

            newEntry.FindPropertyRelative("name").stringValue         = view.name;
            newEntry.FindPropertyRelative("perspective").boolValue    = view.perspective;
            newEntry.FindPropertyRelative("position").vector3Value    = view.position;
            newEntry.FindPropertyRelative("rotation").quaternionValue = view.rotation;
            newEntry.FindPropertyRelative("size").floatValue          = view.size;
            newEntry.FindPropertyRelative("fov").floatValue           = view.fov;
            newEntry.FindPropertyRelative("aspect").floatValue        = view.aspect;
            newEntry.FindPropertyRelative("minRange").floatValue      = view.minRange;
            newEntry.FindPropertyRelative("maxRange").floatValue      = view.maxRange;
        }
Beispiel #2
0
        protected virtual void OnEnable()
        {
            modelGroup           = (ModelGroup)target;
            processedProp        = serializedObject.FindProperty("processed");
            optimizationsProp    = serializedObject.FindProperty("optimizations");
            randomizeNormalsProp = serializedObject.FindProperty("randomizeNormals");
            //imperfectionsProp = serializedObject.FindProperty("imperfections");
            viewsProp = serializedObject.FindProperty("views");

            // Collect views from other model groups.
            otherGroupViews.Clear();
            var groups = GameObject.FindObjectsOfType <ModelGroup>();

            foreach (var otherGroup in groups)
            {
                if (otherGroup != modelGroup)
                {
                    foreach (var view in otherGroup.views)
                    {
                        if (!otherGroupViews.ContainsKey(otherGroup))
                        {
                            otherGroupViews.Add(otherGroup, new List <CullingCameraConfig>());
                        }
                        otherGroupViews[otherGroup].Add(view);
                    }
                }
            }
            // Collect views from light sources.
            lightViews.Clear();
            var    lights                 = GameObject.FindObjectsOfType <Light>();
            Bounds groupBounds            = new Bounds();
            bool   hasComputedGroupBounds = false;

            foreach (var light in lights)
            {
                switch (light.type)
                {
                case LightType.Spot:
                {
                    CullingCameraConfig view = new CullingCameraConfig()
                    {
                        name        = light.name,
                        perspective = true,
                        position    = light.transform.position,
                        rotation    = light.transform.rotation,
                        fov         = light.spotAngle,
                        minRange    = light.shadowNearPlane,
                        maxRange    = light.range,
                        aspect      = 1.0f
                    };
                    lightViews.Add(light, view);
                    break;
                }

                case LightType.Directional:
                {
                    // Need to compute group bounds.
                    if (!hasComputedGroupBounds)
                    {
                        var renderers = modelGroup.transform.GetComponentsInChildren <MeshRenderer>();
                        if (renderers.Length > 0)
                        {
                            groupBounds = new Bounds(renderers[0].bounds.center, renderers[0].bounds.size);

                            foreach (var renderer in renderers)
                            {
                                groupBounds.Encapsulate(renderer.bounds);
                            }
                        }

                        hasComputedGroupBounds = true;
                    }

                    // Find bounds' corners in light space.
                    var corners = new List <Vector3>()
                    {
                        light.transform.InverseTransformPoint(groupBounds.center + Vector3.Scale(new Vector3(-1, -1, -1), groupBounds.extents)),
                        light.transform.InverseTransformPoint(groupBounds.center + Vector3.Scale(new Vector3(-1, -1, 1), groupBounds.extents)),
                        light.transform.InverseTransformPoint(groupBounds.center + Vector3.Scale(new Vector3(-1, 1, -1), groupBounds.extents)),
                        light.transform.InverseTransformPoint(groupBounds.center + Vector3.Scale(new Vector3(-1, 1, 1), groupBounds.extents)),
                        light.transform.InverseTransformPoint(groupBounds.center + Vector3.Scale(new Vector3(1, -1, -1), groupBounds.extents)),
                        light.transform.InverseTransformPoint(groupBounds.center + Vector3.Scale(new Vector3(1, -1, 1), groupBounds.extents)),
                        light.transform.InverseTransformPoint(groupBounds.center + Vector3.Scale(new Vector3(1, 1, -1), groupBounds.extents)),
                        light.transform.InverseTransformPoint(groupBounds.center + Vector3.Scale(new Vector3(1, 1, 1), groupBounds.extents))
                    };

                    // Find min and max range for corners.
                    var min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
                    var max = new Vector3(float.MinValue, float.MinValue, float.MinValue);
                    foreach (var corner in corners)
                    {
                        min = Vector3.Min(min, corner);
                        max = Vector3.Max(max, corner);
                    }

                    // Get size.
                    Vector2 size = new Vector2(Mathf.Max(Mathf.Abs(min.x), Mathf.Abs(max.x)), Mathf.Max(Mathf.Abs(min.y), Mathf.Abs(max.y)));

                    CullingCameraConfig view = new CullingCameraConfig()
                    {
                        name        = light.name,
                        perspective = false,
                        position    = light.transform.position,
                        rotation    = light.transform.rotation,
                        size        = size.y,
                        minRange    = min.z,
                        maxRange    = max.z,
                        aspect      = size.x / size.y
                    };
                    lightViews.Add(light, view);
                    break;
                }
                }
            }
            // Collect view from cameras.
            cameraViews.Clear();
            var cameras = GameObject.FindObjectsOfType <Camera>();

            foreach (var camera in cameras)
            {
                CullingCameraConfig view = new CullingCameraConfig()
                {
                    name        = camera.name,
                    perspective = !camera.orthographic,
                    position    = camera.transform.position,
                    rotation    = camera.transform.rotation,
                    size        = camera.orthographicSize,
                    fov         = camera.fieldOfView,
                    aspect      = camera.aspect,
                    minRange    = camera.nearClipPlane,
                    maxRange    = camera.farClipPlane
                };
                cameraViews.Add(camera, view);
            }
        }