Torque3D/Engine/source/gui/controls/guiMaterialCtrl.cpp
JeffR c96d58ae34 - Updates the material and terrain material assets to utilize AssetRef and drop the old macros like how Image and ShapeAssets have been updated.
- Updates the various classes using materials to comply to the change.
- Also standardizes some getter names to follow the general convention image and shape used to keep things more consistent across the board to minimize usage friction.
- Shifts handling of fallback asset lookups for the thus-far converted classes to have them be loaded during core initialization rather than being loaded as part of other asset loading as that sometimes lead to nesting execution/stability errors. Explicit call now ensures the fallbacks are loaded before any other asset tries to load, so there's no room for confounding or having no fallback to work with.
2026-07-03 23:38:43 -05:00

180 lines
5.7 KiB
C++

//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// 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 "platform/platform.h"
#include "gui/controls/guiMaterialCtrl.h"
#include "materials/baseMatInstance.h"
#include "materials/materialManager.h"
#include "materials/sceneData.h"
#include "core/util/safeDelete.h"
#include "console/console.h"
#include "console/consoleTypes.h"
#include "console/engineAPI.h"
#include "gfx/gfxDevice.h"
#include "math/util/matrixSet.h"
#include "scene/sceneRenderState.h"
IMPLEMENT_CONOBJECT( GuiMaterialCtrl );
ConsoleDocClass( GuiMaterialCtrl,
"@brief Container for GuiMaterialPreview\n\n"
"Editor use only.\n\n"
"@internal"
);
GuiMaterialCtrl::GuiMaterialCtrl()
: mMaterialInst( NULL )
{
}
void GuiMaterialCtrl::initPersistFields()
{
docsURL;
addGroup( "Material" );
ADD_FIELD( "materialAsset", TypeMaterialAssetRef, Offset( mMaterialAssetRef, GuiMaterialCtrl ) ).doc( "Material asset to display in this control." );
endGroup( "Material" );
Parent::initPersistFields();
}
bool GuiMaterialCtrl::onWake()
{
if ( !Parent::onWake() )
return false;
setActive( true );
setMaterial( mMaterialAssetRef.assetId );
return true;
}
void GuiMaterialCtrl::onSleep()
{
SAFE_DELETE( mMaterialInst );
Parent::onSleep();
}
bool GuiMaterialCtrl::setMaterial( const String &materialName )
{
SAFE_DELETE( mMaterialInst );
StringTableEntry matId = StringTable->insert( materialName.c_str() );
if ( matId == StringTable->EmptyString() || AssetDatabase.isDeclaredAsset( matId ))
{
mMaterialAssetRef = matId;
}
else
{
StringTableEntry assetId = MaterialAsset::getAssetIdByMaterialName( matId );
if ( assetId != StringTable->EmptyString() )
mMaterialAssetRef = assetId;
}
if ( mMaterialAssetRef.notNull() && isAwake() )
mMaterialInst = MATMGR->createMatInstance(
mMaterialAssetRef.assetPtr->getMaterialName(),
getGFXVertexFormat<GFXVertexPCT>() );
return true;
}
void GuiMaterialCtrl::inspectPostApply()
{
Parent::inspectPostApply();
}
void GuiMaterialCtrl::onRender( Point2I offset, const RectI &updateRect )
{
Parent::onRender( offset, updateRect );
if ( !mMaterialInst )
return;
// Draw a quad with the material assigned
GFXVertexBufferHandle<GFXVertexPCT> verts( GFX, 4, GFXBufferTypeVolatile );
verts.lock();
F32 screenLeft = updateRect.point.x;
F32 screenRight = (updateRect.point.x + updateRect.extent.x);
F32 screenTop = updateRect.point.y;
F32 screenBottom = (updateRect.point.y + updateRect.extent.y);
const F32 fillConv = GFX->getFillConventionOffset();
verts[0].point.set( screenLeft - fillConv, screenTop - fillConv, 0.f );
verts[1].point.set( screenRight - fillConv, screenTop - fillConv, 0.f );
verts[2].point.set( screenLeft - fillConv, screenBottom - fillConv, 0.f );
verts[3].point.set( screenRight - fillConv, screenBottom - fillConv, 0.f );
verts[0].color = verts[1].color = verts[2].color = verts[3].color = ColorI( 255, 255, 255, 255 );
verts[0].texCoord.set( 0.0f, 0.0f );
verts[1].texCoord.set( 1.0f, 0.0f );
verts[2].texCoord.set( 0.0f, 1.0f );
verts[3].texCoord.set( 1.0f, 1.0f );
verts.unlock();
GFX->setVertexBuffer( verts );
MatrixSet matSet;
matSet.setWorld(GFX->getWorldMatrix());
matSet.setView(GFX->getViewMatrix());
matSet.setProjection(GFX->getProjectionMatrix());
MatrixF cameraMatrix( true );
F32 left, right, top, bottom, nearPlane, farPlane;
bool isOrtho;
GFX->getFrustum( &left, &right, &bottom, &top, &nearPlane, &farPlane, &isOrtho );
Frustum frust( isOrtho, left, right, top, bottom, nearPlane, farPlane, cameraMatrix );
SceneRenderState state
(
gClientSceneGraph,
SPT_Diffuse,
SceneCameraState( GFX->getViewport(), frust, GFX->getWorldMatrix(), GFX->getProjectionMatrix() ),
gClientSceneGraph->getDefaultRenderPass(),
false
);
SceneData sgData;
sgData.init( &state );
sgData.wireframe = false; // Don't wireframe this.
while( mMaterialInst->setupPass( &state, sgData ) )
{
mMaterialInst->setSceneInfo( &state, sgData );
mMaterialInst->setTransforms( matSet, &state );
GFX->drawPrimitive( GFXTriangleStrip, 0, 2 );
}
// Clean up
GFX->setShader( NULL );
GFX->setTexture( 0, NULL );
}
DefineEngineMethod( GuiMaterialCtrl, setMaterial, bool, ( const char * materialName ), , "( string materialName )"
"Set the material to be displayed in the control." )
{
return object->setMaterial( materialName );
}