Overte C++ Documentation
graphics-scripting/src/graphics-scripting/Forward.h
1 //
2 // Copyright 2013-2019 High Fidelity, Inc.
3 // Copyright 2019-2021 Vircadia contributors
4 // Copyright 2023 Overte e.V.
5 //
6 // Distributed under the Apache License, Version 2.0.
7 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
8 // SPDX-License-Identifier: Apache-2.0
9 //
10 
11 #pragma once
12 
13 #include <QtCore/QObject>
14 #include <QtCore/QVector>
15 #include <QtCore/QVariant>
16 #include <QtCore/QUuid>
17 #include <QPointer>
18 #include <memory>
19 #include <unordered_map>
20 
21 #include <DependencyManager.h>
22 #include <SpatiallyNestable.h>
23 
24 #include "graphics/Material.h"
25 #include "graphics/TextureMap.h"
26 
27 namespace graphics {
28  class Mesh;
29 }
30 class Model;
31 using ModelPointer = std::shared_ptr<Model>;
32 namespace gpu {
33  class BufferView;
34 }
35 
36 namespace scriptable {
37  using Mesh = graphics::Mesh;
38  using MeshPointer = std::shared_ptr<scriptable::Mesh>;
39  using WeakMeshPointer = std::weak_ptr<scriptable::Mesh>;
40 
41  class ScriptableModelBase;
42  using ScriptableModelBasePointer = QPointer<ScriptableModelBase>;
43 
44  class ModelProvider;
45  using ModelProviderPointer = std::shared_ptr<scriptable::ModelProvider>;
46  using WeakModelProviderPointer = std::weak_ptr<scriptable::ModelProvider>;
47 
48  class ScriptableMaterial {
49  public:
50  ScriptableMaterial() {}
51  ScriptableMaterial(const graphics::MaterialPointer& material);
52  ScriptableMaterial(const ScriptableMaterial& material) { *this = material; }
53  ScriptableMaterial& operator=(const ScriptableMaterial& material);
54 
55  QString name { "" };
56  QString model { "" };
57  float opacity { 0.0f };
58  float roughness { 0.0f };
59  float metallic { 0.0f };
60  float scattering { 0.0f };
61  bool unlit { false };
62  glm::vec3 emissive { 0.0f };
63  glm::vec3 albedo { 0.0f };
64  QString emissiveMap { "" };
65  QString albedoMap { "" };
66  QString opacityMap { "" };
67  QString opacityMapMode { "" };
68  float opacityCutoff { 0.0f };
69  QString metallicMap { "" };
70  QString specularMap { "" };
71  QString roughnessMap { "" };
72  QString glossMap { "" };
73  QString normalMap { "" };
74  QString bumpMap { "" };
75  QString occlusionMap { "" };
76  QString lightMap { "" };
77  QString scatteringMap { "" };
78  std::array<glm::mat4, graphics::Material::NUM_TEXCOORD_TRANSFORMS> texCoordTransforms;
79  QString cullFaceMode { "" };
80  bool defaultFallthrough { false };
81  std::unordered_map<uint, bool> propertyFallthroughs; // not actually exposed to script
82 
83  QString procedural { "" };
84 
85  glm::vec3 shade { 0.0f };
86  QString shadeMap { "" };
87  float shadingShift { 0.0f };
88  QString shadingShiftMap { "" };
89  float shadingToony { 0.0f };
90  glm::vec3 matcap { 0.0f };
91  QString matcapMap { "" };
92  glm::vec3 parametricRim { 0.0f };
93  float parametricRimFresnelPower { 0.0f };
94  float parametricRimLift { 0.0f };
95  QString rimMap { "" };
96  float rimLightingMix { 0.0f };
97  QString outlineWidthMode { "" };
98  float outlineWidth { 0.0f };
99  glm::vec3 outline { 0.0f };
100  QString uvAnimationMaskMap { "" };
101  float uvAnimationScrollXSpeed { 0.0f };
102  float uvAnimationScrollYSpeed { 0.0f };
103  float uvAnimationRotationSpeed { 0.0f };
104 
105  graphics::MaterialKey key { 0 };
106  };
107 
108  /*@jsdoc
109  * A material layer.
110  * @typedef {object} Graphics.MaterialLayer
111  * @property {Entities.Material} material - The layer's material.
112  * @property {number} priority - The priority of the layer. If multiple materials are applied to a mesh part, only the
113  * layer with the highest priority is applied, with materials of the same priority randomly assigned.
114  */
115  class ScriptableMaterialLayer {
116  public:
117  ScriptableMaterialLayer() {}
118  ScriptableMaterialLayer(const graphics::MaterialLayer& materialLayer) : material(materialLayer.material), priority(materialLayer.priority) {}
119  ScriptableMaterialLayer(const ScriptableMaterialLayer& materialLayer) { *this = materialLayer; }
120  ScriptableMaterialLayer& operator=(const ScriptableMaterialLayer& materialLayer);
121 
122  ScriptableMaterial material;
123  quint16 priority;
124  };
125  typedef QHash<QString, QVector<scriptable::ScriptableMaterialLayer>> MultiMaterialMap;
126 
127  class ScriptableMeshBase : public QObject {
128  Q_OBJECT
129  public:
130  WeakModelProviderPointer provider;
131  ScriptableModelBasePointer model;
132  WeakMeshPointer weakMesh;
133  MeshPointer strongMesh;
134  ScriptableMeshBase(WeakModelProviderPointer provider, ScriptableModelBasePointer model, WeakMeshPointer weakMesh, QObject* parent);
135  ScriptableMeshBase(WeakMeshPointer weakMesh = WeakMeshPointer(), QObject* parent = nullptr);
136  ScriptableMeshBase(const ScriptableMeshBase& other, QObject* parent = nullptr) : QObject(parent) { *this = other; }
137  ScriptableMeshBase& operator=(const ScriptableMeshBase& view);
138  virtual ~ScriptableMeshBase();
139 
140  /*@jsdoc
141  * @function GraphicsMesh.getMeshPointer
142  * @deprecated This method is deprecated and will be removed.
143  * @returns {undefined}
144  */
145  // scriptable::MeshPointer is not registered as a JavaScript type.
146  Q_INVOKABLE const scriptable::MeshPointer getMeshPointer() const { return weakMesh.lock(); }
147 
148  /*@jsdoc
149  * @function GraphicsMesh.getModelProviderPointer
150  * @deprecated This method is deprecated and will be removed.
151  * @returns {undefined}
152  */
153  // scriptable::ModelProviderPointer is not registered as a JavaScript type.
154  Q_INVOKABLE const scriptable::ModelProviderPointer getModelProviderPointer() const { return provider.lock(); }
155 
156  /*@jsdoc
157  * @function GraphicsMesh.getModelBasePointer
158  * @deprecated This method is deprecated and will be removed.
159  * @returns {undefined}
160  */
161  // scriptable::ScriptableModelBasePointer is not registered as a JavaScript type.
162  Q_INVOKABLE const scriptable::ScriptableModelBasePointer getModelBasePointer() const { return model; }
163  };
164 
165  // abstract container for holding one or more references to mesh pointers
166  class ScriptableModelBase : public QObject {
167  Q_OBJECT
168  public:
169  WeakModelProviderPointer provider;
170  QUuid objectID; // spatially nestable ID
171  QVector<scriptable::ScriptableMeshBase> meshes;
172  MultiMaterialMap materialLayers;
173  QVector<QString> materialNames;
174 
175  ScriptableModelBase(QObject* parent = nullptr) : QObject(parent) {}
176  ScriptableModelBase(const ScriptableModelBase& other) : QObject(other.parent()) { *this = other; }
177  ScriptableModelBase& operator=(const ScriptableModelBase& other);
178  virtual ~ScriptableModelBase();
179 
180  void append(const ScriptableMeshBase& mesh);
181  void append(scriptable::WeakMeshPointer mesh);
182  void appendMaterial(const graphics::MaterialLayer& materialLayer, int shapeID, std::string materialName);
183  void appendMaterials(const std::unordered_map<std::string, graphics::MultiMaterial>& materialsToAppend);
184  void appendMaterialNames(const std::vector<std::string>& names);
185  // TODO: in future containers for these could go here
186  // QVariantMap shapes;
187  // QVariantMap armature;
188  };
189 
190  // mixin class for Avatar + Entity Rendering that expose their in-memory graphics::Meshes
191  class ModelProvider {
192  public:
193  NestableType modelProviderType;
194  virtual scriptable::ScriptableModelBase getScriptableModel() = 0;
195  virtual bool canReplaceModelMeshPart(int meshIndex, int partIndex) { return false; }
196  virtual bool replaceScriptableModelMeshPart(scriptable::ScriptableModelBasePointer model, int meshIndex, int partIndex) { return false; }
197  };
198 
199  // mixin class for resolving UUIDs into a corresponding ModelProvider
200  class ModelProviderFactory : public QObject, public Dependency {
201  Q_OBJECT
202  public:
203  virtual scriptable::ModelProviderPointer lookupModelProvider(const QUuid& uuid) = 0;
204  signals:
205  void modelAddedToScene(const QUuid& objectID, NestableType nestableType, const ModelPointer& sender);
206  void modelRemovedFromScene(const QUuid& objectID, NestableType nestableType, const ModelPointer& sender);
207  };
208 
209  class ScriptableModel;
210  using ScriptableModelPointer = QPointer<ScriptableModel>;
211  class ScriptableMesh;
212  using ScriptableMeshPointer = QPointer<ScriptableMesh>;
213  class ScriptableMeshPart;
214  using ScriptableMeshPartPointer = QPointer<ScriptableMeshPart>;
215 }
A generic 3D model displaying geometry loaded from a URL.
Definition: Model.h:84