//[MenuItem ("Window/Shader Sandwich Preview")] public static void Init () { ShaderPreview windowG = (ShaderPreview)EditorWindow.GetWindow (typeof (ShaderPreview)); Instance = windowG; windowG.previewCam = null; #if UNITY_5 windowG.defaultBackCube = ShaderSandwich.DayCube; #else windowG.defaultBackCube = ShaderSandwich.KitchenCube; #if !UNITY_PRO_LICENCE EditorUtility.DisplayDialog("Unity 4 Free","Hi! Unity 4 Free does not support render textures, so the preview window will render using a technique called handles. However, this does not always look correct and can have z-writing issues. If you want, I'd recommend turning on Expose and opening the file ShaderSandwichPreviewScene.unity and using that as the preview window :).","Ok"); #endif #endif windowG.Setup(); windowG.wantsMouseMove = true; windowG.minSize = new Vector2(200,200); //windowG.maxSize = new Vector2(400,400); windowG.title = "Shader Preview"; // Debug.Log(windowG.previewMat); //windowG.Meshes = null; }
//Rect WinRectTest = new Rect(20,20,200,200); void OnGUI(){ Vector2 WinSize = new Vector2(position.width,position.height); if (ShaderSandwich.Instance!=null){ Instance = this; Repaint(); Setup(); previewCam.GetComponent<Camera>().farClipPlane = 20F; previewCam.GetComponent<Camera>().nearClipPlane = 0.05F; Vector2 WindowSize = new Vector2(position.width,position.height); GUISkin oldskin = GUI.skin; if(Event.current.type==EventType.Repaint) ShaderUtil.AddProSkin(WindowSize); Rect PreviewRect = new Rect(0,TopBarHeight,WindowSize.x,WindowSize.y-TopBarHeight); TopBarHeight = Mathf.Min(Math.Max(TopBarHeight,66),(int)WinSize.y-32); //GUI.Box(PreviewRect,"","GameViewBackground"); //Texture2D texture = new Texture2D(1, 1); //texture.SetPixel(0,0,previewCam.GetComponent<Camera>().backgroundColor); //texture.Apply(); GUILayout.BeginHorizontal(); GUILayout.BeginVertical(); Rect MeshTypeRect = EditorGUILayout.GetControlRect(false,40,GUILayout.Width(Mathf.Min(220,WinSize.x-5))); ShaderUtil.MakeTooltip(1,MeshTypeRect,"The object to preview.\nYou can select a custom one to the right."); //Debug.Log(MeshTypeRect); //Debug.Log(Event.current.mousePosition); //Debug.Log(ShaderUtil.Tooltip); MeshType = GUI.SelectionGrid(MeshTypeRect,MeshType,new string[]{"Cube","Sphere","Monkey","Tesselated Cube"},2); if (MeshType!=-1){ ((MeshFilter)previewObject.GetComponent("MeshFilter")).sharedMesh = Meshes[MeshType]; customObject = null; } GUILayout.BeginHorizontal(); GUI.backgroundColor = new Color(0.2f,0.2f,0.2f,1f); GUIStyle ButtonStyle = new GUIStyle(GUI.skin.button); ButtonStyle.padding = new RectOffset(0,0,0,0); ButtonStyle.margin = new RectOffset(0,0,0,0); int Subtracter = (int)Mathf.Max(0,(225-(int)WinSize.x)/2f); GUILayout.Space(4); //Rect MeshTypeRect = EditorGUILayout.GetControlRect(GUILayout.Width(Mathf.Min(220,WinSize.x-5))); Rect WireframeRect = new Rect(4,MeshTypeRect.height,108-Subtracter,20); ShaderUtil.MakeTooltip(1,WireframeRect,"Enable wireframe mode. Doesn't work\nsometimes (I think on Windows 10?), sorry :(."); if (Wireframe) Wireframe = GUILayout.Toggle(Wireframe,new GUIContent("Wireframe",ShaderSandwich.Tick),ButtonStyle, GUILayout.Height(20),GUILayout.Width(108-Subtracter)); else Wireframe = GUILayout.Toggle(Wireframe,new GUIContent("Wireframe",ShaderSandwich.Cross),ButtonStyle, GUILayout.Height(20),GUILayout.Width(108-Subtracter)); //GUI.Label(new Rect(1,40,95,20),"Wireframe"); GUILayout.Space(4); Rect SmoothDampRect = new Rect(WireframeRect.width+8,MeshTypeRect.height,108-Subtracter,20); ShaderUtil.MakeTooltip(1,SmoothDampRect,"Make rotating and zooming\nin the viewport smooth."); if (SmoothDamp) SmoothDamp = GUILayout.Toggle(SmoothDamp,new GUIContent("Smooth Cam",ShaderSandwich.Tick),ButtonStyle, GUILayout.Height(20),GUILayout.Width(108-Subtracter)); else SmoothDamp = GUILayout.Toggle(SmoothDamp,new GUIContent("Smooth Cam",ShaderSandwich.Cross),ButtonStyle, GUILayout.Height(20),GUILayout.Width(108-Subtracter)); //GUI.Label(new Rect(101,40,95,20),"Smooth Cam"); GUILayout.EndHorizontal(); GUILayout.EndVertical(); //if (BlackCube=null){ // BlackCube = EditorGUIUtility.Load("Misc/BlackCube.png") as Cubemap; //} GUI.changed = false; Color OldCol = GUI.color; Color OldColb = GUI.backgroundColor; GUI.color = new Color(1,1,1,1); GUI.backgroundColor = new Color(1,1,1,1); Subtracter = (int)Mathf.Max(0,(343-(int)WinSize.x)); if (WinSize.x<304){ GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); Subtracter = 0; int PrevRectSub = Math.Min(66,(int)PreviewRect.height-32); PreviewRect.height-=PrevRectSub; PreviewRect.y+=PrevRectSub; //TopBarHeight+=66; } GUILayout.BeginVertical(); GUILayout.Space(4); Rect CubemapRect = EditorGUILayout.GetControlRect(false,GUILayout.MaxWidth(108)); ShaderUtil.MakeTooltip(1,CubemapRect,"Choose the background cubemap."); //backCube = (Cubemap)EditorGUILayout.ObjectField(backCube,typeof(Cubemap),false,GUILayout.MaxWidth(108)); backCube = (Cubemap)EditorGUI.ObjectField(CubemapRect,backCube,typeof(Cubemap),false); if (GUI.changed){ Cubemap TempCube = defaultBackCube; if (backCube!=null) TempCube = backCube; CalcAmbientLight(TempCube); backMat.SetTexture("_Cube",TempCube); previewMat.SetTexture("_Cube",TempCube); //RenderSettings.customReflection = TempCube; (previewCam.GetComponent("PreviewCameraSet") as PreviewCameraSet).NewCubemap = TempCube; } GUILayout.Space(2); Rect CustomRect = EditorGUILayout.GetControlRect(false,GUILayout.MaxWidth(108)); ShaderUtil.MakeTooltip(1,CustomRect,"Choose a custom preview object."); customObject = (Mesh)EditorGUI.ObjectField(CustomRect,customObject,typeof(Mesh),false); //customObject = (Mesh)EditorGUILayout.ObjectField(customObject,typeof(Mesh),false,GUILayout.MaxWidth(108)); if (customObject){ MeshType = -1; ((MeshFilter)previewObject.GetComponent("MeshFilter")).sharedMesh = customObject; } GUI.color = OldCol; GUI.backgroundColor = OldColb; GUILayout.Space(3); //Rect ExposeRect = new Rect(SmoothDampRect.width+WireframeRect.width+8,MeshTypeRect.height,108-Subtracter,20); Rect ExposeRect = EditorGUILayout.GetControlRect(true,GUILayout.Height(20),GUILayout.Width(108-Subtracter)); ShaderUtil.MakeTooltip(1,ExposeRect,"Expose the temp shader as one you can\nuse in a material (Shader Sandwich/SSTemp)."); /*if (Expose) Expose = GUILayout.Toggle(Expose,new GUIContent("Expose",ShaderSandwich.Tick),ButtonStyle, GUILayout.Height(20),GUILayout.Width(108-Subtracter)); else Expose = GUILayout.Toggle(Expose,new GUIContent("Expose",ShaderSandwich.Cross),ButtonStyle, GUILayout.Height(20),GUILayout.Width(108-Subtracter));*/ if (Expose) Expose = GUI.Toggle(ExposeRect,Expose,new GUIContent("Expose",ShaderSandwich.Tick),ButtonStyle); else Expose = GUI.Toggle(ExposeRect,Expose,new GUIContent("Expose",ShaderSandwich.Cross),ButtonStyle); GUILayout.EndVertical(); //else{ //PreviewRect.height-=58; //PreviewRect.y+=58; //backCube = (Cubemap)EditorGUI.ObjectField(new Rect(1,60,80,50), backCube,typeof(Cubemap),false); //} //GUILayout.EndHorizontal(); if (ShaderUtil.MouseDownIn(new Rect(0,PreviewRect.y-3,WinSize.x,6),false)) InResize = true; if (Event.current.type == EventType.MouseUp) InResize = false; bool InResize2 = Event.current.type == EventType.MouseDrag&&InResize; if (InResize2){ TopBarHeight+=(int)Event.current.delta.y; } //if (backCube==null) //previewBack.GetComponent<Transform>().localPosition = new Vector3(1000000,0,0); //else //previewBack.GetComponent<Transform>().localPosition = new Vector3(0,0,0); float time = (float)EditorApplication.timeSinceStartup; Vector4 vTime = new Vector4(( time / 20f)%1000f, time%1000f, (time*2f)%1000f, (time*3f)%1000f); Vector4 vCosTime = new Vector4( Mathf.Cos(time / 8f), Mathf.Cos(time/4f), Mathf.Cos(time/2f), Mathf.Cos(time)); Vector4 vSinTime = new Vector4( Mathf.Sin(time / 8f), Mathf.Sin(time/4f), Mathf.Sin(time/2f), Mathf.Sin(time)); //(t/8, t/4, t/2, t) Shader.SetGlobalVector( "_SSTime", vTime ); Shader.SetGlobalVector( "_SSCosTime", vCosTime ); Shader.SetGlobalVector( "_SSSinTime", vSinTime ); if (Event.current.type==EventType.Repaint){ Camera PC = previewCam.GetComponent<Camera>(); //Camera PCW = previewCamW.GetComponent<Camera>(); PC.backgroundColor = new Color(0,0,0,0); //PCW.backgroundColor = new Color(0,0,0,0); #if UNITY_5||UNITY_PRO_LICENSE if (PC.targetTexture==null||PC.targetTexture.width != (int)PreviewRect.width||PC.targetTexture.height != (int)PreviewRect.height){ if (NormalRenderTexture!=null) NormalRenderTexture.Release(); if (WireframeRenderTexture!=null) WireframeRenderTexture.Release(); NormalRenderTexture = new RenderTexture((int)PreviewRect.width,(int)PreviewRect.height,16); WireframeRenderTexture = new RenderTexture((int)PreviewRect.width,(int)PreviewRect.height,16); PC.targetTexture = NormalRenderTexture; //PCW.targetTexture = NormalRenderTexture; NormalRenderTexture.antiAliasing = 8; NormalRenderTexture.depth = 24; WireframeRenderTexture.antiAliasing = 4; NormalRenderTexture.Create(); WireframeRenderTexture.Create(); } PC.enabled = false; PC.depthTextureMode = DepthTextureMode.Depth; //PCW.enabled = false; (previewCam.GetComponent("PreviewCameraSet") as PreviewCameraSet).CamStart(); //previewBack.GetComponent<MeshRenderer>().enabled = false; if (!Wireframe){ PC.clearFlags = CameraClearFlags.SolidColor; PC.targetTexture = NormalRenderTexture; PC.Render(); } else{ PC.clearFlags = CameraClearFlags.SolidColor; PC.targetTexture = NormalRenderTexture; PC.Render(); previewBack.GetComponent<MeshRenderer>().enabled = false; PC.clearFlags = CameraClearFlags.SolidColor; PC.targetTexture = WireframeRenderTexture; GL.wireframe = true; if (ShaderSandwich.Instance.OpenShader.TransparencyType.Type==1&&ShaderSandwich.Instance.OpenShader.TransparencyOn.On) previewObject.GetComponent<MeshRenderer>().material = previewMatW; PC.Render(); previewObject.GetComponent<MeshRenderer>().material = previewMat; GL.wireframe = false; previewBack.GetComponent<MeshRenderer>().enabled = true; } (previewCam.GetComponent("PreviewCameraSet") as PreviewCameraSet).CamEnd(); GUI.DrawTexture(PreviewRect,NormalRenderTexture,ScaleMode.StretchToFill,true); if (Wireframe){ Graphics.DrawTexture(PreviewRect,WireframeRenderTexture,ShaderUtil.GetMaterial("Mix",new Color(0,0,0,1),true)); } #else Handles.BeginGUI(); (previewCam.GetComponent("PreviewCameraSet") as PreviewCameraSet).CamStart(); Handles.DrawCamera(PreviewRect,previewCam.GetComponent<Camera>()); (previewCam.GetComponent("PreviewCameraSet") as PreviewCameraSet).CamEnd(); Handles.EndGUI(); #endif // } if (!ShaderSandwich.Instance.RealtimePreviewUpdates){ OldCol = GUI.color; GUI.color = new Color(1,1,1,1); GUI.skin.label.fontStyle = FontStyle.Bold; ShaderUtil.Label(new Rect(PreviewRect.x+1,PreviewRect.y+1+PreviewRect.height-20,PreviewRect.width,PreviewRect.height),"Realtime Updates are turned off...",15); ShaderUtil.Label(new Rect(PreviewRect.x-1,PreviewRect.y-1+PreviewRect.height-20,PreviewRect.width,PreviewRect.height),"Realtime Updates are turned off...",15); ShaderUtil.Label(new Rect(PreviewRect.x,PreviewRect.y-1+PreviewRect.height-20,PreviewRect.width,PreviewRect.height),"Realtime Updates are turned off...",15); ShaderUtil.Label(new Rect(PreviewRect.x,PreviewRect.y+1+PreviewRect.height-20,PreviewRect.width,PreviewRect.height),"Realtime Updates are turned off...",15); ShaderUtil.Label(new Rect(PreviewRect.x-1,PreviewRect.y+PreviewRect.height-20,PreviewRect.width,PreviewRect.height),"Realtime Updates are turned off...",15); ShaderUtil.Label(new Rect(PreviewRect.x+1,PreviewRect.y+PreviewRect.height-20,PreviewRect.width,PreviewRect.height),"Realtime Updates are turned off...",15); GUI.color = new Color(1,0,0,1); ShaderUtil.Label(new Rect(PreviewRect.x,PreviewRect.y+PreviewRect.height-20,PreviewRect.width,PreviewRect.height),"Realtime Updates are turned off...",15); GUI.color = OldCol; GUI.skin.label.fontStyle = FontStyle.Normal; ShaderUtil.Label(new Rect(0,0,0,0),"Realtime Updates are turned off...",12); } EditorGUIUtility.AddCursorRect(new Rect(0,PreviewRect.y-3,WinSize.x,6),MouseCursor.ResizeVertical); bool InDrag2 = InDrag; if (InDrag2&&Event.current.button == 0) EditorGUIUtility.AddCursorRect(PreviewRect,MouseCursor.Pan); else if (InDrag2&&Event.current.button == 1) EditorGUIUtility.AddCursorRect(PreviewRect,MouseCursor.ResizeVertical); GUI.Box(new Rect(0,PreviewRect.y-3,WinSize.x,6),"","Button"); WindowPreviewUpdate(PreviewRect); GUI.skin = oldskin; ShaderUtil.DrawTooltip(1,WinSize); // Debug.Log("FMYlAG"); } }