Skip to content

Commit daddb56

Browse files
author
Dorian Stoll
committedJun 8, 2017
Add project
1 parent 8577261 commit daddb56

22 files changed

+212
-0
lines changed
 

‎Assets/Editor.meta

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Assets/Editor/ExportAssetBundle.cs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
using System.Linq;
4+
using System.IO;
5+
using System.Collections;
6+
7+
public class CreateAssetBundles
8+
{
9+
[MenuItem ("Assets/Build Shader Bundle")]
10+
static void BuildAllAssetBundles ()
11+
{
12+
// Bring up save panel
13+
string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "unity3d");
14+
15+
var opts = BuildAssetBundleOptions.DeterministicAssetBundle | BuildAssetBundleOptions.ForceRebuildAssetBundle | BuildAssetBundleOptions.CollectDependencies;
16+
BuildTarget[] platforms = { BuildTarget.StandaloneWindows, BuildTarget.StandaloneOSXUniversal, BuildTarget.StandaloneLinux };
17+
string[] platformExts = { "-windows", "-macosx", "-linux" };
18+
19+
for (var i = 0; i < platforms.Length; ++i)
20+
{
21+
// Build the resource file from the active selection.
22+
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.Assets);
23+
BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path.Replace(".unity3d", platformExts[i] + ".unity3d"), opts, platforms[i]);
24+
Selection.objects = selection;
25+
}
26+
}
27+
}

