Ejemplo n.º 1
0
			void PreviewOutput (Generator gen, Generator.Output output, bool inWindow)
			{
				if (gen==null || output==null || mapMagic==null) 
				{
					Preview.Clear(); 
				}
				else
				{
					Preview.Show(mapMagic, gen, output);

					if (inWindow) PreviewWindow.ShowWindow();
					else Preview.drawGizmos = true;

					mapMagic.Generate(); 
				}

				//select MapMagic or Voxeland object to make gizmos update
				if (Selection.activeTransform == null)
				{
					if (MapMagic.instance != null) Selection.activeTransform = MapMagic.instance.transform;
					else
					{
						#if VOXELAND
						if (Voxeland5.Voxeland.instances!=null && Voxeland5.Voxeland.instances.Count!=0) Selection.activeTransform = Voxeland5.Voxeland.instances.Any().transform;
						#endif
					}
				}

				SceneView.RepaintAll();
			} 
Ejemplo n.º 2
0
        void OnGUI()
        {
            //updating layouts
            if (baseLayout == null)
            {
                baseLayout = new Layout();
            }
            baseLayout.maxZoom = 8; baseLayout.minZoom = 0.125f; baseLayout.zoomStep = 0.125f;
            baseLayout.Zoom(); baseLayout.Scroll();                     //scrolling and zooming

            if (infoLayout == null)
            {
                infoLayout = new Layout();
            }
            infoLayout.cursor = new Rect();
            infoLayout.margin = 10; infoLayout.rightMargin = 10;
            infoLayout.field  = new Rect(this.position.width - 200 - 10, this.position.height - 100 - 10, 200, 100);

            //no output exit
            if (Preview.previewOutput == null)
            {
                baseLayout.Label("No preview output is selected"); return;
            }


            //drawing main object
            //TODO: preview all of the textures in window
            int counter = 0;

            foreach (Chunk.Results result in Preview.mapMagic.Results())
            {
                //displaing currently selected chunk
                if (counter != displayedObjectNum)
                {
                    counter++; continue;
                }

                //no object
                if (result == null)
                {
                    baseLayout.Label("Please wait until preview \nresult is being generated."); return;
                }
                object previewBox = Preview.previewOutput.GetObject <object>(result);
                if (previewBox == null)
                {
                    baseLayout.Label("Please wait until preview \nobject is being generated."); return;
                }

                //displaying matrix
                if (Preview.previewOutput.type == Generator.InoutType.Map)
                {
                    //refreshing matrices if needed
                    if (Preview.RefreshMatricesNeeded())
                    {
                        Preview.RefreshMatrices(range.x, range.y);
                    }

                    //finding matrix and texture
                    Matrix    matrix  = (Matrix)previewBox;
                    Texture2D texture = Preview.matrices[matrix];

                    //drawing texture
                    EditorGUI.DrawPreviewTexture(baseLayout.ToDisplay(new Rect(0, 0, texture.width, texture.height)), texture);

                    //drawing texture info

                    UnityEditor.EditorGUI.HelpBox(infoLayout.field, "", UnityEditor.MessageType.None);
                    UnityEditor.EditorGUI.HelpBox(infoLayout.field, "", UnityEditor.MessageType.None);
                    infoLayout.Par(2);
                    infoLayout.Field(ref displayedObjectNum, "Tile number", fieldSize: 0.3f);
                    infoLayout.fieldSize = 0.7f;                             //infoLayout.inputSize = 0.3f;
                    infoLayout.Label("Size: " + texture.width + "x" + texture.height);
                    infoLayout.Field(ref baseLayout.zoom, "Zoom: ", min: baseLayout.minZoom, max: baseLayout.maxZoom, slider: true, quadratic: true);

                    infoLayout.Field(ref range, "Range: ", min: 0, max: 1, slider: true);
                    if (infoLayout.lastChange)
                    {
                        Preview.RefreshMatrices(range.x, range.y);
                    }

                    infoLayout.Par(3);
                    if (infoLayout.Button("Save To Texture"))
                    {
                                                        #if !UNITY_WEBPLAYER //you cannot get access to files for web player platform. Even for an editor. Seems to be Unity bug.
                        string path = UnityEditor.EditorUtility.SaveFilePanel(
                            "Save Output Texture",
                            "Assets",
                            "OutputTexture.png",
                            "png");
                        if (path != null && path.Length != 0)
                        {
                            byte[] bytes = texture.EncodeToPNG();
                            System.IO.File.WriteAllBytes(path, bytes);
                        }
                                                        #endif
                    }
                }

                else if (Preview.previewOutput.type == Generator.InoutType.Objects)
                {
                    SpatialHash spatialHash = (SpatialHash)previewBox;

                    for (int i = 0; i < spatialHash.cells.Length; i++)
                    {
                        SpatialHash.Cell cell = spatialHash.cells[i];

                        //drawing grid
                        UnityEditor.Handles.color = new Color(0.6f, 0.6f, 0.6f);                                 //TODO: meight be too light in pro skin
                        UnityEditor.Handles.DrawPolyLine(
                            baseLayout.ToDisplay((cell.min - spatialHash.offset) / spatialHash.size * 1000),
                            baseLayout.ToDisplay((new Vector2(cell.max.x, cell.min.y) - spatialHash.offset) / spatialHash.size * 1000),
                            baseLayout.ToDisplay((cell.max - spatialHash.offset) / spatialHash.size * 1000),
                            baseLayout.ToDisplay((new Vector2(cell.min.x, cell.max.y) - spatialHash.offset) / spatialHash.size * 1000),
                            baseLayout.ToDisplay((cell.min - spatialHash.offset) / spatialHash.size * 1000));

                        //drawing objects


                        UnityEditor.Handles.color = new Color(0.3f, 0.5f, 0.1f);
                        for (int j = 0; j < cell.objs.Count; j++)
                        {
                            Vector2 pos    = baseLayout.ToDisplay((cell.objs[j].pos - spatialHash.offset) / spatialHash.size * 1000);
                            float   radius = cell.objs[j].size * baseLayout.zoom / 2;
                            if (radius < 3)
                            {
                                radius = 3;
                            }

                            UnityEditor.Handles.DrawAAConvexPolygon(
                                pos + new Vector2(0, 1) * radius,
                                pos + new Vector2(0.71f, 0.71f) * radius,
                                pos + new Vector2(1, 0) * radius,
                                pos + new Vector2(0.71f, -0.71f) * radius,
                                pos + new Vector2(0, -1) * radius,
                                pos + new Vector2(-0.71f, -0.71f) * radius,
                                pos + new Vector2(-1, 0) * radius,
                                pos + new Vector2(-0.71f, 0.71f) * radius);
                        }
                    }

                    //drawing info
                    UnityEditor.EditorGUI.HelpBox(infoLayout.field, "", UnityEditor.MessageType.None);
                    UnityEditor.EditorGUI.HelpBox(infoLayout.field, "", UnityEditor.MessageType.None);
                    infoLayout.Par();
                    infoLayout.fieldSize = 0.7f;                             //infoLayout.inputSize = 0.3f;
                    infoLayout.Label("Count: " + spatialHash.Count);
                }

                break; //no need to do anything when selected chunk showed
            }          //foreach in results
        }              //OnGUI