Пример #1
0
 public SphereTreeNode()
 {
     _sphere = new Sphere(Vector3.zero, 1.0f);
     _data   = default(T);
 }
Пример #2
0
 public SphereTreeNode(T data, Sphere sphere)
 {
     _sphere = sphere;
     _data   = data;
 }
        public Texture2D Generate(GameObject unityPrefab)
        {
            if (!_isGenSessionActive || _renderCamera.targetTexture == null)
            {
                return(null);
            }

            RenderTexture oldRenderTexture = UnityEngine.RenderTexture.active;

            RenderTexture.active = _renderCamera.targetTexture;
            GL.Clear(true, true, _previewLookAndFeel.BkColor);

            bool hasMesh   = unityPrefab.HierarchyHasMesh();
            bool hasSprite = unityPrefab.HierarchyHasSprite();

            PreviewObjectType previewObjectType = PreviewObjectType.Mesh;

            if (!hasMesh && hasSprite)
            {
                previewObjectType = PreviewObjectType.Sprite;
            }
            else if (!hasMesh && !hasSprite)
            {
                if (unityPrefab.HierarchyHasObjectsOfType(GameObjectType.Light))
                {
                    previewObjectType = PreviewObjectType.Light;
                }
                else if (unityPrefab.HierarchyHasObjectsOfType(GameObjectType.ParticleSystem))
                {
                    previewObjectType = PreviewObjectType.ParticleSystem;
                }
                else
                {
                    previewObjectType = PreviewObjectType.Other;
                }
            }

            GameObject previewObject = null;

            if (previewObjectType == PreviewObjectType.Mesh || previewObjectType == PreviewObjectType.Sprite)
            {
                previewObject = GameObject.Instantiate(unityPrefab);
            }
            else
            {
                previewObject = _nonMeshPreviewObject;
            }

            Transform previewObjectTransform = previewObject.transform;

            previewObjectTransform.position   = Vector3.zero;
            previewObjectTransform.rotation   = Quaternion.identity;
            previewObjectTransform.localScale = unityPrefab.transform.lossyScale;

            AABB   sceneAABB   = RTScene.Get.CalculateBounds();
            Sphere sceneSphere = new Sphere(sceneAABB);
            AABB   previewAABB = new AABB();

            previewAABB = ObjectBounds.CalcHierarchyWorldAABB(previewObject, _boundsQConfig);
            Sphere previewSphere = new Sphere(previewAABB);

            Vector3 previewSphereCenter = sceneSphere.Center - Vector3.right * (sceneSphere.Radius + previewSphere.Radius + 90.0f);

            previewObjectTransform.position += (previewSphereCenter - previewSphere.Center);
            previewAABB          = ObjectBounds.CalcHierarchyWorldAABB(previewObject, _boundsQConfig);
            previewSphere.Center = previewSphereCenter;

            Transform camTransform = _renderCamera.transform;

            if (previewObjectType == PreviewObjectType.Mesh || previewObjectType == PreviewObjectType.Sprite)
            {
                camTransform.rotation = Quaternion.identity;
                if (previewObjectType != PreviewObjectType.Sprite)
                {
                    camTransform.rotation = Quaternion.AngleAxis(-45.0f, Vector3.up) * Quaternion.AngleAxis(35.0f, camTransform.right);
                }
                camTransform.position = previewSphere.Center - camTransform.forward * (previewSphere.Radius * 2.0f + _renderCamera.nearClipPlane);
            }
            else
            {
                camTransform.rotation = previewObjectTransform.rotation;
                camTransform.position = previewSphere.Center - camTransform.forward * (previewSphere.Radius * 2.0f + _renderCamera.nearClipPlane);

                Texture2D previewIcon = previewObjectType == PreviewObjectType.Light ? RTScene.Get.LookAndFeel.LightIcon : RTScene.Get.LookAndFeel.ParticleSystemIcon;
                _nonMeshPreviewObject.GetComponent <MeshRenderer>().sharedMaterial.SetTexture("_MainTex", previewIcon);
            }

            _previewLight.transform.forward = camTransform.forward;
            _renderCamera.Render();
            if (previewObject != _nonMeshPreviewObject)
            {
                GameObject.DestroyImmediate(previewObject);
            }

            Texture2D previewTexture = new Texture2D(_previewLookAndFeel.PreviewWidth, _previewLookAndFeel.PreviewHeight, TextureFormat.ARGB32, true, true);

            previewTexture.ReadPixels(new Rect(0, 0, _previewLookAndFeel.PreviewWidth, _previewLookAndFeel.PreviewHeight), 0, 0);
            previewTexture.Apply();
            UnityEngine.RenderTexture.active = oldRenderTexture;

            return(previewTexture);
        }