mirror of
https://github.com/TorqueGameEngines/Torque3D.git
synced 2026-04-27 23:35:45 +00:00
Merge branch 'PBR_ProbeArrayGLWIP' of https://github.com/Azaezel/Torque3D into development
This commit is contained in:
commit
e83ec69292
407 changed files with 47737 additions and 6168 deletions
|
|
@ -97,7 +97,7 @@ LevelInfo::LevelInfo()
|
|||
|
||||
mNetFlags.set( ScopeAlways | Ghostable );
|
||||
|
||||
mAdvancedLightmapSupport = false;
|
||||
mAdvancedLightmapSupport = true;
|
||||
mAccuTextureName = "";
|
||||
mAccuTexture = NULL;
|
||||
|
||||
|
|
@ -163,8 +163,8 @@ void LevelInfo::initPersistFields()
|
|||
addField( "ambientLightBlendCurve", TypeEaseF, Offset( mAmbientLightBlendCurve, LevelInfo ),
|
||||
"Interpolation curve to use for blending from one ambient light color to a different one." );
|
||||
|
||||
addField( "advancedLightmapSupport", TypeBool, Offset( mAdvancedLightmapSupport, LevelInfo ),
|
||||
"Enable expanded support for mixing static and dynamic lighting (more costly)" );
|
||||
//addField( "advancedLightmapSupport", TypeBool, Offset( mAdvancedLightmapSupport, LevelInfo ),
|
||||
// "Enable expanded support for mixing static and dynamic lighting (more costly)" );
|
||||
|
||||
addProtectedField("AccuTexture", TypeStringFilename, Offset(mAccuTextureName, LevelInfo),
|
||||
&_setLevelAccuTexture, &defaultProtectedGetFn, "Accumulation texture.");
|
||||
|
|
|
|||
632
Engine/source/T3D/lighting/IBLUtilities.cpp
Normal file
632
Engine/source/T3D/lighting/IBLUtilities.cpp
Normal file
|
|
@ -0,0 +1,632 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// 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 "T3D/lighting/IBLUtilities.h"
|
||||
#include "console/engineAPI.h"
|
||||
#include "materials/shaderData.h"
|
||||
#include "gfx/gfxTextureManager.h"
|
||||
#include "gfx/gfxTransformSaver.h"
|
||||
#include "gfx/bitmap/cubemapSaver.h"
|
||||
#include "core/stream/fileStream.h"
|
||||
#include "gfx/bitmap/imageUtils.h"
|
||||
|
||||
namespace IBLUtilities
|
||||
{
|
||||
void GenerateIrradianceMap(GFXTextureTargetRef renderTarget, GFXCubemapHandle cubemap, GFXCubemapHandle &cubemapOut)
|
||||
{
|
||||
GFXTransformSaver saver;
|
||||
|
||||
GFXStateBlockRef irrStateBlock;
|
||||
|
||||
ShaderData *irrShaderData;
|
||||
GFXShaderRef irrShader = Sim::findObject("IrradianceShader", irrShaderData) ? irrShaderData->getShader() : NULL;
|
||||
if (!irrShader)
|
||||
{
|
||||
Con::errorf("IBLUtilities::GenerateIrradianceMap() - could not find IrradianceShader");
|
||||
return;
|
||||
}
|
||||
|
||||
GFXShaderConstBufferRef irrConsts = irrShader->allocConstBuffer();
|
||||
GFXShaderConstHandle* irrEnvMapSC = irrShader->getShaderConstHandle("$environmentMap");
|
||||
GFXShaderConstHandle* irrFaceSC = irrShader->getShaderConstHandle("$face");
|
||||
|
||||
GFXStateBlockDesc desc;
|
||||
desc.zEnable = false;
|
||||
desc.samplersDefined = true;
|
||||
desc.samplers[0].addressModeU = GFXAddressClamp;
|
||||
desc.samplers[0].addressModeV = GFXAddressClamp;
|
||||
desc.samplers[0].addressModeW = GFXAddressClamp;
|
||||
desc.samplers[0].magFilter = GFXTextureFilterLinear;
|
||||
desc.samplers[0].minFilter = GFXTextureFilterLinear;
|
||||
desc.samplers[0].mipFilter = GFXTextureFilterLinear;
|
||||
|
||||
irrStateBlock = GFX->createStateBlock(desc);
|
||||
|
||||
GFX->pushActiveRenderTarget();
|
||||
GFX->setShader(irrShader);
|
||||
GFX->setShaderConstBuffer(irrConsts);
|
||||
GFX->setStateBlock(irrStateBlock);
|
||||
GFX->setVertexBuffer(NULL);
|
||||
GFX->setCubeTexture(0, cubemap);
|
||||
|
||||
for (U32 i = 0; i < 6; i++)
|
||||
{
|
||||
renderTarget->attachTexture(GFXTextureTarget::Color0, cubemapOut, i);
|
||||
irrConsts->setSafe(irrFaceSC, (S32)i);
|
||||
GFX->setActiveRenderTarget(renderTarget);
|
||||
GFX->clear(GFXClearTarget, LinearColorF::BLACK, 1.0f, 0);
|
||||
GFX->drawPrimitive(GFXTriangleList, 0, 3);
|
||||
renderTarget->resolve();
|
||||
}
|
||||
|
||||
GFX->popActiveRenderTarget();
|
||||
}
|
||||
|
||||
void GenerateAndSaveIrradianceMap(String outputPath, S32 resolution, GFXCubemapHandle cubemap, GFXCubemapHandle &cubemapOut)
|
||||
{
|
||||
if (outputPath.isEmpty())
|
||||
{
|
||||
Con::errorf("IBLUtilities::GenerateAndSaveIrradianceMap - Cannot save to an empty path!");
|
||||
return;
|
||||
}
|
||||
|
||||
GFXTextureTargetRef renderTarget = GFX->allocRenderToTextureTarget(false);
|
||||
|
||||
IBLUtilities::GenerateIrradianceMap(renderTarget, cubemap, cubemapOut);
|
||||
|
||||
//Write it out
|
||||
CubemapSaver::save(cubemapOut, outputPath);
|
||||
|
||||
if (!Platform::isFile(outputPath))
|
||||
{
|
||||
Con::errorf("IBLUtilities::GenerateAndSaveIrradianceMap - Failed to properly save out the baked irradiance!");
|
||||
}
|
||||
}
|
||||
|
||||
void SaveCubeMap(String outputPath, GFXCubemapHandle &cubemap)
|
||||
{
|
||||
if (outputPath.isEmpty())
|
||||
{
|
||||
Con::errorf("IBLUtilities::SaveCubeMap - Cannot save to an empty path!");
|
||||
return;
|
||||
}
|
||||
|
||||
//Write it out
|
||||
CubemapSaver::save(cubemap, outputPath);
|
||||
|
||||
if (!Platform::isFile(outputPath))
|
||||
{
|
||||
Con::errorf("IBLUtilities::SaveCubeMap - Failed to properly save out the baked irradiance!");
|
||||
}
|
||||
}
|
||||
|
||||
void GeneratePrefilterMap(GFXTextureTargetRef renderTarget, GFXCubemapHandle cubemap, U32 mipLevels, GFXCubemapHandle &cubemapOut)
|
||||
{
|
||||
GFXTransformSaver saver;
|
||||
|
||||
ShaderData *prefilterShaderData;
|
||||
GFXShaderRef prefilterShader = Sim::findObject("PrefiterCubemapShader", prefilterShaderData) ? prefilterShaderData->getShader() : NULL;
|
||||
if (!prefilterShader)
|
||||
{
|
||||
Con::errorf("IBLUtilities::GeneratePrefilterMap() - could not find PrefiterCubemapShader");
|
||||
return;
|
||||
}
|
||||
|
||||
GFXShaderConstBufferRef prefilterConsts = prefilterShader->allocConstBuffer();
|
||||
GFXShaderConstHandle* prefilterEnvMapSC = prefilterShader->getShaderConstHandle("$environmentMap");
|
||||
GFXShaderConstHandle* prefilterFaceSC = prefilterShader->getShaderConstHandle("$face");
|
||||
GFXShaderConstHandle* prefilterRoughnessSC = prefilterShader->getShaderConstHandle("$roughness");
|
||||
GFXShaderConstHandle* prefilterMipSizeSC = prefilterShader->getShaderConstHandle("$mipSize");
|
||||
GFXShaderConstHandle* prefilterResolutionSC = prefilterShader->getShaderConstHandle("$resolution");
|
||||
|
||||
GFX->pushActiveRenderTarget();
|
||||
GFX->setShader(prefilterShader);
|
||||
GFX->setShaderConstBuffer(prefilterConsts);
|
||||
GFX->setCubeTexture(0, cubemap);
|
||||
|
||||
U32 prefilterSize = cubemapOut->getSize();
|
||||
|
||||
U32 resolutionSize = prefilterSize;
|
||||
|
||||
for (U32 face = 0; face < 6; face++)
|
||||
{
|
||||
prefilterConsts->setSafe(prefilterFaceSC, (S32)face);
|
||||
prefilterConsts->setSafe(prefilterResolutionSC, (S32)resolutionSize);
|
||||
|
||||
for (U32 mip = 0; mip < mipLevels; mip++)
|
||||
{
|
||||
S32 mipSize = prefilterSize >> mip;
|
||||
F32 roughness = (float)mip / (float)(mipLevels - 1);
|
||||
prefilterConsts->setSafe(prefilterRoughnessSC, roughness);
|
||||
prefilterConsts->setSafe(prefilterMipSizeSC, mipSize);
|
||||
U32 size = prefilterSize * mPow(0.5f, mip);
|
||||
renderTarget->attachTexture(GFXTextureTarget::Color0, cubemapOut, face, mip);
|
||||
GFX->setActiveRenderTarget(renderTarget, false);//we set the viewport ourselves
|
||||
GFX->setViewport(RectI(0, 0, size, size));
|
||||
GFX->clear(GFXClearTarget, LinearColorF::BLACK, 1.0f, 0);
|
||||
GFX->drawPrimitive(GFXTriangleList, 0, 3);
|
||||
renderTarget->resolve();
|
||||
}
|
||||
}
|
||||
|
||||
GFX->popActiveRenderTarget();
|
||||
}
|
||||
|
||||
void GenerateAndSavePrefilterMap(String outputPath, S32 resolution, GFXCubemapHandle cubemap, U32 mipLevels, GFXCubemapHandle &cubemapOut)
|
||||
{
|
||||
if (outputPath.isEmpty())
|
||||
{
|
||||
Con::errorf("IBLUtilities::GenerateAndSavePrefilterMap - Cannot save to an empty path!");
|
||||
return;
|
||||
}
|
||||
|
||||
GFXTextureTargetRef renderTarget = GFX->allocRenderToTextureTarget(false);
|
||||
|
||||
IBLUtilities::GeneratePrefilterMap(renderTarget, cubemap, mipLevels, cubemapOut);
|
||||
|
||||
//Write it out
|
||||
CubemapSaver::save(cubemapOut, outputPath);
|
||||
|
||||
if (!Platform::isFile(outputPath))
|
||||
{
|
||||
Con::errorf("IBLUtilities::GenerateAndSavePrefilterMap - Failed to properly save out the baked irradiance!");
|
||||
}
|
||||
}
|
||||
|
||||
void bakeReflection(String outputPath, S32 resolution)
|
||||
{
|
||||
//GFXDEBUGEVENT_SCOPE(ReflectionProbe_Bake, ColorI::WHITE);
|
||||
|
||||
/*PostEffect *preCapture = dynamic_cast<PostEffect*>(Sim::findObject("AL_PreCapture"));
|
||||
PostEffect *deferredShading = dynamic_cast<PostEffect*>(Sim::findObject("AL_DeferredShading"));
|
||||
if (preCapture)
|
||||
preCapture->enable();
|
||||
if (deferredShading)
|
||||
deferredShading->disable();
|
||||
|
||||
//if (mReflectionModeType == StaticCubemap || mReflectionModeType == BakedCubemap || mReflectionModeType == SkyLight)
|
||||
{
|
||||
if (!mCubemap)
|
||||
{
|
||||
mCubemap = new CubemapData();
|
||||
mCubemap->registerObject();
|
||||
}
|
||||
}
|
||||
|
||||
if (mReflectionModeType == DynamicCubemap && mDynamicCubemap.isNull())
|
||||
{
|
||||
//mCubemap->createMap();
|
||||
mDynamicCubemap = GFX->createCubemap();
|
||||
mDynamicCubemap->initDynamic(resolution, GFXFormatR8G8B8);
|
||||
}
|
||||
else if (mReflectionModeType != DynamicCubemap)
|
||||
{
|
||||
if (mReflectionPath.isEmpty() || !mPersistentId)
|
||||
{
|
||||
if (!mPersistentId)
|
||||
mPersistentId = getOrCreatePersistentId();
|
||||
|
||||
mReflectionPath = outputPath.c_str();
|
||||
|
||||
mProbeUniqueID = std::to_string(mPersistentId->getUUID().getHash()).c_str();
|
||||
}
|
||||
}
|
||||
|
||||
bool validCubemap = true;
|
||||
|
||||
// Save the current transforms so we can restore
|
||||
// it for child control rendering below.
|
||||
GFXTransformSaver saver;
|
||||
|
||||
//bool saveEditingMission = gEditingMission;
|
||||
//gEditingMission = false;
|
||||
|
||||
//Set this to true to use the prior method where it goes through the SPT_Reflect path for the bake
|
||||
bool probeRenderState = ReflectionProbe::smRenderReflectionProbes;
|
||||
ReflectionProbe::smRenderReflectionProbes = false;
|
||||
for (U32 i = 0; i < 6; ++i)
|
||||
{
|
||||
GFXTexHandle blendTex;
|
||||
blendTex.set(resolution, resolution, GFXFormatR8G8B8A8, &GFXRenderTargetProfile, "");
|
||||
|
||||
GFXTextureTargetRef mBaseTarget = GFX->allocRenderToTextureTarget();
|
||||
|
||||
GFX->clearTextureStateImmediate(0);
|
||||
if (mReflectionModeType == DynamicCubemap)
|
||||
mBaseTarget->attachTexture(GFXTextureTarget::Color0, mDynamicCubemap, i);
|
||||
else
|
||||
mBaseTarget->attachTexture(GFXTextureTarget::Color0, blendTex);
|
||||
|
||||
// Standard view that will be overridden below.
|
||||
VectorF vLookatPt(0.0f, 0.0f, 0.0f), vUpVec(0.0f, 0.0f, 0.0f), vRight(0.0f, 0.0f, 0.0f);
|
||||
|
||||
switch (i)
|
||||
{
|
||||
case 0: // D3DCUBEMAP_FACE_POSITIVE_X:
|
||||
vLookatPt = VectorF(1.0f, 0.0f, 0.0f);
|
||||
vUpVec = VectorF(0.0f, 1.0f, 0.0f);
|
||||
break;
|
||||
case 1: // D3DCUBEMAP_FACE_NEGATIVE_X:
|
||||
vLookatPt = VectorF(-1.0f, 0.0f, 0.0f);
|
||||
vUpVec = VectorF(0.0f, 1.0f, 0.0f);
|
||||
break;
|
||||
case 2: // D3DCUBEMAP_FACE_POSITIVE_Y:
|
||||
vLookatPt = VectorF(0.0f, 1.0f, 0.0f);
|
||||
vUpVec = VectorF(0.0f, 0.0f, -1.0f);
|
||||
break;
|
||||
case 3: // D3DCUBEMAP_FACE_NEGATIVE_Y:
|
||||
vLookatPt = VectorF(0.0f, -1.0f, 0.0f);
|
||||
vUpVec = VectorF(0.0f, 0.0f, 1.0f);
|
||||
break;
|
||||
case 4: // D3DCUBEMAP_FACE_POSITIVE_Z:
|
||||
vLookatPt = VectorF(0.0f, 0.0f, 1.0f);
|
||||
vUpVec = VectorF(0.0f, 1.0f, 0.0f);
|
||||
break;
|
||||
case 5: // D3DCUBEMAP_FACE_NEGATIVE_Z:
|
||||
vLookatPt = VectorF(0.0f, 0.0f, -1.0f);
|
||||
vUpVec = VectorF(0.0f, 1.0f, 0.0f);
|
||||
break;
|
||||
}
|
||||
|
||||
// create camera matrix
|
||||
VectorF cross = mCross(vUpVec, vLookatPt);
|
||||
cross.normalizeSafe();
|
||||
|
||||
MatrixF matView(true);
|
||||
matView.setColumn(0, cross);
|
||||
matView.setColumn(1, vLookatPt);
|
||||
matView.setColumn(2, vUpVec);
|
||||
matView.setPosition(getPosition());
|
||||
matView.inverse();
|
||||
|
||||
// set projection to 90 degrees vertical and horizontal
|
||||
F32 left, right, top, bottom;
|
||||
F32 nearPlane = 0.01f;
|
||||
F32 farDist = 1000.f;
|
||||
|
||||
MathUtils::makeFrustum(&left, &right, &top, &bottom, M_HALFPI_F, 1.0f, nearPlane);
|
||||
Frustum frustum(false, left, right, top, bottom, nearPlane, farDist);
|
||||
|
||||
renderFrame(&mBaseTarget, matView, frustum, StaticObjectType | StaticShapeObjectType & EDITOR_RENDER_TYPEMASK, gCanvasClearColor);
|
||||
|
||||
mBaseTarget->resolve();
|
||||
|
||||
mCubemap->setCubeFaceTexture(i, blendTex);
|
||||
}
|
||||
|
||||
if (mReflectionModeType != DynamicCubemap && validCubemap)
|
||||
{
|
||||
if (mCubemap->mCubemap)
|
||||
mCubemap->updateFaces();
|
||||
else
|
||||
mCubemap->createMap();
|
||||
|
||||
char fileName[256];
|
||||
dSprintf(fileName, 256, "%s%s.DDS", mReflectionPath.c_str(), mProbeUniqueID.c_str());
|
||||
|
||||
CubemapSaver::save(mCubemap->mCubemap, fileName);
|
||||
|
||||
if (!Platform::isFile(fileName))
|
||||
{
|
||||
validCubemap = false; //if we didn't save right, just
|
||||
Con::errorf("Failed to properly save out the skylight baked cubemap!");
|
||||
}
|
||||
|
||||
mDirty = false;
|
||||
}
|
||||
|
||||
//calculateSHTerms();
|
||||
|
||||
ReflectionProbe::smRenderReflectionProbes = probeRenderState;
|
||||
setMaskBits(-1);
|
||||
|
||||
if (preCapture)
|
||||
preCapture->disable();
|
||||
if (deferredShading)
|
||||
deferredShading->enable();*/
|
||||
}
|
||||
|
||||
LinearColorF decodeSH(Point3F normal, const LinearColorF SHTerms[9], const F32 SHConstants[5])
|
||||
{
|
||||
float x = normal.x;
|
||||
float y = normal.y;
|
||||
float z = normal.z;
|
||||
|
||||
LinearColorF l00 = SHTerms[0];
|
||||
|
||||
LinearColorF l10 = SHTerms[1];
|
||||
LinearColorF l11 = SHTerms[2];
|
||||
LinearColorF l12 = SHTerms[3];
|
||||
|
||||
LinearColorF l20 = SHTerms[4];
|
||||
LinearColorF l21 = SHTerms[5];
|
||||
LinearColorF l22 = SHTerms[6];
|
||||
LinearColorF l23 = SHTerms[7];
|
||||
LinearColorF l24 = SHTerms[8];
|
||||
|
||||
LinearColorF result = (
|
||||
l00 * SHConstants[0] +
|
||||
|
||||
l12 * SHConstants[1] * x +
|
||||
l10 * SHConstants[1] * y +
|
||||
l11 * SHConstants[1] * z +
|
||||
|
||||
l20 * SHConstants[2] * x*y +
|
||||
l21 * SHConstants[2] * y*z +
|
||||
l22 * SHConstants[3] * (3.0*z*z - 1.0) +
|
||||
l23 * SHConstants[2] * x*z +
|
||||
l24 * SHConstants[4] * (x*x - y * y)
|
||||
);
|
||||
|
||||
return LinearColorF(mMax(result.red, 0), mMax(result.green, 0), mMax(result.blue, 0));
|
||||
}
|
||||
|
||||
MatrixF getSideMatrix(U32 side)
|
||||
{
|
||||
// Standard view that will be overridden below.
|
||||
VectorF vLookatPt(0.0f, 0.0f, 0.0f), vUpVec(0.0f, 0.0f, 0.0f), vRight(0.0f, 0.0f, 0.0f);
|
||||
|
||||
switch (side)
|
||||
{
|
||||
case 0: // D3DCUBEMAP_FACE_POSITIVE_X:
|
||||
vLookatPt = VectorF(1.0f, 0.0f, 0.0f);
|
||||
vUpVec = VectorF(0.0f, 1.0f, 0.0f);
|
||||
break;
|
||||
case 1: // D3DCUBEMAP_FACE_NEGATIVE_X:
|
||||
vLookatPt = VectorF(-1.0f, 0.0f, 0.0f);
|
||||
vUpVec = VectorF(0.0f, 1.0f, 0.0f);
|
||||
break;
|
||||
case 2: // D3DCUBEMAP_FACE_POSITIVE_Y:
|
||||
vLookatPt = VectorF(0.0f, 1.0f, 0.0f);
|
||||
vUpVec = VectorF(0.0f, 0.0f, -1.0f);
|
||||
break;
|
||||
case 3: // D3DCUBEMAP_FACE_NEGATIVE_Y:
|
||||
vLookatPt = VectorF(0.0f, -1.0f, 0.0f);
|
||||
vUpVec = VectorF(0.0f, 0.0f, 1.0f);
|
||||
break;
|
||||
case 4: // D3DCUBEMAP_FACE_POSITIVE_Z:
|
||||
vLookatPt = VectorF(0.0f, 0.0f, 1.0f);
|
||||
vUpVec = VectorF(0.0f, 1.0f, 0.0f);
|
||||
break;
|
||||
case 5: // D3DCUBEMAP_FACE_NEGATIVE_Z:
|
||||
vLookatPt = VectorF(0.0f, 0.0f, -1.0f);
|
||||
vUpVec = VectorF(0.0f, 1.0f, 0.0f);
|
||||
break;
|
||||
}
|
||||
|
||||
// create camera matrix
|
||||
VectorF cross = mCross(vUpVec, vLookatPt);
|
||||
cross.normalizeSafe();
|
||||
|
||||
MatrixF rotMat(true);
|
||||
rotMat.setColumn(0, cross);
|
||||
rotMat.setColumn(1, vLookatPt);
|
||||
rotMat.setColumn(2, vUpVec);
|
||||
//rotMat.inverse();
|
||||
|
||||
return rotMat;
|
||||
}
|
||||
|
||||
F32 harmonics(U32 termId, Point3F normal)
|
||||
{
|
||||
F32 x = normal.x;
|
||||
F32 y = normal.y;
|
||||
F32 z = normal.z;
|
||||
|
||||
switch (termId)
|
||||
{
|
||||
case 0:
|
||||
return 1.0;
|
||||
case 1:
|
||||
return y;
|
||||
case 2:
|
||||
return z;
|
||||
case 3:
|
||||
return x;
|
||||
case 4:
|
||||
return x * y;
|
||||
case 5:
|
||||
return y * z;
|
||||
case 6:
|
||||
return 3.0*z*z - 1.0;
|
||||
case 7:
|
||||
return x * z;
|
||||
default:
|
||||
return x * x - y * y;
|
||||
}
|
||||
}
|
||||
|
||||
LinearColorF sampleSide(GBitmap* cubeFaceBitmaps[6], const U32& cubemapResolution, const U32& termindex, const U32& sideIndex)
|
||||
{
|
||||
MatrixF sideRot = getSideMatrix(sideIndex);
|
||||
|
||||
LinearColorF result = LinearColorF::ZERO;
|
||||
F32 divider = 0;
|
||||
|
||||
for (int y = 0; y<cubemapResolution; y++)
|
||||
{
|
||||
for (int x = 0; x<cubemapResolution; x++)
|
||||
{
|
||||
Point2F sidecoord = ((Point2F(x, y) + Point2F(0.5, 0.5)) / Point2F(cubemapResolution, cubemapResolution))*2.0 - Point2F(1.0, 1.0);
|
||||
Point3F normal = Point3F(sidecoord.x, sidecoord.y, -1.0);
|
||||
normal.normalize();
|
||||
|
||||
F32 minBrightness = Con::getFloatVariable("$pref::GI::Cubemap_Sample_MinBrightness", 0.001f);
|
||||
|
||||
LinearColorF texel = cubeFaceBitmaps[sideIndex]->sampleTexel(y, x);
|
||||
texel = LinearColorF(mMax(texel.red, minBrightness), mMax(texel.green, minBrightness), mMax(texel.blue, minBrightness)) * Con::getFloatVariable("$pref::GI::Cubemap_Gain", 1.5);
|
||||
|
||||
Point3F dir;
|
||||
sideRot.mulP(normal, &dir);
|
||||
|
||||
result += texel * harmonics(termindex, dir) * -normal.z;
|
||||
divider += -normal.z;
|
||||
}
|
||||
}
|
||||
|
||||
result /= divider;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
//SH Calculations
|
||||
// From http://sunandblackcat.com/tipFullView.php?l=eng&topicid=32&topic=Spherical-Harmonics-From-Cube-Texture
|
||||
// With shader decode logic from https://github.com/nicknikolov/cubemap-sh
|
||||
void calculateSHTerms(GFXCubemapHandle cubemap, LinearColorF SHTerms[9], F32 SHConstants[5])
|
||||
{
|
||||
if (!cubemap)
|
||||
return;
|
||||
|
||||
const VectorF cubemapFaceNormals[6] =
|
||||
{
|
||||
// D3DCUBEMAP_FACE_POSITIVE_X:
|
||||
VectorF(1.0f, 0.0f, 0.0f),
|
||||
// D3DCUBEMAP_FACE_NEGATIVE_X:
|
||||
VectorF(-1.0f, 0.0f, 0.0f),
|
||||
// D3DCUBEMAP_FACE_POSITIVE_Y:
|
||||
VectorF(0.0f, 1.0f, 0.0f),
|
||||
// D3DCUBEMAP_FACE_NEGATIVE_Y:
|
||||
VectorF(0.0f, -1.0f, 0.0f),
|
||||
// D3DCUBEMAP_FACE_POSITIVE_Z:
|
||||
VectorF(0.0f, 0.0f, 1.0f),
|
||||
// D3DCUBEMAP_FACE_NEGATIVE_Z:
|
||||
VectorF(0.0f, 0.0f, -1.0f),
|
||||
};
|
||||
|
||||
U32 cubemapResolution = cubemap->getSize();
|
||||
|
||||
GBitmap* cubeFaceBitmaps[6];
|
||||
|
||||
for (U32 i = 0; i < 6; i++)
|
||||
{
|
||||
cubeFaceBitmaps[i] = new GBitmap(cubemapResolution, cubemapResolution, false, GFXFormatR16G16B16A16F);
|
||||
}
|
||||
|
||||
//If we fail to parse the cubemap for whatever reason, we really can't continue
|
||||
if (!CubemapSaver::getBitmaps(cubemap, GFXFormatR8G8B8A8, cubeFaceBitmaps))
|
||||
return;
|
||||
|
||||
//Set up our constants
|
||||
F32 L0 = Con::getFloatVariable("$pref::GI::SH_Term_L0", 1.0f);
|
||||
F32 L1 = Con::getFloatVariable("$pref::GI::SH_Term_L1", 1.8f);
|
||||
F32 L2 = Con::getFloatVariable("$pref::GI::SH_Term_L2", 0.83f);
|
||||
F32 L2m2_L2m1_L21 = Con::getFloatVariable("$pref::GI::SH_Term_L2m2", 2.9f);
|
||||
F32 L20 = Con::getFloatVariable("$pref::GI::SH_Term_L20", 0.58f);
|
||||
F32 L22 = Con::getFloatVariable("$pref::GI::SH_Term_L22", 1.1f);
|
||||
|
||||
SHConstants[0] = L0;
|
||||
SHConstants[1] = L1;
|
||||
SHConstants[2] = L2 * L2m2_L2m1_L21;
|
||||
SHConstants[3] = L2 * L20;
|
||||
SHConstants[4] = L2 * L22;
|
||||
|
||||
for (U32 i = 0; i < 9; i++)
|
||||
{
|
||||
//Clear it, just to be sure
|
||||
SHTerms[i] = LinearColorF(0.f, 0.f, 0.f);
|
||||
|
||||
//Now, encode for each side
|
||||
SHTerms[i] = sampleSide(cubeFaceBitmaps, cubemapResolution, i, 0); //POS_X
|
||||
SHTerms[i] += sampleSide(cubeFaceBitmaps, cubemapResolution, i, 1); //NEG_X
|
||||
SHTerms[i] += sampleSide(cubeFaceBitmaps, cubemapResolution, i, 2); //POS_Y
|
||||
SHTerms[i] += sampleSide(cubeFaceBitmaps, cubemapResolution, i, 3); //NEG_Y
|
||||
SHTerms[i] += sampleSide(cubeFaceBitmaps, cubemapResolution, i, 4); //POS_Z
|
||||
SHTerms[i] += sampleSide(cubeFaceBitmaps, cubemapResolution, i, 5); //NEG_Z
|
||||
|
||||
//Average
|
||||
SHTerms[i] /= 6;
|
||||
}
|
||||
|
||||
for (U32 i = 0; i < 6; i++)
|
||||
SAFE_DELETE(cubeFaceBitmaps[i]);
|
||||
|
||||
/*bool mExportSHTerms = false;
|
||||
if (mExportSHTerms)
|
||||
{
|
||||
for (U32 f = 0; f < 6; f++)
|
||||
{
|
||||
char fileName[256];
|
||||
dSprintf(fileName, 256, "%s%s_DecodedFaces_%d.png", mReflectionPath.c_str(),
|
||||
mProbeUniqueID.c_str(), f);
|
||||
|
||||
LinearColorF color = decodeSH(cubemapFaceNormals[f]);
|
||||
|
||||
FileStream stream;
|
||||
if (stream.open(fileName, Torque::FS::File::Write))
|
||||
{
|
||||
GBitmap bitmap(mCubemapResolution, mCubemapResolution, false, GFXFormatR8G8B8);
|
||||
|
||||
bitmap.fill(color.toColorI());
|
||||
|
||||
bitmap.writeBitmap("png", stream);
|
||||
}
|
||||
}
|
||||
|
||||
for (U32 f = 0; f < 9; f++)
|
||||
{
|
||||
char fileName[256];
|
||||
dSprintf(fileName, 256, "%s%s_SHTerms_%d.png", mReflectionPath.c_str(),
|
||||
mProbeUniqueID.c_str(), f);
|
||||
|
||||
LinearColorF color = mProbeInfo->SHTerms[f];
|
||||
|
||||
FileStream stream;
|
||||
if (stream.open(fileName, Torque::FS::File::Write))
|
||||
{
|
||||
GBitmap bitmap(mCubemapResolution, mCubemapResolution, false, GFXFormatR8G8B8);
|
||||
|
||||
bitmap.fill(color.toColorI());
|
||||
|
||||
bitmap.writeBitmap("png", stream);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
F32 areaElement(F32 x, F32 y)
|
||||
{
|
||||
return mAtan2(x * y, (F32)mSqrt(x * x + y * y + 1.0));
|
||||
}
|
||||
|
||||
F32 texelSolidAngle(F32 aU, F32 aV, U32 width, U32 height)
|
||||
{
|
||||
// transform from [0..res - 1] to [- (1 - 1 / res) .. (1 - 1 / res)]
|
||||
// ( 0.5 is for texel center addressing)
|
||||
const F32 U = (2.0 * (aU + 0.5) / width) - 1.0;
|
||||
const F32 V = (2.0 * (aV + 0.5) / height) - 1.0;
|
||||
|
||||
// shift from a demi texel, mean 1.0 / size with U and V in [-1..1]
|
||||
const F32 invResolutionW = 1.0 / width;
|
||||
const F32 invResolutionH = 1.0 / height;
|
||||
|
||||
// U and V are the -1..1 texture coordinate on the current face.
|
||||
// get projected area for this texel
|
||||
const F32 x0 = U - invResolutionW;
|
||||
const F32 y0 = V - invResolutionH;
|
||||
const F32 x1 = U + invResolutionW;
|
||||
const F32 y1 = V + invResolutionH;
|
||||
const F32 angle = areaElement(x0, y0) - areaElement(x0, y1) - areaElement(x1, y0) + areaElement(x1, y1);
|
||||
|
||||
return angle;
|
||||
}
|
||||
};
|
||||
70
Engine/source/T3D/lighting/IBLUtilities.h
Normal file
70
Engine/source/T3D/lighting/IBLUtilities.h
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// 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.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef IBL_UTILS_H_
|
||||
#define IBL_UTILS_H_
|
||||
|
||||
#ifndef _GFXTARGET_H_
|
||||
#include "gfx/gfxTarget.h"
|
||||
#endif
|
||||
|
||||
#ifndef _GFXCUBEMAP_H_
|
||||
#include "gfx/gfxCubemap.h"
|
||||
#endif
|
||||
|
||||
#ifndef _COLOR_H_
|
||||
#include "core/color.h"
|
||||
#endif
|
||||
|
||||
|
||||
namespace IBLUtilities
|
||||
{
|
||||
void GenerateIrradianceMap(GFXTextureTargetRef renderTarget, GFXCubemapHandle cubemap, GFXCubemapHandle &cubemapOut);
|
||||
void GenerateAndSaveIrradianceMap(String outputPath, S32 resolution, GFXCubemapHandle cubemap, GFXCubemapHandle &cubemapOut);
|
||||
|
||||
void GeneratePrefilterMap(GFXTextureTargetRef renderTarget, GFXCubemapHandle cubemap, U32 mipLevels, GFXCubemapHandle &cubemapOut);
|
||||
void GenerateAndSavePrefilterMap(String outputPath, S32 resolution, GFXCubemapHandle cubemap, U32 mipLevels, GFXCubemapHandle &cubemapOut);
|
||||
|
||||
void SaveCubeMap(String outputPath, GFXCubemapHandle &cubemap);
|
||||
|
||||
void bakeReflection(String outputPath, S32 resolution);
|
||||
|
||||
LinearColorF decodeSH(Point3F normal, const LinearColorF SHTerms[9], const F32 SHConstants[5]);
|
||||
|
||||
MatrixF getSideMatrix(U32 side);
|
||||
|
||||
F32 harmonics(U32 termId, Point3F normal);
|
||||
|
||||
LinearColorF sampleSide(GBitmap* cubeFaceBitmaps[6], const U32& cubemapResolution, const U32& termindex, const U32& sideIndex);
|
||||
|
||||
//
|
||||
//SH Calculations
|
||||
// From http://sunandblackcat.com/tipFullView.php?l=eng&topicid=32&topic=Spherical-Harmonics-From-Cube-Texture
|
||||
// With shader decode logic from https://github.com/nicknikolov/cubemap-sh
|
||||
void calculateSHTerms(GFXCubemapHandle cubemap, LinearColorF SHTerms[9], F32 SHConstants[5]);
|
||||
|
||||
F32 texelSolidAngle(F32 aU, F32 aV, U32 width, U32 height);
|
||||
|
||||
F32 areaElement(F32 x, F32 y);
|
||||
};
|
||||
|
||||
#endif
|
||||
180
Engine/source/T3D/lighting/boxEnvironmentProbe.cpp
Normal file
180
Engine/source/T3D/lighting/boxEnvironmentProbe.cpp
Normal file
|
|
@ -0,0 +1,180 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// 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 "T3D/lighting/boxEnvironmentProbe.h"
|
||||
#include "math/mathIO.h"
|
||||
#include "scene/sceneRenderState.h"
|
||||
#include "console/consoleTypes.h"
|
||||
#include "core/stream/bitStream.h"
|
||||
#include "materials/baseMatInstance.h"
|
||||
#include "console/engineAPI.h"
|
||||
#include "gfx/gfxDrawUtil.h"
|
||||
#include "gfx/gfxDebugEvent.h"
|
||||
#include "gfx/gfxTransformSaver.h"
|
||||
#include "math/mathUtils.h"
|
||||
#include "gfx/bitmap/gBitmap.h"
|
||||
#include "core/stream/fileStream.h"
|
||||
#include "core/fileObject.h"
|
||||
#include "core/resourceManager.h"
|
||||
#include "console/simPersistId.h"
|
||||
#include "T3D/gameFunctions.h"
|
||||
#include "postFx/postEffect.h"
|
||||
#include "renderInstance/renderProbeMgr.h"
|
||||
#include "renderInstance/renderProbeMgr.h"
|
||||
|
||||
#include "math/util/sphereMesh.h"
|
||||
#include "materials/materialManager.h"
|
||||
#include "math/util/matrixSet.h"
|
||||
#include "gfx/bitmap/cubemapSaver.h"
|
||||
|
||||
#include "materials/materialFeatureTypes.h"
|
||||
|
||||
#include "materials/shaderData.h"
|
||||
#include "gfx/gfxTextureManager.h"
|
||||
|
||||
#include "gfx/bitmap/imageUtils.h"
|
||||
|
||||
#include "T3D/lighting/IBLUtilities.h"
|
||||
|
||||
extern bool gEditingMission;
|
||||
extern ColorI gCanvasClearColor;
|
||||
|
||||
IMPLEMENT_CO_NETOBJECT_V1(BoxEnvironmentProbe);
|
||||
|
||||
ConsoleDocClass(BoxEnvironmentProbe,
|
||||
"@brief An example scene object which renders a mesh.\n\n"
|
||||
"This class implements a basic SceneObject that can exist in the world at a "
|
||||
"3D position and render itself. There are several valid ways to render an "
|
||||
"object in Torque. This class implements the preferred rendering method which "
|
||||
"is to submit a MeshRenderInst along with a Material, vertex buffer, "
|
||||
"primitive buffer, and transform and allow the RenderMeshMgr handle the "
|
||||
"actual setup and rendering for you.\n\n"
|
||||
"See the C++ code for implementation details.\n\n"
|
||||
"@ingroup Examples\n");
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Object setup and teardown
|
||||
//-----------------------------------------------------------------------------
|
||||
BoxEnvironmentProbe::BoxEnvironmentProbe() : ReflectionProbe()
|
||||
{
|
||||
mCaptureMask = REFLECTION_PROBE_CAPTURE_TYPEMASK;
|
||||
mProbeShapeType = ProbeRenderInst::Box;
|
||||
mAtten = 0.0;
|
||||
}
|
||||
|
||||
BoxEnvironmentProbe::~BoxEnvironmentProbe()
|
||||
{
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Object Editing
|
||||
//-----------------------------------------------------------------------------
|
||||
void BoxEnvironmentProbe::initPersistFields()
|
||||
{
|
||||
// SceneObject already handles exposing the transform
|
||||
Parent::initPersistFields();
|
||||
|
||||
addField("attenuation", TypeF32, Offset(mAtten, BoxEnvironmentProbe), "falloff percent");
|
||||
|
||||
removeField("radius");
|
||||
}
|
||||
|
||||
void BoxEnvironmentProbe::inspectPostApply()
|
||||
{
|
||||
Parent::inspectPostApply();
|
||||
|
||||
mDirty = true;
|
||||
|
||||
// Flag the network mask to send the updates
|
||||
// to the client object
|
||||
setMaskBits(-1);
|
||||
}
|
||||
|
||||
bool BoxEnvironmentProbe::onAdd()
|
||||
{
|
||||
if (!Parent::onAdd())
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void BoxEnvironmentProbe::onRemove()
|
||||
{
|
||||
Parent::onRemove();
|
||||
}
|
||||
|
||||
void BoxEnvironmentProbe::setTransform(const MatrixF & mat)
|
||||
{
|
||||
// Let SceneObject handle all of the matrix manipulation
|
||||
Parent::setTransform(mat);
|
||||
|
||||
mDirty = true;
|
||||
|
||||
// Dirty our network mask so that the new transform gets
|
||||
// transmitted to the client object
|
||||
setMaskBits(TransformMask);
|
||||
}
|
||||
|
||||
U32 BoxEnvironmentProbe::packUpdate(NetConnection *conn, U32 mask, BitStream *stream)
|
||||
{
|
||||
// Allow the Parent to get a crack at writing its info
|
||||
U32 retMask = Parent::packUpdate(conn, mask, stream);
|
||||
|
||||
if (stream->writeFlag(mask & UpdateMask))
|
||||
{
|
||||
stream->write(mAtten);
|
||||
}
|
||||
|
||||
return retMask;
|
||||
}
|
||||
|
||||
void BoxEnvironmentProbe::unpackUpdate(NetConnection *conn, BitStream *stream)
|
||||
{
|
||||
// Let the Parent read any info it sent
|
||||
Parent::unpackUpdate(conn, stream);
|
||||
|
||||
if (stream->readFlag()) // UpdateMask
|
||||
{
|
||||
stream->read(&mAtten);
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Object Rendering
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
void BoxEnvironmentProbe::updateProbeParams()
|
||||
{
|
||||
Parent::updateProbeParams();
|
||||
|
||||
mProbeInfo->mProbeShapeType = ProbeRenderInst::Box;
|
||||
mProbeInfo->mAtten = mAtten;
|
||||
|
||||
PROBEMGR->updateProbes();
|
||||
|
||||
updateCubemaps();
|
||||
}
|
||||
|
||||
void BoxEnvironmentProbe::setPreviewMatParameters(SceneRenderState* renderState, BaseMatInstance* mat)
|
||||
{
|
||||
Parent::setPreviewMatParameters(renderState, mat);
|
||||
}
|
||||
116
Engine/source/T3D/lighting/boxEnvironmentProbe.h
Normal file
116
Engine/source/T3D/lighting/boxEnvironmentProbe.h
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// 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.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef BOX_ENVIRONMENT_PROBE_H
|
||||
#define BOX_ENVIRONMENT_PROBE_H
|
||||
|
||||
#ifndef REFLECTIONPROBE_H
|
||||
#include "T3D/lighting/reflectionProbe.h"
|
||||
#endif
|
||||
#ifndef _GFXVERTEXBUFFER_H_
|
||||
#include "gfx/gfxVertexBuffer.h"
|
||||
#endif
|
||||
#ifndef _GFXPRIMITIVEBUFFER_H_
|
||||
#include "gfx/gfxPrimitiveBuffer.h"
|
||||
#endif
|
||||
#ifndef _TSSHAPEINSTANCE_H_
|
||||
#include "ts/tsShapeInstance.h"
|
||||
#endif
|
||||
#include "lighting/lightInfo.h"
|
||||
|
||||
#ifndef _RENDERPASSMANAGER_H_
|
||||
#include "renderInstance/renderPassManager.h"
|
||||
#endif
|
||||
|
||||
class BaseMatInstance;
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// This class implements a basic SceneObject that can exist in the world at a
|
||||
// 3D position and render itself. There are several valid ways to render an
|
||||
// object in Torque. This class implements the preferred rendering method which
|
||||
// is to submit a MeshRenderInst along with a Material, vertex buffer,
|
||||
// primitive buffer, and transform and allow the RenderMeshMgr handle the
|
||||
// actual setup and rendering for you.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
class BoxEnvironmentProbe : public ReflectionProbe
|
||||
{
|
||||
typedef ReflectionProbe Parent;
|
||||
F32 mAtten;
|
||||
private:
|
||||
|
||||
//Debug rendering
|
||||
static bool smRenderPreviewProbes;
|
||||
|
||||
public:
|
||||
BoxEnvironmentProbe();
|
||||
virtual ~BoxEnvironmentProbe();
|
||||
|
||||
// Declare this object as a ConsoleObject so that we can
|
||||
// instantiate it into the world and network it
|
||||
DECLARE_CONOBJECT(BoxEnvironmentProbe);
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Object Editing
|
||||
// Since there is always a server and a client object in Torque and we
|
||||
// actually edit the server object we need to implement some basic
|
||||
// networking functions
|
||||
//--------------------------------------------------------------------------
|
||||
// Set up any fields that we want to be editable (like position)
|
||||
static void initPersistFields();
|
||||
|
||||
// Allows the object to update its editable settings
|
||||
// from the server object to the client
|
||||
virtual void inspectPostApply();
|
||||
|
||||
// Handle when we are added to the scene and removed from the scene
|
||||
bool onAdd();
|
||||
void onRemove();
|
||||
|
||||
// Override this so that we can dirty the network flag when it is called
|
||||
void setTransform(const MatrixF &mat);
|
||||
|
||||
// This function handles sending the relevant data from the server
|
||||
// object to the client object
|
||||
U32 packUpdate(NetConnection *conn, U32 mask, BitStream *stream);
|
||||
// This function handles receiving relevant data from the server
|
||||
// object and applying it to the client object
|
||||
void unpackUpdate(NetConnection *conn, BitStream *stream);
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Object Rendering
|
||||
// Torque utilizes a "batch" rendering system. This means that it builds a
|
||||
// list of objects that need to render (via RenderInst's) and then renders
|
||||
// them all in one batch. This allows it to optimized on things like
|
||||
// minimizing texture, state, and shader switching by grouping objects that
|
||||
// use the same Materials.
|
||||
//--------------------------------------------------------------------------
|
||||
virtual void updateProbeParams();
|
||||
|
||||
// This is the function that allows this object to submit itself for rendering
|
||||
//void prepRenderImage(SceneRenderState *state);
|
||||
|
||||
void setPreviewMatParameters(SceneRenderState* renderState, BaseMatInstance* mat);
|
||||
};
|
||||
|
||||
#endif // BOX_ENVIRONMENT_PROBE_H
|
||||
968
Engine/source/T3D/lighting/reflectionProbe.cpp
Normal file
968
Engine/source/T3D/lighting/reflectionProbe.cpp
Normal file
|
|
@ -0,0 +1,968 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// 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 "T3D/lighting/reflectionProbe.h"
|
||||
#include "math/mathIO.h"
|
||||
#include "scene/sceneRenderState.h"
|
||||
#include "console/consoleTypes.h"
|
||||
#include "core/stream/bitStream.h"
|
||||
#include "materials/baseMatInstance.h"
|
||||
#include "console/engineAPI.h"
|
||||
#include "gfx/gfxDrawUtil.h"
|
||||
#include "gfx/gfxDebugEvent.h"
|
||||
#include "gfx/gfxTransformSaver.h"
|
||||
#include "math/mathUtils.h"
|
||||
#include "gfx/bitmap/gBitmap.h"
|
||||
#include "core/stream/fileStream.h"
|
||||
#include "core/fileObject.h"
|
||||
#include "core/resourceManager.h"
|
||||
#include "console/simPersistId.h"
|
||||
#include "T3D/gameFunctions.h"
|
||||
#include "postFx/postEffect.h"
|
||||
#include "renderInstance/renderProbeMgr.h"
|
||||
#include "renderInstance/renderProbeMgr.h"
|
||||
|
||||
#include "math/util/sphereMesh.h"
|
||||
#include "materials/materialManager.h"
|
||||
#include "math/util/matrixSet.h"
|
||||
#include "gfx/bitmap/cubemapSaver.h"
|
||||
|
||||
#include "materials/materialFeatureTypes.h"
|
||||
|
||||
#include "gfx/gfxTextureManager.h"
|
||||
#include "T3D/lighting/IBLUtilities.h"
|
||||
|
||||
#include "scene/reflector.h"
|
||||
|
||||
extern bool gEditingMission;
|
||||
extern ColorI gCanvasClearColor;
|
||||
bool ReflectionProbe::smRenderPreviewProbes = true;
|
||||
|
||||
IMPLEMENT_CO_NETOBJECT_V1(ReflectionProbe);
|
||||
|
||||
ConsoleDocClass(ReflectionProbe,
|
||||
"@brief An example scene object which renders a mesh.\n\n"
|
||||
"This class implements a basic SceneObject that can exist in the world at a "
|
||||
"3D position and render itself. There are several valid ways to render an "
|
||||
"object in Torque. This class implements the preferred rendering method which "
|
||||
"is to submit a MeshRenderInst along with a Material, vertex buffer, "
|
||||
"primitive buffer, and transform and allow the RenderMeshMgr handle the "
|
||||
"actual setup and rendering for you.\n\n"
|
||||
"See the C++ code for implementation details.\n\n"
|
||||
"@ingroup Examples\n");
|
||||
|
||||
ImplementEnumType(ReflectProbeType,
|
||||
"Type of mesh data available in a shape.\n"
|
||||
"@ingroup gameObjects")
|
||||
{ ProbeRenderInst::Sphere, "Sphere", "Sphere shaped" },
|
||||
{ ProbeRenderInst::Box, "Box", "Box shape" }
|
||||
EndImplementEnumType;
|
||||
|
||||
ImplementEnumType(ReflectionModeEnum,
|
||||
"Type of mesh data available in a shape.\n"
|
||||
"@ingroup gameObjects")
|
||||
{ ReflectionProbe::NoReflection, "No Reflections", "This probe does not provide any local reflection data"},
|
||||
{ ReflectionProbe::StaticCubemap, "Static Cubemap", "Uses a static CubemapData" },
|
||||
{ ReflectionProbe::BakedCubemap, "Baked Cubemap", "Uses a cubemap baked from the probe's current position" },
|
||||
//{ ReflectionProbe::DynamicCubemap, "Dynamic Cubemap", "Uses a cubemap baked from the probe's current position, updated at a set rate" },
|
||||
EndImplementEnumType;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Object setup and teardown
|
||||
//-----------------------------------------------------------------------------
|
||||
ReflectionProbe::ReflectionProbe()
|
||||
{
|
||||
// Flag this object so that it will always
|
||||
// be sent across the network to clients
|
||||
mNetFlags.set(Ghostable | ScopeAlways);
|
||||
|
||||
mTypeMask = LightObjectType | MarkerObjectType;
|
||||
|
||||
mProbeShapeType = ProbeRenderInst::Box;
|
||||
|
||||
mReflectionModeType = BakedCubemap;
|
||||
|
||||
mEnabled = true;
|
||||
mBake = false;
|
||||
mDirty = false;
|
||||
|
||||
mRadius = 10;
|
||||
mObjScale = Point3F::One * 10;
|
||||
mProbeRefScale = Point3F::One*10;
|
||||
|
||||
mUseHDRCaptures = true;
|
||||
|
||||
mStaticCubemap = NULL;
|
||||
mProbeUniqueID = "";
|
||||
|
||||
mEditorShapeInst = NULL;
|
||||
mEditorShape = NULL;
|
||||
|
||||
mRefreshRateMS = 200;
|
||||
mDynamicLastBakeMS = 0;
|
||||
|
||||
mMaxDrawDistance = 75;
|
||||
|
||||
mResourcesCreated = false;
|
||||
|
||||
mProbeInfo = nullptr;
|
||||
|
||||
mPrefilterSize = 64;
|
||||
mPrefilterMipLevels = mLog2(F32(mPrefilterSize));
|
||||
mPrefilterMap = nullptr;
|
||||
mIrridianceMap = nullptr;
|
||||
|
||||
mProbeRefOffset = Point3F::Zero;
|
||||
mEditPosOffset = false;
|
||||
|
||||
mProbeInfoIdx = -1;
|
||||
|
||||
mCaptureMask = REFLECTION_PROBE_CAPTURE_TYPEMASK;
|
||||
}
|
||||
|
||||
ReflectionProbe::~ReflectionProbe()
|
||||
{
|
||||
if (mEditorShapeInst)
|
||||
SAFE_DELETE(mEditorShapeInst);
|
||||
|
||||
if (mProbeInfo)
|
||||
SAFE_DELETE(mProbeInfo);
|
||||
|
||||
if (mReflectionModeType != StaticCubemap && mStaticCubemap)
|
||||
mStaticCubemap->deleteObject();
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Object Editing
|
||||
//-----------------------------------------------------------------------------
|
||||
void ReflectionProbe::initPersistFields()
|
||||
{
|
||||
addGroup("Rendering");
|
||||
addProtectedField("enabled", TypeBool, Offset(mEnabled, ReflectionProbe),
|
||||
&_setEnabled, &defaultProtectedGetFn, "Regenerate Voxel Grid");
|
||||
endGroup("Rendering");
|
||||
|
||||
addGroup("Reflection");
|
||||
addProtectedField("radius", TypeF32, Offset(mRadius, ReflectionProbe), &_setRadius, &defaultProtectedGetFn,
|
||||
"The name of the material used to render the mesh.");
|
||||
|
||||
addProtectedField("EditPosOffset", TypeBool, Offset(mEditPosOffset, ReflectionProbe),
|
||||
&_toggleEditPosOffset, &defaultProtectedGetFn, "Toggle Edit Pos Offset Mode", AbstractClassRep::FieldFlags::FIELD_ComponentInspectors);
|
||||
|
||||
addField("refOffset", TypePoint3F, Offset(mProbeRefOffset, ReflectionProbe), "");
|
||||
addField("refScale", TypePoint3F, Offset(mProbeRefScale, ReflectionProbe), "");
|
||||
|
||||
addProtectedField("ReflectionMode", TypeReflectionModeEnum, Offset(mReflectionModeType, ReflectionProbe), &_setReflectionMode, &defaultProtectedGetFn,
|
||||
"The type of mesh data to use for collision queries.");
|
||||
|
||||
addField("StaticCubemap", TypeCubemapName, Offset(mCubemapName, ReflectionProbe), "Cubemap used instead of reflection texture if fullReflect is off.");
|
||||
|
||||
addProtectedField("Bake", TypeBool, Offset(mBake, ReflectionProbe),
|
||||
&_doBake, &defaultProtectedGetFn, "Regenerate Voxel Grid", AbstractClassRep::FieldFlags::FIELD_ComponentInspectors);
|
||||
endGroup("Reflection");
|
||||
|
||||
Con::addVariable("$Light::renderReflectionProbes", TypeBool, &RenderProbeMgr::smRenderReflectionProbes,
|
||||
"Toggles rendering of light frustums when the light is selected in the editor.\n\n"
|
||||
"@note Only works for shadow mapped lights.\n\n"
|
||||
"@ingroup Lighting");
|
||||
|
||||
Con::addVariable("$Light::renderPreviewProbes", TypeBool, &ReflectionProbe::smRenderPreviewProbes,
|
||||
"Toggles rendering of light frustums when the light is selected in the editor.\n\n"
|
||||
"@note Only works for shadow mapped lights.\n\n"
|
||||
"@ingroup Lighting");
|
||||
|
||||
// SceneObject already handles exposing the transform
|
||||
Parent::initPersistFields();
|
||||
}
|
||||
|
||||
void ReflectionProbe::inspectPostApply()
|
||||
{
|
||||
Parent::inspectPostApply();
|
||||
|
||||
mDirty = true;
|
||||
|
||||
// Flag the network mask to send the updates
|
||||
// to the client object
|
||||
setMaskBits(-1);
|
||||
}
|
||||
|
||||
bool ReflectionProbe::_setEnabled(void *object, const char *index, const char *data)
|
||||
{
|
||||
ReflectionProbe* probe = reinterpret_cast< ReflectionProbe* >(object);
|
||||
|
||||
probe->mEnabled = dAtob(data);
|
||||
probe->setMaskBits(-1);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ReflectionProbe::_doBake(void *object, const char *index, const char *data)
|
||||
{
|
||||
ReflectionProbe* probe = reinterpret_cast< ReflectionProbe* >(object);
|
||||
|
||||
probe->bake();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ReflectionProbe::_toggleEditPosOffset(void *object, const char *index, const char *data)
|
||||
{
|
||||
ReflectionProbe* probe = reinterpret_cast< ReflectionProbe* >(object);
|
||||
|
||||
probe->mEditPosOffset = !probe->mEditPosOffset;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ReflectionProbe::_setRadius(void *object, const char *index, const char *data)
|
||||
{
|
||||
ReflectionProbe* probe = reinterpret_cast<ReflectionProbe*>(object);
|
||||
|
||||
if (probe->mProbeShapeType != ProbeRenderInst::Sphere)
|
||||
return false;
|
||||
|
||||
probe->mObjScale = Point3F(probe->mRadius, probe->mRadius, probe->mRadius);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ReflectionProbe::_setReflectionMode(void *object, const char *index, const char *data)
|
||||
{
|
||||
ReflectionProbe* probe = reinterpret_cast<ReflectionProbe*>(object);
|
||||
|
||||
if (data == "Static Cubemap")
|
||||
{
|
||||
probe->mReflectionModeType = StaticCubemap;
|
||||
}
|
||||
else if (data == "Baked Cubemap")
|
||||
{
|
||||
//Clear our cubemap if we changed it to be baked, just for cleanliness
|
||||
probe->mReflectionModeType = BakedCubemap;
|
||||
probe->mCubemapName = "";
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ReflectionProbe::onAdd()
|
||||
{
|
||||
if (!Parent::onAdd())
|
||||
return false;
|
||||
|
||||
mEditPosOffset = false;
|
||||
|
||||
mObjBox.minExtents.set(-0.5, -0.5, -0.5);
|
||||
mObjBox.maxExtents.set(0.5, 0.5, 0.5);
|
||||
|
||||
// Skip our transform... it just dirties mask bits.
|
||||
Parent::setTransform(mObjToWorld);
|
||||
|
||||
resetWorldBox();
|
||||
|
||||
// Add this object to the scene
|
||||
addToScene();
|
||||
|
||||
if (isServerObject())
|
||||
{
|
||||
if (!mPersistentId)
|
||||
mPersistentId = getOrCreatePersistentId();
|
||||
|
||||
mProbeUniqueID = String::ToString(mPersistentId->getUUID().getHash());
|
||||
}
|
||||
|
||||
// Refresh this object's material (if any)
|
||||
if (isClientObject())
|
||||
{
|
||||
createGeometry();
|
||||
updateProbeParams();
|
||||
}
|
||||
|
||||
setMaskBits(-1);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ReflectionProbe::onRemove()
|
||||
{
|
||||
if (isClientObject())
|
||||
{
|
||||
PROBEMGR->unregisterProbe(mProbeInfoIdx);
|
||||
}
|
||||
// Remove this object from the scene
|
||||
removeFromScene();
|
||||
|
||||
Parent::onRemove();
|
||||
}
|
||||
|
||||
void ReflectionProbe::handleDeleteAction()
|
||||
{
|
||||
//we're deleting it?
|
||||
//Then we need to clear out the processed cubemaps(if we have them)
|
||||
|
||||
String prefilPath = getPrefilterMapPath();
|
||||
if (Platform::isFile(prefilPath))
|
||||
{
|
||||
Platform::fileDelete(prefilPath);
|
||||
}
|
||||
|
||||
String irrPath = getIrradianceMapPath();
|
||||
if (Platform::isFile(irrPath))
|
||||
{
|
||||
Platform::fileDelete(irrPath);
|
||||
}
|
||||
|
||||
Parent::handleDeleteAction();
|
||||
}
|
||||
|
||||
void ReflectionProbe::setTransform(const MatrixF & mat)
|
||||
{
|
||||
// Let SceneObject handle all of the matrix manipulation
|
||||
if (!mEditPosOffset)
|
||||
Parent::setTransform(mat);
|
||||
else
|
||||
mProbeRefOffset = mat.getPosition();
|
||||
|
||||
mDirty = true;
|
||||
|
||||
// Dirty our network mask so that the new transform gets
|
||||
// transmitted to the client object
|
||||
setMaskBits(TransformMask);
|
||||
}
|
||||
|
||||
const MatrixF& ReflectionProbe::getTransform() const
|
||||
{
|
||||
if (!mEditPosOffset)
|
||||
return mObjToWorld;
|
||||
else
|
||||
{
|
||||
MatrixF transformMat = MatrixF::Identity;
|
||||
transformMat.setPosition(mProbeRefOffset);
|
||||
|
||||
return transformMat;
|
||||
}
|
||||
}
|
||||
|
||||
void ReflectionProbe::setScale(const VectorF &scale)
|
||||
{
|
||||
if (!mEditPosOffset)
|
||||
Parent::setScale(scale);
|
||||
else
|
||||
mProbeRefScale = scale;
|
||||
|
||||
mDirty = true;
|
||||
|
||||
// Dirty our network mask so that the new transform gets
|
||||
// transmitted to the client object
|
||||
setMaskBits(TransformMask);
|
||||
}
|
||||
|
||||
const VectorF& ReflectionProbe::getScale() const
|
||||
{
|
||||
if (!mEditPosOffset)
|
||||
return mObjScale;
|
||||
else
|
||||
return mProbeRefScale;
|
||||
}
|
||||
|
||||
bool ReflectionProbe::writeField(StringTableEntry fieldname, const char *value)
|
||||
{
|
||||
if (fieldname == StringTable->insert("Bake") || fieldname == StringTable->insert("EditPosOffset"))
|
||||
return false;
|
||||
|
||||
return Parent::writeField(fieldname, value);
|
||||
}
|
||||
|
||||
U32 ReflectionProbe::packUpdate(NetConnection *conn, U32 mask, BitStream *stream)
|
||||
{
|
||||
// Allow the Parent to get a crack at writing its info
|
||||
U32 retMask = Parent::packUpdate(conn, mask, stream);
|
||||
|
||||
// Write our transform information
|
||||
if (stream->writeFlag(mask & TransformMask))
|
||||
{
|
||||
stream->writeFlag(mEditPosOffset);
|
||||
mathWrite(*stream, mObjToWorld);
|
||||
mathWrite(*stream, mObjScale);
|
||||
mathWrite(*stream, mProbeRefOffset);
|
||||
mathWrite(*stream, mProbeRefScale);
|
||||
}
|
||||
|
||||
if (stream->writeFlag(mask & ShapeTypeMask))
|
||||
{
|
||||
stream->write((U32)mProbeShapeType);
|
||||
}
|
||||
|
||||
if (stream->writeFlag(mask & UpdateMask))
|
||||
{
|
||||
stream->write(mRadius);
|
||||
}
|
||||
|
||||
if (stream->writeFlag(mask & BakeInfoMask))
|
||||
{
|
||||
stream->write(mProbeUniqueID);
|
||||
}
|
||||
|
||||
if (stream->writeFlag(mask & EnabledMask))
|
||||
{
|
||||
stream->writeFlag(mEnabled);
|
||||
}
|
||||
|
||||
if (stream->writeFlag(mask & ModeMask))
|
||||
{
|
||||
stream->write((U32)mReflectionModeType);
|
||||
}
|
||||
|
||||
if (stream->writeFlag(mask & CubemapMask))
|
||||
{
|
||||
stream->write(mCubemapName);
|
||||
}
|
||||
|
||||
return retMask;
|
||||
}
|
||||
|
||||
void ReflectionProbe::unpackUpdate(NetConnection *conn, BitStream *stream)
|
||||
{
|
||||
// Let the Parent read any info it sent
|
||||
Parent::unpackUpdate(conn, stream);
|
||||
|
||||
if (stream->readFlag()) // TransformMask
|
||||
{
|
||||
mEditPosOffset = stream->readFlag();
|
||||
mathRead(*stream, &mObjToWorld);
|
||||
mathRead(*stream, &mObjScale);
|
||||
|
||||
Parent::setTransform(mObjToWorld);
|
||||
|
||||
resetWorldBox();
|
||||
|
||||
mathRead(*stream, &mProbeRefOffset);
|
||||
mathRead(*stream, &mProbeRefScale);
|
||||
|
||||
mDirty = true;
|
||||
}
|
||||
|
||||
if (stream->readFlag()) // ShapeTypeMask
|
||||
{
|
||||
U32 shapeType = ProbeRenderInst::Sphere;
|
||||
stream->read(&shapeType);
|
||||
|
||||
mProbeShapeType = (ProbeRenderInst::ProbeShapeType)shapeType;
|
||||
createGeometry();
|
||||
|
||||
mDirty = true;
|
||||
}
|
||||
|
||||
if (stream->readFlag()) // UpdateMask
|
||||
{
|
||||
stream->read(&mRadius);
|
||||
|
||||
mDirty = true;
|
||||
}
|
||||
|
||||
if (stream->readFlag()) // BakeInfoMask
|
||||
{
|
||||
stream->read(&mProbeUniqueID);
|
||||
|
||||
mDirty = true;
|
||||
}
|
||||
|
||||
if (stream->readFlag()) // EnabledMask
|
||||
{
|
||||
mEnabled = stream->readFlag();
|
||||
|
||||
mDirty = true;
|
||||
}
|
||||
|
||||
if (stream->readFlag()) // ModeMask
|
||||
{
|
||||
U32 reflectModeType = BakedCubemap;
|
||||
stream->read(&reflectModeType);
|
||||
mReflectionModeType = (ReflectionModeType)reflectModeType;
|
||||
|
||||
mDirty = true;
|
||||
}
|
||||
|
||||
if (stream->readFlag()) // CubemapMask
|
||||
{
|
||||
String newCubemapName;
|
||||
stream->read(&mCubemapName);
|
||||
|
||||
//if (newCubemapName != mCubemapName)
|
||||
{
|
||||
processStaticCubemap();
|
||||
}
|
||||
|
||||
mDirty = true;
|
||||
}
|
||||
|
||||
if (mDirty)
|
||||
{
|
||||
updateProbeParams();
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Object Rendering
|
||||
//-----------------------------------------------------------------------------
|
||||
void ReflectionProbe::updateProbeParams()
|
||||
{
|
||||
if (mProbeInfo == nullptr)
|
||||
{
|
||||
mProbeInfo = new ProbeRenderInst();
|
||||
mProbeInfoIdx = ProbeRenderInst::all.size() - 1;
|
||||
mProbeInfo->mIsEnabled = false;
|
||||
|
||||
PROBEMGR->registerProbe(mProbeInfoIdx);
|
||||
}
|
||||
|
||||
mProbeInfo->mProbeShapeType = mProbeShapeType;
|
||||
|
||||
MatrixF transform = getTransform();
|
||||
mProbeInfo->mPosition = getPosition();
|
||||
|
||||
if (mProbeShapeType == ProbeRenderInst::Sphere)
|
||||
mObjScale.set(mRadius, mRadius, mRadius);
|
||||
|
||||
transform.scale(getScale());
|
||||
mProbeInfo->mTransform = transform.inverse();
|
||||
|
||||
// Skip our transform... it just dirties mask bits.
|
||||
Parent::setTransform(mObjToWorld);
|
||||
|
||||
resetWorldBox();
|
||||
|
||||
mProbeInfo->mBounds = mWorldBox;
|
||||
mProbeInfo->mExtents = getScale();
|
||||
mProbeInfo->mRadius = mRadius;
|
||||
|
||||
mProbeInfo->mIsSkylight = false;
|
||||
|
||||
mProbeInfo->mProbeRefOffset = mProbeRefOffset;
|
||||
mProbeInfo->mProbeRefScale = mProbeRefScale;
|
||||
|
||||
mProbeInfo->mDirty = true;
|
||||
mProbeInfo->mScore = mMaxDrawDistance;
|
||||
}
|
||||
|
||||
void ReflectionProbe::processStaticCubemap()
|
||||
{
|
||||
if (mReflectionModeType != StaticCubemap)
|
||||
return;
|
||||
|
||||
createClientResources();
|
||||
|
||||
Sim::findObject(mCubemapName, mStaticCubemap);
|
||||
|
||||
if (!mStaticCubemap)
|
||||
{
|
||||
Con::errorf("ReflectionProbe::updateMaterial() - unable to find static cubemap file!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (mStaticCubemap->mCubemap == nullptr)
|
||||
{
|
||||
mStaticCubemap->createMap();
|
||||
mStaticCubemap->updateFaces();
|
||||
}
|
||||
|
||||
String prefilPath = getPrefilterMapPath();
|
||||
String irrPath = getIrradianceMapPath();
|
||||
|
||||
if (mUseHDRCaptures)
|
||||
{
|
||||
mIrridianceMap->mCubemap->initDynamic(mPrefilterSize, GFXFormatR16G16B16A16F);
|
||||
mPrefilterMap->mCubemap->initDynamic(mPrefilterSize, GFXFormatR16G16B16A16F);
|
||||
}
|
||||
else
|
||||
{
|
||||
mIrridianceMap->mCubemap->initDynamic(mPrefilterSize, GFXFormatR8G8B8A8);
|
||||
mPrefilterMap->mCubemap->initDynamic(mPrefilterSize, GFXFormatR8G8B8A8);
|
||||
}
|
||||
|
||||
//if (!Platform::isFile(irrPath) || !Platform::isFile(prefilPath))
|
||||
{
|
||||
GFXTextureTargetRef renderTarget = GFX->allocRenderToTextureTarget(false);
|
||||
|
||||
IBLUtilities::GenerateIrradianceMap(renderTarget, mStaticCubemap->mCubemap, mIrridianceMap->mCubemap);
|
||||
IBLUtilities::GeneratePrefilterMap(renderTarget, mStaticCubemap->mCubemap, mPrefilterMipLevels, mPrefilterMap->mCubemap);
|
||||
|
||||
IBLUtilities::SaveCubeMap(getIrradianceMapPath(), mIrridianceMap->mCubemap);
|
||||
IBLUtilities::SaveCubeMap(getPrefilterMapPath(), mPrefilterMap->mCubemap);
|
||||
}
|
||||
|
||||
mProbeInfo->mPrefilterCubemap = mPrefilterMap->mCubemap;
|
||||
mProbeInfo->mIrradianceCubemap = mIrridianceMap->mCubemap;
|
||||
|
||||
//Update the probe manager with our new texture!
|
||||
if(!mProbeInfo->mIsSkylight)
|
||||
PROBEMGR->updateProbeTexture(mProbeInfo);
|
||||
}
|
||||
|
||||
void ReflectionProbe::updateCubemaps()
|
||||
{
|
||||
createClientResources();
|
||||
|
||||
if (mReflectionModeType != DynamicCubemap)
|
||||
{
|
||||
mProbeInfo->mCubeReflector.unregisterReflector();
|
||||
|
||||
if ((mReflectionModeType == BakedCubemap) && !mProbeUniqueID.isEmpty())
|
||||
{
|
||||
if (mPrefilterMap != nullptr && mPrefilterMap->mCubemap.isValid())
|
||||
{
|
||||
mProbeInfo->mPrefilterCubemap = mPrefilterMap->mCubemap;
|
||||
}
|
||||
else
|
||||
{
|
||||
mEnabled = false;
|
||||
}
|
||||
if (mIrridianceMap != nullptr && mIrridianceMap->mCubemap.isValid())
|
||||
{
|
||||
mProbeInfo->mIrradianceCubemap = mIrridianceMap->mCubemap;
|
||||
}
|
||||
else
|
||||
{
|
||||
mEnabled = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (mReflectionModeType == DynamicCubemap && !mDynamicCubemap.isNull())
|
||||
{
|
||||
mProbeInfo->mPrefilterCubemap = mDynamicCubemap;
|
||||
|
||||
mProbeInfo->mCubeReflector.registerReflector(this, reflectorDesc); //need to decide how we wanna do the reflectorDesc. static name or a field
|
||||
}
|
||||
else
|
||||
{
|
||||
mEnabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
//Make us ready to render
|
||||
if (mEnabled)
|
||||
mProbeInfo->mIsEnabled = true;
|
||||
else
|
||||
mProbeInfo->mIsEnabled = false;
|
||||
|
||||
if (!mProbeInfo->mIsSkylight && mProbeInfo->mPrefilterCubemap->isInitialized() && mProbeInfo->mIrradianceCubemap->isInitialized())
|
||||
PROBEMGR->updateProbeTexture(mProbeInfo);
|
||||
}
|
||||
|
||||
bool ReflectionProbe::createClientResources()
|
||||
{
|
||||
//irridiance resources
|
||||
if (!mIrridianceMap)
|
||||
{
|
||||
mIrridianceMap = new CubemapData();
|
||||
mIrridianceMap->registerObject();
|
||||
|
||||
mIrridianceMap->createMap();
|
||||
}
|
||||
|
||||
String irrPath = getIrradianceMapPath();
|
||||
if (Platform::isFile(irrPath))
|
||||
{
|
||||
mIrridianceMap->setCubemapFile(FileName(irrPath));
|
||||
mIrridianceMap->updateFaces();
|
||||
}
|
||||
|
||||
if (mIrridianceMap->mCubemap.isNull())
|
||||
Con::errorf("ReflectionProbe::createClientResources() - Unable to load baked irradiance map at %s", getIrradianceMapPath().c_str());
|
||||
|
||||
//
|
||||
if (!mPrefilterMap)
|
||||
{
|
||||
mPrefilterMap = new CubemapData();
|
||||
mPrefilterMap->registerObject();
|
||||
|
||||
mPrefilterMap->createMap();
|
||||
}
|
||||
|
||||
String prefilPath = getPrefilterMapPath();
|
||||
if (Platform::isFile(prefilPath))
|
||||
{
|
||||
mPrefilterMap->setCubemapFile(FileName(prefilPath));
|
||||
mPrefilterMap->updateFaces();
|
||||
}
|
||||
|
||||
if (mPrefilterMap->mCubemap.isNull())
|
||||
Con::errorf("ReflectionProbe::createClientResources() - Unable to load baked prefilter map at %s", getPrefilterMapPath().c_str());
|
||||
|
||||
mResourcesCreated = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
String ReflectionProbe::getPrefilterMapPath()
|
||||
{
|
||||
if (mProbeUniqueID.isEmpty())
|
||||
{
|
||||
Con::errorf("ReflectionProbe::getPrefilterMapPath() - We don't have a set output path or persistant id, so no valid path can be provided!");
|
||||
return "";
|
||||
}
|
||||
|
||||
String path = Con::getVariable("$pref::ReflectionProbes::CurrentLevelPath", "levels/");
|
||||
|
||||
char fileName[256];
|
||||
dSprintf(fileName, 256, "%s%s_Prefilter.dds", path.c_str(), mProbeUniqueID.c_str());
|
||||
|
||||
return fileName;
|
||||
}
|
||||
|
||||
String ReflectionProbe::getIrradianceMapPath()
|
||||
{
|
||||
if (mProbeUniqueID.isEmpty())
|
||||
{
|
||||
Con::errorf("ReflectionProbe::getIrradianceMapPath() - We don't have a set output path or persistant id, so no valid path can be provided!");
|
||||
return "";
|
||||
}
|
||||
|
||||
String path = Con::getVariable("$pref::ReflectionProbes::CurrentLevelPath", "levels/");
|
||||
|
||||
char fileName[256];
|
||||
dSprintf(fileName, 256, "%s%s_Irradiance.dds", path.c_str(), mProbeUniqueID.c_str());
|
||||
|
||||
return fileName;
|
||||
}
|
||||
|
||||
void ReflectionProbe::bake()
|
||||
{
|
||||
if (mReflectionModeType == DynamicCubemap)
|
||||
return;
|
||||
|
||||
PROBEMGR->bakeProbe(this);
|
||||
|
||||
setMaskBits(CubemapMask);
|
||||
}
|
||||
//-----------------------------------------------------------------------------
|
||||
//Rendering of editing/debug stuff
|
||||
//-----------------------------------------------------------------------------
|
||||
void ReflectionProbe::createGeometry()
|
||||
{
|
||||
// Clean up our previous shape
|
||||
if (mEditorShapeInst)
|
||||
SAFE_DELETE(mEditorShapeInst);
|
||||
|
||||
mEditorShape = NULL;
|
||||
|
||||
String shapeFile = "tools/resources/ReflectProbeSphere.dae";
|
||||
|
||||
// Attempt to get the resource from the ResourceManager
|
||||
mEditorShape = ResourceManager::get().load(shapeFile);
|
||||
if (mEditorShape)
|
||||
{
|
||||
mEditorShapeInst = new TSShapeInstance(mEditorShape, isClientObject());
|
||||
}
|
||||
}
|
||||
|
||||
void ReflectionProbe::prepRenderImage(SceneRenderState *state)
|
||||
{
|
||||
if (!mEnabled || !RenderProbeMgr::smRenderReflectionProbes)
|
||||
return;
|
||||
|
||||
Point3F distVec = getRenderPosition() - state->getCameraPosition();
|
||||
F32 dist = distVec.len();
|
||||
|
||||
//Culling distance. Can be adjusted for performance options considerations via the scalar
|
||||
if (dist > mMaxDrawDistance * Con::getFloatVariable("$pref::GI::ProbeDrawDistScale", 1.0))
|
||||
{
|
||||
mProbeInfo->mScore = mMaxDrawDistance;
|
||||
return;
|
||||
}
|
||||
|
||||
if (mReflectionModeType == DynamicCubemap && mRefreshRateMS < (Platform::getRealMilliseconds() - mDynamicLastBakeMS))
|
||||
{
|
||||
bake();
|
||||
mDynamicLastBakeMS = Platform::getRealMilliseconds();
|
||||
}
|
||||
|
||||
//Submit our probe to actually do the probe action
|
||||
// Get a handy pointer to our RenderPassmanager
|
||||
//RenderPassManager *renderPass = state->getRenderPass();
|
||||
|
||||
//Update our score based on our radius, distance
|
||||
mProbeInfo->mScore = mProbeInfo->mRadius/mMax(dist,1.0f);
|
||||
|
||||
Point3F vect = distVec;
|
||||
vect.normalizeSafe();
|
||||
|
||||
mProbeInfo->mScore *= mMax(mAbs(mDot(vect, state->getCameraTransform().getForwardVector())),0.001f);
|
||||
|
||||
//Register
|
||||
//PROBEMGR->registerProbe(mProbeInfoIdx);
|
||||
|
||||
if (ReflectionProbe::smRenderPreviewProbes && gEditingMission && mEditorShapeInst && mPrefilterMap != nullptr)
|
||||
{
|
||||
GFXTransformSaver saver;
|
||||
|
||||
// Calculate the distance of this object from the camera
|
||||
Point3F cameraOffset;
|
||||
getRenderTransform().getColumn(3, &cameraOffset);
|
||||
cameraOffset -= state->getDiffuseCameraPosition();
|
||||
dist = cameraOffset.len();
|
||||
if (dist < 0.01f)
|
||||
dist = 0.01f;
|
||||
|
||||
// Set up the LOD for the shape
|
||||
F32 invScale = (1.0f / getMax(getMax(mObjScale.x, mObjScale.y), mObjScale.z));
|
||||
|
||||
mEditorShapeInst->setDetailFromDistance(state, dist * invScale);
|
||||
|
||||
// Make sure we have a valid level of detail
|
||||
if (mEditorShapeInst->getCurrentDetail() < 0)
|
||||
return;
|
||||
|
||||
BaseMatInstance* probePrevMat = mEditorShapeInst->getMaterialList()->getMaterialInst(0);
|
||||
|
||||
setPreviewMatParameters(state, probePrevMat);
|
||||
|
||||
// GFXTransformSaver is a handy helper class that restores
|
||||
// the current GFX matrices to their original values when
|
||||
// it goes out of scope at the end of the function
|
||||
|
||||
// Set up our TS render state
|
||||
TSRenderState rdata;
|
||||
rdata.setSceneState(state);
|
||||
rdata.setFadeOverride(1.0f);
|
||||
|
||||
if(mReflectionModeType != DynamicCubemap)
|
||||
rdata.setCubemap(mPrefilterMap->mCubemap);
|
||||
else
|
||||
rdata.setCubemap(mDynamicCubemap);
|
||||
|
||||
// We might have some forward lit materials
|
||||
// so pass down a query to gather lights.
|
||||
LightQuery query;
|
||||
query.init(getWorldSphere());
|
||||
rdata.setLightQuery(&query);
|
||||
|
||||
// Set the world matrix to the objects render transform
|
||||
MatrixF mat = getRenderTransform();
|
||||
GFX->setWorldMatrix(mat);
|
||||
|
||||
// Animate the the shape
|
||||
mEditorShapeInst->animate();
|
||||
|
||||
// Allow the shape to submit the RenderInst(s) for itself
|
||||
mEditorShapeInst->render(rdata);
|
||||
|
||||
saver.restore();
|
||||
}
|
||||
|
||||
// If the light is selected or light visualization
|
||||
// is enabled then register the callback.
|
||||
const bool isSelectedInEditor = (gEditingMission && isSelected());
|
||||
if (isSelectedInEditor)
|
||||
{
|
||||
ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
|
||||
ri->renderDelegate.bind(this, &ReflectionProbe::_onRenderViz);
|
||||
ri->type = RenderPassManager::RIT_Editor;
|
||||
state->getRenderPass()->addInst(ri);
|
||||
}
|
||||
}
|
||||
|
||||
void ReflectionProbe::_onRenderViz(ObjectRenderInst *ri,
|
||||
SceneRenderState *state,
|
||||
BaseMatInstance *overrideMat)
|
||||
{
|
||||
if (!RenderProbeMgr::smRenderReflectionProbes)
|
||||
return;
|
||||
|
||||
GFXDrawUtil *draw = GFX->getDrawUtil();
|
||||
|
||||
GFXStateBlockDesc desc;
|
||||
desc.setZReadWrite(true, false);
|
||||
desc.setCullMode(GFXCullNone);
|
||||
desc.setBlend(true);
|
||||
desc.fillMode = GFXFillWireframe;
|
||||
// Base the sphere color on the light color.
|
||||
ColorI color = ColorI(255, 0, 255, 63);
|
||||
|
||||
const MatrixF worldToObjectXfm = mObjToWorld;
|
||||
if (mProbeShapeType == ProbeRenderInst::Sphere)
|
||||
{
|
||||
draw->drawSphere(desc, mRadius, getPosition(), color);
|
||||
}
|
||||
else
|
||||
{
|
||||
Point3F tscl = worldToObjectXfm.getScale();
|
||||
|
||||
Box3F projCube(-mObjScale/2, mObjScale / 2);
|
||||
projCube.setCenter(getPosition());
|
||||
draw->drawCube(desc, projCube, color, &worldToObjectXfm);
|
||||
}
|
||||
|
||||
Point3F renderPos = getRenderTransform().getPosition();
|
||||
|
||||
Box3F refCube = Box3F(-mProbeRefScale / 2, mProbeRefScale / 2);
|
||||
refCube.setCenter(renderPos + mProbeRefOffset);
|
||||
color = ColorI(0, 255, 255, 63);
|
||||
draw->drawCube(desc, refCube, color, &worldToObjectXfm);
|
||||
}
|
||||
|
||||
void ReflectionProbe::setPreviewMatParameters(SceneRenderState* renderState, BaseMatInstance* mat)
|
||||
{
|
||||
if (!mat->getFeatures().hasFeature(MFT_isDeferred))
|
||||
return;
|
||||
|
||||
//Set up the params
|
||||
MaterialParameters *matParams = mat->getMaterialParameters();
|
||||
|
||||
//Get the deferred render target
|
||||
NamedTexTarget* deferredTexTarget = NamedTexTarget::find("deferred");
|
||||
|
||||
GFXTextureObject *deferredTexObject = deferredTexTarget->getTexture();
|
||||
if (!deferredTexObject)
|
||||
return;
|
||||
|
||||
GFX->setTexture(0, deferredTexObject);
|
||||
|
||||
//Set the cubemap
|
||||
GFX->setCubeTexture(1, mPrefilterMap->mCubemap);
|
||||
|
||||
//Set the invViewMat
|
||||
MatrixSet &matrixSet = renderState->getRenderPass()->getMatrixSet();
|
||||
const MatrixF &worldToCameraXfm = matrixSet.getWorldToCamera();
|
||||
|
||||
MaterialParameterHandle *invViewMat = mat->getMaterialParameterHandle("$invViewMat");
|
||||
|
||||
matParams->setSafe(invViewMat, worldToCameraXfm);
|
||||
}
|
||||
|
||||
DefineEngineMethod(ReflectionProbe, postApply, void, (), ,
|
||||
"A utility method for forcing a network update.\n")
|
||||
{
|
||||
object->inspectPostApply();
|
||||
}
|
||||
|
||||
DefineEngineMethod(ReflectionProbe, Bake, void, (), ,
|
||||
"@brief returns true if control object is inside the fog\n\n.")
|
||||
{
|
||||
ReflectionProbe *clientProbe = (ReflectionProbe*)object->getClientObject();
|
||||
|
||||
if (clientProbe)
|
||||
{
|
||||
clientProbe->bake();
|
||||
}
|
||||
}
|
||||
256
Engine/source/T3D/lighting/reflectionProbe.h
Normal file
256
Engine/source/T3D/lighting/reflectionProbe.h
Normal file
|
|
@ -0,0 +1,256 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// 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.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef REFLECTIONPROBE_H
|
||||
#define REFLECTIONPROBE_H
|
||||
|
||||
#ifndef _SCENEOBJECT_H_
|
||||
#include "scene/sceneObject.h"
|
||||
#endif
|
||||
#ifndef _GFXVERTEXBUFFER_H_
|
||||
#include "gfx/gfxVertexBuffer.h"
|
||||
#endif
|
||||
#ifndef _GFXPRIMITIVEBUFFER_H_
|
||||
#include "gfx/gfxPrimitiveBuffer.h"
|
||||
#endif
|
||||
#ifndef _TSSHAPEINSTANCE_H_
|
||||
#include "ts/tsShapeInstance.h"
|
||||
#endif
|
||||
#include "lighting/lightInfo.h"
|
||||
|
||||
#ifndef _RENDERPASSMANAGER_H_
|
||||
#include "renderInstance/renderPassManager.h"
|
||||
#endif
|
||||
|
||||
#ifndef RENDER_PROBE_MGR_H
|
||||
#include "renderInstance/renderProbeMgr.h"
|
||||
#endif
|
||||
|
||||
class BaseMatInstance;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// This class implements a basic SceneObject that can exist in the world at a
|
||||
// 3D position and render itself. There are several valid ways to render an
|
||||
// object in Torque. This class implements the preferred rendering method which
|
||||
// is to submit a MeshRenderInst along with a Material, vertex buffer,
|
||||
// primitive buffer, and transform and allow the RenderMeshMgr handle the
|
||||
// actual setup and rendering for you.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
class ReflectionProbe : public SceneObject
|
||||
{
|
||||
typedef SceneObject Parent;
|
||||
friend class RenderProbeMgr;
|
||||
|
||||
public:
|
||||
|
||||
enum ReflectionModeType
|
||||
{
|
||||
NoReflection = 0,
|
||||
StaticCubemap = 1,
|
||||
BakedCubemap = 2,
|
||||
DynamicCubemap = 5,
|
||||
};
|
||||
|
||||
protected:
|
||||
|
||||
// Networking masks
|
||||
// We need to implement a mask specifically to handle
|
||||
// updating our transform from the server object to its
|
||||
// client-side "ghost". We also need to implement a
|
||||
// maks for handling editor updates to our properties
|
||||
// (like material).
|
||||
enum MaskBits
|
||||
{
|
||||
TransformMask = Parent::NextFreeMask << 0,
|
||||
UpdateMask = Parent::NextFreeMask << 1,
|
||||
EnabledMask = Parent::NextFreeMask << 2,
|
||||
CubemapMask = Parent::NextFreeMask << 3,
|
||||
ModeMask = Parent::NextFreeMask << 4,
|
||||
RadiusMask = Parent::NextFreeMask << 5,
|
||||
ShapeTypeMask = Parent::NextFreeMask << 6,
|
||||
BakeInfoMask = Parent::NextFreeMask << 7,
|
||||
NextFreeMask = Parent::NextFreeMask << 8
|
||||
};
|
||||
|
||||
bool mBake;
|
||||
bool mEnabled;
|
||||
bool mDirty;
|
||||
|
||||
Resource<TSShape> mEditorShape;
|
||||
TSShapeInstance* mEditorShapeInst;
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Rendering variables
|
||||
//--------------------------------------------------------------------------
|
||||
ProbeRenderInst::ProbeShapeType mProbeShapeType;
|
||||
|
||||
ProbeRenderInst* mProbeInfo;
|
||||
U32 mProbeInfoIdx;
|
||||
|
||||
//Reflection Contribution stuff
|
||||
ReflectionModeType mReflectionModeType;
|
||||
|
||||
F32 mRadius;
|
||||
Point3F mProbeRefOffset;
|
||||
Point3F mProbeRefScale;
|
||||
bool mEditPosOffset;
|
||||
|
||||
String mCubemapName;
|
||||
CubemapData *mStaticCubemap;
|
||||
GFXCubemapHandle mDynamicCubemap;
|
||||
|
||||
String cubeDescName;
|
||||
U32 cubeDescId;
|
||||
ReflectorDesc *reflectorDesc;
|
||||
|
||||
///Prevents us from saving out the cubemaps(for now) but allows us the full HDR range on the in-memory cubemap captures
|
||||
bool mUseHDRCaptures;
|
||||
|
||||
//irridiance resources
|
||||
CubemapData *mIrridianceMap;
|
||||
|
||||
//prefilter resources
|
||||
CubemapData *mPrefilterMap;
|
||||
U32 mPrefilterMipLevels;
|
||||
U32 mPrefilterSize;
|
||||
|
||||
String mProbeUniqueID;
|
||||
|
||||
// Define our vertex format here so we don't have to
|
||||
// change it in multiple spots later
|
||||
typedef GFXVertexPNTTB VertexType;
|
||||
|
||||
// The GFX vertex and primitive buffers
|
||||
GFXVertexBufferHandle< VertexType > mVertexBuffer;
|
||||
GFXPrimitiveBufferHandle mPrimitiveBuffer;
|
||||
|
||||
U32 mSphereVertCount;
|
||||
U32 mSpherePrimitiveCount;
|
||||
|
||||
//Debug rendering
|
||||
static bool smRenderPreviewProbes;
|
||||
|
||||
U32 mDynamicLastBakeMS;
|
||||
U32 mRefreshRateMS;
|
||||
|
||||
GBitmap* mCubeFaceBitmaps[6];
|
||||
U32 mCubemapResolution;
|
||||
|
||||
F32 mMaxDrawDistance;
|
||||
|
||||
bool mResourcesCreated;
|
||||
U32 mCaptureMask;
|
||||
|
||||
public:
|
||||
ReflectionProbe();
|
||||
virtual ~ReflectionProbe();
|
||||
|
||||
// Declare this object as a ConsoleObject so that we can
|
||||
// instantiate it into the world and network it
|
||||
DECLARE_CONOBJECT(ReflectionProbe);
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Object Editing
|
||||
// Since there is always a server and a client object in Torque and we
|
||||
// actually edit the server object we need to implement some basic
|
||||
// networking functions
|
||||
//--------------------------------------------------------------------------
|
||||
// Set up any fields that we want to be editable (like position)
|
||||
static void initPersistFields();
|
||||
|
||||
// Allows the object to update its editable settings
|
||||
// from the server object to the client
|
||||
virtual void inspectPostApply();
|
||||
|
||||
static bool _setEnabled(void *object, const char *index, const char *data);
|
||||
static bool _doBake(void *object, const char *index, const char *data);
|
||||
static bool _toggleEditPosOffset(void *object, const char *index, const char *data);
|
||||
static bool _setRadius(void *object, const char *index, const char *data);
|
||||
static bool _setReflectionMode(void *object, const char *index, const char *data);
|
||||
|
||||
// Handle when we are added to the scene and removed from the scene
|
||||
bool onAdd();
|
||||
void onRemove();
|
||||
|
||||
virtual void handleDeleteAction();
|
||||
|
||||
// Override this so that we can dirty the network flag when it is called
|
||||
virtual void setTransform(const MatrixF &mat);
|
||||
virtual const MatrixF& getTransform() const;
|
||||
virtual void setScale(const VectorF &scale);
|
||||
virtual const VectorF& getScale() const;
|
||||
|
||||
virtual bool writeField(StringTableEntry fieldname, const char *value);
|
||||
|
||||
// This function handles sending the relevant data from the server
|
||||
// object to the client object
|
||||
U32 packUpdate(NetConnection *conn, U32 mask, BitStream *stream);
|
||||
// This function handles receiving relevant data from the server
|
||||
// object and applying it to the client object
|
||||
void unpackUpdate(NetConnection *conn, BitStream *stream);
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Object Rendering
|
||||
// Torque utilizes a "batch" rendering system. This means that it builds a
|
||||
// list of objects that need to render (via RenderInst's) and then renders
|
||||
// them all in one batch. This allows it to optimized on things like
|
||||
// minimizing texture, state, and shader switching by grouping objects that
|
||||
// use the same Materials.
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
// Create the geometry for rendering
|
||||
void createGeometry();
|
||||
|
||||
// Get the Material instance
|
||||
void updateCubemaps();
|
||||
|
||||
virtual void updateProbeParams();
|
||||
|
||||
bool createClientResources();
|
||||
|
||||
void processStaticCubemap();
|
||||
|
||||
// This is the function that allows this object to submit itself for rendering
|
||||
void prepRenderImage(SceneRenderState *state);
|
||||
|
||||
void _onRenderViz(ObjectRenderInst *ri,
|
||||
SceneRenderState *state,
|
||||
BaseMatInstance *overrideMat);
|
||||
|
||||
void setPreviewMatParameters(SceneRenderState* renderState, BaseMatInstance* mat);
|
||||
|
||||
//Baking
|
||||
String getPrefilterMapPath();
|
||||
String getIrradianceMapPath();
|
||||
void bake();
|
||||
|
||||
const U32 getProbeInfoIndex() { return mProbeInfoIdx; }
|
||||
};
|
||||
|
||||
typedef ProbeRenderInst::ProbeShapeType ReflectProbeType;
|
||||
DefineEnumType(ReflectProbeType);
|
||||
|
||||
typedef ReflectionProbe::ReflectionModeType ReflectionModeEnum;
|
||||
DefineEnumType(ReflectionModeEnum);
|
||||
|
||||
#endif // _ReflectionProbe_H_
|
||||
276
Engine/source/T3D/lighting/skylight.cpp
Normal file
276
Engine/source/T3D/lighting/skylight.cpp
Normal file
|
|
@ -0,0 +1,276 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// 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 "T3D/lighting/Skylight.h"
|
||||
#include "math/mathIO.h"
|
||||
#include "scene/sceneRenderState.h"
|
||||
#include "console/consoleTypes.h"
|
||||
#include "core/stream/bitStream.h"
|
||||
#include "materials/baseMatInstance.h"
|
||||
#include "console/engineAPI.h"
|
||||
#include "gfx/gfxDrawUtil.h"
|
||||
#include "gfx/gfxDebugEvent.h"
|
||||
#include "gfx/gfxTransformSaver.h"
|
||||
#include "math/mathUtils.h"
|
||||
#include "gfx/bitmap/gBitmap.h"
|
||||
#include "core/stream/fileStream.h"
|
||||
#include "core/fileObject.h"
|
||||
#include "core/resourceManager.h"
|
||||
#include "console/simPersistId.h"
|
||||
#include "T3D/gameFunctions.h"
|
||||
#include "postFx/postEffect.h"
|
||||
#include "renderInstance/renderProbeMgr.h"
|
||||
#include "renderInstance/renderProbeMgr.h"
|
||||
|
||||
#include "math/util/sphereMesh.h"
|
||||
#include "materials/materialManager.h"
|
||||
#include "math/util/matrixSet.h"
|
||||
#include "gfx/bitmap/cubemapSaver.h"
|
||||
|
||||
#include "materials/materialFeatureTypes.h"
|
||||
|
||||
#include "materials/shaderData.h"
|
||||
#include "gfx/gfxTextureManager.h"
|
||||
|
||||
#include "gfx/bitmap/imageUtils.h"
|
||||
|
||||
#include "T3D/lighting/IBLUtilities.h"
|
||||
|
||||
extern bool gEditingMission;
|
||||
extern ColorI gCanvasClearColor;
|
||||
bool Skylight::smRenderSkylights = true;
|
||||
|
||||
IMPLEMENT_CO_NETOBJECT_V1(Skylight);
|
||||
|
||||
ConsoleDocClass(Skylight,
|
||||
"@brief An example scene object which renders a mesh.\n\n"
|
||||
"This class implements a basic SceneObject that can exist in the world at a "
|
||||
"3D position and render itself. There are several valid ways to render an "
|
||||
"object in Torque. This class implements the preferred rendering method which "
|
||||
"is to submit a MeshRenderInst along with a Material, vertex buffer, "
|
||||
"primitive buffer, and transform and allow the RenderMeshMgr handle the "
|
||||
"actual setup and rendering for you.\n\n"
|
||||
"See the C++ code for implementation details.\n\n"
|
||||
"@ingroup Examples\n");
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Object setup and teardown
|
||||
//-----------------------------------------------------------------------------
|
||||
Skylight::Skylight() : ReflectionProbe()
|
||||
{
|
||||
mCaptureMask = SKYLIGHT_CAPTURE_TYPEMASK;
|
||||
}
|
||||
|
||||
Skylight::~Skylight()
|
||||
{
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Object Editing
|
||||
//-----------------------------------------------------------------------------
|
||||
void Skylight::initPersistFields()
|
||||
{
|
||||
// SceneObject already handles exposing the transform
|
||||
Parent::initPersistFields();
|
||||
|
||||
removeField("radius");
|
||||
removeField("scale");
|
||||
removeField("EditPosOffset");
|
||||
removeField("refOffset");
|
||||
removeField("refScale");
|
||||
}
|
||||
|
||||
void Skylight::inspectPostApply()
|
||||
{
|
||||
Parent::inspectPostApply();
|
||||
|
||||
mDirty = true;
|
||||
|
||||
// Flag the network mask to send the updates
|
||||
// to the client object
|
||||
setMaskBits(-1);
|
||||
}
|
||||
|
||||
bool Skylight::onAdd()
|
||||
{
|
||||
if (!Parent::onAdd())
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Skylight::onRemove()
|
||||
{
|
||||
Parent::onRemove();
|
||||
}
|
||||
|
||||
void Skylight::setTransform(const MatrixF & mat)
|
||||
{
|
||||
// Let SceneObject handle all of the matrix manipulation
|
||||
Parent::setTransform(mat);
|
||||
|
||||
mDirty = true;
|
||||
|
||||
// Dirty our network mask so that the new transform gets
|
||||
// transmitted to the client object
|
||||
setMaskBits(TransformMask);
|
||||
}
|
||||
|
||||
U32 Skylight::packUpdate(NetConnection *conn, U32 mask, BitStream *stream)
|
||||
{
|
||||
// Allow the Parent to get a crack at writing its info
|
||||
U32 retMask = Parent::packUpdate(conn, mask, stream);
|
||||
|
||||
return retMask;
|
||||
}
|
||||
|
||||
void Skylight::unpackUpdate(NetConnection *conn, BitStream *stream)
|
||||
{
|
||||
// Let the Parent read any info it sent
|
||||
Parent::unpackUpdate(conn, stream);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Object Rendering
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
void Skylight::updateProbeParams()
|
||||
{
|
||||
Parent::updateProbeParams();
|
||||
|
||||
mProbeInfo->mProbeShapeType = ProbeRenderInst::Skylight;
|
||||
|
||||
mProbeInfo->setPosition(getPosition());
|
||||
|
||||
// Skip our transform... it just dirties mask bits.
|
||||
Parent::setTransform(mObjToWorld);
|
||||
|
||||
resetWorldBox();
|
||||
|
||||
F32 visDist = gClientSceneGraph->getVisibleDistance();
|
||||
Box3F skylightBounds = Box3F(visDist * 2);
|
||||
|
||||
skylightBounds.setCenter(Point3F::Zero);
|
||||
|
||||
mProbeInfo->setPosition(Point3F::Zero);
|
||||
|
||||
mProbeInfo->mBounds = skylightBounds;
|
||||
|
||||
setGlobalBounds();
|
||||
|
||||
mProbeInfo->mIsSkylight = true;
|
||||
mProbeInfo->mScore = -1.0f; //sky comes first
|
||||
|
||||
PROBEMGR->updateProbes();
|
||||
|
||||
updateCubemaps();
|
||||
}
|
||||
|
||||
void Skylight::prepRenderImage(SceneRenderState *state)
|
||||
{
|
||||
if (!mEnabled || !Skylight::smRenderSkylights)
|
||||
return;
|
||||
|
||||
//special hook-in for skylights
|
||||
Point3F camPos = state->getCameraPosition();
|
||||
mProbeInfo->mBounds.setCenter(camPos);
|
||||
|
||||
mProbeInfo->setPosition(camPos);
|
||||
|
||||
//Submit our probe to actually do the probe action
|
||||
// Get a handy pointer to our RenderPassmanager
|
||||
//RenderPassManager *renderPass = state->getRenderPass();
|
||||
|
||||
//PROBEMGR->registerSkylight(mProbeInfo, this);
|
||||
|
||||
if (Skylight::smRenderPreviewProbes && gEditingMission && mEditorShapeInst && mPrefilterMap != nullptr)
|
||||
{
|
||||
GFXTransformSaver saver;
|
||||
|
||||
// Calculate the distance of this object from the camera
|
||||
Point3F cameraOffset;
|
||||
getRenderTransform().getColumn(3, &cameraOffset);
|
||||
cameraOffset -= state->getDiffuseCameraPosition();
|
||||
F32 dist = cameraOffset.len();
|
||||
if (dist < 0.01f)
|
||||
dist = 0.01f;
|
||||
|
||||
// Set up the LOD for the shape
|
||||
F32 invScale = (1.0f / getMax(getMax(mObjScale.x, mObjScale.y), mObjScale.z));
|
||||
|
||||
mEditorShapeInst->setDetailFromDistance(state, dist * invScale);
|
||||
|
||||
// Make sure we have a valid level of detail
|
||||
if (mEditorShapeInst->getCurrentDetail() < 0)
|
||||
return;
|
||||
|
||||
BaseMatInstance* probePrevMat = mEditorShapeInst->getMaterialList()->getMaterialInst(0);
|
||||
|
||||
setPreviewMatParameters(state, probePrevMat);
|
||||
|
||||
// GFXTransformSaver is a handy helper class that restores
|
||||
// the current GFX matrices to their original values when
|
||||
// it goes out of scope at the end of the function
|
||||
|
||||
// Set up our TS render state
|
||||
TSRenderState rdata;
|
||||
rdata.setSceneState(state);
|
||||
rdata.setFadeOverride(1.0f);
|
||||
|
||||
// We might have some forward lit materials
|
||||
// so pass down a query to gather lights.
|
||||
LightQuery query;
|
||||
query.init(getWorldSphere());
|
||||
rdata.setLightQuery(&query);
|
||||
|
||||
// Set the world matrix to the objects render transform
|
||||
MatrixF mat = getRenderTransform();
|
||||
mat.scale(Point3F(1, 1, 1));
|
||||
GFX->setWorldMatrix(mat);
|
||||
|
||||
// Animate the the shape
|
||||
mEditorShapeInst->animate();
|
||||
|
||||
// Allow the shape to submit the RenderInst(s) for itself
|
||||
mEditorShapeInst->render(rdata);
|
||||
|
||||
saver.restore();
|
||||
}
|
||||
|
||||
// If the light is selected or light visualization
|
||||
// is enabled then register the callback.
|
||||
const bool isSelectedInEditor = (gEditingMission && isSelected());
|
||||
if (isSelectedInEditor)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
void Skylight::setPreviewMatParameters(SceneRenderState* renderState, BaseMatInstance* mat)
|
||||
{
|
||||
Parent::setPreviewMatParameters(renderState, mat);
|
||||
}
|
||||
|
||||
DefineEngineMethod(Skylight, postApply, void, (), ,
|
||||
"A utility method for forcing a network update.\n")
|
||||
{
|
||||
object->inspectPostApply();
|
||||
}
|
||||
115
Engine/source/T3D/lighting/skylight.h
Normal file
115
Engine/source/T3D/lighting/skylight.h
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// 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.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef SKYLIGHT_H
|
||||
#define SKYLIGHT_H
|
||||
|
||||
#ifndef REFLECTIONPROBE_H
|
||||
#include "T3D/lighting/reflectionProbe.h"
|
||||
#endif
|
||||
#ifndef _GFXVERTEXBUFFER_H_
|
||||
#include "gfx/gfxVertexBuffer.h"
|
||||
#endif
|
||||
#ifndef _GFXPRIMITIVEBUFFER_H_
|
||||
#include "gfx/gfxPrimitiveBuffer.h"
|
||||
#endif
|
||||
#ifndef _TSSHAPEINSTANCE_H_
|
||||
#include "ts/tsShapeInstance.h"
|
||||
#endif
|
||||
#include "lighting/lightInfo.h"
|
||||
|
||||
#ifndef _RENDERPASSMANAGER_H_
|
||||
#include "renderInstance/renderPassManager.h"
|
||||
#endif
|
||||
|
||||
class BaseMatInstance;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// This class implements a basic SceneObject that can exist in the world at a
|
||||
// 3D position and render itself. There are several valid ways to render an
|
||||
// object in Torque. This class implements the preferred rendering method which
|
||||
// is to submit a MeshRenderInst along with a Material, vertex buffer,
|
||||
// primitive buffer, and transform and allow the RenderMeshMgr handle the
|
||||
// actual setup and rendering for you.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
class Skylight : public ReflectionProbe
|
||||
{
|
||||
typedef ReflectionProbe Parent;
|
||||
|
||||
private:
|
||||
|
||||
//Debug rendering
|
||||
static bool smRenderSkylights;
|
||||
|
||||
public:
|
||||
Skylight();
|
||||
virtual ~Skylight();
|
||||
|
||||
// Declare this object as a ConsoleObject so that we can
|
||||
// instantiate it into the world and network it
|
||||
DECLARE_CONOBJECT(Skylight);
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Object Editing
|
||||
// Since there is always a server and a client object in Torque and we
|
||||
// actually edit the server object we need to implement some basic
|
||||
// networking functions
|
||||
//--------------------------------------------------------------------------
|
||||
// Set up any fields that we want to be editable (like position)
|
||||
static void initPersistFields();
|
||||
|
||||
// Allows the object to update its editable settings
|
||||
// from the server object to the client
|
||||
virtual void inspectPostApply();
|
||||
|
||||
// Handle when we are added to the scene and removed from the scene
|
||||
bool onAdd();
|
||||
void onRemove();
|
||||
|
||||
// Override this so that we can dirty the network flag when it is called
|
||||
void setTransform(const MatrixF &mat);
|
||||
|
||||
// This function handles sending the relevant data from the server
|
||||
// object to the client object
|
||||
U32 packUpdate(NetConnection *conn, U32 mask, BitStream *stream);
|
||||
// This function handles receiving relevant data from the server
|
||||
// object and applying it to the client object
|
||||
void unpackUpdate(NetConnection *conn, BitStream *stream);
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Object Rendering
|
||||
// Torque utilizes a "batch" rendering system. This means that it builds a
|
||||
// list of objects that need to render (via RenderInst's) and then renders
|
||||
// them all in one batch. This allows it to optimized on things like
|
||||
// minimizing texture, state, and shader switching by grouping objects that
|
||||
// use the same Materials.
|
||||
//--------------------------------------------------------------------------
|
||||
virtual void updateProbeParams();
|
||||
|
||||
// This is the function that allows this object to submit itself for rendering
|
||||
void prepRenderImage(SceneRenderState *state);
|
||||
|
||||
void setPreviewMatParameters(SceneRenderState* renderState, BaseMatInstance* mat);
|
||||
};
|
||||
|
||||
#endif // _Skylight_H_
|
||||
237
Engine/source/T3D/lighting/sphereEnvironmentProbe.cpp
Normal file
237
Engine/source/T3D/lighting/sphereEnvironmentProbe.cpp
Normal file
|
|
@ -0,0 +1,237 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// 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 "T3D/lighting/sphereEnvironmentProbe.h"
|
||||
#include "math/mathIO.h"
|
||||
#include "scene/sceneRenderState.h"
|
||||
#include "console/consoleTypes.h"
|
||||
#include "core/stream/bitStream.h"
|
||||
#include "materials/baseMatInstance.h"
|
||||
#include "console/engineAPI.h"
|
||||
#include "gfx/gfxDrawUtil.h"
|
||||
#include "gfx/gfxDebugEvent.h"
|
||||
#include "gfx/gfxTransformSaver.h"
|
||||
#include "math/mathUtils.h"
|
||||
#include "gfx/bitmap/gBitmap.h"
|
||||
#include "core/stream/fileStream.h"
|
||||
#include "core/fileObject.h"
|
||||
#include "core/resourceManager.h"
|
||||
#include "console/simPersistId.h"
|
||||
#include "T3D/gameFunctions.h"
|
||||
#include "postFx/postEffect.h"
|
||||
#include "renderInstance/renderProbeMgr.h"
|
||||
#include "renderInstance/renderProbeMgr.h"
|
||||
|
||||
#include "math/util/sphereMesh.h"
|
||||
#include "materials/materialManager.h"
|
||||
#include "math/util/matrixSet.h"
|
||||
#include "gfx/bitmap/cubemapSaver.h"
|
||||
|
||||
#include "materials/materialFeatureTypes.h"
|
||||
|
||||
#include "materials/shaderData.h"
|
||||
#include "gfx/gfxTextureManager.h"
|
||||
|
||||
#include "gfx/bitmap/imageUtils.h"
|
||||
|
||||
#include "T3D/lighting/IBLUtilities.h"
|
||||
|
||||
extern bool gEditingMission;
|
||||
extern ColorI gCanvasClearColor;
|
||||
|
||||
IMPLEMENT_CO_NETOBJECT_V1(SphereEnvironmentProbe);
|
||||
|
||||
ConsoleDocClass(SphereEnvironmentProbe,
|
||||
"@brief An example scene object which renders a mesh.\n\n"
|
||||
"This class implements a basic SceneObject that can exist in the world at a "
|
||||
"3D position and render itself. There are several valid ways to render an "
|
||||
"object in Torque. This class implements the preferred rendering method which "
|
||||
"is to submit a MeshRenderInst along with a Material, vertex buffer, "
|
||||
"primitive buffer, and transform and allow the RenderMeshMgr handle the "
|
||||
"actual setup and rendering for you.\n\n"
|
||||
"See the C++ code for implementation details.\n\n"
|
||||
"@ingroup Examples\n");
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Object setup and teardown
|
||||
//-----------------------------------------------------------------------------
|
||||
SphereEnvironmentProbe::SphereEnvironmentProbe() : ReflectionProbe()
|
||||
{
|
||||
mCaptureMask = REFLECTION_PROBE_CAPTURE_TYPEMASK;
|
||||
mProbeShapeType = ProbeRenderInst::Sphere;
|
||||
}
|
||||
|
||||
SphereEnvironmentProbe::~SphereEnvironmentProbe()
|
||||
{
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Object Editing
|
||||
//-----------------------------------------------------------------------------
|
||||
void SphereEnvironmentProbe::initPersistFields()
|
||||
{
|
||||
// SceneObject already handles exposing the transform
|
||||
Parent::initPersistFields();
|
||||
|
||||
removeField("scale");
|
||||
}
|
||||
|
||||
void SphereEnvironmentProbe::inspectPostApply()
|
||||
{
|
||||
Parent::inspectPostApply();
|
||||
|
||||
mDirty = true;
|
||||
|
||||
// Flag the network mask to send the updates
|
||||
// to the client object
|
||||
setMaskBits(-1);
|
||||
}
|
||||
|
||||
bool SphereEnvironmentProbe::onAdd()
|
||||
{
|
||||
if (!Parent::onAdd())
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void SphereEnvironmentProbe::onRemove()
|
||||
{
|
||||
Parent::onRemove();
|
||||
}
|
||||
|
||||
void SphereEnvironmentProbe::setTransform(const MatrixF & mat)
|
||||
{
|
||||
// Let SceneObject handle all of the matrix manipulation
|
||||
Parent::setTransform(mat);
|
||||
|
||||
mDirty = true;
|
||||
|
||||
// Dirty our network mask so that the new transform gets
|
||||
// transmitted to the client object
|
||||
setMaskBits(TransformMask);
|
||||
}
|
||||
|
||||
U32 SphereEnvironmentProbe::packUpdate(NetConnection *conn, U32 mask, BitStream *stream)
|
||||
{
|
||||
// Allow the Parent to get a crack at writing its info
|
||||
U32 retMask = Parent::packUpdate(conn, mask, stream);
|
||||
|
||||
return retMask;
|
||||
}
|
||||
|
||||
void SphereEnvironmentProbe::unpackUpdate(NetConnection *conn, BitStream *stream)
|
||||
{
|
||||
// Let the Parent read any info it sent
|
||||
Parent::unpackUpdate(conn, stream);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Object Rendering
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
void SphereEnvironmentProbe::updateProbeParams()
|
||||
{
|
||||
Parent::updateProbeParams();
|
||||
|
||||
mProbeInfo->mProbeShapeType = ProbeRenderInst::Sphere;
|
||||
|
||||
PROBEMGR->updateProbes();
|
||||
|
||||
updateCubemaps();
|
||||
}
|
||||
|
||||
void SphereEnvironmentProbe::prepRenderImage(SceneRenderState *state)
|
||||
{
|
||||
if (!mEnabled || !ReflectionProbe::smRenderPreviewProbes)
|
||||
return;
|
||||
|
||||
if (ReflectionProbe::smRenderPreviewProbes && gEditingMission && mEditorShapeInst && mPrefilterMap != nullptr)
|
||||
{
|
||||
GFXTransformSaver saver;
|
||||
|
||||
// Calculate the distance of this object from the camera
|
||||
Point3F cameraOffset;
|
||||
getRenderTransform().getColumn(3, &cameraOffset);
|
||||
cameraOffset -= state->getDiffuseCameraPosition();
|
||||
F32 dist = cameraOffset.len();
|
||||
if (dist < 0.01f)
|
||||
dist = 0.01f;
|
||||
|
||||
// Set up the LOD for the shape
|
||||
F32 invScale = (1.0f / getMax(getMax(mObjScale.x, mObjScale.y), mObjScale.z));
|
||||
|
||||
mEditorShapeInst->setDetailFromDistance(state, dist * invScale);
|
||||
|
||||
// Make sure we have a valid level of detail
|
||||
if (mEditorShapeInst->getCurrentDetail() < 0)
|
||||
return;
|
||||
|
||||
BaseMatInstance* probePrevMat = mEditorShapeInst->getMaterialList()->getMaterialInst(0);
|
||||
|
||||
setPreviewMatParameters(state, probePrevMat);
|
||||
|
||||
// GFXTransformSaver is a handy helper class that restores
|
||||
// the current GFX matrices to their original values when
|
||||
// it goes out of scope at the end of the function
|
||||
|
||||
// Set up our TS render state
|
||||
TSRenderState rdata;
|
||||
rdata.setSceneState(state);
|
||||
rdata.setFadeOverride(1.0f);
|
||||
|
||||
// We might have some forward lit materials
|
||||
// so pass down a query to gather lights.
|
||||
LightQuery query;
|
||||
query.init(getWorldSphere());
|
||||
rdata.setLightQuery(&query);
|
||||
|
||||
// Set the world matrix to the objects render transform
|
||||
MatrixF mat = getRenderTransform();
|
||||
mat.scale(Point3F(1, 1, 1));
|
||||
GFX->setWorldMatrix(mat);
|
||||
|
||||
// Animate the the shape
|
||||
mEditorShapeInst->animate();
|
||||
|
||||
// Allow the shape to submit the RenderInst(s) for itself
|
||||
mEditorShapeInst->render(rdata);
|
||||
|
||||
saver.restore();
|
||||
}
|
||||
|
||||
// If the light is selected or light visualization
|
||||
// is enabled then register the callback.
|
||||
const bool isSelectedInEditor = (gEditingMission && isSelected());
|
||||
if (isSelectedInEditor)
|
||||
{
|
||||
ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
|
||||
ri->renderDelegate.bind(this, &ReflectionProbe::_onRenderViz);
|
||||
ri->type = RenderPassManager::RIT_Editor;
|
||||
state->getRenderPass()->addInst(ri);
|
||||
}
|
||||
}
|
||||
|
||||
void SphereEnvironmentProbe::setPreviewMatParameters(SceneRenderState* renderState, BaseMatInstance* mat)
|
||||
{
|
||||
Parent::setPreviewMatParameters(renderState, mat);
|
||||
}
|
||||
111
Engine/source/T3D/lighting/sphereEnvironmentProbe.h
Normal file
111
Engine/source/T3D/lighting/sphereEnvironmentProbe.h
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// 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.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef SPHERE_ENVIRONMENT_PROBE_H
|
||||
#define SPHERE_ENVIRONMENT_PROBE_H
|
||||
|
||||
#ifndef REFLECTIONPROBE_H
|
||||
#include "T3D/lighting/reflectionProbe.h"
|
||||
#endif
|
||||
#ifndef _GFXVERTEXBUFFER_H_
|
||||
#include "gfx/gfxVertexBuffer.h"
|
||||
#endif
|
||||
#ifndef _GFXPRIMITIVEBUFFER_H_
|
||||
#include "gfx/gfxPrimitiveBuffer.h"
|
||||
#endif
|
||||
#ifndef _TSSHAPEINSTANCE_H_
|
||||
#include "ts/tsShapeInstance.h"
|
||||
#endif
|
||||
#include "lighting/lightInfo.h"
|
||||
|
||||
#ifndef _RENDERPASSMANAGER_H_
|
||||
#include "renderInstance/renderPassManager.h"
|
||||
#endif
|
||||
|
||||
class BaseMatInstance;
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// This class implements a basic SceneObject that can exist in the world at a
|
||||
// 3D position and render itself. There are several valid ways to render an
|
||||
// object in Torque. This class implements the preferred rendering method which
|
||||
// is to submit a MeshRenderInst along with a Material, vertex buffer,
|
||||
// primitive buffer, and transform and allow the RenderMeshMgr handle the
|
||||
// actual setup and rendering for you.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
class SphereEnvironmentProbe : public ReflectionProbe
|
||||
{
|
||||
typedef ReflectionProbe Parent;
|
||||
|
||||
public:
|
||||
SphereEnvironmentProbe();
|
||||
virtual ~SphereEnvironmentProbe();
|
||||
|
||||
// Declare this object as a ConsoleObject so that we can
|
||||
// instantiate it into the world and network it
|
||||
DECLARE_CONOBJECT(SphereEnvironmentProbe);
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Object Editing
|
||||
// Since there is always a server and a client object in Torque and we
|
||||
// actually edit the server object we need to implement some basic
|
||||
// networking functions
|
||||
//--------------------------------------------------------------------------
|
||||
// Set up any fields that we want to be editable (like position)
|
||||
static void initPersistFields();
|
||||
|
||||
// Allows the object to update its editable settings
|
||||
// from the server object to the client
|
||||
virtual void inspectPostApply();
|
||||
|
||||
// Handle when we are added to the scene and removed from the scene
|
||||
bool onAdd();
|
||||
void onRemove();
|
||||
|
||||
// Override this so that we can dirty the network flag when it is called
|
||||
void setTransform(const MatrixF &mat);
|
||||
|
||||
// This function handles sending the relevant data from the server
|
||||
// object to the client object
|
||||
U32 packUpdate(NetConnection *conn, U32 mask, BitStream *stream);
|
||||
// This function handles receiving relevant data from the server
|
||||
// object and applying it to the client object
|
||||
void unpackUpdate(NetConnection *conn, BitStream *stream);
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Object Rendering
|
||||
// Torque utilizes a "batch" rendering system. This means that it builds a
|
||||
// list of objects that need to render (via RenderInst's) and then renders
|
||||
// them all in one batch. This allows it to optimized on things like
|
||||
// minimizing texture, state, and shader switching by grouping objects that
|
||||
// use the same Materials.
|
||||
//--------------------------------------------------------------------------
|
||||
virtual void updateProbeParams();
|
||||
|
||||
// This is the function that allows this object to submit itself for rendering
|
||||
void prepRenderImage(SceneRenderState *state);
|
||||
|
||||
void setPreviewMatParameters(SceneRenderState* renderState, BaseMatInstance* mat);
|
||||
};
|
||||
|
||||
#endif // SPHERE_ENVIRONMENT_PROBE_H
|
||||
|
|
@ -82,7 +82,7 @@ ConsoleDocClass( MissionMarker,
|
|||
|
||||
MissionMarker::MissionMarker()
|
||||
{
|
||||
mTypeMask |= StaticObjectType;
|
||||
mTypeMask |= StaticObjectType | MarkerObjectType;
|
||||
mDataBlock = 0;
|
||||
mAddedToScene = false;
|
||||
mNetFlags.set(Ghostable | ScopeAlways);
|
||||
|
|
|
|||
|
|
@ -216,7 +216,10 @@ enum SceneObjectTypeMasks : U32
|
|||
///
|
||||
/// @note Terrains have their own means for rendering inside interior zones.
|
||||
OUTDOOR_OBJECT_TYPEMASK = ( TerrainObjectType |
|
||||
EnvironmentObjectType )
|
||||
EnvironmentObjectType ),
|
||||
|
||||
SKYLIGHT_CAPTURE_TYPEMASK = (EnvironmentObjectType),
|
||||
REFLECTION_PROBE_CAPTURE_TYPEMASK = (StaticObjectType | StaticShapeObjectType)
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1176,6 +1176,13 @@ void ShapeBase::onRemove()
|
|||
for (S32 i = 0; i < MaxSoundThreads; i++)
|
||||
stopAudio(i);
|
||||
|
||||
// Accumulation and environment mapping
|
||||
if (isClientObject() && mShapeInstance)
|
||||
{
|
||||
if (mShapeInstance->hasAccumulation())
|
||||
AccumulationVolume::removeObject(this);
|
||||
}
|
||||
|
||||
if ( isClientObject() )
|
||||
{
|
||||
mCubeReflector.unregisterReflector();
|
||||
|
|
@ -3724,6 +3731,18 @@ void ShapeBase::setCurrentWaterObject( WaterObject *obj )
|
|||
mCurrentWaterObject = obj;
|
||||
}
|
||||
|
||||
void ShapeBase::setTransform(const MatrixF & mat)
|
||||
{
|
||||
Parent::setTransform(mat);
|
||||
|
||||
// Accumulation and environment mapping
|
||||
if (isClientObject() && mShapeInstance)
|
||||
{
|
||||
if (mShapeInstance->hasAccumulation())
|
||||
AccumulationVolume::updateObject(this);
|
||||
}
|
||||
}
|
||||
|
||||
void ShapeBase::notifyCollisionCallbacks(SceneObject* obj, const VectorF& vel)
|
||||
{
|
||||
for (S32 i = 0; i < collision_callbacks.size(); i++)
|
||||
|
|
|
|||
|
|
@ -1842,7 +1842,7 @@ public:
|
|||
virtual WaterObject* getCurrentWaterObject();
|
||||
|
||||
void setCurrentWaterObject( WaterObject *obj );
|
||||
|
||||
void setTransform(const MatrixF & mat);
|
||||
virtual F32 getMass() const { return mMass; }
|
||||
|
||||
/// @name Network
|
||||
|
|
|
|||
|
|
@ -1,30 +0,0 @@
|
|||
#pragma once
|
||||
#include "console/engineAPI.h"
|
||||
|
||||
template<typename T>
|
||||
class SystemInterface
|
||||
{
|
||||
public:
|
||||
bool mIsEnabled;
|
||||
bool mIsServer;
|
||||
|
||||
static Vector<T*> all;
|
||||
|
||||
SystemInterface()
|
||||
{
|
||||
all.push_back((T*)this);
|
||||
}
|
||||
|
||||
virtual ~SystemInterface()
|
||||
{
|
||||
for (U32 i = 0; i < all.size(); i++)
|
||||
{
|
||||
if (all[i] == (T*)this)
|
||||
{
|
||||
all.erase(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
template<typename T> Vector<T*> SystemInterface<T>::all(0);
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
#pragma once
|
||||
#include "scene/sceneRenderState.h"
|
||||
#include "T3D/systems/componentSystem.h"
|
||||
#include "core/util/SystemInterfaceList.h"
|
||||
#include "ts/tsShape.h"
|
||||
#include "ts/tsShapeInstance.h"
|
||||
#include "T3D/assets/ShapeAsset.h"
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
#pragma once
|
||||
#include "componentSystem.h"
|
||||
#include "core/util/SystemInterfaceList.h"
|
||||
|
||||
class UpdateSystemInterface : public SystemInterface<UpdateSystemInterface>
|
||||
{
|
||||
|
|
|
|||
|
|
@ -377,6 +377,10 @@ bool TSStatic::_createShape()
|
|||
resetWorldBox();
|
||||
|
||||
mShapeInstance = new TSShapeInstance( mShape, isClientObject() );
|
||||
if (isClientObject())
|
||||
{
|
||||
mShapeInstance->cloneMaterialList();
|
||||
}
|
||||
|
||||
if (isClientObject())
|
||||
mShapeInstance->cloneMaterialList();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue