Skip to content

Instantly share code, notes, and snippets.

@openroomxyz
openroomxyz / TakeSnapshot.cs
Last active April 21, 2020 11:44
Unity : Take A picture at Run-time?
public static void TakeSnapshot(Camera cam, int resolution, string filename)
{
if (cam.targetTexture == null)
{
cam.targetTexture = new RenderTexture(resolution, resolution, 24);
}
Texture2D snapshoot = new Texture2D(resolution, resolution, TextureFormat.RGB24, false);
cam.Render();
@openroomxyz
openroomxyz / AlmostEqual.cs
Last active April 1, 2020 13:22
Unity : How to compare two Vector3?
public static bool isEqual(float a, float b)
{
if (a >= b - Mathf.Epsilon && a <= b + Mathf.Epsilon)
{
return true;
}
else
{
return false;
}
@openroomxyz
openroomxyz / IsVectorOrtagonal.cs
Last active April 1, 2020 13:32
Unity : How to check if two Vectors are Orthogonal?
public static bool isOrtagonal(Vector4 a, Vector4 b)
{
return isEqual(Vector3.Dot(a, b), 0.0f);
}
public static bool isOrtagonal(Vector3 a, Vector3 b)
{
return isEqual(Vector3.Dot(a, b), 0.0f);
}
public static bool isOrtagonal(Vector2 a, Vector2 b)
{
@openroomxyz
openroomxyz / isRectangle.cs
Created April 1, 2020 13:39
Unity : How to check if 4 Points a, b, c, d form a rectangle? (2d, 3d, 4d)
public static bool isOrtagonal(Vector4 a, Vector4 b)
{
return isEqual(Vector3.Dot(a, b), 0.0f);
}
public static bool isOrtagonal(Vector3 a, Vector3 b)
{
return isEqual(Vector3.Dot(a, b), 0.0f);
}
public static bool isOrtagonal(Vector2 a, Vector2 b)
{
@openroomxyz
openroomxyz / DowloadAndSave.py
Created April 6, 2020 08:38
Python : How to download a file and save it locally?
import requests
import shutil
URL = "https://"
save_as = 'img.png'
response = requests.get(URL)
response = requests.get(URL, stream=True)
with open(save_as, 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
@openroomxyz
openroomxyz / save_display_numpy_array.py
Last active April 6, 2020 09:33
Python Jupyter : How we save numpy array as image, and display in in notebook?
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
#generating
w, h = 512, 512
data = np.zeros((h, w, 3), dtype=np.uint8)
data[0:256, 0:256] = [50, 150, 200] # red patch in upper left
#displaying
@openroomxyz
openroomxyz / HowDoYouSetTextureProgramaticallyHDR.cs
Created April 6, 2020 11:15
Unity : How do you set diffuse texture problematically when using HDRp?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MLib.BuildInMaterialsTools.HDR
{
public class Lit
{
public static void SetBaseColorMap(ref Renderer renderer,in RenderTexture render_texture)
{
@openroomxyz
openroomxyz / ConvertRenderTextureToTexture2d.cs
Created April 6, 2020 11:21
Unity : How to convert Render Texture into Texture2d?
private static Texture2D toTexture2D(RenderTexture rTex)
{
int widht = rTex.width;
int height = rTex.height;
Texture2D tex = new Texture2D(widht, height, TextureFormat.RGB24, false);
RenderTexture.active = rTex;
tex.ReadPixels(new Rect(0, 0, rTex.width, rTex.height), 0, 0);
tex.Apply();
return tex;
@openroomxyz
openroomxyz / ConvertTextrure2dIntoRenderTexture.cs
Created April 6, 2020 11:23
Unity : How to convert Texture2d into Render Texture (RenderTexture) ?
private static void ConveretTexture2dIntoRenderTexture(out RenderTexture out_renderTexture, in Texture2D input_texture2d, int resolution)
{
//int resolution = 1024 * 4;
// texRef is your Texture2D
// You can also reduice your texture 2D that way
out_renderTexture = new RenderTexture(resolution, resolution, 0);
out_renderTexture.enableRandomWrite = true;
RenderTexture.active = out_renderTexture;
// Copy your texture ref to the render texture
Graphics.Blit(input_texture2d, out_renderTexture);
@openroomxyz
openroomxyz / LoadTexture2dFromFile.cs
Created April 6, 2020 11:26
Unity : How to load Texture2d from file (PNG) on disk?
private static Texture2D LoadPNG(string filePath)
{
Texture2D tex = null;
byte[] fileData;
if (System.IO.File.Exists(filePath))
{
fileData = System.IO.File.ReadAllBytes(filePath);
tex = new Texture2D(2, 2);