windDeformation.cpp
Engine/source/forest/windDeformation.cpp
Public Functions
ImplementFeatureType(MFT_WindEffect , MFG_PreTransform , 0 , false )
Detailed Description
Public Functions
ImplementFeatureType(MFT_WindEffect , MFG_PreTransform , 0 , false )
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "platform/platform.h" 25#include "forest/windDeformation.h" 26 27#include "forest/forestItem.h" 28#include "forest/forestWindMgr.h" 29#include "forest/forestWindAccumulator.h" 30#include "materials/sceneData.h" 31#include "scene/sceneRenderState.h" 32#include "gfx/gfxShader.h" 33#include "gfx/gfxStructs.h" 34 35 36ImplementFeatureType( MFT_WindEffect, MFG_PreTransform, 0, false ); 37 38 39void WindDeformationConstHandles::init( GFXShader *shader ) 40{ 41 mWindDirAndSpeed = shader->getShaderConstHandle( "$windDirAndSpeed" ); 42 mWindParams = shader->getShaderConstHandle( "$windParams" ); 43} 44 45void WindDeformationConstHandles::setConsts( SceneRenderState *state, 46 const SceneData &sgData, 47 GFXShaderConstBuffer *buffer ) 48{ 49 PROFILE_SCOPE( WindDeformationConstHandles_setConsts ); 50 51 Point3F windDir( 0, 0, 0 ); 52 F32 windSpeed = 0; 53 F32 bendScale = 0; 54 F32 branchAmp = 0; 55 F32 detailAmp = 0; 56 F32 detailFreq = 0; 57 58 const ForestItem *item = (const ForestItem*)sgData.materialHint; 59 60 ForestWindAccumulator *wind = NULL; 61 if ( item ) 62 { 63 // First setup the per-datablock shader constants. 64 // 65 // These cannot be skipped as they modifiy the rest 66 // state of a tree even without wind. 67 // 68 const ForestItemData *itemData = item->getData(); 69 const F32 windScale = itemData->mWindScale; 70 bendScale = itemData->mTrunkBendScale * windScale; 71 branchAmp = itemData->mWindBranchAmp * windScale; 72 detailAmp = itemData->mWindDetailAmp * windScale; 73 detailFreq = itemData->mWindDetailFreq * windScale; 74 75 // Look for wind state. 76 wind = WINDMGR->getLocalWind( item->getKey() ); 77 } 78 79 if ( wind ) 80 { 81 // Calculate distance to camera fade. 82 F32 toCamLen = ( state->getDiffuseCameraPosition() - wind->getPosition()).len(); 83 F32 toCamInterp = 1.0f - (toCamLen / ForestWindMgr::smWindEffectRadius); 84 toCamInterp = mClampF( toCamInterp, 0.0f, 1.0f ); 85 86 windDir.set( wind->getDirection().x, wind->getDirection().y, 0.0f ); 87 windSpeed = wind->getStrength(); 88 89 // Since the effect is just a displacement 90 // of geometry i need to scale it up for smaller 91 // trees so it looks like its affecting it more. 92 // 93 // TODO: This is possibly a side effect of not 94 // properly doing the displacement physics in the 95 // first place. 96 // 97 const F32 strengthScale = 1.0f / item->getScale(); 98 windDir *= strengthScale; 99 windSpeed *= strengthScale; 100 101 // Add in fade. 102 windDir *= toCamInterp; 103 windSpeed *= toCamInterp; 104 105 MatrixF invXfm( item->getTransform() ); 106 invXfm.inverse(); 107 invXfm.mulV( windDir ); 108 } 109 else 110 { 111 /* 112 // HACK: This should go away... we need to work out a strategy 113 // for allocating accumulators that accounts for random objects 114 // and not just forest elements. 115 // 116 // Maybe add a generic 'id' value to SceneData that is set 117 // by the RenderInst that can be any unique identifier for that 118 // object that we can used down here to fetch an item id. 119 120 // TODO: This is horrible... we should avoid access to the script 121 // system while rendering as its really slow! 122 if ( Con::getBoolVariable( "$tsStaticWindHack", false ) ) 123 { 124 VectorF windVec( mSin( Sim::getCurrentTime() / 1000.0f ) * 0.25f, 0.0f, 0.0f ); 125 //sgData.objTrans.mulV( windVec ); 126 windDir.x = windVec.x; 127 windDir.y = windVec.y; 128 windSpeed = 0.75f; 129 } 130 */ 131 } 132 133 buffer->setSafe( mWindDirAndSpeed, Point3F( windDir.x, windDir.y, windSpeed ) ); 134 buffer->setSafe( mWindParams, Point4F( bendScale, branchAmp, detailAmp, detailFreq ) ); 135} 136