Guest User

Untitled

a guest
Dec 25th, 2012
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. // Source
  2. // http://www.gamedev.net/topic/592001-random-number-generation-based-on-time-in-hlsl/
  3.  
  4.  
  5. // Each #kernel tells which function to compile; you can have many kernels
  6. #pragma kernel CSMain
  7.  
  8. RWTexture2D<float4> Result;
  9.  
  10. RWStructuredBuffer<float3> posBuffer : register(u0);
  11. RWStructuredBuffer<float3> colBuffer : register(u1);
  12. RWStructuredBuffer<float3> tarBuffer : register(u2);
  13.  
  14. float Time;
  15. float4 CoreLoc;
  16.  
  17. float rand_1_05(in float2 uv)
  18. {
  19. float2 noise = (frac(sin(dot(uv ,float2(12.9898,78.233)*2.0)) * 43758.5453));
  20. return abs(noise.x + noise.y) * 0.5;
  21. }
  22.  
  23. float randomSpread(float2 seed)
  24. {
  25. float part1 = rand_1_05(seed);
  26. float part2 = 1;
  27.  
  28. if (part1 < 0.5)
  29. {
  30. part2 = 1;
  31. }
  32. else{
  33. part2 = -1;
  34. }
  35.  
  36. return part2;
  37. }
  38.  
  39.  
  40. float3 ranWithinSphere(float2 seed)
  41. {
  42. float3 temp = float3(0,0,0);
  43.  
  44. temp.x = randomSpread(seed);
  45. temp.y = randomSpread(seed);
  46. temp.z = randomSpread(seed);
  47.  
  48. return temp;
  49. }
  50.  
  51. float distance(float3 point1, float3 point2)
  52. {
  53. float x = (point1.x - point2.x) * (point1.x - point2.x);
  54. float y = (point1.y - point2.y) * (point1.x - point2.y);
  55. float z = (point1.z - point2.z) * (point1.x - point2.z);
  56.  
  57. return sqrt(x + y + z);
  58. }
  59.  
  60. [numthreads(10,1,1)]
  61. void CSMain (uint3 id : SV_DispatchThreadID)
  62. {
  63. float2 seed = float2(id.x * Time, Time);
  64.  
  65.  
  66. colBuffer[id.x].x = cos(Time) * sin(Time);
  67. colBuffer[id.x].y = sin(Time);
  68.  
  69. tarBuffer[id.x].x = CoreLoc.x + ((cos(Time * id.x) * sin(Time))) * 4;
  70. tarBuffer[id.x].y = CoreLoc.y + ((sin(Time * id.x) * cos(Time))) * 7;
  71. tarBuffer[id.x].z = CoreLoc.z + ((cos(Time * id.x) * sin(Time))) * 12;
  72.  
  73.  
  74. // Work your way towards the target
  75. float changeX = tarBuffer[id.x].x - posBuffer[id.x].x;
  76. float changeY = tarBuffer[id.x].y - posBuffer[id.x].y;
  77. float changeZ = tarBuffer[id.x].z - posBuffer[id.x].z;
  78.  
  79.  
  80. posBuffer[id.x].x += (changeX / 40);
  81. posBuffer[id.x].y += (changeY / 40);
  82. posBuffer[id.x].z += (changeZ / 40);
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment