Skip to content

Instantly share code, notes, and snippets.

@BruceJillis
Last active August 29, 2015 14:04
Show Gist options
  • Save BruceJillis/8dc60fc3444948c27219 to your computer and use it in GitHub Desktop.
Save BruceJillis/8dc60fc3444948c27219 to your computer and use it in GitHub Desktop.
Rough translation of https://glsl.heroku.com/e#18500.0 to Unity Cg
Shader "Custom/Stars" {
Properties {
iterations("_iterations", int) = 17
formuparam("formuparam", float) = 0.53
volsteps("volsteps", int) = 20
stepsize("stepsize", float) = 0.1
zoom("zoom", float) = 0.800
tile("tile", float) = 0.850
speed("speed", float) = 0.010
brightness("brightness", float) = 0.015
darkmatter("darkmatter", float) = 0.30
distfading("distfading", float) = 0.70
saturation("saturation", float) = 0.80
mouse("mouse",Vector) = (0., 0., 0.)
resolution("resolution",Vector) = (1280., 1024., 0.)
}
SubShader {
Pass {
CGPROGRAM
// Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it does not contain a surface program or both vertex and fragment programs.
#pragma exclude_renderers gles
#include "UnityCG.cginc"
#pragma fragment frag
#pragma vertex vert
// uniforms corresponding to properties
uniform float iterations;
uniform float formuparam;
uniform float volsteps;
uniform float stepsize;
uniform float zoom;
uniform float tile;
uniform float speed;
uniform float brightness;
uniform float darkmatter;
uniform float distfading;
uniform float saturation;
uniform float time;
uniform float2 mouse;
uniform float2 resolution;
float4 vert(float4 v:POSITION) : SV_POSITION {
return mul (UNITY_MATRIX_MVP, v);
}
fixed4 frag(float4 sp:VPOS) : COLOR {
//return float4(1, 0, 0, 1);
//get coords and direction
float2 uv = sp.xy/resolution.xy - 0.5;
uv.y *= resolution.y/resolution.x;
float3 dir = float3(uv*zoom, 1.);
float time = _Time[0] * speed + 0.25;
//mouse rotation
float a1=.5+mouse.x/resolution.x*2.;
float a2=.8+mouse.y/resolution.y*2.;
float2x2 rot1 = float2x2(cos(a1),sin(a1),-sin(a1),cos(a1));
float2x2 rot2 = float2x2(cos(a2),sin(a2),-sin(a2),cos(a2));
dir.xz = mul(dir.xz, rot1);
dir.xy = mul(dir.xy, rot2);
float3 from = float3(1.0,0.5,0.5);
from += float3(time*2.,time,-2.);
from.xz = mul(from.xz, rot1);
from.xy = mul(from.xy, rot2);
//volumetric rendering
float s=0.1, fade=1.;
float3 v = float3(0.0, 0.0, 0.0);
for (int r=0; r<volsteps; r++) {
float3 p=from+s*dir*.5;
p = abs(float3(tile,tile,tile) - fmod(p, float3(tile*2.0, tile*2.0, tile*2.0))); // tiling fold
float pa,a=pa=0.;
for (int i=0; i<iterations; i++) {
p=abs(p)/dot(p,p)-formuparam; // the magic formula
a+=abs(length(p)-pa); // absolute sum of average change
pa=length(p);
}
float dm=max(0.,darkmatter-a*a*.001); //dark matter
a*=a*a; // add contrast
if (r>6) fade*=1.-dm; // dark matter, don't render near
//v+=float3(dm,dm*.5,0.);
v+=fade;
v+=float3(s,s*s,s*s*s*s)*a*brightness*fade; // coloring based on distance
fade*=distfading; // distance fading
s+=stepsize;
}
v = lerp(float3(length(v), length(v), length(v)), v, saturation); //color adjust
return float4(v*0.01, 1.0);
}
ENDCG
}
}
}
using UnityEngine;
using System;
using Random = UnityEngine.Random;
using System.Collections;
public class UpdateShader : MonoBehaviour {
public Material shaderMaterial;
void Start () {
}
void Update() {
shaderMaterial.SetVector("mouse", new Vector3(Input.mousePosition.x, Input.mousePosition.y));
}
}
@agustindidiego
Copy link

Hi, How are you?
I found your shader and I believe that it's very nice :)
I'm trying to use your shader for a Skybox, but I'm not being able to correctly map the stars onto a sphere. I assume that it shouldn't be very hard, but I have very low and no experience with Shaders and I'm already trying to make it work since 2 days ago searching for references on the internet with no luck.
Could you give me some advice or help me a bit?
Thanks a lot.

My closest approach was to get a ray in the vertex program and use the ray coordinates instead of the sp:VPOS in the fragment program, but the stars looks stretched in some places do to wrong uv mapping or something.

Code added to the vertex program:
float3 eyeRay = normalize(mul((float3x3)_Object2World, v.vertex.xyz));
OUT.rayDir = half3(-eyeRay);

Code changed in the fragment program:
fixed4 frag(v2f IN) : COLOR {
float2 uv = IN.rayDir.xy/resolution.xy - 0.5;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment