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  uint8_t layers { 1 };
81  QString splatMap { "" };
82  bool defaultFallthrough { false };
83  std::unordered_map<uint, bool> propertyFallthroughs; // not actually exposed to script
84 
85  QString procedural { "" };
86 
87  glm::vec3 shade { 0.0f };
88  QString shadeMap { "" };
89  float shadingShift { 0.0f };
90  QString shadingShiftMap { "" };
91  float shadingToony { 0.0f };
92  glm::vec3 matcap { 0.0f };
93  QString matcapMap { "" };
94  glm::vec3 parametricRim { 0.0f };
95  float parametricRimFresnelPower { 0.0f };
96  float parametricRimLift { 0.0f };
97  QString rimMap { "" };
98  float rimLightingMix { 0.0f };
99  QString outlineWidthMode { "" };
100  float outlineWidth { 0.0f };
101  glm::vec3 outline { 0.0f };
102  QString uvAnimationMaskMap { "" };
103  float uvAnimationScrollXSpeed { 0.0f };
104  float uvAnimationScrollYSpeed { 0.0f };
105  float uvAnimationRotationSpeed { 0.0f };
106 
107  graphics::MaterialKey key { 0 };
108  };
109 
110  /*@jsdoc
111  * A material layer.
112  * @typedef {object} Graphics.MaterialLayer
113  * @property {Entities.Material} material - The layer's material.
114  * @property {number} priority - The priority of the layer. If multiple materials are applied to a mesh part, only the
115  * layer with the highest priority is applied, with materials of the same priority randomly assigned.
116  */
117  class ScriptableMaterialLayer {
118  public:
119  ScriptableMaterialLayer() {}
120  ScriptableMaterialLayer(const graphics::MaterialLayer& materialLayer) : material(materialLayer.material), priority(materialLayer.priority) {}
121  ScriptableMaterialLayer(const ScriptableMaterialLayer& materialLayer) { *this = materialLayer; }
122  ScriptableMaterialLayer& operator=(const ScriptableMaterialLayer& materialLayer);
123 
124  ScriptableMaterial material;
125  quint16 priority;
126  };
127  typedef QHash<QString, QVector<scriptable::ScriptableMaterialLayer>> MultiMaterialMap;
128 
129  class ScriptableMeshBase : public QObject {
130  Q_OBJECT
131  public:
132  WeakModelProviderPointer provider;
133  ScriptableModelBasePointer model;
134  WeakMeshPointer weakMesh;
135  MeshPointer strongMesh;
136  ScriptableMeshBase(WeakModelProviderPointer provider, ScriptableModelBasePointer model, WeakMeshPointer weakMesh, QObject* parent);
137  ScriptableMeshBase(WeakMeshPointer weakMesh = WeakMeshPointer(), QObject* parent = nullptr);
138  ScriptableMeshBase(const ScriptableMeshBase& other, QObject* parent = nullptr) : QObject(parent) { *this = other; }
139  ScriptableMeshBase& operator=(const ScriptableMeshBase& view);
140  virtual ~ScriptableMeshBase();
141 
142  /*@jsdoc
143  * @function GraphicsMesh.getMeshPointer
144  * @deprecated This method is deprecated and will be removed.
145  * @returns {undefined}
146  */
147  // scriptable::MeshPointer is not registered as a JavaScript type.
148  Q_INVOKABLE const scriptable::MeshPointer getMeshPointer() const { return weakMesh.lock(); }
149 
150  /*@jsdoc
151  * @function GraphicsMesh.getModelProviderPointer
152  * @deprecated This method is deprecated and will be removed.
153  * @returns {undefined}
154  */
155  // scriptable::ModelProviderPointer is not registered as a JavaScript type.
156  Q_INVOKABLE const scriptable::ModelProviderPointer getModelProviderPointer() const { return provider.lock(); }
157 
158  /*@jsdoc
159  * @function GraphicsMesh.getModelBasePointer
160  * @deprecated This method is deprecated and will be removed.
161  * @returns {undefined}
162  */
163  // scriptable::ScriptableModelBasePointer is not registered as a JavaScript type.
164  Q_INVOKABLE const scriptable::ScriptableModelBasePointer getModelBasePointer() const { return model; }
165  };
166 
167  // abstract container for holding one or more references to mesh pointers
168  class ScriptableModelBase : public QObject {
169  Q_OBJECT
170  public:
171  WeakModelProviderPointer provider;
172  QUuid objectID; // spatially nestable ID
173  QVector<scriptable::ScriptableMeshBase> meshes;
174  MultiMaterialMap materialLayers;
175  QVector<QString> materialNames;
176 
177  ScriptableModelBase(QObject* parent = nullptr) : QObject(parent) {}
178  ScriptableModelBase(const ScriptableModelBase& other) : QObject(other.parent()) { *this = other; }
179  ScriptableModelBase& operator=(const ScriptableModelBase& other);
180  virtual ~ScriptableModelBase();
181 
182  void append(const ScriptableMeshBase& mesh);
183  void append(scriptable::WeakMeshPointer mesh);
184  void appendMaterial(const graphics::MaterialLayer& materialLayer, int shapeID, std::string materialName);
185  void appendMaterials(const std::unordered_map<std::string, graphics::MultiMaterial>& materialsToAppend);
186  void appendMaterialNames(const std::vector<std::string>& names);
187  // TODO: in future containers for these could go here
188  // QVariantMap shapes;
189  // QVariantMap armature;
190  };
191 
192  // mixin class for Avatar + Entity Rendering that expose their in-memory graphics::Meshes
193  class ModelProvider {
194  public:
195  NestableType modelProviderType;
196  virtual scriptable::ScriptableModelBase getScriptableModel() = 0;
197  virtual bool canReplaceModelMeshPart(int meshIndex, int partIndex) { return false; }
198  virtual bool replaceScriptableModelMeshPart(scriptable::ScriptableModelBasePointer model, int meshIndex, int partIndex) { return false; }
199  };
200 
201  // mixin class for resolving UUIDs into a corresponding ModelProvider
202  class ModelProviderFactory : public QObject, public Dependency {
203  Q_OBJECT
204  public:
205  virtual scriptable::ModelProviderPointer lookupModelProvider(const QUuid& uuid) = 0;
206  signals:
207  void modelAddedToScene(const QUuid& objectID, NestableType nestableType, const ModelPointer& sender);
208  void modelRemovedFromScene(const QUuid& objectID, NestableType nestableType, const ModelPointer& sender);
209  };
210 
211  class ScriptableModel;
212  using ScriptableModelPointer = QPointer<ScriptableModel>;
213  class ScriptableMesh;
214  using ScriptableMeshPointer = QPointer<ScriptableMesh>;
215  class ScriptableMeshPart;
216  using ScriptableMeshPartPointer = QPointer<ScriptableMeshPart>;
217 }
A generic 3D model displaying geometry loaded from a URL.
Definition: Model.h:85