‎Assets/Editor/ExportAssetBundle.cs.meta

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Assets/ringsShader.shader

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
//Ring shader for Kopernicus
2+
//by Ghassen Lahmar (blackrack)
3+
4+
Shader "Kopernicus/Rings"
5+
{
6+
SubShader
7+
{
8+
Tags
9+
{
10+
"Queue" = "Transparent"
11+
"IgnoreProjector" = "True"
12+
"RenderType" = "Transparent"
13+
}
14+
15+
Pass
16+
{
17+
ZWrite off
18+
19+
Cull Off
20+
Blend SrcAlpha OneMinusSrcAlpha //alpha blend
21+
22+
CGPROGRAM
23+
#pragma vertex vert
24+
#pragma fragment frag
25+
#pragma glsl
26+
#pragma target 3.0
27+
28+
#include "UnityCG.cginc"
29+
30+
uniform sampler2D _MainTex;
31+
32+
uniform sampler2D _noiseTexture;
33+
34+
uniform float innerRadius;
35+
uniform float outerRadius;
36+
37+
uniform float planetRadius;
38+
uniform float sunRadius;
39+
40+
uniform float3 sunPosRelativeToPlanet;
41+
42+
uniform float penumbraMultiplier;
43+
44+
#define M_PI 3.1415926535897932384626
45+
46+
struct v2f
47+
{
48+
float4 pos: SV_POSITION;
49+
float3 worldPos: TEXCOORD0;
50+
//float3 worldNormal: TEXCOORD1; //we don't need normals where we're going
51+
//LIGHTING_COORDS(3,4) //nor do we need this crap
52+
float3 planetOrigin: TEXCOORD1; //moved from fragment shader
53+
//float4 objectVertex: TEXCOORD2;
54+
};
55+
56+
v2f vert(appdata_base v)
57+
{
58+
v2f o;
59+
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
60+
61+
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
62+
63+
//o.viewDir = normalize(o.worldPos - _WorldSpaceCameraPos); //viewdir calculation moved to fragment shader because of interpolation artifacts (that frankly don't make any sense to me)
64+
65+
//o.worldNormal = normalize(mul( unity_ObjectToWorld, float4(v.normal, 0)).xyz); //should be fine
66+
67+
o.planetOrigin = mul(unity_ObjectToWorld, float4(0, 0, 0, 1)).xyz;
68+
69+
//o.objectVertex = v.vertex;
70+
71+
return o;
72+
}
73+
74+
//mie scattering
75+
//copied from scatterer/Proland
76+
float PhaseFunctionM(float mu, float mieG)
77+
{
78+
// Mie phase function
79+
return 1.5 * 1.0 / (4.0 * M_PI) * (1.0 - mieG * mieG) * pow(1.0 + (mieG * mieG) - 2.0 * mieG * mu, -3.0 / 2.0) * (1.0 + mu * mu) / (2.0 + mieG * mieG);
80+
}
81+
82+
//eclipse function from scatterer
83+
//used here to cast the planet shadow on the ring
84+
//will simplify it later and keep only the necessary bits for the ring
85+
//Original Source: wikibooks.org/wiki/GLSL_Programming/Unity/Soft_Shadows_of_Spheres
86+
float getEclipseShadow(float3 worldPos, float3 worldLightPos, float3 occluderSpherePosition,float3 occluderSphereRadius, float3 lightSourceRadius)
87+
{
88+
float3 lightDirection = float3(worldLightPos - worldPos);
89+
float3 lightDistance = length(lightDirection);
90+
lightDirection = lightDirection / lightDistance;
91+
92+
// computation of level of shadowing w
93+
float3 sphereDirection = float3(occluderSpherePosition - worldPos); //occluder planet
94+
float sphereDistance = length(sphereDirection);
95+
sphereDirection = sphereDirection / sphereDistance;
96+
97+
float dd = lightDistance * (asin(min(1.0, length(cross(lightDirection, sphereDirection)))) - asin(min(1.0, occluderSphereRadius / sphereDistance)));
98+
99+
float w = smoothstep(-1.0, 1.0, -dd / lightSourceRadius);
100+
w = w * smoothstep(0.0, 0.2, dot(lightDirection, sphereDirection));
101+
102+
return (1 - w);
103+
}
104+
105+
float4 frag(v2f i): COLOR
106+
{
107+
108+
//fix this for additional lights later, will be useful when I do the planetshine update for scatterer
109+
float3 lightDir = normalize(_WorldSpaceLightPos0.xyz); //assuming directional light only for now
110+
111+
float3 worldPosRelPlanet = i.worldPos - i.planetOrigin;
112+
float distance = length(worldPosRelPlanet);
113+
114+
float texturePosition = (distance - innerRadius) / (outerRadius - innerRadius);
115+
texturePosition = 1 - texturePosition; //flip to match UVs
116+
117+
//lighting
118+
//float dotLight = dot (i.worldNormal,lightDir); //boring
119+
//instead use the viewing direction (inspired from observing space engine rings)
120+
//looks more interesting than I expected
121+
float3 viewdir = normalize(i.worldPos - _WorldSpaceCameraPos);
122+
float mu = dot(lightDir, -viewdir);
123+
float dotLight = 0.5 * (mu + 1);
124+
125+
//mie scattering through rings when observed from the back
126+
float mieG = -0.95; //needs to be negative?
127+
float mieScattering = PhaseFunctionM(mu, mieG);
128+
mieScattering *= 0.03; // result too bright for some reason, this fixes it
129+
130+
//planet shadow on ring
131+
//do everything relative to planet position
132+
float shadow = getEclipseShadow(worldPosRelPlanet * 6000, sunPosRelativeToPlanet, 0, planetRadius, sunRadius * penumbraMultiplier); //*6000 to convert to local space, might be simpler in scaled?
133+
134+
//TODO: Fade in some noise here when getting close to the rings
135+
//make it procedural noise?
136+
137+
float4 color = tex2D(_MainTex, float2(texturePosition, 0.25)); //let's say left side = front texture and right side = back texture
138+
float4 backColor = tex2D(_MainTex, float2(texturePosition, 0.75)); //back color, same as frontColor if only one band is included
139+
//haven't tested a different back texture yet
140+
141+
color.xyz = color.xyz * dotLight + backColor.xyz * mieScattering; //for now alpha is taken from front color only, I'll try to think of something
142+
color.xyz *= shadow;
143+
144+
color = (texturePosition > 1 || texturePosition < 0) ? float4(0, 0, 0, 0) : color; //return transparent pixels if not between inner and outer radiuses
145+
146+
//I'm kinda proud of this shader so far, it's short and clean
147+
148+
return color;
149+
}
150+
ENDCG
151+
}
152+
}
153+
}

‎Assets/ringsShader.shader.meta

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎ProjectSettings/AudioManager.asset

4.04 KB
Binary file not shown.
4.01 KB
Binary file not shown.

‎ProjectSettings/DynamicsManager.asset

4.18 KB
Binary file not shown.
4.01 KB
Binary file not shown.

‎ProjectSettings/EditorSettings.asset

4.11 KB
Binary file not shown.
4.29 KB
Binary file not shown.

‎ProjectSettings/InputManager.asset

5.39 KB
Binary file not shown.

‎ProjectSettings/NavMeshAreas.asset

4.28 KB
Binary file not shown.

‎ProjectSettings/NetworkManager.asset

4.02 KB
Binary file not shown.
4.26 KB
Binary file not shown.

‎ProjectSettings/ProjectSettings.asset

42.1 KB
Binary file not shown.

‎ProjectSettings/ProjectVersion.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
m_EditorVersion: 5.4.0p4
2+
m_StandardAssetsVersion: 0

‎ProjectSettings/QualitySettings.asset

4.89 KB
Binary file not shown.

‎ProjectSettings/TagManager.asset

4.21 KB
Binary file not shown.

‎ProjectSettings/TimeManager.asset

4.02 KB
Binary file not shown.
4.02 KB
Binary file not shown.
4.09 KB
Binary file not shown.

0 commit comments

Comments
 (0)
Please sign in to comment.