BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / Spatializer / scripts / OculusSpatializerUnity.cs

OculusSpatializerUnity.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
OculusSpatializerUnity.cs 12.4 KiB Download Raw
1/************************************************************************************
2Filename : OculusSpatializerUnity.cs
3Content : Interface into real-time geometry reflection engine for native Unity
4Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
5
6Licensed under the Oculus SDK Version 3.5 (the "License");
7you may not use the Oculus SDK except in compliance with the License,
8which is provided at the time of installation or download, or which
9otherwise accompanies this software in either electronic or hard copy form.
10
11You may obtain a copy of the License at
12
13https://developer.oculus.com/licenses/sdk-3.5/
14
15Unless required by applicable law or agreed to in writing, the Oculus SDK
16distributed under the License is distributed on an "AS IS" BASIS,
17WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18See the License for the specific language governing permissions and
19limitations under the License.
20************************************************************************************/
21using System.Collections;
22using System.Collections.Generic;
23using UnityEngine;
24using System.Runtime.InteropServices;
25
26public class OculusSpatializerUnity : MonoBehaviour
27{
28 public LayerMask layerMask = -1;
29 public bool visualizeRoom = true;
30 bool roomVisualizationInitialized = false;
31
32 public int raysPerSecond = 256;
33 public float roomInterpSpeed = 0.9f;
34 public float maxWallDistance = 50.0f;
35 public int rayCacheSize = 512;
36
37 public bool dynamicReflectionsEnabled = true;
38
39 AudioRaycastCallback _raycastCallback; // cache an instance of the delegate so the GC doesn't nuke it!
40
41 float particleSize = 0.2f;
42 float particleOffset = 0.1f;
43
44 GameObject room;
45 Renderer[] wallRenderer = new Renderer[6];
46
47 float[] dims = new float[3] { 1.0f, 1.0f, 1.0f };
48 float[] coefs = new float[6];
49
50 const int HIT_COUNT = 2048;
51
52 Vector3[] points = new Vector3[HIT_COUNT];
53 Vector3[] normals = new Vector3[HIT_COUNT];
54
55 ParticleSystem sys;
56 ParticleSystem.Particle[] particles = new ParticleSystem.Particle[HIT_COUNT];
57
58
59 static LayerMask gLayerMask = -1;
60 static Vector3 swapHandedness(Vector3 vec) { return new Vector3(vec.x, vec.y, -vec.z); }
61 static void AudioRaycast(Vector3 origin, Vector3 direction, out Vector3 point, out Vector3 normal, System.IntPtr data)
62 {
63 point = Vector3.zero;
64 normal = Vector3.zero;
65
66 RaycastHit hitInfo;
67 if (Physics.Raycast(swapHandedness(origin), swapHandedness(direction), out hitInfo, 1000.0f, gLayerMask.value))
68 {
69 point = swapHandedness(hitInfo.point);
70 normal = swapHandedness(hitInfo.normal);
71 }
72 }
73
74 void Start()
75 {
76 _raycastCallback = new AudioRaycastCallback(AudioRaycast);
77 OSP_Unity_AssignRaycastCallback(_raycastCallback, System.IntPtr.Zero);
78 }
79
80 void OnDestroy()
81 {
82 OSP_Unity_AssignRaycastCallback(System.IntPtr.Zero, System.IntPtr.Zero);
83 }
84
85 void Update()
86 {
87 if (dynamicReflectionsEnabled)
88 {
89 OSP_Unity_AssignRaycastCallback(_raycastCallback, System.IntPtr.Zero);
90 }
91 else
92 {
93 OSP_Unity_AssignRaycastCallback(System.IntPtr.Zero, System.IntPtr.Zero);
94 }
95
96 OSP_Unity_SetDynamicRoomRaysPerSecond(raysPerSecond);
97 OSP_Unity_SetDynamicRoomInterpSpeed(roomInterpSpeed);
98 OSP_Unity_SetDynamicRoomMaxWallDistance(maxWallDistance);
99 OSP_Unity_SetDynamicRoomRaysRayCacheSize(rayCacheSize);
100
101 gLayerMask = layerMask;
102 OSP_Unity_UpdateRoomModel(1.0f);
103
104 if (visualizeRoom)
105 {
106 if (!roomVisualizationInitialized)
107 {
108 inititalizeRoomVisualization();
109 roomVisualizationInitialized = true;
110 }
111
112 Vector3 pos;
113 OSP_Unity_GetRoomDimensions(dims, coefs, out pos);
114
115 pos.z *= -1; // swap to left-handed
116
117 var size = new Vector3(dims[0], dims[1], dims[2]);
118
119 float magSqrd = size.sqrMagnitude;
120
121 if (!float.IsNaN(magSqrd) && 0.0f < magSqrd && magSqrd < 1000000.0f)
122 {
123 transform.localScale = size * 0.999f;
124 }
125
126 transform.position = pos;
127
128 OSP_Unity_GetRaycastHits(points, normals, HIT_COUNT);
129
130 for (int i = 0; i < HIT_COUNT; ++i)
131 {
132 if (points[i] == Vector3.zero)
133 points[i].y = -10000.0f; // hide it
134
135 // swap to left-handed
136 points[i].z *= -1;
137 normals[i].z *= -1;
138
139 particles[i].position = points[i] + normals[i] * particleOffset;
140
141 if (normals[i] != Vector3.zero)
142 particles[i].rotation3D = Quaternion.LookRotation(normals[i]).eulerAngles;
143
144 particles[i].startSize = particleSize;
145 particles[i].startColor = new Color(208 / 255f, 38 / 255f, 174 / 255f, 1.0f);
146 }
147
148 for (int wall = 0; wall < 6; ++wall)
149 {
150 var color = Color.Lerp(Color.red, Color.green, coefs[wall]);
151 wallRenderer[wall].material.SetColor("_TintColor", color);
152 }
153
154 sys.SetParticles(particles, particles.Length);
155 }
156 }
157
158 private void inititalizeRoomVisualization()
159 {
160 Debug.Log("Oculus Audio dynamic room estimation visualization enabled");
161 transform.position = Vector3.zero; // move to the origin otherwise things are displaced
162
163 // Create a particle system to visualize the ray cast hits
164 GameObject decalManager = new GameObject("DecalManager");
165 decalManager.transform.parent = transform;
166 sys = decalManager.AddComponent<ParticleSystem>();
167 {
168 var main = sys.main;
169 main.simulationSpace = ParticleSystemSimulationSpace.World;
170 main.loop = false;
171 main.playOnAwake = false;
172 var emission = sys.emission;
173 emission.enabled = false;
174 var shape = sys.shape;
175 shape.enabled = false;
176 var renderer = sys.GetComponent<ParticleSystemRenderer>();
177 renderer.renderMode = ParticleSystemRenderMode.Mesh;
178 renderer.material.shader = Shader.Find("Particles/Additive");
179
180 Texture2D decalTex;
181 {
182 const int SIZE = 64;
183 const int RING_COUNT = 2;
184
185 decalTex = new Texture2D(SIZE, SIZE);
186 const int HALF_SIZE = SIZE / 2;
187 for (int i = 0; i < SIZE / 2; ++i)
188 {
189 for (int j = 0; j < SIZE / 2; ++j)
190 {
191 // distance from center
192 float deltaX = (float)(HALF_SIZE - i);
193 float deltaY = (float)(HALF_SIZE - j);
194 float dist = Mathf.Sqrt((deltaX * deltaX) + (deltaY * deltaY));
195 float t = (RING_COUNT * dist) / HALF_SIZE;
196
197 float alpha = (dist < HALF_SIZE) ? Mathf.Clamp01(Mathf.Sin(Mathf.PI * 2.0f * t)) : 0.0f;
198 Color col = new Color(1.0f, 1.0f, 1.0f, alpha);
199
200 // Two way symmetry
201 decalTex.SetPixel(i, j, col);
202 decalTex.SetPixel(SIZE - i, j, col);
203 decalTex.SetPixel(i, SIZE - j, col);
204 decalTex.SetPixel(SIZE - i, SIZE - j, col);
205 }
206 }
207
208 decalTex.Apply();
209 }
210
211 renderer.material.mainTexture = decalTex;
212 // Make a quad
213 var m = new Mesh();
214 m.name = "ParticleQuad";
215 const float size = 0.5f;
216 m.vertices = new Vector3[] {
217 new Vector3(-size, -size, 0.0f),
218 new Vector3( size, -size, 0.0f),
219 new Vector3( size, size, 0.0f),
220 new Vector3(-size, size, 0.0f)
221 };
222 m.uv = new Vector2[] {
223 new Vector2(0, 0),
224 new Vector2(0, 1),
225 new Vector2(1, 1),
226 new Vector2(1, 0)
227 };
228 m.triangles = new int[] { 0, 1, 2, 0, 2, 3 };
229 m.RecalculateNormals();
230 renderer.mesh = m;
231
232 }
233 sys.Emit(HIT_COUNT);
234
235 // Construct the visual representation of the room
236 room = new GameObject("RoomVisualizer");
237 room.transform.parent = transform;
238 room.transform.localPosition = Vector3.zero;
239
240 Texture2D wallTex;
241 {
242 const int SIZE = 32;
243 wallTex = new Texture2D(SIZE, SIZE);
244
245 Color transparent = new Color(0.0f, 0.0f, 0.0f, 0.0f);
246 for (int i = 0; i < SIZE; ++i)
247 {
248 for (int j = 0; j < SIZE; ++j)
249 {
250 wallTex.SetPixel(i, j, transparent);
251 }
252 }
253
254 for (int i = 0; i < SIZE; ++i)
255 {
256 Color color1 = Color.white * 0.125f;
257
258 wallTex.SetPixel(SIZE / 4, i, color1);
259 wallTex.SetPixel(i, SIZE / 4, color1);
260
261 wallTex.SetPixel(3 * SIZE / 4, i, color1);
262 wallTex.SetPixel(i, 3 * SIZE / 4, color1);
263
264 color1 *= 2.0f;
265
266 wallTex.SetPixel(SIZE / 2, i, color1);
267 wallTex.SetPixel(i, SIZE / 2, color1);
268
269 color1 *= 2.0f;
270
271 wallTex.SetPixel(0, i, color1);
272 wallTex.SetPixel(i, 0, color1);
273 }
274 wallTex.Apply();
275 }
276
277 for (int wall = 0; wall < 6; ++wall)
278 {
279 var m = new Mesh();
280 m.name = "Plane" + wall;
281 const float size = 0.5f;
282 var verts = new Vector3[4];
283
284 int axis = wall / 2;
285 int sign = (wall % 2 == 0) ? 1 : -1;
286
287 for (int i = 0; i < 4; ++i)
288 {
289 verts[i][axis] = sign * size;
290 verts[i][(axis + 1) % 3] = size * ((i == 1 || i == 2) ? 1 : -1);
291 verts[i][(axis + 2) % 3] = size * ((i == 2 || i == 3) ? 1 : -1);
292 }
293
294 m.vertices = verts;
295
296 m.uv = new Vector2[]
297 {
298 new Vector2(0, 0),
299 new Vector2(0, 1),
300 new Vector2(1, 1),
301 new Vector2(1, 0)
302 };
303
304 m.triangles = new int[] { 0, 1, 2, 0, 2, 3 };
305 m.RecalculateNormals();
306 var go = new GameObject("Wall_" + wall);
307 go.AddComponent<MeshFilter>().mesh = m;
308 var renderer = go.AddComponent<MeshRenderer>();
309 wallRenderer[wall] = renderer;
310 renderer.material.shader = Shader.Find("Particles/Additive");
311 renderer.material.mainTexture = wallTex;
312 renderer.material.mainTextureScale = new Vector2(8, 8);
313 go.transform.parent = room.transform;
314 room.transform.localPosition = Vector3.zero;
315 }
316 }
317
318 // * * * * * * * * * * * * *
319 // Import functions
320 public delegate void AudioRaycastCallback(Vector3 origin, Vector3 direction,
321 out Vector3 point, out Vector3 normal,
322 System.IntPtr data);
323
324 private const string strOSP = "AudioPluginOculusSpatializer";
325
326 [DllImport(strOSP)]
327 private static extern int OSP_Unity_AssignRaycastCallback(System.MulticastDelegate callback, System.IntPtr data);
328 [DllImport(strOSP)]
329 private static extern int OSP_Unity_AssignRaycastCallback(System.IntPtr callback, System.IntPtr data);
330
331 [DllImport(strOSP)]
332 private static extern int OSP_Unity_SetDynamicRoomRaysPerSecond(int RaysPerSecond);
333 [DllImport(strOSP)]
334 private static extern int OSP_Unity_SetDynamicRoomInterpSpeed(float InterpSpeed);
335 [DllImport(strOSP)]
336 private static extern int OSP_Unity_SetDynamicRoomMaxWallDistance(float MaxWallDistance);
337 [DllImport(strOSP)]
338 private static extern int OSP_Unity_SetDynamicRoomRaysRayCacheSize(int RayCacheSize);
339 [DllImport(strOSP)]
340 private static extern int OSP_Unity_UpdateRoomModel(float wetLevel); // call from main thread!!
341 [DllImport(strOSP)]
342 private static extern int OSP_Unity_GetRoomDimensions(float[] roomDimensions, float[] reflectionsCoefs, out Vector3 position);
343 [DllImport(strOSP)]
344 private static extern int OSP_Unity_GetRaycastHits(Vector3[] points, Vector3[] normals, int length);
345}