Example #1
0
        public void Update(PreviewRenderUtility context, TransformParam transformParam)
        {
            Bounds bounds;

            if (gameObject != null)
            {
                gameObject.transform.localPosition    = transformParam.localPosition;
                gameObject.transform.localEulerAngles = transformParam.localRotation;
                gameObject.transform.localScale       = transformParam.localScale;
            }
            if (TryGetBoundingBox(out bounds) != false)
            {
                boundsObject.transform.localPosition = bounds.center;
                boundsObject.transform.localScale    = bounds.size;
            }
        }
Example #2
0
        public void ToPreset(CameraPreset preset, TextureParam textureParam, TransformParam transformParam, MeshParam meshParam)
        {
            float  aspecct = (float)textureParam.width / (float)textureParam.height;
            Bounds bounds;

            if (meshParam.TryGetBoundingBox(out bounds) != false)
            {
                if (orthographic == false)
                {
                    switch (preset)
                    {
                    case CameraPreset.kFit:
                    {
                        float length;
                        float fov;

                        if (bounds.extents.x < bounds.extents.y)
                        {
                            length = bounds.extents.y;
                            fov    = fieldOfView;
                        }
                        else
                        {
                            length = bounds.extents.x;
                            fov    = Camera.VerticalToHorizontalFieldOfView(fieldOfView, aspecct);
                        }
                        float distance = length / Mathf.Tan(fov * 0.5f * Mathf.Deg2Rad) + bounds.extents.z;
                        if (distance <= 0.0f)
                        {
                            return;
                        }
                        Vector3 direction = lookAtPosition - localPosition;

                        if (Mathf.Approximately(direction.magnitude, 0.0f) != false)
                        {
                            return;
                        }
                        lookAtPosition = bounds.center;
                        direction      = Vector3.Normalize(direction);
                        localPosition  = lookAtPosition - direction * distance;
                        localRotation  = Quaternion.LookRotation(direction).eulerAngles;
                        nearClipPlane  = distance - 0.01f;
                        farClipPlane   = nearClipPlane + bounds.size.z + 0.02f;
                        break;
                    }

                    case CameraPreset.kFront:
                    {
                        float length;
                        float fov;

                        if (bounds.extents.x < bounds.extents.y)
                        {
                            length = bounds.extents.y;
                            fov    = fieldOfView;
                        }
                        else
                        {
                            length = bounds.extents.x;
                            fov    = Camera.VerticalToHorizontalFieldOfView(fieldOfView, aspecct);
                        }
                        float distance = length / Mathf.Tan(fov * 0.5f * Mathf.Deg2Rad);
                        float z        = -(distance + bounds.extents.z - bounds.center.z);
                        localPosition  = new Vector3(bounds.center.x, bounds.center.y, z);
                        localRotation  = Vector3.zero;
                        lookAtPosition = bounds.center;
                        nearClipPlane  = distance - 0.01f;
                        farClipPlane   = nearClipPlane + bounds.size.z + 0.02f;
                        break;
                    }

                    case CameraPreset.kBack:
                    {
                        float length;
                        float fov;

                        if (bounds.extents.x < bounds.extents.y)
                        {
                            length = bounds.extents.y;
                            fov    = fieldOfView;
                        }
                        else
                        {
                            length = bounds.extents.x;
                            fov    = Camera.VerticalToHorizontalFieldOfView(fieldOfView, aspecct);
                        }
                        float distance = length / Mathf.Tan(fov * 0.5f * Mathf.Deg2Rad);
                        float z        = distance + bounds.extents.z + bounds.center.z;
                        localPosition  = new Vector3(bounds.center.x, bounds.center.y, z);
                        localRotation  = new Vector3(0, 180, 0);
                        lookAtPosition = bounds.center;
                        nearClipPlane  = distance - 0.01f;
                        farClipPlane   = nearClipPlane + bounds.size.z + 0.02f;
                        break;
                    }

                    case CameraPreset.kTop:
                    {
                        float length;
                        float fov;

                        if (bounds.extents.x < bounds.extents.z)
                        {
                            length = bounds.extents.z;
                            fov    = fieldOfView;
                        }
                        else
                        {
                            length = bounds.extents.x;
                            fov    = Camera.VerticalToHorizontalFieldOfView(fieldOfView, aspecct);
                        }
                        float distance = length / Mathf.Tan(fov * 0.5f * Mathf.Deg2Rad);
                        float y        = distance + bounds.extents.y + bounds.center.y;
                        localPosition  = new Vector3(bounds.center.x, y, bounds.center.z);
                        localRotation  = new Vector3(90, 0, 0);
                        lookAtPosition = bounds.center;
                        nearClipPlane  = distance - 0.01f;
                        farClipPlane   = nearClipPlane + bounds.size.y + 0.02f;
                        break;
                    }

                    case CameraPreset.kBottom:
                    {
                        float length;
                        float fov;

                        if (bounds.extents.x < bounds.extents.z)
                        {
                            length = bounds.extents.z;
                            fov    = fieldOfView;
                        }
                        else
                        {
                            length = bounds.extents.x;
                            fov    = Camera.VerticalToHorizontalFieldOfView(fieldOfView, aspecct);
                        }
                        float distance = length / Mathf.Tan(fov * 0.5f * Mathf.Deg2Rad);
                        float y        = -(distance + bounds.extents.y - bounds.center.y);
                        localPosition  = new Vector3(bounds.center.x, y, bounds.center.z);
                        localRotation  = new Vector3(-90, 0, 0);
                        lookAtPosition = bounds.center;
                        nearClipPlane  = distance - 0.01f;
                        farClipPlane   = nearClipPlane + bounds.size.y + 0.02f;
                        break;
                    }

                    case CameraPreset.kLeft:
                    {
                        float length;
                        float fov;

                        if (bounds.extents.z < bounds.extents.y)
                        {
                            length = bounds.extents.y;
                            fov    = fieldOfView;
                        }
                        else
                        {
                            length = bounds.extents.z;
                            fov    = Camera.VerticalToHorizontalFieldOfView(fieldOfView, aspecct);
                        }
                        float distance = length / Mathf.Tan(fov * 0.5f * Mathf.Deg2Rad);
                        float x        = -(distance + bounds.extents.x - bounds.center.x);
                        localPosition  = new Vector3(x, bounds.center.y, bounds.center.z);
                        localRotation  = new Vector3(0, 90, 0);
                        lookAtPosition = bounds.center;
                        nearClipPlane  = distance - 0.01f;
                        farClipPlane   = nearClipPlane + bounds.size.x + 0.02f;
                        break;
                    }

                    case CameraPreset.kRight:
                    {
                        float length;
                        float fov;

                        if (bounds.extents.z < bounds.extents.y)
                        {
                            length = bounds.extents.y;
                            fov    = fieldOfView;
                        }
                        else
                        {
                            length = bounds.extents.z;
                            fov    = Camera.VerticalToHorizontalFieldOfView(fieldOfView, aspecct);
                        }
                        float distance = length / Mathf.Tan(fov * 0.5f * Mathf.Deg2Rad);
                        float x        = distance + bounds.extents.x + bounds.center.x;
                        localPosition  = new Vector3(x, bounds.center.y, bounds.center.z);
                        localRotation  = new Vector3(0, -90, 0);
                        lookAtPosition = bounds.center;
                        nearClipPlane  = distance - 0.01f;
                        farClipPlane   = nearClipPlane + bounds.size.x + 0.02f;
                        break;
                    }
                    }
                }
                else
                {
                    const float nearLength = 1.00f;

                    switch (preset)
                    {
                    case CameraPreset.kFront:
                    {
                        localPosition    = new Vector3(bounds.center.x, bounds.center.y, -(bounds.extents.z - bounds.center.z + nearLength));
                        localRotation    = Vector3.zero;
                        lookAtPosition   = bounds.center;
                        orthographicSize = Mathf.Max(Camera.HorizontalToVerticalFieldOfView(bounds.extents.x, aspecct), bounds.extents.y) * 1.005f;
                        nearClipPlane    = nearLength;
                        farClipPlane     = nearClipPlane + bounds.size.z + 0.001f;
                        break;
                    }

                    case CameraPreset.kBack:
                    {
                        localPosition    = new Vector3(bounds.center.x, bounds.center.y, bounds.extents.z + bounds.center.z + nearLength);
                        localRotation    = new Vector3(0, 180, 0);
                        lookAtPosition   = bounds.center;
                        orthographicSize = Mathf.Max(Camera.HorizontalToVerticalFieldOfView(bounds.extents.x, aspecct), bounds.extents.y) * 1.005f;
                        nearClipPlane    = nearLength;
                        farClipPlane     = nearClipPlane + bounds.size.z + 0.001f;
                        break;
                    }

                    case CameraPreset.kTop:
                    {
                        localPosition    = new Vector3(bounds.center.x, bounds.extents.y + bounds.center.y + nearLength, bounds.center.z);
                        localRotation    = new Vector3(90, 0, 0);
                        lookAtPosition   = bounds.center;
                        orthographicSize = Mathf.Max(Camera.HorizontalToVerticalFieldOfView(bounds.extents.x, aspecct), bounds.extents.z) * 1.005f;
                        nearClipPlane    = nearLength;
                        farClipPlane     = nearClipPlane + bounds.size.y + 0.001f + 100;
                        break;
                    }

                    case CameraPreset.kBottom:
                    {
                        localPosition    = new Vector3(bounds.center.x, -(bounds.extents.y - bounds.center.y + nearLength), bounds.center.z);
                        localRotation    = new Vector3(-90, 0, 0);
                        lookAtPosition   = bounds.center;
                        orthographicSize = Mathf.Max(Camera.HorizontalToVerticalFieldOfView(bounds.extents.x, aspecct), bounds.extents.z) * 1.005f;
                        nearClipPlane    = nearLength;
                        farClipPlane     = nearClipPlane + bounds.size.y + 0.001f;
                        break;
                    }

                    case CameraPreset.kLeft:
                    {
                        localPosition    = new Vector3(-(bounds.extents.x - bounds.center.x + nearLength), bounds.center.y, bounds.center.z);
                        localRotation    = new Vector3(0, 90, 0);
                        lookAtPosition   = bounds.center;
                        orthographicSize = Mathf.Max(Camera.HorizontalToVerticalFieldOfView(bounds.extents.z, aspecct), bounds.extents.y) * 1.005f;
                        nearClipPlane    = nearLength;
                        farClipPlane     = nearClipPlane + bounds.size.y + 0.001f;
                        break;
                    }

                    case CameraPreset.kRight:
                    {
                        localPosition    = new Vector3(bounds.extents.x - bounds.center.x + nearLength, bounds.center.y, bounds.center.z);
                        localRotation    = new Vector3(0, -90, 0);
                        lookAtPosition   = bounds.center;
                        orthographicSize = Mathf.Max(Camera.HorizontalToVerticalFieldOfView(bounds.extents.z, aspecct), bounds.extents.y) * 1.005f;
                        nearClipPlane    = nearLength;
                        farClipPlane     = nearClipPlane + bounds.size.y + 0.001f;
                        break;
                    }
                    }
                }
                transformParam.localPosition = Vector3.zero;
                transformParam.localRotation = Vector3.zero;
                transformParam.localScale    = Vector3.one;

                nearClipPlane -= 10.0f;
                farClipPlane  += 100.0f;

                if (nearClipPlane < 0.1f)
                {
                    nearClipPlane = 0.1f;
                }
                if (nearClipPlane >= 3.402823e+37f)
                {
                    nearClipPlane = 3.402823e+37f;
                }
                if (farClipPlane < nearClipPlane)
                {
                    farClipPlane = nearClipPlane + 0.01f;
                }
            }
        }