public static float GetValueBilinear(float[] data, int width, int height, Vector2 uv) { float value = 0; Vector2 pixelCoord = new Vector2( Mathf.Lerp(0, width - 1, uv.x), Mathf.Lerp(0, height - 1, uv.y)); //apply a bilinear filter int xFloor = Mathf.FloorToInt(pixelCoord.x); int xCeil = Mathf.CeilToInt(pixelCoord.x); int yFloor = Mathf.FloorToInt(pixelCoord.y); int yCeil = Mathf.CeilToInt(pixelCoord.y); float f00 = data[JUtilities.To1DIndex(xFloor, yFloor, width)]; float f01 = data[JUtilities.To1DIndex(xFloor, yCeil, width)]; float f10 = data[JUtilities.To1DIndex(xCeil, yFloor, width)]; float f11 = data[JUtilities.To1DIndex(xCeil, yCeil, width)]; Vector2 unitCoord = new Vector2( pixelCoord.x - xFloor, pixelCoord.y - yFloor); value = f00 * (1 - unitCoord.x) * (1 - unitCoord.y) + f01 * (1 - unitCoord.x) * unitCoord.y + f10 * unitCoord.x * (1 - unitCoord.y) + f11 * unitCoord.x * unitCoord.y; return(value); }
public static Color GetColorBilinear(Color[] textureData, int width, int height, Vector2 uv) { Color color = Color.clear; Vector2 pixelCoord = new Vector2( Mathf.Lerp(0, width - 1, uv.x), Mathf.Lerp(0, height - 1, uv.y)); //apply a bilinear filter int xFloor = Mathf.FloorToInt(pixelCoord.x); int xCeil = Mathf.CeilToInt(pixelCoord.x); int yFloor = Mathf.FloorToInt(pixelCoord.y); int yCeil = Mathf.CeilToInt(pixelCoord.y); Color f00 = textureData[JUtilities.To1DIndex(xFloor, yFloor, width)]; Color f01 = textureData[JUtilities.To1DIndex(xFloor, yCeil, width)]; Color f10 = textureData[JUtilities.To1DIndex(xCeil, yFloor, width)]; Color f11 = textureData[JUtilities.To1DIndex(xCeil, yCeil, width)]; Vector2 unitCoord = new Vector2( pixelCoord.x - xFloor, pixelCoord.y - yFloor); color = f00 * (1 - unitCoord.x) * (1 - unitCoord.y) + f01 * (1 - unitCoord.x) * unitCoord.y + f10 * unitCoord.x * (1 - unitCoord.y) + f11 * unitCoord.x * unitCoord.y; return(color); }
public static void FillTexture(Texture2D t, Color c) { Color[] colors = new Color[t.width * t.height]; JUtilities.Fill(colors, c); t.SetPixels(colors); t.Apply(); }
public static RenderTexture CopyToRT(Texture src, int startX, int startY, int width, int height, Color defaultColor) { int endX = startX + width - 1; int endY = startY + height - 1; Vector2 startUV = new Vector2( JUtilities.InverseLerpUnclamped(0, src.width - 1, startX), JUtilities.InverseLerpUnclamped(0, src.height - 1, startY)); Vector2 endUV = new Vector2( JUtilities.InverseLerpUnclamped(0, src.width - 1, endX), JUtilities.InverseLerpUnclamped(0, src.height - 1, endY)); Material mat = JInternalMaterials.CopyTextureMaterial; mat.SetTexture("_MainTex", src); mat.SetVector("_StartUV", startUV); mat.SetVector("_EndUV", endUV); mat.SetColor("_DefaultColor", defaultColor); mat.SetPass(0); RenderTexture rt = new RenderTexture(width, height, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB); RenderTexture.active = rt; Graphics.Blit(src, mat); RenderTexture.active = null; return(rt); }
public static void FillTexture(RenderTexture rt, Color c) { Texture2D tex = new Texture2D(1, 1, TextureFormat.ARGB32, false); tex.SetPixel(0, 0, c); tex.Apply(); CopyToRT(tex, rt); JUtilities.DestroyObject(tex); }
public static void CopyTexture(Texture2D src, Texture2D des) { RenderTexture rt = new RenderTexture(des.width, des.height, 0, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Default); CopyToRT(src, rt); CopyFromRT(des, rt); rt.Release(); JUtilities.DestroyObject(rt); }
public static Texture2D CreateTexture(int resolution, Color fill, TextureFormat format = TextureFormat.ARGB32) { Texture2D t = new Texture2D(resolution, resolution, format, false); Color[] colors = new Color[resolution * resolution]; JUtilities.Fill(colors, fill); t.SetPixels(colors); t.Apply(); return(t); }
public static void ResetArray <T>(ref T[] array, int count, T defaultValue) { if (array != null && array.Length == count) { JUtilities.Fill(array, defaultValue); } else { array = new T[count]; } }
private void CleanUp() { if (environmentProbe != null) { JUtilities.DestroyGameobject(environmentProbe.gameObject); } if (environmentReflection != null) { JUtilities.DestroyObject(environmentReflection); } }
public static bool Render(JCubemapRendererArgs args) { GameObject go = new GameObject("~CubemapRendererCamera"); go.transform.position = args.CameraPosition; Camera cam = go.AddComponent <Camera>(); cam.clearFlags = args.CameraClearFlag; cam.nearClipPlane = args.CameraNearPlane; cam.farClipPlane = args.CameraFarPlane; cam.backgroundColor = args.CameraBackgroundColor; bool result = cam.RenderToCubemap(args.Cubemap, (int)args.Face); JUtilities.DestroyGameobject(go); return(result); }
public static Texture2D CreateTextureFromCurve(AnimationCurve curve, int width, int height) { Texture2D t = new Texture2D(width, height, TextureFormat.ARGB32, false); t.wrapMode = TextureWrapMode.Clamp; Color[] colors = new Color[width * height]; for (int x = 0; x < width; ++x) { float f = Mathf.InverseLerp(0, width - 1, x); float value = curve.Evaluate(f); Color c = new Color(value, value, value, value); for (int y = 0; y < height; ++y) { colors[JUtilities.To1DIndex(x, y, width)] = c; } } t.filterMode = FilterMode.Bilinear; t.SetPixels(colors); t.Apply(); return(t); }
private void ExportFace(Cubemap cube, CubemapFace face) { Texture2D tex = new Texture2D(cube.width, cube.height); Color[] data = cube.GetPixels(face); Color[] flipData = new Color[data.Length]; for (int y = 0; y < Resolution; ++y) { for (int x = 0; x < Resolution; ++x) { flipData[JUtilities.To1DIndex(x, y, Resolution)] = data[JUtilities.To1DIndex(Resolution - 1 - x, Resolution - 1 - y, Resolution)]; } } tex.SetPixels(flipData); tex.Apply(); byte[] bytes = tex.EncodeToPNG(); string fileName = Path.Combine(Directory, cube.name + "-" + face.ToString() + ".png"); File.WriteAllBytes(fileName, bytes); JUtilities.DestroyObject(tex); }
private void Export() { JUtilities.EnsureDirectoryExists(Directory); Cubemap cube = new Cubemap(Resolution, TextureFormat.ARGB32, false); JCubemapRendererArgs args = new JCubemapRendererArgs() { CameraPosition = this.CameraPosition, CameraNearPlane = this.CameraNearPlane, CameraFarPlane = this.CameraFarPlane, CameraClearFlag = this.CameraClearFlag, CameraBackgroundColor = this.CameraBackgroundColor, Resolution = this.Resolution, Cubemap = cube, Face = (CubemapFace)63 }; JCubemapRenderer.Render(args); string fileName = Path.Combine(Directory, "Cubemap-" + JCommon.GetUniqueID() + ".cubemap"); AssetDatabase.CreateAsset(cube, fileName); if (ExportFaceTextures) { ExportFace(cube, CubemapFace.PositiveX); ExportFace(cube, CubemapFace.NegativeX); ExportFace(cube, CubemapFace.PositiveY); ExportFace(cube, CubemapFace.NegativeY); ExportFace(cube, CubemapFace.PositiveZ); ExportFace(cube, CubemapFace.NegativeZ); } AssetDatabase.Refresh(); EditorGUIUtility.PingObject(cube); Selection.activeObject = cube; }
public static IEnumerable <Rect> CompareHeightMap(int gridSize, Color[] oldValues, Color[] newValues) { if (oldValues.LongLength != newValues.LongLength) { return(new Rect[1] { new Rect(0, 0, 1, 1) }); } Rect[] rects = new Rect[gridSize * gridSize]; for (int x = 0; x < gridSize; ++x) { for (int z = 0; z < gridSize; ++z) { rects[JUtilities.To1DIndex(x, z, gridSize)] = GetUvRange(gridSize, x, z); } } HashSet <Rect> dirtyRects = new HashSet <Rect>(); int index = 0; int resolution = Mathf.RoundToInt(Mathf.Sqrt(newValues.LongLength)); for (int rectIndex = 0; rectIndex < rects.Length; ++rectIndex) { Rect r = rects[rectIndex]; int startX = (int)Mathf.Lerp(0, resolution - 1, r.min.x); int startY = (int)Mathf.Lerp(0, resolution - 1, r.min.y); int endX = (int)Mathf.Lerp(0, resolution - 1, r.max.x); int endY = (int)Mathf.Lerp(0, resolution - 1, r.max.y); for (int x = startX; x <= endX; ++x) { for (int y = startY; y <= endY; ++y) { index = JUtilities.To1DIndex(x, y, resolution); if (oldValues[index].r == newValues[index].r && oldValues[index].g == newValues[index].g && oldValues[index].b == newValues[index].b && oldValues[index].a == newValues[index].a) { continue; } dirtyRects.Add(r); Rect hRect = new Rect(); hRect.size = new Vector2(r.width * 1.2f, r.height); hRect.center = r.center; dirtyRects.Add(hRect); Rect vRect = new Rect(); vRect.size = new Vector2(r.width, r.height * 1.2f); vRect.center = r.center; dirtyRects.Add(vRect); break; } if (dirtyRects.Contains(r)) { break; } } } return(dirtyRects); }
public static T ObjectSelectorDragDrop <T>(Rect r, string message, string filter = "") where T : class { r = EditorGUI.IndentedRect(r); int controlId = EditorGUIUtility.GetControlID(FocusType.Passive, r); T objectToReturn = default(T); Color bgColor = EditorGUIUtility.isProSkin ? JEditorCommon.darkGrey : JEditorCommon.lightGrey; EditorGUI.DrawRect(r, bgColor); JEditorCommon.DrawOutlineBox(r, JEditorCommon.midGrey); Rect messageRect = new Rect(); messageRect.size = new Vector2(r.width, 12); messageRect.center = r.center - Vector2.up * (messageRect.size.y * 0.5f + 2); EditorGUI.LabelField(messageRect, message, JEditorCommon.CenteredLabel); Rect buttonRect = new Rect(); buttonRect.size = new Vector2(47, 15); buttonRect.center = r.center + Vector2.up * (buttonRect.size.y * 0.5f + 2); if (GUI.Button(buttonRect, "Browse", JEditorCommon.CenteredLabel)) { EditorGUIUtility.ShowObjectPicker <Object>(null, false, filter, controlId); } JEditorCommon.DrawLine( new Vector2(buttonRect.min.x, buttonRect.max.y), new Vector2(buttonRect.max.x, buttonRect.max.y), CenteredLabel.normal.textColor); EditorGUIUtility.AddCursorRect(buttonRect, MouseCursor.Link); Event e = Event.current; if (e != null && r.Contains(e.mousePosition) && (e.type == EventType.DragUpdated || e.type == EventType.Repaint)) { Object[] draggedObject = DragAndDrop.objectReferences; if (draggedObject.Length == 1 && draggedObject[0] is T) { DragAndDrop.AcceptDrag(); DragAndDrop.visualMode = DragAndDropVisualMode.Copy; DragAndDrop.activeControlID = controlId; EditorGUI.DrawRect(r, JUtilities.GetColor(JEditorCommon.selectedItemColor, 0.5f)); JEditorCommon.DrawOutlineBox(r, JEditorCommon.selectedItemColor); } } else if (e != null && e.type == EventType.DragPerform && r.Contains(e.mousePosition)) { Object[] draggedObject = DragAndDrop.objectReferences; if (draggedObject.Length == 1 && draggedObject[0] is T) { objectToReturn = draggedObject[0] as T; } } else if (e != null && e.type == EventType.ExecuteCommand && e.commandName.Equals("ObjectSelectorClosed")) { int id = EditorGUIUtility.GetObjectPickerControlID(); Object o = EditorGUIUtility.GetObjectPickerObject(); if (id == controlId && o != null) { objectToReturn = o as T; } } return(objectToReturn); }