Skip to content

Instantly share code, notes, and snippets.

@chakmeshma
Last active September 22, 2024 15:11
Show Gist options
  • Select an option

  • Save chakmeshma/9246b1cb17b30f3ffb07ce79f1d1db66 to your computer and use it in GitHub Desktop.

Select an option

Save chakmeshma/9246b1cb17b30f3ffb07ce79f1d1db66 to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;
public class TextureSlicer : MonoBehaviour
{
public Texture2D inputTexture;
public GameObject sphere;
private Texture2D[,] sliced;
private float elapsedTime = 0.0f;
private int tileCounter = 0;
void SplitTexture(Texture2D texture, int width, int height, out Texture2D[,] theSlicedTexture)
{
int xTiles = texture.width / width;
int yTiles = texture.height / height;
theSlicedTexture = new Texture2D[xTiles, yTiles];
for (int y = 0; y < yTiles; y++)
{
for (int x = 0; x < xTiles; x++)
{
Color[] pixels = texture.GetPixels(x * width, y * height, width, height);
theSlicedTexture[x, y] = new Texture2D(width, height);
theSlicedTexture[x, y].SetPixels(pixels);
theSlicedTexture[x, y].Apply();
}
}
}
// Start is called before the first frame update
void Start()
{
Debug.Log("slicing");
SplitTexture(inputTexture, 8192, 8192, out sliced);
string path = "Assets/sliced0.asset";
AssetDatabase.CreateAsset(sliced[0,0], path);
AssetDatabase.SaveAssets();
Debug.Log("done");
}
// Update is called once per frame
void Update()
{
elapsedTime += Time.deltaTime;
if(elapsedTime > 1.0f)
{
int hozTileCount = sliced.GetLength(0);
int verTileCount = sliced.GetLength(1);
int x = tileCounter % hozTileCount;
int y = tileCounter / hozTileCount;
sphere.GetComponent<Renderer>().material.mainTexture = sliced[x, y];
tileCounter += 1;
if (tileCounter == hozTileCount * verTileCount) tileCounter = 0;
elapsedTime = 0.0f;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment