lenia: more smoothing, better kernel generator

This commit is contained in:
Crispy 2023-06-24 16:30:53 +02:00
parent 87680b3ec5
commit d0557ff466
13 changed files with 95 additions and 172 deletions

View file

@ -439,7 +439,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 336599934}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -4.33, y: 0.894, z: 1.465}
m_LocalPosition: {x: -0.612, y: 0.137, z: 0}
m_LocalScale: {x: 0.1, y: 0.3, z: 0.03}
m_Children: []
m_Father: {fileID: 0}
@ -619,7 +619,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!114 &1206554217
MonoBehaviour:
m_ObjectHideFlags: 0
@ -1214,7 +1214,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1829861728}
m_LocalRotation: {x: 0, y: 1, z: 0, w: 0}
m_LocalPosition: {x: -2, y: 1, z: 1.5}
m_LocalPosition: {x: -2, y: 1, z: 1.75}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
@ -1396,7 +1396,7 @@ Camera:
m_Depth: 0
m_CullingMask:
serializedVersion: 2
m_Bits: 2147880759
m_Bits: 2147880727
m_RenderingPath: -1
m_TargetTexture: {fileID: 8400000, guid: b8b62218c9d4ca16f887e8ae9d9d2942, type: 2}
m_TargetDisplay: 0
@ -1441,7 +1441,7 @@ BoxCollider:
m_IsTrigger: 1
m_Enabled: 1
serializedVersion: 2
m_Size: {x: 1, y: 1, z: 0.1}
m_Size: {x: 1, y: 1, z: 0.2}
m_Center: {x: 0, y: 0, z: 0}
--- !u!114 &2044521649
MonoBehaviour:
@ -1550,8 +1550,8 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2044521647}
m_LocalRotation: {x: 0, y: 1, z: 0, w: 0}
m_LocalPosition: {x: -1.517, y: 0.543, z: 0.562}
m_LocalScale: {x: 0.123, y: 0.123, z: 0.123}
m_LocalPosition: {x: -2.402, y: 0.506, z: 0.292}
m_LocalScale: {x: 0.125, y: 0.125, z: 0.125}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 6
@ -1579,7 +1579,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!114 &2129299479
MonoBehaviour:
m_ObjectHideFlags: 0

View file

