Exemple #1
0
 public List <GameObject> OverlapSphere(Sphere sphere)
 {
     return(_gameObjectSphereTree.OverlapSphere(sphere));
 }
        public Texture2D GeneratePreview(Prefab prefab)
        {
            if (prefab == null || prefab.UnityPrefab == null || !_isPreiewGenSessionActive)
            {
                return(null);
            }

            Camera renderCam = GetRenderCamera();

            renderCam.backgroundColor = _backgroundColor;
            renderCam.orthographic    = false;
            renderCam.fieldOfView     = 65.0f;
            renderCam.targetTexture   = GetRenderTexture();
            renderCam.clearFlags      = CameraClearFlags.Color;
            renderCam.nearClipPlane   = 0.0001f;
            if (renderCam.targetTexture == null)
            {
                return(null);
            }

            RenderTexture oldRenderTexture = UnityEngine.RenderTexture.active;

            RenderTexture.active = renderCam.targetTexture;
            GL.Clear(true, true, _backgroundColor);

            GameObject previewObjectRoot = GameObject.Instantiate(prefab.UnityPrefab);

            previewObjectRoot.hideFlags = HideFlags.HideAndDontSave;
            Transform previewObjectTransform = previewObjectRoot.transform;

            previewObjectTransform.position   = Vector3.zero;
            previewObjectTransform.rotation   = Quaternion.identity;
            previewObjectTransform.localScale = prefab.InitialWorldScale;

            Box    worldBox    = previewObjectRoot.GetHierarchyWorldBox();
            Sphere worldSphere = worldBox.GetEncpasulatingSphere();

            Sphere  sceneSphere            = Octave3DWorldBuilder.ActiveInstance.Octave3DScene.GetEncapuslatingSphere();
            Vector3 newPreviewSphereCenter = sceneSphere.Center - Vector3.right * (sceneSphere.Radius + worldSphere.Radius + 90.0f);

            previewObjectTransform.position += (newPreviewSphereCenter - worldSphere.Center);
            worldBox    = previewObjectRoot.GetHierarchyWorldBox();
            worldSphere = worldBox.GetEncpasulatingSphere();

            Transform camTransform = renderCam.transform;

            camTransform.rotation = Quaternion.identity;
            camTransform.rotation = Quaternion.AngleAxis(-45.0f, Vector3.up) * Quaternion.AngleAxis(35.0f, camTransform.right);
            camTransform.position = worldSphere.Center - camTransform.forward * (worldSphere.Radius * 2.0f + renderCam.nearClipPlane);

            SetPreviewLightsActive(true);
            SetupPreviewLights();
            renderCam.Render();
            SetPreviewLightsActive(false);

            /*GL.PushMatrix();
             * GL.LoadProjectionMatrix(renderCam.projectionMatrix);
             *
             * List<GameObject> allPreviewObjects = previewObjectRoot.GetAllChildrenIncludingSelf();
             * foreach (var previewObject in allPreviewObjects)
             * {
             *  MeshFilter meshFilter = previewObject.GetComponent<MeshFilter>();
             *  if (meshFilter != null)
             *  {
             *      Mesh mesh = meshFilter.sharedMesh;
             *      if (mesh == null) continue;
             *      MeshRenderer meshRenderer = previewObject.GetComponent<MeshRenderer>();
             *      if (meshRenderer == null) continue;
             *
             *      Matrix4x4 worldMatrix = previewObject.transform.localToWorldMatrix;
             *      Matrix4x4 modelViewMtx = renderCam.worldToCameraMatrix * worldMatrix;
             *      for (int subMeshIndex = 0; subMeshIndex < mesh.subMeshCount; ++subMeshIndex)
             *      {
             *          Material material = meshRenderer.sharedMaterials[subMeshIndex];
             *          if (material == null) continue;
             *          material.SetPass(0);
             *
             *          //Graphics.DrawMeshNow(mesh, modelViewMtx, subMeshIndex);
             *          Graphics.DrawMesh(mesh, worldMatrix, material, previewObjectRoot.layer, renderCam, subMeshIndex);
             *      }
             *  }
             * }
             * GL.PopMatrix();*/

            GameObject.DestroyImmediate(previewObjectRoot);
            Texture2D previewTexture = new Texture2D(_previewWidth, _previewHeight, TextureFormat.ARGB32, true, true);

            previewTexture.ReadPixels(new Rect(0, 0, _previewWidth, _previewHeight), 0, 0);
            previewTexture.Apply();
            UnityEngine.RenderTexture.active = oldRenderTexture;

            return(previewTexture);
        }