-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleRaytest.cs
More file actions
261 lines (209 loc) · 8.98 KB
/
SimpleRaytest.cs
File metadata and controls
261 lines (209 loc) · 8.98 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.Rendering;
public class SimpleRaytest : MonoBehaviour
{
public RenderTexture renderTexture;
public RayTracingShader rtshader;
public Vector3Int Res = new Vector3Int(512,512,512);
public float scaler = 1.0f;
public Vector3 WPosition;
public Renderer DebugRenderer;
public Light[] Lights;
public uint AreaLightSamples = 16;
public uint EnviormentalLightSamples = 128;
public Cubemap EnvCubemap;
List<Light> PointLights,ConeLights,DirectionalLights,AreaLights;
RayTracingAccelerationStructure accelerationStructure;
Vector3Int threads;
struct PointLightData
{
//Point
public Vector3 PointLightsPos;
public Vector4 PointLightsColors;
}
struct ConeLightData
{
public Vector3 ConeLightsWS;
public Vector4 ConeLightsColors;
public Vector3 ConeLightsDir;
public Vector2 ConeLightsPram;
}
struct DirLightData
{
public Vector3 DirLightsDir;
public Vector4 DirLightsColors;
}
struct AreaLightData
{
public Matrix4x4 AreaLightsMatrix;
public Matrix4x4 AreaLightsMatrixInv;
public Vector3 AreaLightsPos;
public Vector4 AreaLightsColors;
public Vector3 AreaLightsSize;
}
void Start()
{
initializeSystem();
// UnityEditor.Rendering.EditorGraphicsSettings.
// UnityEditor.PlayerSettings.SetGraphicsAPIs(UnityEditor.BuildTarget.StandaloneWindows64, UnityEngine.Rendering.GraphicsDeviceType.Direct3D12);
}
void initializeSystem()
{
accelerationStructure = new RayTracingAccelerationStructure();
CollectGeo();
RenderTextureDescriptor textureDescriptor = new RenderTextureDescriptor();
textureDescriptor.dimension = UnityEngine.Rendering.TextureDimension.Tex3D;
renderTexture = new RenderTexture(Res.x, Res.y, Res.z);
renderTexture.enableRandomWrite = true;
renderTexture.dimension = UnityEngine.Rendering.TextureDimension.Tex3D;
renderTexture.volumeDepth = Res.z;
renderTexture.depth = 0;
renderTexture.Create();
DebugRenderer.material.SetTexture("_BaseMap", renderTexture);
rtshader.SetTexture("g_Output", renderTexture);
// threads = new Vector3Int(Mathf.FloorToInt(Res.x / 8f), Mathf.FloorToInt(Res.y / 8f), 1);
threads = new Vector3Int(Res.x, Res.y, Res.z);
SetUpLights();
MakeCubemap();
}
void SetUpLights()
{
Lights = FindObjectsOfType<Light>();
PointLights = new List<Light>();
ConeLights = new List<Light>();
DirectionalLights = new List<Light>();
AreaLights = new List<Light>();
for (int i = 0; i < Lights.Length; i++) {
switch (Lights[i].type)
{
case LightType.Point:
PointLights.Add(Lights[i]);
break;
case LightType.Spot:
ConeLights.Add(Lights[i]);
break;
case LightType.Directional:
DirectionalLights.Add(Lights[i]);
break;
case LightType.Area:
AreaLights.Add(Lights[i]);
break;
case LightType.Disc:
AreaLights.Add(Lights[i]); //Stacking area and disc
break;
default:
break;
}
}
}
private void Update()
{
UpdateLights();
}
void MakeCubemap()
{
if (EnvCubemap == null)
{
RenderTexture cubetex = new RenderTexture(128, 128, 1, RenderTextureFormat.ARGB32);
cubetex.enableRandomWrite = true;
cubetex.dimension = UnityEngine.Rendering.TextureDimension.Cube;
cubetex.Create();
Camera renderCam = new GameObject().AddComponent<Camera>();
renderCam.cullingMask = 0;
renderCam.RenderToCubemap(cubetex);
rtshader.SetTexture("_SkyTexture", cubetex);
Destroy(renderCam);
}
}
private void UpdateLights()
{
// CollectGeo();
//Set up buffers with data stride
ComputeBuffer pointBuffer = new ComputeBuffer(PointLights.Count, (3 + 4) * 4);
ComputeBuffer coneBuffer = new ComputeBuffer(ConeLights.Count, (3 + 4 + 3 + 2) * 4);
ComputeBuffer dirBuffer = new ComputeBuffer(DirectionalLights.Count, (3 + 4) * 4);
ComputeBuffer areaBuffer = new ComputeBuffer(AreaLights.Count, (4*4 + 4*4 + 3 + 4 + 3) * 4);
PointLightData[] PointLDatas = new PointLightData[PointLights.Count];
ConeLightData[] ConeLDatas = new ConeLightData[ConeLights.Count];
DirLightData[] DirLDatas = new DirLightData[DirectionalLights.Count];
AreaLightData[] AreaLDatas = new AreaLightData[AreaLights.Count];
for (int i =0; i< PointLDatas.Length; i++)
{
PointLDatas[i].PointLightsPos = PointLights[i].transform.position;
PointLDatas[i].PointLightsColors = PointLights[i].color * PointLights[i].intensity;
}
for (int i = 0; i < ConeLDatas.Length; i++)
{
ConeLDatas[i].ConeLightsWS = ConeLights[i].transform.position;
ConeLDatas[i].ConeLightsColors = ConeLights[i].color * ConeLights[i].intensity;
ConeLDatas[i].ConeLightsDir = ConeLights[i].transform.forward;
float flPhiDot = Mathf.Clamp01(Mathf.Cos(ConeLights[i].spotAngle * 0.5f * Mathf.Deg2Rad)); // outer cone
float flThetaDot = Mathf.Clamp01(Mathf.Cos(ConeLights[i].innerSpotAngle * 0.5f * Mathf.Deg2Rad)); // inner cone
ConeLDatas[i].ConeLightsPram = new Vector4(flPhiDot, 1.0f / Mathf.Max(0.01f, flThetaDot - flPhiDot), 0, 0);
}
for (int i = 0; i < DirLDatas.Length; i++)
{
DirLDatas[i].DirLightsDir = DirectionalLights[i].transform.forward;
DirLDatas[i].DirLightsColors = DirectionalLights[i].color * DirectionalLights[i].intensity;
}
for (int i = 0; i < AreaLDatas.Length; i++)
{
AreaLDatas[i].AreaLightsPos = AreaLights[i].transform.position;
AreaLDatas[i].AreaLightsMatrix = Matrix4x4.TRS(AreaLights[i].transform.position, AreaLights[i].transform.rotation , Vector3.one);
AreaLDatas[i].AreaLightsMatrixInv = AreaLDatas[i].AreaLightsMatrix.inverse;
AreaLDatas[i].AreaLightsColors = AreaLights[i].color * AreaLights[i].intensity;
AreaLDatas[i].AreaLightsSize = new Vector3( AreaLights[i].areaSize.x, AreaLights[i].areaSize.y, AreaLights[i].type == LightType.Disc ? 1:0 ) ; //Packing for area or disc logic
}
pointBuffer.SetData(PointLDatas);
coneBuffer.SetData(ConeLDatas);
dirBuffer.SetData(DirLDatas);
areaBuffer.SetData(AreaLDatas);
//General
rtshader.SetVector("Size", Vector3.one * scaler);
rtshader.SetVector("WPosition", WPosition);
rtshader.SetFloat("_Seed", ( Random.Range(0.0f,64.0f) ) );
//Point
rtshader.SetInt("PointLightCount", PointLDatas.Length); //Add a stack overflow loop or computebuffer
rtshader.SetBuffer("PLD", pointBuffer); ;
//Cone
rtshader.SetInt("ConeLightCount", ConeLDatas.Length); //Add a stack overflow loop or computebuffer
rtshader.SetBuffer("CLD", coneBuffer);
//Directional
rtshader.SetInt("DirLightCount", DirLDatas.Length); //Add a stack overflow loop or computebuffer
rtshader.SetBuffer("DLD", dirBuffer);
//Area
rtshader.SetInt("AreaLightCount", AreaLDatas.Length); //Add a stack overflow loop or computebuffer
rtshader.SetInt("AreaLightSamples", System.Convert.ToInt32(AreaLightSamples) );
rtshader.SetBuffer("ALD", areaBuffer);
rtshader.SetInt("EnvLightSamples", System.Convert.ToInt32(EnviormentalLightSamples) );
rtshader.SetShaderPass("Raytest");
//VertexAttributeInfo attributeInfo = new VertexAttributeInfo();
//ComputeBuffer attBuffer = new ComputeBuffer(1,4*2); //uint = 2bytes
if (EnvCubemap != null) rtshader.SetTexture("_SkyTexture", EnvCubemap);
//Dispatching
rtshader.Dispatch("MainRayGenShader", threads.x, threads.y, threads.z);
pointBuffer.Release();
coneBuffer.Release();
dirBuffer.Release();
areaBuffer.Release();
}
struct VertexAttributeInfo
{
uint InputSlot; // Not supported. Always assumed to be 0.
uint Format;
uint ByteOffset;
uint Dimension;
};
void CollectGeo()
{
Renderer[] Meshes = FindObjectsOfType<Renderer>();
for (int i = 0; i< Meshes.Length; i++)
{
accelerationStructure.AddInstance(Meshes[i]);
}
accelerationStructure.Build();
rtshader.SetAccelerationStructure("g_SceneAccelStruct", accelerationStructure);
}
}