Plasma Dynamics

Edit

Development Notes

Experimental results obtained in 1879 when conducting electricity through rarefied gases in a vacuum tube and modulated by a magnetic field. From left to right, the tube is filled with nitrogen, oxygen, carbon dioxide and tin(IV) chloride. The positive electrode is on top. The tube with nitrogen produced a spiral, the one with carbon dioxide a set of nine stacked toroids embracing a Y-shaped column. (c) John Rand Capron.

Birkeland Currents

Dave Brown – Plasma Experiments

using System.Collections.Generic;
using UnityEngine;
using System.IO;

#if UNITY_EDITOR
using UnityEditor;
#endif

[ExecuteInEditMode]
public class GradientMapper : MonoBehaviour
{
    [Header("Gradient map parameters")]
    public Vector2Int gradientMapDimensions = new Vector2Int(128, 32);
    public Gradient gradient;

    [Header("Enable testing")]
    public bool testing = false;

    private SpriteRenderer spriteRenderer;
    private Material material;

    [HideInInspector]
    public Texture2D texture;

    public static int totalMaps = 0;
    [Range(2, 50)]
    public int numKeys = 2;
    [Range(-1f, 1f)]
    public float posterize = 0;

    [Range(0f, 0.999f)]
    public float drift = 0;

    public Color color_1;
    public Color color_2;

    private void OnEnable()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
        if (spriteRenderer == null)
        {
            Debug.LogWarning("No sprite renderer on this game object! Removing GradientMapper");
            DestroyImmediate(this);
        }
        else
        {
            material = spriteRenderer.sharedMaterial;
        }
    }

    void Update()
    {
        if (testing)
        {
            if (texture == null)
            {
                texture = new Texture2D(gradientMapDimensions.x, gradientMapDimensions.y);
            }
            texture.wrapMode = TextureWrapMode.Clamp;

            Color col_1;
            Color col_2;
            float t = 0;

            int pix_per_Key = gradientMapDimensions.x / (numKeys - 1);

            int curr_key = -1;

            int mod;

            float dt = 1f / (float)pix_per_Key;
            for (int x = 0; x < gradientMapDimensions.x; x++)
            {
                mod = x % pix_per_Key;
                if (mod == (int)(pix_per_Key*drift))
                {
                    curr_key++;
                    t = 0;
                }
                if (curr_key % 2 == 0)
                {
                    col_1 = color_1;
                    col_2 = color_2;
                }
                else
                {
                    col_1 = color_2;
                    col_2 = color_1;
                }
                

                Color color = Color.Lerp(col_1, col_2, t+posterize);
                t += dt;
                for (int y = 0; y < gradientMapDimensions.y; y++)
                {
                    texture.SetPixel(x, y, color);
                }
            }
            texture.Apply();
            if (material.HasProperty("_GradientMap"))
            {
                material.SetTexture("_GradientMap", texture);
            }
        }
    }
}

 

Shader "Unlit/SpriteGradientMap"
{
     Properties
    {
        [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
        _Color ("Tint", Color) = (1,1,1,1)
        [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
        [HideInInspector] _RendererColor ("RendererColor", Color) = (1,1,1,1)
        [HideInInspector] _Flip ("Flip", Vector) = (1,1,1,1)
        [PerRendererData] _AlphaTex ("External Alpha", 2D) = "white" {}
        [PerRendererData] _EnableExternalAlpha ("Enable External Alpha", Float) = 0
 
        _GradientMap ("Gradient map", 2D) = "white" {}
    }
 
    SubShader
    {
        Tags
        {
            "Queue"="Transparent"
            "IgnoreProjector"="True"
            "RenderType"="Transparent"
            "PreviewType"="Plane"
            "CanUseSpriteAtlas"="True"
        }
 
        Cull Off
        Lighting Off
        ZWrite Off
        Blend One OneMinusSrcAlpha
        Pass
 
 
        {
        CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 2.0
            #pragma multi_compile_instancing
            #pragma multi_compile _ PIXELSNAP_ON
            #pragma multi_compile _ ETC1_EXTERNAL_ALPHA
            #include "UnitySprites.cginc"
 
 
            v2f vert(appdata_t IN)
            {
                v2f OUT;
 
                UNITY_SETUP_INSTANCE_ID (IN);
                UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
 
            #ifdef UNITY_INSTANCING_ENABLED
                IN.vertex.xy *= _Flip.xy;
            #endif
 
                OUT.vertex = UnityObjectToClipPos(IN.vertex);
                OUT.texcoord = IN.texcoord;
                OUT.color = IN.color * _Color * _RendererColor;
 
                #ifdef PIXELSNAP_ON
                OUT.vertex = UnityPixelSnap (OUT.vertex);
                #endif
 
                return OUT;
            }
 
            sampler2D _GradientMap;
 
            fixed4 frag(v2f IN) : SV_Target
            {
                fixed4 c = SampleSpriteTexture (IN.texcoord);
                float grayscale = dot(c.rgb, float3(0.3, 0.59, 0.11));
                fixed4 gradientCol = tex2D(_GradientMap, float2(grayscale, 0));
                return gradientCol * c.a * IN.color;
            }
 
        ENDCG
        }
    }
 
Fallback "Transparent/VertexLit"
}