@ -40,7 +40,7 @@ Material:
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 2800000, guid: 46b1bc13f81b050d9b62892b4611d5b9, type: 3}
m_Texture: {fileID: 2800000, guid: 54ef4ec6401904800b71f7ed8b36f52b, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.5 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

View file

@ -31,7 +31,7 @@ TextureImporter:
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: 1
filterMode: 0
aniso: 1
mipBias: 0
wrapU: 0
@ -64,7 +64,19 @@ TextureImporter:
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
textureCompression: 2
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Standalone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 2
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

View file

@ -1,104 +0,0 @@
fileFormatVersion: 2
guid: 46b1bc13f81b050d9b62892b4611d5b9
TextureImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 11
mipmaps:
mipMapMode: 0
enableMipMap: 1
sRGBTexture: 1
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapsPreserveCoverage: 0
alphaTestReferenceValue: 0.5
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
isReadable: 0
streamingMipmaps: 0
streamingMipmapsPriority: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: 0
aniso: 1
mipBias: 0
wrapU: 0
wrapV: 0
wrapW: 0
nPOTScale: 1
lightmap: 0
compressionQuality: 50
spriteMode: 0
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spritePixelsToUnits: 100
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spriteGenerateFallbackPhysicsShape: 1
alphaUsage: 1
alphaIsTransparency: 0
spriteTessellationDetail: -1
textureType: 0
textureShape: 1
singleChannelComponent: 0
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
applyGammaDecoding: 0
platformSettings:
- serializedVersion: 3
buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Standalone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
spriteSheet:
serializedVersion: 2
sprites: []
outline: []
physicsShape: []
bones: []
spriteID:
internalID: 0
vertices: []
indices:
edges: []
weights: []
secondaryTextures: []
spritePackingTag:
pSDRemoveMatte: 0
pSDShowRemoveMatteOption: 0
userData:
assetBundleName:
assetBundleVariant:

View file

@ -3,11 +3,8 @@
Properties
{
_LastFrame ("Texture", 2D) = "white" {}
// _Radius ("Radius", Range(1,100)) = 10
_GrowtCenter ("Growth fn center (mu)", Range(0, 1)) = 0.2
_GrowthWidth ("Growth fn width (sigma / std deviation)", Range(0, 1)) = 0.07
// _KSharpness ("Kernel sharpness", Range(1, 100)) = 2
// _KOffset ("Kernel offset", Range(0, 1)) = 0.2
_Speed ("Speed factor", Range(0.001, 0.5)) = 0.1
}
SubShader
@ -37,11 +34,8 @@
};
sampler2D _LastFrame;
// int _Radius;
float _GrowtCenter;
float _GrowthWidth;
// float _KSharpness;
// float _KOffset;
float _Speed;
v2f vert (appdata v)
@ -49,15 +43,13 @@
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
float kernel(float p) {
float r = p / 8.0;
float k_sharpness=28;
float k_offset =0.435;
float k_sharpness = 28;
float k_offset = 0.435;
return exp(-((r - k_offset) * (r - k_offset)) * k_sharpness);
// float r = p / _Radius;
// // -- normal
@ -94,31 +86,8 @@
const float resolution = 512.0;
const float d = 1.0 / resolution;
// generated by the rust program
const int Radius = 16;
// /*
#define RADIUS 16
// kernel LUT size is 544 bytes
const half Kernel[17][16] = {
{0.0205, 0.0678, 0.1799, 0.3835, 0.6569, 0.9041, 0.9998, 0.8884, 0.6343, 0.3639, 0.1678, 0.0621, 0.0185, 0.0044, 0.0009, 0.0001, },
{0.0346, 0.0871, 0.2065, 0.4147, 0.6847, 0.9193, 0.9987, 0.8756, 0.6186, 0.3520, 0.1611, 0.0593, 0.0176, 0.0042, 0.0008, 0.0001, },
{0.0871, 0.1546, 0.2921, 0.5082, 0.7624, 0.9568, 0.9889, 0.8345, 0.5721, 0.3177, 0.1425, 0.0516, 0.0151, 0.0035, 0.0007, 0.0001, },
{0.2065, 0.2921, 0.4459, 0.6569, 0.8699, 0.9931, 0.9541, 0.7600, 0.4974, 0.2659, 0.1156, 0.0407, 0.0116, 0.0027, 0.0005, 0.0001, },
{0.4147, 0.5082, 0.6569, 0.8305, 0.9667, 0.9931, 0.8756, 0.6501, 0.4014, 0.2043, 0.0852, 0.0290, 0.0080, 0.0018, 0.0003, 0.0000, },
{0.6847, 0.7624, 0.8699, 0.9667, 0.9987, 0.9240, 0.7445, 0.5120, 0.2961, 0.1425, 0.0567, 0.0185, 0.0049, 0.0011, 0.0002, 0.0000, },
{0.9193, 0.9568, 0.9931, 0.9931, 0.9240, 0.7753, 0.5721, 0.3639, 0.1966, 0.0891, 0.0336, 0.0105, 0.0027, 0.0006, 0.0001, 0.0000, },
{0.9987, 0.9889, 0.9541, 0.8756, 0.7445, 0.5721, 0.3887, 0.2292, 0.1156, 0.0493, 0.0176, 0.0052, 0.0013, 0.0003, 0.0000, 0.0000, },
{0.8756, 0.8345, 0.7600, 0.6501, 0.5120, 0.3639, 0.2292, 0.1258, 0.0593, 0.0238, 0.0080, 0.0023, 0.0005, 0.0001, 0.0000, 0.0000, },
{0.6186, 0.5721, 0.4974, 0.4014, 0.2961, 0.1966, 0.1156, 0.0593, 0.0263, 0.0099, 0.0032, 0.0009, 0.0002, 0.0000, 0.0000, 0.0000, },
{0.3520, 0.3177, 0.2659, 0.2043, 0.1425, 0.0891, 0.0493, 0.0238, 0.0099, 0.0035, 0.0011, 0.0003, 0.0001, 0.0000, 0.0000, 0.0000, },
{0.1611, 0.1425, 0.1156, 0.0852, 0.0567, 0.0336, 0.0176, 0.0080, 0.0032, 0.0011, 0.0003, 0.0001, 0.0000, 0.0000, 0.0000, 0.0000, },
{0.0593, 0.0516, 0.0407, 0.0290, 0.0185, 0.0105, 0.0052, 0.0023, 0.0009, 0.0003, 0.0001, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, },
{0.0176, 0.0151, 0.0116, 0.0080, 0.0049, 0.0027, 0.0013, 0.0005, 0.0002, 0.0001, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, },
{0.0042, 0.0035, 0.0027, 0.0018, 0.0011, 0.0006, 0.0003, 0.0001, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, },
{0.0008, 0.0007, 0.0005, 0.0003, 0.0002, 0.0001, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, },
{0.0001, 0.0001, 0.0001, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, },
};
const float total_max = 234.37535;
// Defines RADIUS Radius Kernel total_max
#include "lenia_generated_kernel.cginc"
float total = 0.0;
[unroll(RADIUS)]
@ -152,7 +121,7 @@
float state = activation(count) * _Speed + old_state;
state = clamp(state, 0, 1);
// // kernel visualization: lookup table
// kernel visualization: lookup table (SLOW)
// float k = 0;
// {
// float2 p = (i.uv - 0.5 ) * resolution;
@ -168,19 +137,17 @@
// }
// }
// kernel visualisation: real size
// float2 p = (i.uv - 0.5) * resolution;
// float k = kernel(length(p)) * (max(abs(p.x), abs(p.y)) <= _Radius); // kernel visualisation: real size
// float k = kernel(length(i.uv - 0.5) * _Radius * 2); // kernel visualisation: fill square
// float k = kernel(length(p)) * (max(abs(p.x), abs(p.y)) <= _Radius);
// kernel visualisation: fill square
// float k = kernel(length(i.uv - 0.5) * _Radius * 2);
// float a = activation(i.uv.x);
// float4 col = float4(state, k, a, 1);
// float4 col = float4(state, k, k2, 1);
// float4 col = float4(state, state * 0.5, 0, 1);
// float4 col = float4(state, i.uv.x * state, i.uv.y * state, 1);
float4 col = state;
col.a = 1;
float4 col = float4(state, i.uv.x * state, i.uv.y * state, 1);
return col;
}
ENDCG

