SHOW:
|
|
- or go back to the newest paste.
1 | - | // Source |
1 | + | |
2 | - | // http://www.gamedev.net/topic/592001-random-number-generation-based-on-time-in-hlsl/ |
2 | + | |
3 | #pragma kernel CSMain | |
4 | ||
5 | RWTexture2D<float4> Result; | |
6 | ||
7 | RWStructuredBuffer<float3> posBuffer : register(u0); | |
8 | RWStructuredBuffer<float3> colBuffer : register(u1); | |
9 | RWStructuredBuffer<float3> tarBuffer : register(u2); | |
10 | ||
11 | float Time; | |
12 | float4 CoreLoc; | |
13 | ||
14 | float rand_1_05(in float2 uv) | |
15 | { | |
16 | float2 noise = (frac(sin(dot(uv ,float2(12.9898,78.233)*2.0)) * 43758.5453)); | |
17 | return abs(noise.x + noise.y) * 0.5; | |
18 | } | |
19 | ||
20 | float randomSpread(float2 seed) | |
21 | { | |
22 | float part1 = rand_1_05(seed); | |
23 | float part2 = 1; | |
24 | ||
25 | if (part1 < 0.5) | |
26 | { | |
27 | part2 = 1; | |
28 | } | |
29 | else{ | |
30 | part2 = -1; | |
31 | } | |
32 | ||
33 | return part2; | |
34 | } | |
35 | ||
36 | ||
37 | float3 ranWithinSphere(float2 seed) | |
38 | { | |
39 | float3 temp = float3(0,0,0); | |
40 | ||
41 | temp.x = randomSpread(seed); | |
42 | temp.y = randomSpread(seed); | |
43 | temp.z = randomSpread(seed); | |
44 | ||
45 | return temp; | |
46 | } | |
47 | ||
48 | float distance(float3 point1, float3 point2) | |
49 | { | |
50 | float x = (point1.x - point2.x) * (point1.x - point2.x); | |
51 | float y = (point1.y - point2.y) * (point1.x - point2.y); | |
52 | float z = (point1.z - point2.z) * (point1.x - point2.z); | |
53 | ||
54 | return sqrt(x + y + z); | |
55 | } | |
56 | ||
57 | [numthreads(10,1,1)] | |
58 | void CSMain (uint3 id : SV_DispatchThreadID) | |
59 | { | |
60 | float2 seed = float2(id.x * Time, Time); | |
61 | ||
62 | ||
63 | colBuffer[id.x].x = cos(Time) * sin(Time); | |
64 | colBuffer[id.x].y = sin(Time); | |
65 | ||
66 | tarBuffer[id.x].x = CoreLoc.x + ((cos(Time * id.x) * sin(Time))) * 4; | |
67 | tarBuffer[id.x].y = CoreLoc.y + ((sin(Time * id.x) * cos(Time))) * 7; | |
68 | tarBuffer[id.x].z = CoreLoc.z + ((cos(Time * id.x) * sin(Time))) * 12; | |
69 | ||
70 | ||
71 | // Work your way towards the target | |
72 | float changeX = tarBuffer[id.x].x - posBuffer[id.x].x; | |
73 | float changeY = tarBuffer[id.x].y - posBuffer[id.x].y; | |
74 | float changeZ = tarBuffer[id.x].z - posBuffer[id.x].z; | |
75 | ||
76 | ||
77 | posBuffer[id.x].x += (changeX / 40); | |
78 | posBuffer[id.x].y += (changeY / 40); | |
79 | posBuffer[id.x].z += (changeZ / 40); | |
80 | ||
81 | } |