generated from rbfx/Core.SamplePlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSteamSoundMesh.cpp
More file actions
247 lines (215 loc) · 8.39 KB
/
SteamSoundMesh.cpp
File metadata and controls
247 lines (215 loc) · 8.39 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
//
// Copyright (c) 2024-2024 the Urho3D project.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#include "SteamSoundMesh.h"
#include "SteamAudio.h"
#include <Urho3D/Core/Context.h>
#include <Urho3D/Core/CoreEvents.h>
#include <Urho3D/Graphics/Geometry.h>
#include <Urho3D/Graphics/IndexBuffer.h>
#include <Urho3D/Graphics/Model.h>
#include <Urho3D/Graphics/VertexBuffer.h>
#include <Urho3D/Resource/ResourceCache.h>
#include <Urho3D/Scene/Node.h>
#include <phonon.h>
namespace Urho3D
{
static IPLMaterial materials[11] = {
{0.10f,0.20f,0.30f,0.05f,0.100f,0.050f,0.030f},
{0.03f,0.04f,0.07f,0.05f,0.015f,0.015f,0.015f},
{0.05f,0.07f,0.08f,0.05f,0.015f,0.002f,0.001f},
{0.01f,0.02f,0.02f,0.05f,0.060f,0.044f,0.011f},
{0.60f,0.70f,0.80f,0.05f,0.031f,0.012f,0.008f},
{0.24f,0.69f,0.73f,0.05f,0.020f,0.005f,0.003f},
{0.06f,0.03f,0.02f,0.05f,0.060f,0.044f,0.011f},
{0.12f,0.06f,0.04f,0.05f,0.056f,0.056f,0.004f},
{0.11f,0.07f,0.06f,0.05f,0.070f,0.014f,0.005f},
{0.20f,0.07f,0.06f,0.05f,0.200f,0.025f,0.010f},
{0.13f,0.20f,0.24f,0.05f,0.015f,0.002f,0.001f}
};
static const ea::vector<ea::string> materialNames = {
"Generic",
"Brick",
"Concrete",
"Ceramic",
"Gravel",
"Carpet",
"Glass",
"Plaster",
"Wood",
"Metal",
"Rock"
};
SteamSoundMesh::SteamSoundMesh(Context* context) :
Component(context), modelDirty_(false), mesh_(nullptr), subScene_(nullptr), materialIndex_(SteamSoundMaterial::generic)
{
audio_ = GetSubsystem<SteamAudio>();
material_ = &materials[static_cast<unsigned>(materialIndex_)];
if (audio_) {
// Create subscene
IPLSceneSettings sceneSettings {};
sceneSettings.type = IPL_SCENETYPE_DEFAULT;
iplSceneCreate(audio_->GetPhononContext(), &sceneSettings, &subScene_);
// Subscribe to render updates
SubscribeToEvent(E_RENDERUPDATE, URHO3D_HANDLER(SteamSoundMesh, HandleRenderUpdate));
}
}
SteamSoundMesh::~SteamSoundMesh()
{
if (audio_) {
// Reset model
ResetModel();
// Remove subscene and mesh
iplSceneRelease(&subScene_);
iplStaticMeshRelease(&mesh_);
}
}
void SteamSoundMesh::RegisterObject(Context* context)
{
context->AddFactoryReflection<SteamSoundMesh>(Category_Audio);
URHO3D_ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
URHO3D_MIXED_ACCESSOR_ATTRIBUTE("Model", GetModel, SetModel, ResourceRef, ResourceRef(Model::GetTypeStatic()), AM_DEFAULT);
URHO3D_ENUM_ACCESSOR_ATTRIBUTE("Material", GetMaterial, SetMaterial, SteamSoundMaterial, materialNames, SteamSoundMaterial::generic, AM_DEFAULT);
}
void SteamSoundMesh::SetModel(const ResourceRef& model)
{
auto* cache = GetSubsystem<ResourceCache>();
model_ = cache->GetResource<Model>(model.name_);
MarkModelDirty();
}
void SteamSoundMesh::SetMaterial(SteamSoundMaterial material) {
materialIndex_ = material;
material_ = &materials[static_cast<unsigned>(materialIndex_)];
MarkModelDirty();
}
ResourceRef SteamSoundMesh::GetModel() const
{
return GetResourceRef(model_, Model::GetTypeStatic());
}
void SteamSoundMesh::HandleRenderUpdate(StringHash eventType, VariantMap &eventData)
{
if (modelDirty_) {
ReloadModel();
modelDirty_ = false;
}
}
void SteamSoundMesh::OnNodeSet(Node *previousNode, Node *currentNode)
{
if (previousNode)
previousNode->RemoveListener(this);
if (currentNode)
currentNode->AddListener(this);
}
void SteamSoundMesh::OnMarkedDirty(Node *)
{
if (model_)
UpdateTransform();
}
void SteamSoundMesh::ReloadModel()
{
// Do nothing if no audio subsystem
if (!audio_)
return;
// Clear if no model set
if (!model_) {
ResetModel();
return;
} else if (mesh_) {
ResetModel();
}
// Extract points and indices
ea::vector<Vector3> allPoints;
ea::vector<unsigned> allIndices;
for (const auto& geometry : model_->GetGeometries()) {
for (const auto& geometry : geometry) {
for (const auto& vertexBuffer : geometry->GetVertexBuffers()) {
ea::vector<Vector4> points(vertexBuffer->GetVertexCount());
vertexBuffer->UnpackVertexData(vertexBuffer->GetShadowData(), vertexBuffer->GetVertexSize(), *vertexBuffer->GetElement(SEM_POSITION), 0, vertexBuffer->GetVertexCount(), points.data(), sizeof(Vector4));
for (const auto& point : points)
allPoints.push_back({point.x_, point.y_, point.z_});
}
const auto& indexBuffer = geometry->GetIndexBuffer();
const auto indices = indexBuffer->GetUnpackedData();
allIndices.insert(allIndices.begin(), indices.begin(), indices.end());
}
}
// Convert to phonon points and indices
ea::vector<IPLVector3> phononVertices;
phononVertices.reserve(allPoints.size());
ea::vector<IPLTriangle> phononTriangles;
phononTriangles.reserve(allIndices.size()/3);
for (const auto& point : allPoints)
phononVertices.push_back({point.x_, point.y_, point.z_});
for (unsigned idx = 0; idx < allIndices.size(); idx += 3)
phononTriangles.push_back({int(allIndices[idx+0]), int(allIndices[idx+1]), int(allIndices[idx+2])});
ea::vector<IPLint32> phononMaterialIndices(phononTriangles.size(), 0); // All triangles use the same material (for now)
// Create settings
IPLStaticMeshSettings staticMeshSettings {};
staticMeshSettings.numVertices = phononVertices.size();
staticMeshSettings.numTriangles = phononTriangles.size();
staticMeshSettings.numMaterials = 1;
staticMeshSettings.vertices = phononVertices.data();
staticMeshSettings.triangles = phononTriangles.data();
staticMeshSettings.materialIndices = phononMaterialIndices.data();
staticMeshSettings.materials = material_;
// Create static mesh
iplStaticMeshCreate(subScene_, &staticMeshSettings, &mesh_);
iplStaticMeshAdd(mesh_, subScene_);
iplSceneCommit(subScene_);
iplSceneSaveOBJ(subScene_, ("scene-"+ea::to_string(GetNode()->GetID())+".obj").c_str());
// Create instanced mesh
IPLInstancedMeshSettings instancedMeshSettings {};
instancedMeshSettings.subScene = subScene_;
instancedMeshSettings.transform = GetPhononMatrix();
iplInstancedMeshCreate(audio_->GetScene(), &instancedMeshSettings, &instancedMesh_);
iplInstancedMeshAdd(instancedMesh_, audio_->GetScene());
// Mark scene as dirty
audio_->MarkSceneDirty();
}
void SteamSoundMesh::ResetModel()
{
// Release ressources
iplInstancedMeshRemove(instancedMesh_, audio_->GetScene());
iplInstancedMeshRelease(&instancedMesh_);
iplStaticMeshRemove(mesh_, subScene_);
iplStaticMeshRelease(&mesh_);
mesh_ = nullptr;
// Mark scene as dirty
audio_->MarkSceneDirty();
}
void SteamSoundMesh::UpdateTransform()
{
iplInstancedMeshUpdateTransform(instancedMesh_, audio_->GetScene(), GetPhononMatrix());
audio_->MarkSceneDirty();
}
IPLMatrix4x4 SteamSoundMesh::GetPhononMatrix() const
{
const Matrix3x4 m = GetNode()->GetWorldTransform();
return {
{
{m.Element(0, 0), m.Element(0, 1), m.Element(0, 2), m.Element(0, 3)},
{m.Element(1, 0), m.Element(1, 1), m.Element(1, 2), m.Element(1, 3)},
{m.Element(2, 0), m.Element(2, 1), m.Element(2, 2), m.Element(2, 3)},
{0.0f, 0.0f, 0.0f, 1.0f }
}
};
}
}