Skip to content

Instantly share code, notes, and snippets.

View babon's full-sized avatar

Dmitry babon

View GitHub Profile
public class MMFNativeArray<T> where T : struct
{
MemoryMappedFile mmf;
MemoryMappedViewAccessor accessor;
public NativeArray<T> native;
public MMFNativeArray(string path, FileMode mode, string mapName, int bytes)
{
unsafe
{
@babon
babon / ComputeExtensions.cs
Last active September 23, 2023 12:53
Unity compute shader dispatch helper with better syntax
//Example usage:
shader.Dispatch("Advect", texWidth, texHeight, texDepth,
"_texture", texture,
"_separationPlane", Vector3.Scale(separationPlane.position, tscale),
"_advectFall", advectN == 0,
"_advectCapsuleCenter", Vector3.Scale(playerCapsule.transform.TransformPoint(playerCapsule.center), tscale));
//Looks like a mess but is actually performant and is zero allocations:
//input RenderTexture 'rt3D'
Texture3D output = new Texture3D(width, height, depth, rt3D.graphicsFormat, TextureCreationFlags.None);
//assuming rt3D format to be 8 bits
var a = new NativeArray<byte>(width * height * depth, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
AsyncGPUReadback.RequestIntoNativeArray(ref a, rt3D, 0, (_) =>
{
output.SetPixelData(a, 0);
output.Apply(updateMipmaps: false, makeNoLongerReadable: true);
AssetDatabase.CreateAsset(output, "Assets/3dtex.asset");
//will throw like "[0], [1], [0], [1], [2] [0..1]"
public sealed class LastNQueue<T> : Queue<T>
{
public int HoldLastN { get; }
public LastNQueue(int holdLastN)
{
HoldLastN = holdLastN;
}
/// <summary>
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System;
// TODO / WARNING: This script has some jenky behavior that needs ironing out.
// You probably want to use somebody else's back-button-in-unity implementation.
// TRY HERE: https://forum.unity.com/threads/feature-request-back-button-in-the-editor.653332/#post-4457944
using System.Collections.Generic;
using System.Diagnostics;
using UnityEditor;
using UnityEditor.Compilation;
using Debug = UnityEngine.Debug;
[InitializeOnLoad]
internal static class CompilationTimeTracker
{
private static readonly Dictionary<string, Stopwatch> Dictionary;
void p(string s, uint n)
{
string ns = Convert.ToString(n, 2).PadLeft(32, '0');
var list = Enumerable.Range(0, ns.Length / 8).Select(i => ns.Substring(i * 8, 8));
var nsf = string.Join("_", list);
print(s + " ; " + nsf);
}
p("(1 << (32-7))", (1 << (32 - 7)));
p("((1 << (32-7)) - 1)", ((1 << (32 - 7)) - 1));
p("((1 << (32 - 7)) - 1) >> (32 - 7) - 1 << (32 - 7) - 1", ((1 << (32 - 7)) - 1) >> (32 - 7) - 1 << (32 - 7) - 1);
voxel temp;
vox.sunlight = id.y == resolution.y - 1 ? 15 : 0;
vox.torchlight = 0.;
//pull 6 neighbours and -1
if (id.y < resolution.y - 1.)
{
temp = getVoxel(id + int3(0,1,0));
vox.sunlight = max(vox.sunlight, temp.sunlight);
void SavePartMM(int offset = 0, int partSize = fullWidth * fullHeight * fullDepth, int chunkSizeDiv = 1024)
{
//crashes if empty save file for some reason, too unreliable to use
var mmf = MemoryMappedFile.CreateFromFile(Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.OSXPlayer ? "/Users/user/Downloads/test.bytes" : "C:\\test.bytes", FileMode.OpenOrCreate, "apoca_save");
int chunkSize = fullWidth * fullHeight * fullDepth / chunkSizeDiv; //2 4 8 16 32 64 128 256 512 [1024] 2048
//const int chunkSize = /*536_752_128*//*67_094_016*//*8_323_200*//*4_193_376*//*2_096_688*/520_200/*262_086*/;
saveLock = true;
var stopwatch = new Stopwatch(); stopwatch.Start();
@babon
babon / ExtensionlessTexture2DToPNG.cs
Created July 28, 2020 05:42
Convert unity texture without extension as png
[ContextMenuItem("Yo", "Yo")]
public Texture2D tex;
public void Yo()
{
Texture2D DeCompress(Texture2D source)
{
RenderTexture renderTex = RenderTexture.GetTemporary(
source.width,
source.height,
0,