@@ -28,7 +28,9 @@ public class RayTracing : MonoBehaviour
28
28
//float lightIntensity;
29
29
30
30
Material addMaterial ;
31
+
31
32
ComputeBuffer sphereBuffer ;
33
+ private static List < Sphere > spheres = new List < Sphere > ( ) ;
32
34
33
35
private static List < MeshObject > meshObjects = new List < MeshObject > ( ) ;
34
36
private static List < Vector3 > vertices = new List < Vector3 > ( ) ;
@@ -79,6 +81,7 @@ void OnRenderImage(RenderTexture source, RenderTexture destination)
79
81
}
80
82
81
83
//SetUpGeometryBuffer();
84
+ //BuildSphereBuffer();
82
85
BuildMeshBuffer ( ) ;
83
86
84
87
// Send stuff to the computer shader.
@@ -88,7 +91,7 @@ void OnRenderImage(RenderTexture source, RenderTexture destination)
88
91
rayTracer . SetVector ( "_cameraRotation" , camera . transform . eulerAngles ) ;
89
92
//rayTracer.SetVector("_LightVector", new Vector4(lightDirection.x, lightDirection.y, lightDirection.z, lightIntensity));
90
93
91
- // rayTracer.SetBuffer(0, "_SphereBuffer", sphereBuffer);
94
+ rayTracer . SetBuffer ( 0 , "_SphereBuffer" , sphereBuffer ) ;
92
95
rayTracer . SetBuffer ( 0 , "_meshObjects" , meshObjectBuffer ) ;
93
96
rayTracer . SetBuffer ( 0 , "_vertices" , vertexBuffer ) ;
94
97
rayTracer . SetBuffer ( 0 , "_indices" , indexBuffer ) ;
@@ -124,55 +127,17 @@ void OnRenderImage(RenderTexture source, RenderTexture destination)
124
127
125
128
ReleaseBuffer ( ) ;
126
129
}
127
- /*
130
+
128
131
struct Sphere
129
132
{
130
133
public Vector3 Position ;
131
134
public float Radius ;
132
- public Vector3 Specular;
133
- public Vector3 Albedo;
134
- public Vector3 Emission;
135
135
} ;
136
-
137
- void SetUpGeometryBuffer()
138
- {
139
- List<Sphere> spheres = new List<Sphere>();
140
-
141
- Sphere sphere;
142
- sphere.Position = new Vector3(0, .5f, 0);
143
- sphere.Radius = .5f;
144
- sphere.Specular = new Vector3(0f, 0f, 0f);
145
- sphere.Albedo = new Vector3(1f, 1f, 1f);
146
- sphere.Emission = new Vector3(1f,1f,1f);
147
-
148
- spheres.Add(sphere);
149
-
150
- sphere.Position = new Vector3(4f, 2f, 1f);
151
- sphere.Radius = 1f;
152
- sphere.Specular = new Vector3(1f, 1f, 1f);
153
- sphere.Albedo = new Vector3(0f, 0f, 0f);
154
- sphere.Emission = new Vector3(0f, 0f, 0f);
155
-
156
-
157
- spheres.Add(sphere);
158
-
159
- sphere.Position = new Vector3(1f, 2f, 1f);
160
- sphere.Radius = 1f;
161
- sphere.Specular = new Vector3(0f, 0f, 0f);
162
- sphere.Albedo = new Vector3(1f, 0f, 0f);
163
- sphere.Emission = new Vector3(0f, 0f, 0f);
164
-
165
-
166
- spheres.Add(sphere);
167
-
168
- sphereBuffer = new ComputeBuffer(spheres.Count, 52);
169
- sphereBuffer.SetData(spheres);
170
- }
171
- */
136
+
172
137
void OnEnable ( )
173
138
{
174
139
sampleNumber = 0 ;
175
- // BuildMeshBuffer();
140
+ BuildMeshBuffer ( ) ;
176
141
}
177
142
178
143
void Update ( )
@@ -323,6 +288,8 @@ private void BuildMeshBuffer()
323
288
324
289
RayTracing . indices . AddRange ( indicesOffset ) ;
325
290
291
+ Matrix4x4 localToWorld = obj . gameObject . transform . localToWorldMatrix ;
292
+
326
293
meshObjects . Add ( new MeshObject ( )
327
294
{
328
295
localToWorldMatrix = obj . transform . localToWorldMatrix ,
@@ -333,6 +300,12 @@ private void BuildMeshBuffer()
333
300
emission = obj . emission ,
334
301
alpha = obj . alpha
335
302
} ) ;
303
+
304
+ spheres . Add ( new Sphere ( ) {
305
+ Position = meshAverage ( mesh , localToWorld ) ,
306
+ Radius = 100000f
307
+ //Radius = meshRadius(mesh, localToWorld)
308
+ } ) ;
336
309
}
337
310
338
311
// Set-up the buffers.
@@ -348,5 +321,63 @@ private void BuildMeshBuffer()
348
321
normalBuffer = new ComputeBuffer ( normals . Count , 12 ) ;
349
322
normalBuffer . SetData ( normals ) ;
350
323
324
+ sphereBuffer = new ComputeBuffer ( spheres . Count , 16 ) ;
325
+ sphereBuffer . SetData ( spheres ) ;
326
+
327
+ }
328
+
329
+
330
+
331
+ Vector3 meshAverage ( Mesh mesh , Matrix4x4 localToWorld )
332
+ {
333
+ Vector3 [ ] vertexList = mesh . vertices ;
334
+
335
+ Vector3 average = Vector3 . zero ;
336
+ int count = 0 ;
337
+
338
+ foreach ( Vector3 vertex in vertexList )
339
+ {
340
+ //print(vertex);
341
+ //localToWorld.MultiplyPoint3x4(mf.mesh.vertices[i]);
342
+ average += localToWorld . MultiplyPoint3x4 ( vertex ) ;
343
+ count ++ ;
344
+
345
+ }
346
+ //print(average);
347
+ return average / count ;
348
+ }
349
+
350
+ float meshRadius ( Mesh mesh , Matrix4x4 localToWorld )
351
+ {
352
+ Vector3 [ ] vertexList = mesh . vertices ;
353
+
354
+ float minX = 100000000000000 ;
355
+ float minY = 100000000000000 ;
356
+ float minZ = 100000000000000 ;
357
+ float maxX = 0 ;
358
+ float maxY = 0 ;
359
+ float maxZ = 0 ;
360
+
361
+ foreach ( Vector3 vertexIt in vertexList )
362
+ {
363
+ Vector3 vertex = localToWorld . MultiplyPoint3x4 ( vertexIt ) ;
364
+
365
+ if ( vertex . x < minX ) { minX = vertex . x ; }
366
+ if ( vertex . y < minY ) { minY = vertex . y ; }
367
+ if ( vertex . z < minZ ) { minY = vertex . z ; }
368
+
369
+ if ( vertex . x > maxX ) { maxX = vertex . x ; }
370
+ if ( vertex . y > maxY ) { maxY = vertex . y ; }
371
+ if ( vertex . z > maxZ ) { maxZ = vertex . z ; }
372
+ }
373
+
374
+ float radius = 0 ;
375
+
376
+ if ( maxX - minX > radius ) { radius = maxX - minX ; }
377
+ if ( maxY - minY > radius ) { radius = maxY - minY ; }
378
+ if ( maxZ - minZ > radius ) { radius = maxZ - minZ ; }
379
+
380
+ return Mathf . Abs ( radius ) ;
351
381
}
352
- }
382
+ }
383
+
0 commit comments