-
Notifications
You must be signed in to change notification settings - Fork 349
Expand file tree
/
Copy pathMoonShading.compute
More file actions
87 lines (68 loc) · 2.75 KB
/
MoonShading.compute
File metadata and controls
87 lines (68 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#pragma kernel CSMain
#include "../../Includes/FractalNoise.cginc"
#include "../../Includes/Math.cginc"
//
StructuredBuffer<float3> vertices;
RWStructuredBuffer<float4> shadingData;
uint numVertices;
StructuredBuffer<float4> points;
int numRandomPoints;
float4 noiseParams_biomeWarp[3];
float4 noiseParams_detailWarp[3];
float4 noiseParams_detail[3];
float4 params;
StructuredBuffer<float4> ejectaCraters;
int numEjectaCraters;
float calcSphereNoise(float3 pos) {
float sphereNoise = 3.402823466e+38F;
for (int i = 0; i < numRandomPoints; i ++) {
float3 sphereCentre = points[i].xyz;
float sphereRadius = points[i].w;
float dst = length(pos - sphereCentre);
float t = dst / sphereRadius;
sphereNoise = min(sphereNoise, t);
}
return sphereNoise;
}
[numthreads(512,1,1)]
void CSMain (uint id : SV_DispatchThreadID)
{
if (id >= numVertices) { return; }
float3 pos = vertices[id];
float3 domainWarp = 0;
domainWarp.x = simpleNoise(pos, noiseParams_biomeWarp);
domainWarp.y = simpleNoise(pos + 100, noiseParams_biomeWarp);
domainWarp.z = simpleNoise(pos - 100, noiseParams_biomeWarp);
float3 detailWarp = 0;
detailWarp.x = simpleNoise(pos, noiseParams_detailWarp);
detailWarp.y = simpleNoise(pos + 100, noiseParams_detailWarp);
detailWarp.z = simpleNoise(pos - 100, noiseParams_detailWarp);
float domainWarp2 = simpleNoise(pos, noiseParams_detailWarp);
// Sphere noise
float3 warpedPos = pos + (domainWarp) * 0.1;
float sphereNoise = calcSphereNoise(warpedPos);
float detailNoise = simpleNoise(pos + domainWarp2 * 0.1, noiseParams_detail);
// Calculate uv coords for crater 'ejecta ray' texture to be projected onto the surface around select craters
float2 ejectaUV = 1;
float minDst = 999;
for (int craterIndex = 0; craterIndex < numEjectaCraters; craterIndex ++) {
float3 craterCentre = ejectaCraters[craterIndex].xyz;
float dst = length(craterCentre - pos);
float scaledDst = dst / ejectaCraters[craterIndex].w;
if (scaledDst < minDst) {
minDst = scaledDst;
float3 craterUp = craterCentre;
float3 craterForward = normalize(cross(craterUp, float3(0,1,0)));
float3 forward = normalize(cross(pos, craterUp));
float cross_x = forward.y * craterForward.z - forward.z * craterForward.y;
float cross_y = forward.z * craterForward.x - forward.x * craterForward.z;
float cross_z = forward.x * craterForward.y - forward.y * craterForward.x;
float angleSign = sign(craterUp.x * cross_x + craterUp.y * cross_y + craterUp.z * cross_z);
float angle = acos(dot(craterForward, forward)) * angleSign;
//float estBiomeVal = Blend(7 * 0.8, 0.8, calcSphereNoise(craterCentre));
//float biomeSign = (estBiomeVal < 0.5)?-1:1;
ejectaUV = float2 (angle, scaledDst);
}
}
shadingData[id] = float4(ejectaUV.xy, detailNoise, sphereNoise);
}