/// @endcond private void DrawDirectivityPattern(int size) { directivityTexture.Resize(size, size); // Draw the axes. Color axisColor = 0.5f * Color.black; for (int i = 0; i < size; ++i) { directivityTexture.SetPixel(i, size / 2, axisColor); directivityTexture.SetPixel(size / 2, i, axisColor); } // Draw the 2D polar directivity pattern. Color cardioidColor = 0.75f * Color.blue; float offset = 0.5f * size; float cardioidSize = 0.45f * size; Vector2[] vertices = GvrAudio.Generate2dPolarPattern(directivityAlpha.floatValue, directivitySharpness.floatValue, 180); for (int i = 0; i < vertices.Length; ++i) { directivityTexture.SetPixel((int)(offset + cardioidSize * vertices[i].x), (int)(offset + cardioidSize * vertices[i].y), cardioidColor); } directivityTexture.Apply(); // Show the texture. GUILayout.Box(directivityTexture); }
// Draws a 3D gizmo in the Scene View that shows the selected directivity pattern. private void DrawDirectivityGizmo(Transform target, float alpha, float sharpness, int resolution) { Vector2[] points = GvrAudio.Generate2dPolarPattern(alpha, sharpness, resolution); // Compute |vertices| from the polar pattern |points|. int numVertices = resolution + 1; Vector3[] vertices = new Vector3[numVertices]; vertices[0] = Vector3.zero; for (int i = 0; i < points.Length; ++i) { vertices[i + 1] = new Vector3(points[i].x, 0.0f, points[i].y); } // Generate |triangles| from |vertices|. Two triangles per each sweep to avoid backface culling. int[] triangles = new int[6 * numVertices]; for (int i = 0; i < numVertices - 1; ++i) { int index = 6 * i; if (i < numVertices - 2) { triangles[index] = 0; triangles[index + 1] = i + 1; triangles[index + 2] = i + 2; } else { // Last vertex is connected back to the first for the last triangle. triangles[index] = 0; triangles[index + 1] = numVertices - 1; triangles[index + 2] = 1; } // The second triangle facing the opposite direction. triangles[index + 3] = triangles[index]; triangles[index + 4] = triangles[index + 2]; triangles[index + 5] = triangles[index + 1]; } // Construct a new mesh for the gizmo. Mesh directivityGizmoMesh = new Mesh(); directivityGizmoMesh.hideFlags = HideFlags.DontSaveInEditor; directivityGizmoMesh.vertices = vertices; directivityGizmoMesh.triangles = triangles; directivityGizmoMesh.RecalculateNormals(); // Draw the mesh. Vector3 scale = 2.0f * Mathf.Max(target.lossyScale.x, target.lossyScale.z) * Vector3.one; Gizmos.DrawMesh(directivityGizmoMesh, target.position, target.rotation, scale); }