View file

@ -0,0 +1,27 @@
// generated by the rust program
const int Radius = 20;
#define RADIUS 20
const half Kernel[21][20] = {
{0.00, 0.04, 0.10, 0.20, 0.38, 0.60, 0.80, 0.96, 0.98, 0.88, 0.68, 0.46, 0.26, 0.12, 0.06, 0.02, 0.00, 0.00, 0.00, 0.00, },
{0.02, 0.04, 0.10, 0.22, 0.40, 0.60, 0.82, 0.96, 0.98, 0.88, 0.68, 0.44, 0.26, 0.12, 0.06, 0.02, 0.00, 0.00, 0.00, 0.00, },
{0.04, 0.08, 0.16, 0.28, 0.46, 0.66, 0.86, 0.98, 0.98, 0.84, 0.64, 0.42, 0.24, 0.12, 0.04, 0.02, 0.00, 0.00, 0.00, 0.00, },
{0.10, 0.16, 0.24, 0.38, 0.56, 0.74, 0.92, 0.98, 0.94, 0.80, 0.58, 0.38, 0.22, 0.10, 0.04, 0.00, 0.00, 0.00, 0.00, 0.00, },
{0.22, 0.28, 0.38, 0.52, 0.68, 0.84, 0.96, 0.98, 0.90, 0.74, 0.52, 0.32, 0.18, 0.08, 0.02, 0.00, 0.00, 0.00, 0.00, 0.00, },
{0.40, 0.46, 0.56, 0.68, 0.82, 0.94, 0.98, 0.96, 0.82, 0.64, 0.44, 0.26, 0.14, 0.06, 0.02, 0.00, 0.00, 0.00, 0.00, 0.00, },
{0.60, 0.66, 0.74, 0.84, 0.94, 0.98, 0.98, 0.88, 0.72, 0.54, 0.34, 0.20, 0.10, 0.04, 0.02, 0.00, 0.00, 0.00, 0.00, 0.00, },
{0.82, 0.86, 0.92, 0.96, 0.98, 0.98, 0.90, 0.76, 0.58, 0.42, 0.26, 0.14, 0.06, 0.02, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, },
{0.96, 0.98, 0.98, 0.98, 0.96, 0.88, 0.76, 0.60, 0.44, 0.30, 0.18, 0.10, 0.04, 0.02, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, },
{0.98, 0.98, 0.94, 0.90, 0.82, 0.72, 0.58, 0.44, 0.32, 0.20, 0.10, 0.06, 0.02, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, },
{0.88, 0.84, 0.80, 0.74, 0.64, 0.54, 0.42, 0.30, 0.20, 0.12, 0.06, 0.02, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, },
{0.68, 0.64, 0.58, 0.52, 0.44, 0.34, 0.26, 0.18, 0.10, 0.06, 0.02, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, },
{0.44, 0.42, 0.38, 0.32, 0.26, 0.20, 0.14, 0.10, 0.06, 0.02, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, },
{0.26, 0.24, 0.22, 0.18, 0.14, 0.10, 0.06, 0.04, 0.02, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, },
{0.12, 0.12, 0.10, 0.08, 0.06, 0.04, 0.02, 0.02, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, },
{0.06, 0.04, 0.04, 0.02, 0.02, 0.02, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, },
{0.02, 0.02, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, },
{0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, },
{0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, },
{0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, },
{0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, },
};
const float total_max = 356.63977;

