1#include "BasicTerrainComponent.h"
6#include "Resources/MaterialManager.h"
7#include "Resources/TextureManager.h"
8#include "Resources/Texture.h"
10#include "Systems/Geometry/AdaptivePlanarGridSystem.h"
12#include "Foundation/ComponentModel/Attributes.h"
22 instance->setVariant(
"HasNoData", terrainComp->noDataEnabled);
23 switch (terrainComp->elevationChannel) {
24 case BasicTerrainElevationChannel::Red:
25 instance->setVariant(
"ElevationChannel",
"Red");
27 case BasicTerrainElevationChannel::Alpha:
28 instance->setVariant(
"ElevationChannel",
"Alpha");
30 case BasicTerrainElevationChannel::RedGreenBlue:
31 instance->setVariant(
"ElevationChannel",
"RedGreenBlue");
34 assert(
false &&
"Unhandled case.");
36 if (terrainComp->colorMap) {
37 switch (terrainComp->transparencyMode) {
38 case BasicTerrainTransparencyMode::None:
40 instance->setVariant(
"Color",
"TextureRGB");
43 case BasicTerrainTransparencyMode::Regular:
46 instance->setVariant(
"Color",
"TextureRGBA");
48 case BasicTerrainTransparencyMode::BlendWithZWrite:
50 instance->setVariant(
"Color",
"TextureRGBA");
54 assert(
false &&
"Unhandled case.");
58 instance->setVariant(
"Color",
"Solid");
60 instance->setVariant(
"ElevationToAlpha", terrainComp->elevationToAlpha);
62 instance->
setVec4Property(terrainComp->elevationTransformKey, terrainComp->elevationTransform);
63 instance->
setVec2Property(terrainComp->normalTransformKey, terrainComp->normalTransform);
64 instance->
setVec3Property(terrainComp->elevationToAlphaTransformKey, terrainComp->elevationToAlphaTransform);
69 instance->
setVec4Property(terrainComp->diffuseColorKey, terrainComp->diffuseColor);
70 instance->
setFloatProperty(terrainComp->noDataValueKey, terrainComp->noDataValue);
71 instance->
setFloatProperty(terrainComp->vibranceKey, glm::clamp(2.f-terrainComp->vibrance, 0.001f, 2.f));
78void Cogs::Core::BasicTerrainComponent::registerType()
86 TypeDatabase::createType<BasicTerrainElevationChannel>().setEnumerators(enumerators);
94 TypeDatabase::createType<BasicTerrainTransparencyMode>().setEnumerators(enumerators);
98 Field(
Name(
"elevationChannel"), &BasicTerrainComponent::elevationChannel),
99 Field(
Name(
"elevationMap"), &BasicTerrainComponent::elevationMap),
101 Field(
Name(
"colorMap"), &BasicTerrainComponent::colorMap),
113 Field(
Name(
"elevationToAlpha"), &BasicTerrainComponent::elevationToAlpha),
114 Field(
Name(
"elevationToAlphaMin"), &BasicTerrainComponent::elevationToAlphaMin),
115 Field(
Name(
"elevationToAlphaMax"), &BasicTerrainComponent::elevationToAlphaMax),
116 Field(
Name(
"elevationToAlphaPower"), &BasicTerrainComponent::elevationToAlphaPower)
118 Field(
Name(
"cullMode"), &BasicTerrainComponent::cullMode)
122 Method(
Name(
"initialize"), &BasicTerrainComponent::initialize),
123 Method(
Name(
"update"), &BasicTerrainComponent::update),
126 DynamicComponent::registerDerivedType<BasicTerrainComponent>()
128 .setMethods(methods);
131void Cogs::Core::BasicTerrainComponent::initialize(
Context * context)
133 this->context = context;
135 material = context->materialManager->loadMaterial(
"BasicTerrainMaterial.material");
137 context->materialManager->processLoading();
139 elevationTexKey = material->getTextureKey(
"Elevation");
140 colorTexKey = material->getTextureKey(
"Color");
141 diffuseColorKey = material->getVec4Key(
"diffuseColor");
142 elevationTransformKey = material->getVec2Key(
"elevationTransform");
143 normalTransformKey = material->getVec2Key(
"normalTransform");
144 noDataValueKey = material->getFloatKey(
"noDataValue");
145 vibranceKey = material->getFloatKey(
"vibrance");
146 elevationToAlphaTransformKey = material->getFloatKey(
"elevationToAlphaTransform");
148 auto gridComp = getComponent<AdaptivePlanarGridComponent>();
150 context->adaptivePlanarGridSystem->registerMaterial(gridComp, material,
nullptr,
nullptr);
152 gridComp->setChanged();
155void Cogs::Core::BasicTerrainComponent::update()
157 auto * gridComp = getComponent<AdaptivePlanarGridComponent>();
158 if (!gridComp)
return;
160 if (hasChanged() || gridComp->hasChanged()) {
165 if (
auto flags = elevationMap->getFlags(); elevationFlags != flags) {
166 elevationFlags = flags;
171 if (forceUpdate && elevationMap) {
174 material->options.cullMode = cullMode;
176 glm::vec2 extent = glm::vec2(gridComp->extentMax - gridComp->extentMin);
177 if (elevationMapNormalized) {
178 float scale_z = elevationMax - elevationMin;
179 elevationTransform = glm::vec4(scale_z,
181 1.f / elevationMap->description.width,
182 1.f / elevationMap->description.height);
183 normalTransform = glm::vec2(2.f * extent.x / (elevationMap->description.width * scale_z),
184 2.f * extent.y / (elevationMap->description.height * scale_z));
185 elevationToAlphaTransform = glm::vec3((elevationToAlphaMin - elevationMin) / scale_z,
186 scale_z / (elevationToAlphaMax - elevationToAlphaMin),
187 elevationToAlphaPower);
190 elevationTransform = glm::vec4(elevationScale,
192 1.f / elevationMap->description.width,
193 1.f / elevationMap->description.height);
194 normalTransform = glm::vec2(2.f * extent.x / (elevationMap->description.width * elevationScale),
195 2.f * extent.y / (elevationMap->description.height * elevationScale));
196 elevationToAlphaTransform = glm::vec3((elevationToAlphaMin - elevationBias) / elevationScale,
197 elevationScale / (elevationToAlphaMax - elevationToAlphaMin),
198 elevationToAlphaPower);
200 gridComp->displaceMin = glm::vec3(0.f, 0.f, elevationMin);
201 gridComp->displaceMax = glm::vec3(0.f, 0.f, elevationMax);
202 context->adaptivePlanarGridSystem->registerMaterial(gridComp, material, initMaterialInstanceCallback,
this);
203 gridComp->setChanged();
Container for components, providing composition of dynamic entities.
BasicTerrainTransparencyMode transparencyMode
bool elevationMapNormalized
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Field definition describing a single data member of a data structure.
Field & add(T attribute)
Adds the given attribute.
Simple method definition.
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
@ None
No transparency, it is safe to store elevation in colormap's alpha channel.
@ BlendWithZWrite
Treat terrain as opaque, but enable blending with colormap's alpha channel.
@ Regular
Regular transparency, use colormap's alpha channel.
@ RedGreenBlue
Concatenate bits from red, green and blue channels.
@ Alpha
Fetch elevation from alpha channel of elevation texture.
@ Red
Fetch elevation from red channel of elevation texture.
Contains reflection support.
Tags an object as being able to represent a color.
Adds range information to an object.
Material instances represent a specialized Material combined with state for all its buffers and prope...
void setFloatProperty(const VariableKey key, float value)
Set the float property with the given key to value.
void setTransparent()
Set the material instance to transparent, indicating to the renderer that blending should be enabled ...
void setVec3Property(const VariableKey key, glm::vec3 value)
Set the vec3 property with the given key to value.
void setTextureAddressMode(const StringView &key, const StringView &addressMode)
Set texture address mode with textual name.
void setTextureProperty(const StringView &key, TextureHandle value)
Set the texture property with the given key to the texture resource held by value.
void setVec2Property(const VariableKey key, glm::vec2 value)
Set the vec2 property with the given key to value.
MaterialOptions options
Material rendering options used by this instance.
void setVec4Property(const VariableKey key, glm::vec4 value)
Set the vec4 property with the given key to value.
void setOpaque()
Set the material instance to opaque, indicating to the renderer that blending should be disabled for ...
BlendMode blendMode
Blend mode to use when rendering geometry with the material instance.
Represents an unique name.
@ Clamp
Texture coordinates are clamped to the [0, 1] range.