Skip to content

Instantly share code, notes, and snippets.

// Convert tiling from UnityTerrain to CustomTerrain
material.SetTextureScale("_Splat0", new Vector2(1.0f / terrainLayer.tileSize.x * terrainData.size.x, 1.0f / terrainLayer.tileSize.y * terrainData.size.z))
// rotation and transform a 2d array
// aniticlockwise 90 degree
Vector2[] tUV2 = new Vector2[tUV.Length];
System.Array.Copy(tUV, 0, tUV2, 0, tUV.Length);
for (int pi = 0; pi < h; ++pi)
{
for (int pj = 0; pj < w; ++pj)
{
Vector2 pc = tUV2[pi * w + pj];
tUV[(w - pj - 1) * w + pi] = pc;
@chengkehan
chengkehan / gist:e6000f98396c31d8ee82da45f47d661c
Created December 2, 2020 07:30
Check whether is in view frustum
Vector3 pos = targetTransform.position;
Matrix4x4 vp = mainCamera.projectionMatrix * mainCamera.worldToCameraMatrix;
Vector4 porjPos = vp * new Vector4(pos.x, pos.y, pos.z, 1);
// projPos.xyz / projPos.w [-1, 1] in view frustum
@chengkehan
chengkehan / gist:9325affe6c2a80c3c5ad3324e303c428
Created December 10, 2022 05:06
Convert ugui anchored position to world position without RectTransform
private Vector3 AnchoredPositionToWorldPosition(Vector2 anchoredPosition)
{
Vector2 canvasSize = canvasRectTransform.sizeDelta;
Vector2 viewportPosition = new Vector2(
(anchoredPosition.x + (canvasSize.x * 0.5f)) / canvasSize.x,
(anchoredPosition.y + (canvasSize.y * 0.5f)) / canvasSize.y
);
Vector3 worldPosition = CameraManager.GetInstance().GetUICamera().ViewportToWorldPoint(viewportPosition);
worldPosition.z = canvasRectTransform.position.z;