View file

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: f5ad97d01e4ce5aa4aaadce6858bf63f
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

View file

@ -91,7 +91,7 @@ Material:
- _ZWrite: 1
m_Colors:
- _CameraFadeParams: {r: 0, g: Infinity, b: 0, a: 0}
- _Color: {r: 2, g: 2, b: 2, a: 1}
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _ColorAddSubDiff: {r: 0, g: 0, b: 0, a: 0}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
- _SoftParticleFadeParams: {r: 0, g: 0, b: 0, a: 0}

View file

@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
image = { version = "0.24.6", default_features = false, features = ["png"] }

BIN
lenia-kernel/kernel.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

View file

@ -3,33 +3,44 @@ use std::env;
fn main() {
let args: Vec<_> = env::args().skip(1).collect();
if args.is_empty() {
println!("usage: kernel_radius");
println!("specify kernel radius, eg:");
println!("cargo run 16 > ../Assets/automata/Lenia/lenia_generated_kernel.cginc");
return;
}
let radius = args[0].parse().unwrap();
let k_offset = 0.435;
let k_sharpness = 28.0;
let mut img = image::RgbImage::new(radius * 2 + 1, radius * 2 + 1);
let mut total_max = 0.0;
println!(" // generated by the rust program");
println!(" const int Radius = {};", radius);
println!(" #define RADIUS {}", radius);
println!(" // kernel LUT size is {} bytes", radius * (radius + 1) * 2);
println!(" const half Kernel[{}][{}] = {{", radius + 1, radius);
println!("// generated by the rust program");
println!("const int Radius = {};", radius);
println!("#define RADIUS {}", radius);
println!("const half Kernel[{}][{}] = {{", radius + 1, radius);
for y in 0..=radius {
print!(" {{");
for x in 1..=radius {
let k = k(x, y, radius, k_offset, k_sharpness);
let k = (k(x, y, radius, k_offset, k_sharpness) * 50.0).floor() / 50.0;
total_max += k * 4.0;
print!("{:.4}, ", k);
print!("{:.2}, ", k);
{
let pixel = image::Rgb([0, (k * 255.0) as u8, 0]);
// let cx = radius
img.put_pixel(radius + x, radius + y, pixel);
img.put_pixel(radius - y, radius + x, pixel);
img.put_pixel(radius - x, radius - y, pixel);
img.put_pixel(radius + y, radius - x, pixel);
}
}
println!("}},");
}
println!(" }};");
println!(" const float TOTAL_MAX = {};", total_max);
println!("}};");
println!("const float total_max = {};", total_max);
img.save("kernel.png").unwrap();
}
fn k(x: usize, y: usize, radius: usize, k_offset: f32, k_sharpness: f32) -> f32 {
fn k(x: u32, y: u32, radius: u32, k_offset: f32, k_sharpness: f32) -> f32 {
let x = x as f32;
let y = y as f32;
let r = (x * x + y * y).sqrt() / radius as f32;