Files
UnrealEngineUWP/Engine/Source/Programs/UnrealLightmass/Private/ImportExport/LightmassScene.cpp
Marc Audy 03d64a49e8 Copying //UE4/Dev-Framework to //UE4/Dev-Main (Source: //UE4/Dev-Framework @ 3293188)
#rb none
#lockdown Nick.Penwarden

==========================
MAJOR FEATURES + CHANGES
==========================

Change 3203880 on 2016/11/18 by Ori.Cohen

	Copying //UE4/Dev-Physics-Upgrade to Dev-Framework (//UE4/Dev-Framework)

Change 3207429 on 2016/11/22 by Marc.Audy

	Merging //UE4/Dev-Main to Dev-Framework (//UE4/Dev-Framework) @ 3207285

Change 3252627 on 2017/01/10 by Lukasz.Furman

	removed duplicated entries from visual logger shape rendering
	#ue4

Change 3252675 on 2017/01/10 by Ori.Cohen

	Add support for tagged memory regions (Copying //Tasks/UE4/Dev-ImmediateModePhysics to Dev-Framework (//UE4/Dev-Framework))

Change 3252686 on 2017/01/10 by Ori.Cohen

	Refactor BodySetup to make it easier to reuse shape creation (Copying //Tasks/UE4/Dev-ImmediateModePhysics to Dev-Framework (//UE4/Dev-Framework))

Change 3252833 on 2017/01/10 by Ori.Cohen

	Refactor constraint so that it can be used for external solvers. (Copying //Tasks/UE4/Dev-ImmediateModePhysics to Dev-Framework (//UE4/Dev-Framework))

Change 3252887 on 2017/01/10 by Dan.Reynolds

	Increased modes to include:
	Harmonic minor
	Melodic minor (going up)
	Pentatonic (Major)
	Pentatonic (minor)
	Whole Tone
	Diminished (WH)
	and Blues

Change 3252895 on 2017/01/10 by Aaron.McLeran

	update to music utilities.

Change 3253060 on 2017/01/10 by Aaron.McLeran

	Updates to synthesis plugin and some new features to DSP objects

Change 3253061 on 2017/01/10 by Aaron.McLeran

	Updates to music maps

Change 3253078 on 2017/01/10 by Aaron.McLeran

	Removing pragma optimization code accidentally checked in

Change 3253110 on 2017/01/10 by Ori.Cohen

	First iteration of immediate mode ragdoll node (Copying //Tasks/UE4/Dev-ImmediateModePhysics to Dev-Framework (//UE4/Dev-Framework))

Change 3253315 on 2017/01/10 by Aaron.McLeran

	Fixing a few bugs in DSP objects

	- Added a new types file EpicSynth1 and EpicSynth1 component can share enums

Change 3253577 on 2017/01/11 by Aaron.McLeran

	Checking in updates to assets for music -- celestial manager for rotating objects like planets, new ambient map

Change 3254052 on 2017/01/11 by Ori.Cohen

	Fix build.

Change 3254059 on 2017/01/11 by Ori.Cohen

	Turn off html5 trying to build apex.

Change 3254095 on 2017/01/11 by Ori.Cohen

	Fix build

Change 3254200 on 2017/01/11 by Jon.Nabozny

	Make vectorized FTransform Accumulate (with blend) and AccumulateWithAdditive (with blend) consistent with the non-vectorized version and comments.

	#JIRA UE-40469

Change 3254334 on 2017/01/11 by Marc.Audy

	Put in missing virtual

Change 3254397 on 2017/01/11 by dan.reynolds

	Updates to OtonOkeMap

Change 3254410 on 2017/01/11 by Marc.Audy

	Cleanup autos

Change 3254420 on 2017/01/11 by Marc.Audy

	PR #3110: Add missing IsInAudioThread check (Contributed by projectgheist)

	Modified somewhat, but based on what PR indicated as a problem.

	#jira UE-40369

Change 3254423 on 2017/01/11 by Marc.Audy

	Optimize GetDefaultSubobjectByName and GetDefaultSubobjects
	Remove autos

Change 3254826 on 2017/01/11 by Aaron.McLeran

	Bringing optimizations to dev-framework

Change 3254831 on 2017/01/11 by dan.reynolds

	Modified MidiSynthTestBP to use Program Change events to pull a Preset from a Preset Bank--added a Data Blueprint Object ES1Bank_Default (containing Preset arrays) with children classes for different classifications of Presets.

Change 3254833 on 2017/01/11 by dan.reynolds

	Updating MidiSynthTestBP's default SynthPreset pan value.

Change 3254851 on 2017/01/11 by dan.reynolds

	Updating ES1Bank_Bass

	Updating OtonOkeMap

Change 3254854 on 2017/01/11 by Aaron.McLeran

	Some fixups for pan modulation

Change 3255682 on 2017/01/12 by aaron.mcleran

	Turning the bass down a bit on OtonOkeMap

Change 3255721 on 2017/01/12 by Marc.Audy

	Fix spelling error

Change 3255790 on 2017/01/12 by Marc.Audy

	Copying //UE4/Dev-Physics-Upgrade to Dev-Framework (//UE4/Dev-Framework)

Change 3256263 on 2017/01/12 by Ori.Cohen

	Refactor immediate mode api to take PxD6Joint and PxRigidActor instead.

Change 3256288 on 2017/01/12 by Ori.Cohen

	Undo constraint refactor as we found a way around it and it made the code much harder to read/debug

Change 3256360 on 2017/01/12 by Ori.Cohen

	Make sure physx actors passed into immediate mode are done so with proper locks (can probably improve this in the case where the actor is not even in the scene)

Change 3256846 on 2017/01/13 by Marc.Audy

	Deprecate FBox/FBox2D int32 constructor because it makes no sense if you pass in a non 0 value. Use ForceInit instead.

Change 3256954 on 2017/01/13 by Marc.Audy

	Fix missed fixup of deprecated constructor use

Change 3257167 on 2017/01/13 by Jon.Nabozny

	Fix check in FBodyInstance::SetCollisionEnabled.
	Create convenience methods for HasPhysics and HasQuery.

	#jira UE-39633

Change 3257181 on 2017/01/13 by Zak.Parrish

	Adding input map and some testing content to Xenakis

Change 3257183 on 2017/01/13 by Mieszko.Zielinski

	Implemented an improved navigation projection BP function that retrieves both projected locaiton as well as a boolean indicating if the projection succeeded #UE4

	Also, did similar changes to GetRandomReachablePointInRadius and GetRandomPointInNavigableRadius

	#jira UE-40368

Change 3257211 on 2017/01/13 by Jon.Nabozny

	Fix CIS issue caused by 3257167.

Change 3257220 on 2017/01/13 by Marc.Audy

	Additional FBox constructor deprecation fixups

Change 3257236 on 2017/01/13 by zak.parrish

	Fixed error on Xenakis input pawn

Change 3257242 on 2017/01/13 by zak.parrish

	Update to InputListener

Change 3257273 on 2017/01/13 by Marc.Audy

	No reason to pass simple types by reference

Change 3257418 on 2017/01/13 by Ori.Cohen

	Attempt to turn android physx libs back to static libs.

Change 3257445 on 2017/01/13 by Ori.Cohen

	Turn android libs back to OBJ and removed unreal side linking as it seems we are now just merging into a single physx lib

Change 3257903 on 2017/01/14 by Aaron.McLeran

	Additions to synth module and updates to dsp objects

	- Adding ability to create arbitrary modular patches from modulating sources to modulation destinations
	- DSP objects define their default depths but patches can override
	- Creating new SynthesisEditor module for synthesis plugin so we can create synthesis preset assets
	- Adding a preset bank type so we can store a bank of presets (aka factory presets)

Change 3258179 on 2017/01/15 by Seth.Weedin

	Duplicating input test map for some FX work

Change 3258181 on 2017/01/15 by Seth.Weedin

	Modify skybox in test map to be dark and spooky

Change 3258183 on 2017/01/15 by aaron.johnson

	substituted classes, changed wind speed and adjusted level lighting

Change 3258190 on 2017/01/15 by aaron.johnson

	substituted triplet pawn and motion controller classes, enabled grabbing animations

Change 3258191 on 2017/01/15 by Aaron.McLeran

	Getting source effects working for GDC demo

	- Added new synthesis editor module to create instances of user-created source effects
	- Added code to do source effects
	- Modified old design to a newer, more simpler design for calling into client code to set parameters. No longer using the complex struct reflection design and instead just pass in the uobject preset the user created. They'll then cast it to the type that has the actual settings.
	- Tweaks and fixes to existing dsp objects to get source effects working
	- Modified existing engine code to allow for playing out source effect tails
	- Only supporting mono and stereo assets for source effect processing. Multi-channel effect processing is overly complex for this feature though we may extend the capabilities in the future.
	- Fixed issue of pitching with stereo delay effect on setting first interpolated param
	- Moving synth/dsp stuff in synthesis plugins into appropriate public/private folders in plugin/module
	- Deleting some cruft files no longer needed

Change 3258201 on 2017/01/15 by Seth.Weedin

	C++ and BP classes for managing grid cells. Initial grid mapping tests. #rb none

Change 3258206 on 2017/01/15 by aaron.johnson

	map push, triplets interface created, debug widget placed in level

Change 3258222 on 2017/01/15 by Aaron.McLeran

	Fixing crash when there's a null entry in the source effect chain

	Fixed some zippering introduced by applying volume twice.

Change 3258225 on 2017/01/15 by aaron.johnson

	Interface changes, pawn output values wip

Change 3258228 on 2017/01/15 by aaron.johnson

	Pawn should be outputting all correct values for Tripletsinterface

Change 3258242 on 2017/01/15 by Stanley.Hayes

	Edge lights and Spherical Density Materials

Change 3258251 on 2017/01/16 by Seth.Weedin

	More progress on grid FX. Add curve strength modifiers, begin hooking up interaction. #rb none

Change 3258284 on 2017/01/16 by Aaron.McLeran

	Fixing CIS build error

	Surprised that MSVC allows that...

Change 3258525 on 2017/01/16 by Mieszko.Zielinski

	Made UGameplayTask::ResourceOverlapPolicy configurable via ini files #UE4

Change 3258537 on 2017/01/16 by Lukasz.Furman

	fixed duplicated & undo operations not updating navigation area in nav link proxy and nav link component
	#ue4

Change 3258595 on 2017/01/16 by Marc.Audy

	Fix static analysis warning

Change 3259364 on 2017/01/16 by Mieszko.Zielinski

	BTTask_RotateToFaceBBEntry comment spelling fix #UE4

	#jira UE-40669

Change 3259683 on 2017/01/16 by dan.reynolds

	Updated Preset Bank System implemented in MidiSynthTestBP and 4 Preset Banks have been started

Change 3260244 on 2017/01/17 by Lina.Halper

	#anim

	- optimize layer blend node to not create mask weights in run-time but in compile time.

	#code review: Martin.Wilson

Change 3260617 on 2017/01/17 by Ori.Cohen

	Immediate mode spawns its own actors.

Change 3260701 on 2017/01/17 by Ori.Cohen

	Don't bother blending physics with animation when physics is QueryOnly

Change 3260796 on 2017/01/17 by Ori.Cohen

	EndPhysics tick will no longer be scheduled if QueryOnly is used on a ragdoll.

Change 3261207 on 2017/01/17 by Ori.Cohen

	First iteration of contact enabling/disabling for immediate mode.

Change 3262010 on 2017/01/18 by Marc.Audy

	Remove some autos

Change 3262525 on 2017/01/18 by Lina.Halper

	Fix crash with required bones index not using property indexing

	#jira: UE-40786

Change 3263658 on 2017/01/19 by Martin.Wilson

	Add AnimTechDemo to dev-framework (base third person + feng mao)

Change 3263684 on 2017/01/19 by Lina.Halper

	#anim : layer node - fix allocation change I made by mistake

Change 3264523 on 2017/01/19 by Ori.Cohen

	Immediate mode can now add static geometry it finds in the world. Also improve contact gen by caching iteration order

Change 3264701 on 2017/01/19 by Ori.Cohen

	Make it so that immediate mode ragdolls collide with the ground in persona.This is a bit of an editor only hack which allows immediate mode to find non-static actors

Change 3264980 on 2017/01/19 by Ori.Cohen

	Make sure physics asset collision disabled works in immediate mode.

Change 3265011 on 2017/01/19 by Ori.Cohen

	Added the ability to override physics asset for immediate mode

Change 3265030 on 2017/01/19 by Ori.Cohen

	Added override gravity for immediate mode.

Change 3265650 on 2017/01/20 by Benn.Gallagher

	NvCloth Source

Change 3265652 on 2017/01/20 by Benn.Gallagher

	NvCloth Lib
	#rnx

Change 3265653 on 2017/01/20 by Benn.Gallagher

	NvCloth Bin
	#rnx

Change 3266195 on 2017/01/20 by Danny.Bouimad

	Initial ClothTest Assets for NCloth Before and after comparison TM-MultiClothTest (Under Maps>Framework>Cloth)

Change 3266377 on 2017/01/20 by Marc.Audy

	Ensure that OrphanedDataOnly and TrashClass blueprint generated classes are correctly considered a blueprint class for disregard for GC purposes.

Change 3267873 on 2017/01/23 by Jon.Nabozny

	Fix SceneProxy shadowing in UGeometryCacheComponent.

Change 3268025 on 2017/01/23 by Benn.Gallagher

	IWYU change, platform PCH generation seemed to hide this one.

Change 3268026 on 2017/01/23 by Benn.Gallagher

	Fixed LOCTEXT_NAMESPACE being inconsistently scoped in an #if block
	#rnx

Change 3268630 on 2017/01/23 by Zak.Parrish

	Updating to add MIGS shooter content, as well as audio interaction Blueprints

Change 3268663 on 2017/01/23 by Ori.Cohen

	Ragdoll animnode uses raw physics asset pointer to ensure it makes a hard reference.

Change 3268811 on 2017/01/23 by Ori.Cohen

	Added component space sim for immediate mode

Change 3269369 on 2017/01/24 by Benn.Gallagher

	Copying //Tasks/UE4/Dev-UEFW-11-NewClothingPipeline to Dev-Framework (//UE4/Dev-Framework)

	Replaced clothing with new simulation framework

Change 3269417 on 2017/01/24 by danny.bouimad

	Minor Update to cloth map for test

Change 3269420 on 2017/01/24 by Benn.Gallagher

	Removed APEX simulation from clothing framework (used in testing, not fully complete)

Change 3269421 on 2017/01/24 by danny.bouimad

	Small tweaks

Change 3269515 on 2017/01/24 by Lukasz.Furman

	enabled gameplay debugger's OnSelectionChanged event support for both PIE and SIE modes
	fixed GameplayAbility debugger's category not using IAbilitySystemInterface
	#ue4

Change 3269595 on 2017/01/24 by mason.seay

	Break apart physics asset for crash bug

Change 3269819 on 2017/01/24 by Ori.Cohen

	Make the possibly kinematic actor the first actor in the immediate mode joint. This is consistent with physx vanilla solver.

Change 3270364 on 2017/01/24 by Josh.Stoddard

	upgrade to the latest version of v-HACD:
	https://github.com/kmammou/v-hacd/tree/master/src/VHACD_Lib
	commit: 7a09f9d
	NOTE: only updated windows binaries
	  mac and linux still using old binaries until they can be tested
	#jira UE-40124 #rb josh.stoddard

Change 3271188 on 2017/01/25 by Jurre.deBaare

	Post-import script support
	#jira UEFW-80

Change 3271249 on 2017/01/25 by Thomas.Sarkanen

	Move soundwave-internal curve tables to advanced display

	Exposing it was confusing to audio people

Change 3271586 on 2017/01/25 by Marc.Audy

	Don't rerun construction scripts twice on a level that has been hidden and reshown
	#jira UE-40306

Change 3272048 on 2017/01/25 by Ori.Cohen

	Fix for immediate mode sim when root body is the same as the root bone.

Change 3272083 on 2017/01/25 by Ori.Cohen

	Make sure to warn when component space sim and collision are used together. Also handle it gracefully.

Change 3272300 on 2017/01/25 by Ori.Cohen

	Fix incorrect collision generation when a shape's local pose is not identity.

Change 3273195 on 2017/01/26 by Jurre.deBaare

	Fix for Anim import script crash in GetBonePosesForTime

Change 3273204 on 2017/01/26 by Ben.Marsh

	Ignore PRAGMA_DISABLE_SHADOW_VARIABLE_WARNINGS and PRAGMA_ENABLE_SHADOW_VARIABLE_WARNINGS macros between include directives. Fixes CIS warning with IncludeTool.

Change 3273378 on 2017/01/26 by James.Golding

	In AnimBP editor, call CopyNodeDataToPreviewNode when properties are edited, not just pin defaults changed

Change 3273381 on 2017/01/26 by James.Golding

	Big refactor to PoseDriver
	  - RBF logic now moved into its own class/file
	  - Allow editing of transform and radial scaling per-target
	  - Add support for different falloff functions (not just Gaussian)
	  - Allow driving curves directly, rather than always poses
	  - Add details customization for pose driver node
	  - Edits to PoseDriver settings now take immediate effect, don't need to recompile

Change 3273826 on 2017/01/26 by Josh.Stoddard

	modify VHACD to improve quality of hulls generated by convex decomposition
	NOTE: mac libs not included - mac editor will use legacy libs for now

Change 3273902 on 2017/01/26 by Marc.Audy

	Merging //UE4/Dev-Main to Dev-Framework (//UE4/Dev-Framework) @ 3273433

Change 3274018 on 2017/01/26 by Ori.Cohen

	Added immediate physics preview in phat.

Change 3274165 on 2017/01/26 by Ori.Cohen

	PhAT now depends on immediate mode plugin. Fix build

	#JIRA UE-41179

Change 3275001 on 2017/01/27 by Jurre.deBaare

	Fix for crash in Persona with Anim Modifiers

Change 3275297 on 2017/01/27 by Ori.Cohen

	Big refactor to iterate over shapes instead of bodies (allows multiple shape per body collision)

Change 3275340 on 2017/01/27 by Benn.Gallagher

	Fixed Paragon clothing crashes during clothing upgrade step, fixed bone mapping not getting updated on reimport with different hierarchy
	#jira UE-41025
	#jira UE-41039

Change 3275383 on 2017/01/27 by Benn.Gallagher

	Blacklisted double promotion warning on ps4 NvCloth build
	#rnx

Change 3275426 on 2017/01/27 by Benn.Gallagher

	Removed CUDA dependencies from NvCloth cmake files

Change 3275670 on 2017/01/27 by Ori.Cohen

	Fix phat ragdoll in immediate mode updating sketal mesh component transform

Change 3275673 on 2017/01/27 by Ori.Cohen

	Add position/velocity iteration to immediate mode

Change 3276001 on 2017/01/27 by Alan.Noon

	Migrated Immediate Mode Minion Ragdoll Content to GDC AnimTech Project. Updated DefaultInput.ini
	none

Change 3276596 on 2017/01/28 by Aaron.McLeran

	Removing unused #ifdef

Change 3276597 on 2017/01/28 by Aaron.McLeran

	Getting rid of static analysis warning

Change 3277354 on 2017/01/30 by Lukasz.Furman

	fixed custom navlink Id collisions
	#ue4

Change 3277356 on 2017/01/30 by Lukasz.Furman

	fixed comments in GameplayDebugger.h
	#jira UE-41103

Change 3277371 on 2017/01/30 by mason.seay

	Test map for spawn sound/force feedback bug.

Change 3277445 on 2017/01/30 by Lukasz.Furman

	fixed compilation warning
	#ue4

Change 3277560 on 2017/01/30 by Danny.Bouimad

	Made checkin to Fix Crash that occured due to bad content.

Change 3277567 on 2017/01/30 by Ori.Cohen

	Fix immediate mode crashing when joint is empty.

	#JIRA UE-41026

Change 3277928 on 2017/01/30 by Ori.Cohen

	Turn on immediate mode plugin by default

Change 3278433 on 2017/01/30 by Ori.Cohen

	Immediate mode supports heightfield collision.

Change 3278449 on 2017/01/30 by Ori.Cohen

	Fix immediate mode cache not being initialized properly.

Change 3278787 on 2017/01/31 by James.Golding

	Fix CIS error in ImmediatePhysicsSimulation.cpp

Change 3279303 on 2017/01/31 by mason.seay

	Assets for RigidBody node bug

Change 3279352 on 2017/01/31 by Benn.Gallagher

	Fixed inertia blends on self collision cloth assets as we now only have local space simulation and these values weren't used before

Change 3279377 on 2017/01/31 by Alan.Noon

	GDC AnimTech Demo: adjusted minion physics assets
	none

Change 3279425 on 2017/01/31 by james.cobbett

	Updating QA-Physics map.

	Made one of the simulated physics objects more user-friendly, able to enable/disable physics on key-press now.

Change 3279436 on 2017/01/31 by Benn.Gallagher

	Fixed inertia scales on Owen mesh

Change 3279480 on 2017/01/31 by Benn.Gallagher

	Fixes for clothing behavior changes
	#jira UE-41092

Change 3279495 on 2017/01/31 by Ori.Cohen

	Remove unneeded cache clearing when contact pairs are not skipped, but there is no collision.

Change 3279579 on 2017/01/31 by james.cobbett

	Added new scenario to QA-Physics map.

	Moving platforms (up/down, left/right) with physics objects on them.

Change 3279695 on 2017/01/31 by mason.seay

	RigidBody node test asset

Change 3280105 on 2017/01/31 by Ori.Cohen

	Prevent query only ragdolls from simulating if their bodysetup is marked as simulated. Also remove slow check in term body for owning components. This is not true for destructibles or immediate mode

Change 3280148 on 2017/01/31 by mason.seay

	First round of assets for force feedback testing

Change 3280860 on 2017/02/01 by James.Golding

	Merge CL 3280853 to Dev-Framework
	Fix crash with null CurrentSkeleton on AnimInstance when using Re-import button in SkelMesh Editor

Change 3281172 on 2017/02/01 by Marc.Audy

	Merging //UE4/Dev-Main to Dev-Framework (//UE4/Dev-Framework) @ 3281156

Change 3281210 on 2017/02/01 by james.cobbett

	Updated QA-Physics map

	Added cube that starts off with physics enabled, then disables. Made physics toggleable on that and another cube.

Change 3281211 on 2017/02/01 by James.Golding

	Details customization for editing PoseDriver targets list

Change 3281332 on 2017/02/01 by Marc.Audy

	Fix bad merge
	Fix file types

Change 3281388 on 2017/02/01 by mason.seay

	Updated Force Feedback asset

Change 3281396 on 2017/02/01 by mason.seay

	moving asset

Change 3281987 on 2017/02/01 by Benn.Gallagher

	Fixed project generation failing after main merge

Change 3282047 on 2017/02/01 by Marc.Audy

	Fix up Target and build cs files after changes from Dev-Build

Change 3282214 on 2017/02/01 by Ori.Cohen

	Expose radial forces to immediate mode

Change 3282221 on 2017/02/01 by Alan.Noon

	Immediate Mode GDC demo content: development on minion anim B, refined Orbital Laser Pawn controls, tweaked laser parameters
	none

Change 3282273 on 2017/02/01 by Ori.Cohen

	Fix crash when recompiling animbp of immediate mode due to null pointer.

Change 3282368 on 2017/02/01 by Ori.Cohen

	Quick iteration on minion demo

Change 3282824 on 2017/02/02 by James.Golding

	Fix for CIS in RBFSolver.h

Change 3282829 on 2017/02/02 by James.Golding

	Fix CIS in PoseDriverDetails.cpp
	Fix list UI not refreshing after copying targets from PoseAsset

Change 3282834 on 2017/02/02 by Danny.Bouimad

	Adding Pose driver additive assets

Change 3282863 on 2017/02/02 by James.Golding

	Add Mambo mesh and Skeleton

Change 3282892 on 2017/02/02 by James.Golding

	Copy Aurora (Ice) and Mambo meshes/materials/some anims from Dev-General to AnimTechDemo project in Dev-Framework

Change 3283157 on 2017/02/02 by Mieszko.Zielinski

	Cook Orion Win64 fix #UE4

	Had to change the Extent param of K2_ProjectPointToNavigation. Updated the error causing Orion BP

Change 3283159 on 2017/02/02 by Marc.Audy

	Additional CIS fixes

Change 3283179 on 2017/02/02 by Marc.Audy

	More CIS fixes

Change 3283197 on 2017/02/02 by Jurre.deBaare

	Fix for issues importing Fornite geometry cache assets
	#fix Use actual import number of frames instead of total number of frames in the Alembic Cache

Change 3283201 on 2017/02/02 by Marc.Audy

	Keep fixing CIS

Change 3283270 on 2017/02/02 by James.Golding

	Merging CL 3276013 to Dev-Framework
	- fix issue with additive pose preview applying twice

Change 3283499 on 2017/02/02 by Marc.Audy

	More CIS fixes

Change 3283543 on 2017/02/02 by Jon.Nabozny

	Update comment on AActor::GetActorBounds to properly reflect ChildActorComponents aren't included in the calculation.

Change 3283663 on 2017/02/02 by Ori.Cohen

	Fix potential null dereference in ragdoll node

Change 3283757 on 2017/02/02 by Marc.Audy

	May fix remaining CIS issues

Change 3283984 on 2017/02/02 by Marc.Audy

	Fix linux CIS

Change 3284039 on 2017/02/02 by Marc.Audy

	Merging //UE4/Dev-Main to Dev-Framework (//UE4/Dev-Framework) @ 3283913

Change 3284067 on 2017/02/02 by Marc.Audy

	Fixup mistakes in converting redirects

Change 3284187 on 2017/02/02 by Ori.Cohen

	Immediate mode works with radial force (not just radial impulse)

Change 3284358 on 2017/02/02 by Ori.Cohen

	Update arcblade phys asset for immediate mode

Change 3284667 on 2017/02/02 by Marc.Audy

	Arguments is an array not a string now. Fixing commented out code.

Change 3284684 on 2017/02/02 by Marc.Audy

	Move AVIWriter out in to its own module to avoid any possible unity build issues where xwindows.h got indirectly included through the DirectShow third party library and caused FGenericWindow::IsMaximized and IsMinimized to conflict with a macro.

Change 3284707 on 2017/02/02 by Marc.Audy

	Fix AVIWriter module compilation on Mac

Change 3285012 on 2017/02/03 by Benn.Gallagher

	Fixes for Dx NvCloth shader warnings

Change 3285016 on 2017/02/03 by Marc.Audy

	Fix missing include

Change 3285048 on 2017/02/03 by Benn.Gallagher

	Fixed Persona needing a restart when changing number of clothing assets (import/delete)
	#jira UE-41323

Change 3285325 on 2017/02/03 by Marc.Audy

	Properly implement AVIWriter module

Change 3285538 on 2017/02/03 by Marc.Audy

	Merging //UE4/Dev-Main to Dev-Framework (//UE4/Dev-Framework) @ 3285499

Change 3285735 on 2017/02/03 by Jon.Nabozny

	Add IsInAir method to UVehicleWheel.

	#jira UE-38369

Change 3285862 on 2017/02/03 by Aaron.McLeran

	UE-41435 Fixing PIE audio

	- Fixing PIE audio. Recent change to editor preferences from Dev-Editor branch (CL 3234495) caused all audio to be muted in PIE.

Change 3285914 on 2017/02/03 by danny.bouimad

	RecomputeTangents Test Assets

Change 3286246 on 2017/02/03 by Mieszko.Zielinski

	Changes to game-specific BPs containing calls to deprecated NavigationSystem functions #UE4

	#jira UE-41527
	#jira UE-41518

Change 3286308 on 2017/02/03 by Ori.Cohen

	Make sure physx trimesh scale is never too small. Fix box clamping being ignored. Fixes cook warnings for Odin.

	#JIRA UE-41529

Change 3286396 on 2017/02/03 by Ori.Cohen

	Fix CIS

Change 3286479 on 2017/02/03 by Ori.Cohen

	Copying //UE4/Dev-Physics-Upgrade to Dev-Framework (//UE4/Dev-Framework)

Change 3287421 on 2017/02/06 by James.Golding

	Merging //UE4/Dev-Main to Dev-Framework (//UE4/Dev-Framework) @ 3286819

Change 3287427 on 2017/02/06 by James.Golding

	Fix PoseBlendNode to 'pass through' if no poses are activated

Change 3287430 on 2017/02/06 by James.Golding

	- Add support to PoseDriver for evaluating source bone in the space of a different bone
	- Fix driven bone adding a scale of 1
	- Fix posedriver values 'sticking' (reset all weights to zero each frame)
	- Move CopyTargetsFromPoseAsset and AutoSetTargetScales from FAnimNode_PoseDriver to UAnimGraphNode_PoseDriver (not required outside editor)
	- Tranlsation targets now draw larger when selected
	- 'Copy from pose asset' now also auto-sets radius for you
	- Remove spammy warnings for missing poses/curves
	- Add UPoseAsset::GetNumTracks and ::GetFullPose
	- Remove unused ExtractionContext from UPoseAsset::GetBaseAnimationPose
	- Remove bIncludeRefPoseAsNeutralPose option (not really useful since we no longer always normalize weights to 1.0)

Change 3287496 on 2017/02/06 by Chad.Garyet

	fixing busted quotes around defaultvalues

Change 3287569 on 2017/02/06 by Mieszko.Zielinski

	Orion BP fixed after deprecating NavigationSystem's BP API #Orion

Change 3287595 on 2017/02/06 by Benn.Gallagher

	BuildPhysX.Automation: Deploying PhysX & NvCloth Win64 Win32 PS4 libs.
	Built for new NvCloth upgrade

Change 3287598 on 2017/02/06 by Benn.Gallagher

	NvCloth Upgrade to 21604115
	Added Linux+Mac support

Change 3287710 on 2017/02/06 by Lukasz.Furman

	added option to disable navlink polys at the end of generated paths
	#ue4

Change 3287857 on 2017/02/06 by Benn.Gallagher

	Fixed NvCloth module files to correctly set up linux and mac hopefully

Change 3287894 on 2017/02/06 by Benn.Gallagher

	Another fix to NvCloth build files, didn't get picked up in VS for some reason.

Change 3287917 on 2017/02/06 by Lina.Halper

	Copy from CharacterRigging to Dev-Framework

	#code review:Thomas.Sarkanen, Martin.Wilson, James.Golding, Andrew.Rodham

Change 3287938 on 2017/02/06 by Thomas.Sarkanen

	Fix crash opening a media sound wave

	#jira UE-41582 - Editor crashes when running Automation test

Change 3287942 on 2017/02/06 by Marc.Audy

	Merging //UE4/Dev-Main to Dev-Framework (//UE4/Dev-Framework) @ 3287682

Change 3288035 on 2017/02/06 by James.Golding

	Remove C++ GameMode and pawn classes (replace with floating BP instead)
	Resave anims to remove Orion refs
	Add simple AnimBP and map for Mambo testing

Change 3288036 on 2017/02/06 by Benn.Gallagher

	Fix to BuildPhysX task to trigger Mac and Linux builds properly

Change 3288125 on 2017/02/06 by Ori.Cohen

	Change PhysXCommon back to dylib

Change 3288127 on 2017/02/06 by Benn.Gallagher

	Fixed project file identification not working for NvCloth under XCode

Change 3288156 on 2017/02/06 by Benn.Gallagher

	Disable "expansion-to-defined" warning in Linux NvCloth builds

Change 3288159 on 2017/02/06 by Lina.Halper

	potential compile fix for Ocean Editor

	#code review:Thomas.Sarkanen

Change 3288190 on 2017/02/06 by Ori.Cohen

	Link against static PhysXCommon for mac

Change 3288200 on 2017/02/06 by Marc.Audy

	Fix CIS

Change 3288270 on 2017/02/06 by Lina.Halper

	fix compile error

	#code review:Thomas.Sarkanen, Marc.Audy

Change 3288302 on 2017/02/06 by Thomas.Sarkanen

	Fixed ensure when deselecting bones in anim BP editor

	#jira UE-41274 - Ensure when clicking in the viewport of an animation blueprint

Change 3288348 on 2017/02/06 by Lina.Halper

	- Enabled control rig
	- Changed plugin name to be Control Rig

Change 3288490 on 2017/02/06 by Benn.Gallagher

	Fixes for Mac attempting static links against NvCloth and failing to load dynamic libraries. Worked with MasonS to get Mac editor up and running.

Change 3288511 on 2017/02/06 by Lina.Halper

	compile fix

Change 3288513 on 2017/02/06 by Lina.Halper

	Check in content to work with

Change 3288615 on 2017/02/06 by Ori.Cohen

	Fix skeletal mesh not simulating when using an aggregate.

	#JIRA UE-41593

Change 3288791 on 2017/02/06 by thomas.sarkanen

	Exposed transforms to cinematics so they can be animated

Change 3288795 on 2017/02/06 by Ori.Cohen

	Fix lock warnings for physx

	#JIRA UE-41591

Change 3288817 on 2017/02/06 by Charles.Anderson

	GDC Arcblade setup tests.

Change 3288825 on 2017/02/06 by Lina.Halper

	Fix build issue of shadow variable

Change 3289058 on 2017/02/06 by Ori.Cohen

	Fix crash when immediate mode constraint generates 0 rows. This is a potentially temporary fix until NVIDIA replies with a better solution.

	#JIRA UE-41026

Change 3289348 on 2017/02/06 by Lina.Halper

	fix compile issue

Change 3289369 on 2017/02/06 by Lina.Halper

	Renamed leg control to limb control and will be used for arm/feet.
	- changed vars.
	- has unused variables that will be used soon but want to check in so that i don't block content change on BaseHuman.

	#code review:Thomas.Sakanen

Change 3289422 on 2017/02/06 by Lina.Halper

	Fixed IK sinking issue - or moving

	#code review:Thomas.Sarkanen

Change 3289433 on 2017/02/06 by Lina.Halper

	Fixed real shadow error

Change 3289485 on 2017/02/06 by Lina.Halper

	fixed build issue

Change 3289657 on 2017/02/07 by thomas.sarkanen

	Added rig bone mapping to Ice's skeletal mesh

Change 3289658 on 2017/02/07 by thomas.sarkanen

	Added ControlRig map with Ice setup to pose

Change 3289662 on 2017/02/07 by Thomas.Sarkanen

	Fixed up static analysis warning

Change 3289663 on 2017/02/07 by Thomas.Sarkanen

	Fixed crash when attempting to bind to skeletal mesh with already-set anim BP

	Anim instance may not have actually been created when binding, so dont dereference it

Change 3289717 on 2017/02/07 by Benn.Gallagher

	Switch Linux NvCloth to static for Linux builds. Adjust lib directory to match actual directory

Change 3289718 on 2017/02/07 by Benn.Gallagher

	BuildPhysX.Automation: Deploying NvCloth Linux_x86_64-unknown-linux-gnu libs.

Change 3289744 on 2017/02/07 by Benn.Gallagher

	Fixed missing masses causing crash initialising clothing actors
	#jira UE-41599

Change 3289746 on 2017/02/07 by Danny.Bouimad

	Adding Some Content for JamesG he wanted some nicer looking Pose driver test files.

Change 3289756 on 2017/02/07 by danny.bouimad

	Changing the asset for JamesG.

Change 3289785 on 2017/02/07 by James.Golding

	Replace old PoseDrive test with Danny's new one

Change 3289858 on 2017/02/07 by Lina.Halper

	fixed issue with undo transaction buffer

Change 3289860 on 2017/02/07 by Benn.Gallagher

	Fixed crash after reimporting a clothing asset with the clothing config open and then changing the confg
	#jira UE-41655

Change 3289912 on 2017/02/07 by Thomas.Sarkanen

	Merging using Raven_To_Dev-Framework

	Originally from CLs 3249471, 3258522, 3260271, 3273791:

	Sequencer: More work supporting array properties more generically

	+ fixes

Change 3289962 on 2017/02/07 by James.Golding

	Add thickness option to DrawWireDiamond

Change 3289963 on 2017/02/07 by James.Golding

	Add spin option to VectorInputBox

Change 3289966 on 2017/02/07 by James.Golding

	Add weight bar chart to PoseDriver details
	Stop drawing pose weight text in viewport
	Fix position targets not drawing larger when selected

Change 3290094 on 2017/02/07 by Thomas.Sarkanen

	Fixed typo in filename (fallout from search and replace)

Change 3290119 on 2017/02/07 by Thomas.Sarkanen

	Manipulators can now have their IK/FK space set on them

	They are not drawn when the space for the chain that they control is not the same as their setting
	Also fixed a crash with invalid objects when reloading maps.

Change 3290145 on 2017/02/07 by Thomas.Sarkanen

	CIS fix for fallout from Raven changes

	#jira UE-41670 - Mac editor fails to compile with PropertyTrackEditor errors

Change 3290319 on 2017/02/07 by Marc.Audy

	Make sound player nodes hard reference the assets unless they are in a chain below a quality node.

Change 3290484 on 2017/02/07 by Richard.Hinckley

	Fixing grammar in popup messages.

Change 3290533 on 2017/02/07 by Marc.Audy

	Make GetAIController BlueprintPure
	#jira UE-41654

Change 3290624 on 2017/02/07 by Marc.Audy

	Reorder header to avoid include tool warnings

Change 3290697 on 2017/02/07 by Lina.Halper

	- support FK manipulator being in local space
	- fixed FK key spamming issue for making blend weight to be not keyable - this creates conflicts with enum

	#code review: Thomas.Sarkanen

Change 3290748 on 2017/02/07 by Ori.Cohen

	Touch immediate mode file to force physx re-link

Change 3290807 on 2017/02/07 by Richard.Hinckley

	#jira UE-39891
	Updates to assist in automatic documentation generation.

Change 3290946 on 2017/02/07 by Lina.Halper

	Fix issue of notify looping.

	#jira: UE-31463
	#Code review:Martin.Wilson

Change 3291553 on 2017/02/07 by Lina.Halper

	Rename/move file(s)
	- modified mesh mapping controller window to be Control Rig

Change 3291571 on 2017/02/07 by Lina.Halper

	added set up spine option

	#code review:Thomas.Sarkanen

Change 3291581 on 2017/02/07 by Ori.Cohen

	Temporarily turn off phat immediate mode preview which crashes.

Change 3291949 on 2017/02/08 by James.Golding

	Merging //UE4/Dev-Main to Dev-Framework (//UE4/Dev-Framework) @ 3291819

Change 3291966 on 2017/02/08 by Lina.Halper

	Fix issue with notify looping bug

	#jira: UE-31463

Change 3292247 on 2017/02/08 by Marc.Audy

	Clean up bad merge caused by Fortnite integration to main

Change 3292326 on 2017/02/08 by Marc.Audy

	Merging //UE4/Dev-Main to Dev-Framework (//UE4/Dev-Framework) @ 3292313

Change 3292409 on 2017/02/08 by Marc.Audy

	Resubmit FortPawn.cpp with proper code even though perforce doesn't think there is a difference since when you sync it, the contents are wrong.

Change 3292481 on 2017/02/08 by Ori.Cohen

	Fix for convex hull cooking (from Josh.S)

	#JIRA UE-41656

Change 3292492 on 2017/02/08 by Mieszko.Zielinski

	Redone replacement of deprecated navigation system's BP functions in Fortnite BPs #Fortnite

Change 3292778 on 2017/02/08 by Ori.Cohen

	Touch physx DDC key for new cooking.

	#JIRA UE-41656

[CL 3293329 by Marc Audy in Main branch]
2017-02-08 17:53:41 -05:00

1916 lines
81 KiB
C++

// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
#include "LightmassScene.h"
#include "Importer.h"
#include "MonteCarlo.h"
#include "LightingSystem.h"
#include "LMDebug.h"
namespace Lightmass
{
/** Copy ctor that doesn't modify padding in FSceneFileHeader. */
FSceneFileHeader::FSceneFileHeader(const FSceneFileHeader& Other)
{
/** FourCC cookie: 'SCEN' */
Cookie = Other.Cookie;
FormatVersion = Other.FormatVersion;
Guid = Other.Guid;
GeneralSettings = Other.GeneralSettings;
SceneConstants = Other.SceneConstants;
DynamicObjectSettings = Other.DynamicObjectSettings;
PrecomputedVisibilitySettings = Other.PrecomputedVisibilitySettings;
VolumeDistanceFieldSettings = Other.VolumeDistanceFieldSettings;
MeshAreaLightSettings = Other.MeshAreaLightSettings;
AmbientOcclusionSettings = Other.AmbientOcclusionSettings;
ShadowSettings = Other.ShadowSettings;
ImportanceTracingSettings = Other.ImportanceTracingSettings;
PhotonMappingSettings = Other.PhotonMappingSettings;
IrradianceCachingSettings = Other.IrradianceCachingSettings;
MaterialSettings = Other.MaterialSettings;
DebugInput = Other.DebugInput;
/** If true, pad the mappings (shrink the requested size and then pad) */
bPadMappings = Other.bPadMappings;
/** If true, draw a solid border as the padding around mappings */
bDebugPadding = Other.bDebugPadding;
bOnlyCalcDebugTexelMappings = Other.bOnlyCalcDebugTexelMappings;
bColorBordersGreen = Other.bColorBordersGreen;
bUseRandomColors = Other.bUseRandomColors;
bColorByExecutionTime = Other.bColorByExecutionTime;
ExecutionTimeDivisor = Other.ExecutionTimeDivisor;
NumImportanceVolumes = Other.NumImportanceVolumes;
NumCharacterIndirectDetailVolumes = Other.NumCharacterIndirectDetailVolumes;
NumDirectionalLights = Other.NumDirectionalLights;
NumPointLights = Other.NumPointLights;
NumSpotLights = Other.NumSpotLights;
NumSkyLights = Other.NumSkyLights;
NumStaticMeshes = Other.NumStaticMeshes;
NumStaticMeshInstances = Other.NumStaticMeshInstances;
NumFluidSurfaceInstances = Other.NumFluidSurfaceInstances;
NumLandscapeInstances = Other.NumLandscapeInstances;
NumBSPMappings = Other.NumBSPMappings;
NumStaticMeshTextureMappings = Other.NumStaticMeshTextureMappings;
NumFluidSurfaceTextureMappings = Other.NumFluidSurfaceTextureMappings;
NumLandscapeTextureMappings = Other.NumLandscapeTextureMappings;
NumSpeedTreeMappings = Other.NumSpeedTreeMappings;
NumPortals = Other.NumPortals;
}
//----------------------------------------------------------------------------
// Scene class
//----------------------------------------------------------------------------
FScene::FScene() :
EmbreeDevice(NULL),
bVerifyEmbree(false)
{
FMemory::Memzero( (FSceneFileHeader*)this, sizeof(FSceneFileHeader) );
}
FScene::~FScene()
{
#if USE_EMBREE
rtcDeleteDevice(EmbreeDevice);
#endif
}
void FScene::Import( FLightmassImporter& Importer )
{
FSceneFileHeader TempHeader;
// Import into a temp header, since padding in the header overlaps with data members in FScene and ImportData stomps on that padding
Importer.ImportData(&TempHeader);
// Copy header members without modifying padding in FSceneFileHeader
(FSceneFileHeader&)(*this) = TempHeader;
#if USE_EMBREE
if (TempHeader.GeneralSettings.bUseEmbree)
{
EmbreeDevice = rtcNewDevice(NULL);
check(rtcDeviceGetError(EmbreeDevice) == RTC_NO_ERROR);
bVerifyEmbree = TempHeader.GeneralSettings.bVerifyEmbree;
}
#endif
// The assignment above overwrites ImportanceVolumes since FSceneFileHeader has some padding which coincides with ImportanceVolumes
FMemory::Memzero(&ImportanceVolumes, sizeof(ImportanceVolumes));
Importer.SetLevelScale(SceneConstants.StaticLightingLevelScale);
ApplyStaticLightingScale();
FStaticLightingMapping::s_bShowLightmapBorders = bDebugPadding;
TArray<TCHAR>& InstigatorUserNameArray = InstigatorUserName.GetCharArray();
int32 UserNameLen;
Importer.ImportData(&UserNameLen);
Importer.ImportArray(InstigatorUserNameArray, UserNameLen);
InstigatorUserNameArray.Add('\0');
FString PersistentLevelName;
TArray<TCHAR>& PersistentLevelNameArray = PersistentLevelName.GetCharArray();
int32 PersistentLevelNameLen;
Importer.ImportData(&PersistentLevelNameLen);
Importer.ImportArray(PersistentLevelNameArray, PersistentLevelNameLen);
PersistentLevelNameArray.Add('\0');
ImportanceBoundingBox.Init();
for (int32 VolumeIndex = 0; VolumeIndex < NumImportanceVolumes; VolumeIndex++)
{
FBox LMBox;
Importer.ImportData(&LMBox);
ImportanceBoundingBox += LMBox;
ImportanceVolumes.Add(LMBox);
}
if (NumImportanceVolumes == 0)
{
ImportanceBoundingBox = FBox(FVector4(0,0,0), FVector4(0,0,0));
}
for (int32 VolumeIndex = 0; VolumeIndex < NumCharacterIndirectDetailVolumes; VolumeIndex++)
{
FBox LMBox;
Importer.ImportData(&LMBox);
CharacterIndirectDetailVolumes.Add(LMBox);
}
for (int32 PortalIndex = 0; PortalIndex < NumPortals; PortalIndex++)
{
FMatrix LMPortal;
Importer.ImportData(&LMPortal);
Portals.Add(FSphere(LMPortal.GetOrigin(), FVector2D(LMPortal.GetScaleVector().Y, LMPortal.GetScaleVector().Z).Size()));
}
Importer.ImportArray(VisibilityBucketGuids, NumPrecomputedVisibilityBuckets);
int32 NumVisVolumes;
Importer.ImportData(&NumVisVolumes);
PrecomputedVisibilityVolumes.Empty(NumVisVolumes);
PrecomputedVisibilityVolumes.AddZeroed(NumVisVolumes);
for (int32 VolumeIndex = 0; VolumeIndex < NumVisVolumes; VolumeIndex++)
{
FPrecomputedVisibilityVolume& CurrentVolume = PrecomputedVisibilityVolumes[VolumeIndex];
Importer.ImportData(&CurrentVolume.Bounds);
int32 NumPlanes;
Importer.ImportData(&NumPlanes);
Importer.ImportArray(CurrentVolume.Planes, NumPlanes);
}
int32 NumVisOverrideVolumes;
Importer.ImportData(&NumVisOverrideVolumes);
PrecomputedVisibilityOverrideVolumes.Empty(NumVisOverrideVolumes);
PrecomputedVisibilityOverrideVolumes.AddZeroed(NumVisOverrideVolumes);
for (int32 VolumeIndex = 0; VolumeIndex < NumVisOverrideVolumes; VolumeIndex++)
{
FPrecomputedVisibilityOverrideVolume& CurrentVolume = PrecomputedVisibilityOverrideVolumes[VolumeIndex];
Importer.ImportData(&CurrentVolume.Bounds);
int32 NumVisibilityIds;
Importer.ImportData(&NumVisibilityIds);
Importer.ImportArray(CurrentVolume.OverrideVisibilityIds, NumVisibilityIds);
int32 NumInvisibilityIds;
Importer.ImportData(&NumInvisibilityIds);
Importer.ImportArray(CurrentVolume.OverrideInvisibilityIds, NumInvisibilityIds);
}
int32 NumCameraTrackPositions;
Importer.ImportData(&NumCameraTrackPositions);
Importer.ImportArray(CameraTrackPositions, NumCameraTrackPositions);
Importer.ImportObjectArray( DirectionalLights, NumDirectionalLights, Importer.GetLights() );
Importer.ImportObjectArray( PointLights, NumPointLights, Importer.GetLights() );
Importer.ImportObjectArray( SpotLights, NumSpotLights, Importer.GetLights() );
Importer.ImportObjectArray( SkyLights, NumSkyLights, Importer.GetLights() );
Importer.ImportObjectArray( StaticMeshInstances, NumStaticMeshInstances, Importer.GetStaticMeshInstances() );
Importer.ImportObjectArray( FluidMeshInstances, NumFluidSurfaceInstances, Importer.GetFluidMeshInstances() );
Importer.ImportObjectArray( LandscapeMeshInstances, NumLandscapeInstances, Importer.GetLandscapeMeshInstances() );
Importer.ImportObjectArray( BspMappings, NumBSPMappings, Importer.GetBSPMappings() );
Importer.ImportObjectArray( TextureLightingMappings, NumStaticMeshTextureMappings, Importer.GetTextureMappings() );
Importer.ImportObjectArray( FluidMappings, NumFluidSurfaceTextureMappings, Importer.GetFluidMappings() );
Importer.ImportObjectArray( LandscapeMappings, NumLandscapeTextureMappings, Importer.GetLandscapeMappings() );
DebugMapping = FindMappingByGuid(DebugInput.MappingGuid);
if (DebugMapping)
{
const FStaticLightingTextureMapping* TextureMapping = DebugMapping->GetTextureMapping();
// Verify debug input is valid, otherwise there will be an access violation later
if (TextureMapping)
{
check(DebugInput.LocalX >= 0 && DebugInput.LocalX < TextureMapping->CachedSizeX);
check(DebugInput.LocalY >= 0 && DebugInput.LocalY < TextureMapping->CachedSizeY);
check(DebugInput.MappingSizeX == TextureMapping->CachedSizeX && DebugInput.MappingSizeY == TextureMapping->CachedSizeY);
}
}
if (bPadMappings == true)
{
// BSP mappings
for (int32 MappingIdx = 0; MappingIdx < BspMappings.Num(); MappingIdx++)
{
int32 SizeX = BspMappings[MappingIdx].Mapping.SizeX;
int32 SizeY = BspMappings[MappingIdx].Mapping.SizeY;
if (((SizeX - 2) > 0) && ((SizeY - 2) > 0))
{
BspMappings[MappingIdx].Mapping.CachedSizeX = FMath::Clamp<int32>(SizeX, 0, SizeX - 2);
BspMappings[MappingIdx].Mapping.CachedSizeY = FMath::Clamp<int32>(SizeY, 0, SizeY - 2);
BspMappings[MappingIdx].Mapping.bPadded = true;
}
}
// Static mesh texture mappings
for (int32 MappingIdx = 0; MappingIdx < TextureLightingMappings.Num(); MappingIdx++)
{
int32 SizeX = TextureLightingMappings[MappingIdx].SizeX;
int32 SizeY = TextureLightingMappings[MappingIdx].SizeY;
if (((SizeX - 2) > 0) && ((SizeY - 2) > 0))
{
TextureLightingMappings[MappingIdx].CachedSizeX = FMath::Clamp<int32>(SizeX, 0, SizeX - 2);
TextureLightingMappings[MappingIdx].CachedSizeY = FMath::Clamp<int32>(SizeY, 0, SizeY - 2);
TextureLightingMappings[MappingIdx].bPadded = true;
}
}
// Fluid mappings
for (int32 MappingIdx = 0; MappingIdx < FluidMappings.Num(); MappingIdx++)
{
int32 SizeX = FluidMappings[MappingIdx].SizeX;
int32 SizeY = FluidMappings[MappingIdx].SizeY;
if (((SizeX - 2) > 0) && ((SizeY - 2) > 0))
{
FluidMappings[MappingIdx].CachedSizeX = FMath::Clamp<int32>(SizeX, 0, SizeX - 2);
FluidMappings[MappingIdx].CachedSizeY = FMath::Clamp<int32>(SizeY, 0, SizeY - 2);
FluidMappings[MappingIdx].bPadded = true;
}
}
// Landscape mappings - do not get padded by Lightmass...
for (int32 MappingIdx = 0; MappingIdx < LandscapeMappings.Num(); MappingIdx++)
{
LandscapeMappings[MappingIdx].CachedSizeX = LandscapeMappings[MappingIdx].SizeX;
LandscapeMappings[MappingIdx].CachedSizeY = LandscapeMappings[MappingIdx].SizeY;
LandscapeMappings[MappingIdx].bPadded = false;
}
}
if (DebugMapping)
{
const FStaticLightingTextureMapping* TextureMapping = DebugMapping->GetTextureMapping();
// Verify debug input is valid, otherwise there will be an access violation later
if (TextureMapping)
{
check(DebugInput.LocalX >= 0 && DebugInput.LocalX < TextureMapping->CachedSizeX);
check(DebugInput.LocalY >= 0 && DebugInput.LocalY < TextureMapping->CachedSizeY);
check(DebugInput.MappingSizeX == TextureMapping->SizeX && DebugInput.MappingSizeY == TextureMapping->SizeY);
}
}
}
FBoxSphereBounds FScene::GetImportanceBounds() const
{
const FBoxSphereBounds ImportanceBoundSphere(ImportanceBoundingBox);
return ImportanceBoundSphere;
}
const FLight* FScene::FindLightByGuid(const FGuid& InGuid) const
{
for (int32 i = 0; i < DirectionalLights.Num(); i++)
{
if (DirectionalLights[i].Guid == InGuid)
{
return &DirectionalLights[i];
}
}
for (int32 i = 0; i < PointLights.Num(); i++)
{
if (PointLights[i].Guid == InGuid)
{
return &PointLights[i];
}
}
for (int32 i = 0; i < SpotLights.Num(); i++)
{
if (SpotLights[i].Guid == InGuid)
{
return &SpotLights[i];
}
}
for (int32 i = 0; i < SkyLights.Num(); i++)
{
if (SkyLights[i].Guid == InGuid)
{
return &SkyLights[i];
}
}
return NULL;
}
/** Searches through all mapping arrays for the mapping matching FindGuid. */
const FStaticLightingMapping* FScene::FindMappingByGuid(FGuid FindGuid) const
{
// Note: FindGuid can be all 0's and still be valid due to deterministic lighting overriding the Guid
for (int32 i = 0; i < BspMappings.Num(); i++)
{
if (BspMappings[i].Mapping.Guid == FindGuid)
{
return &BspMappings[i].Mapping;
}
}
for (int32 i = 0; i < TextureLightingMappings.Num(); i++)
{
if (TextureLightingMappings[i].Guid == FindGuid)
{
return &TextureLightingMappings[i];
}
}
for (int32 i = 0; i < FluidMappings.Num(); i++)
{
if (FluidMappings[i].Guid == FindGuid)
{
return &FluidMappings[i];
}
}
for (int32 i = 0; i < LandscapeMappings.Num(); i++)
{
if (LandscapeMappings[i].Guid == FindGuid)
{
return &LandscapeMappings[i];
}
}
return NULL;
}
/** Returns true if the specified position is inside any of the importance volumes. */
bool FScene::IsPointInImportanceVolume(const FVector4& Position, float Tolerance) const
{
for (int32 VolumeIndex = 0; VolumeIndex < ImportanceVolumes.Num(); VolumeIndex++)
{
FBox Volume = ImportanceVolumes[VolumeIndex];
if (Position.X + Tolerance > Volume.Min.X && Position.X - Tolerance < Volume.Max.X
&& Position.Y + Tolerance > Volume.Min.Y && Position.Y - Tolerance < Volume.Max.Y
&& Position.Z + Tolerance > Volume.Min.Z && Position.Z - Tolerance < Volume.Max.Z)
{
return true;
}
}
return false;
}
/** Returns true if the specified position is inside any of the visibility volumes. */
bool FScene::IsPointInVisibilityVolume(const FVector4& Position) const
{
for (int32 VolumeIndex = 0; VolumeIndex < PrecomputedVisibilityVolumes.Num(); VolumeIndex++)
{
const FPrecomputedVisibilityVolume& Volume = PrecomputedVisibilityVolumes[VolumeIndex];
bool bInsideAllPlanes = true;
for (int32 PlaneIndex = 0; PlaneIndex < Volume.Planes.Num() && bInsideAllPlanes; PlaneIndex++)
{
const FPlane& Plane = Volume.Planes[PlaneIndex];
bInsideAllPlanes = bInsideAllPlanes && Plane.PlaneDot(Position) < 0.0f;
}
if (bInsideAllPlanes)
{
return true;
}
}
return false;
}
bool FScene::DoesBoxIntersectVisibilityVolume(const FBox& TestBounds) const
{
for (int32 VolumeIndex = 0; VolumeIndex < PrecomputedVisibilityVolumes.Num(); VolumeIndex++)
{
const FPrecomputedVisibilityVolume& Volume = PrecomputedVisibilityVolumes[VolumeIndex];
if (Volume.Bounds.Intersect(TestBounds))
{
return true;
}
}
return false;
}
/** Returns accumulated bounds from all the visibility volumes. */
FBox FScene::GetVisibilityVolumeBounds() const
{
FBox Bounds(ForceInit);
for (int32 VolumeIndex = 0; VolumeIndex < PrecomputedVisibilityVolumes.Num(); VolumeIndex++)
{
const FPrecomputedVisibilityVolume& Volume = PrecomputedVisibilityVolumes[VolumeIndex];
Bounds += Volume.Bounds;
}
if (PrecomputedVisibilityVolumes.Num() > 0)
{
FVector4 DoubleExtent = Bounds.GetExtent() * 2;
DoubleExtent.X = DoubleExtent.X - FMath::Fmod(DoubleExtent.X, PrecomputedVisibilitySettings.CellSize) + PrecomputedVisibilitySettings.CellSize;
DoubleExtent.Y = DoubleExtent.Y - FMath::Fmod(DoubleExtent.Y, PrecomputedVisibilitySettings.CellSize) + PrecomputedVisibilitySettings.CellSize;
// Round the max up to the next cell boundary
Bounds.Max = Bounds.Min + DoubleExtent;
return Bounds;
}
else
{
return FBox(FVector4(0,0,0),FVector4(0,0,0));
}
}
/** Applies GeneralSettings.StaticLightingLevelScale to all scale dependent settings. */
void FScene::ApplyStaticLightingScale()
{
// Scale world space distances directly
SceneConstants.VisibilityRayOffsetDistance *= SceneConstants.StaticLightingLevelScale;
SceneConstants.VisibilityNormalOffsetDistance *= SceneConstants.StaticLightingLevelScale;
SceneConstants.SmallestTexelRadius *= SceneConstants.StaticLightingLevelScale;
MeshAreaLightSettings.MeshAreaLightSimplifyCornerDistanceThreshold *= SceneConstants.StaticLightingLevelScale;
MeshAreaLightSettings.MeshAreaLightGeneratedDynamicLightSurfaceOffset *= SceneConstants.StaticLightingLevelScale;
DynamicObjectSettings.FirstSurfaceSampleLayerHeight *= SceneConstants.StaticLightingLevelScale;
DynamicObjectSettings.SurfaceLightSampleSpacing *= SceneConstants.StaticLightingLevelScale;
DynamicObjectSettings.SurfaceSampleLayerHeightSpacing *= SceneConstants.StaticLightingLevelScale;
DynamicObjectSettings.DetailVolumeSampleSpacing *= SceneConstants.StaticLightingLevelScale;
DynamicObjectSettings.VolumeLightSampleSpacing *= SceneConstants.StaticLightingLevelScale;
VolumeDistanceFieldSettings.VoxelSize *= SceneConstants.StaticLightingLevelScale;
VolumeDistanceFieldSettings.VolumeMaxDistance *= SceneConstants.StaticLightingLevelScale;
ShadowSettings.MaxTransitionDistanceWorldSpace *= SceneConstants.StaticLightingLevelScale;
ShadowSettings.StaticShadowDepthMapTransitionSampleDistanceX *= SceneConstants.StaticLightingLevelScale;
ShadowSettings.StaticShadowDepthMapTransitionSampleDistanceY *= SceneConstants.StaticLightingLevelScale;
IrradianceCachingSettings.RecordRadiusScale *= SceneConstants.StaticLightingLevelScale;
IrradianceCachingSettings.MaxRecordRadius *= SceneConstants.StaticLightingLevelScale;
// Photon mapping does not scale down properly, so this is disabled
/*
PhotonMappingSettings.IndirectPhotonEmitDiskRadius *= SceneConstants.StaticLightingLevelScale;
PhotonMappingSettings.MaxImportancePhotonSearchDistance *= SceneConstants.StaticLightingLevelScale;
PhotonMappingSettings.MinImportancePhotonSearchDistance *= SceneConstants.StaticLightingLevelScale;
// Scale surface densities in world units
const float ScaleSquared = SceneConstants.StaticLightingLevelScale * SceneConstants.StaticLightingLevelScale;
PhotonMappingSettings.DirectPhotonDensity /= ScaleSquared;
PhotonMappingSettings.DirectIrradiancePhotonDensity /= ScaleSquared;
PhotonMappingSettings.DirectPhotonSearchDistance *= SceneConstants.StaticLightingLevelScale;
PhotonMappingSettings.IndirectPhotonPathDensity /= ScaleSquared;
PhotonMappingSettings.IndirectPhotonDensity /= ScaleSquared;
PhotonMappingSettings.IndirectIrradiancePhotonDensity /= ScaleSquared;
PhotonMappingSettings.IndirectPhotonSearchDistance *= SceneConstants.StaticLightingLevelScale;
*/
}
//----------------------------------------------------------------------------
// Light base class
//----------------------------------------------------------------------------
void FLight::Import( FLightmassImporter& Importer )
{
Importer.ImportData( (FLightData*)this );
// The read above stomps on CachedLightSurfaceSamples since that memory is padding in FLightData
FMemory::Memzero(&CachedLightSurfaceSamples, sizeof(CachedLightSurfaceSamples));
// Precalculate the light's indirect color
IndirectColor = FLinearColorUtils::AdjustSaturation(FLinearColor(Color), IndirectLightingSaturation) * IndirectLightingScale;
}
/**
* Tests whether the light affects the given bounding volume.
* @param Bounds - The bounding volume to test.
* @return True if the light affects the bounding volume
*/
bool FLight::AffectsBounds(const FBoxSphereBounds& Bounds) const
{
return true;
}
/**
* Computes the intensity of the direct lighting from this light on a specific point.
*/
FLinearColor FLight::GetDirectIntensity(const FVector4& Point, bool bCalculateForIndirectLighting) const
{
// light profile (IES)
float LightProfileAttenuation;
{
FVector4 NegLightVector = (Position - Point).GetSafeNormal();
LightProfileAttenuation = ComputeLightProfileMultiplier(Dot3(NegLightVector, Direction));
}
if (bCalculateForIndirectLighting)
{
return IndirectColor * (LightProfileAttenuation * Brightness);
}
else
{
return FLinearColor(Color) * (LightProfileAttenuation * Brightness);
}
}
/** Generates and caches samples on the light's surface. */
void FLight::CacheSurfaceSamples(int32 BounceIndex, int32 NumSamples, int32 NumPenumbraSamples, FLMRandomStream& RandomStream)
{
checkSlow(NumSamples > 0);
// Assuming bounce number starts from 0 and increments each time
//@todo - remove the slack
CachedLightSurfaceSamples.AddZeroed(1);
// Allocate for both normal and penumbra even if there aren't any penumbra samples, so we can return an empty array from GetCachedSurfaceSamples
CachedLightSurfaceSamples[BounceIndex].AddZeroed(2);
const int32 NumPenumbraTypes = NumPenumbraSamples > 0 ? 2 : 1;
for (int32 PenumbraType = 0; PenumbraType < NumPenumbraTypes; PenumbraType++)
{
const int32 CurrentNumSamples = PenumbraType == 0 ? NumSamples : NumPenumbraSamples;
CachedLightSurfaceSamples[BounceIndex][PenumbraType].Empty(CurrentNumSamples);
for (int32 SampleIndex = 0; SampleIndex < CurrentNumSamples; SampleIndex++)
{
FLightSurfaceSample LightSample;
SampleLightSurface(RandomStream, LightSample);
CachedLightSurfaceSamples[BounceIndex][PenumbraType].Add(LightSample);
}
}
}
/** Retrieves the array of cached light surface samples. */
const TArray<FLightSurfaceSample>& FLight::GetCachedSurfaceSamples(int32 BounceIndex, bool bPenumbra) const
{
return CachedLightSurfaceSamples[BounceIndex][bPenumbra];
}
//----------------------------------------------------------------------------
// Directional light class
//----------------------------------------------------------------------------
void FDirectionalLight::Import( FLightmassImporter& Importer )
{
FLight::Import( Importer );
Importer.ImportData( (FDirectionalLightData*)this );
}
void FDirectionalLight::Initialize(
const FBoxSphereBounds& InSceneBounds,
bool bInEmitPhotonsOutsideImportanceVolume,
const FBoxSphereBounds& InImportanceBounds,
float InIndirectDiskRadius,
int32 InGridSize,
float InDirectPhotonDensity,
float InOutsideImportanceVolumeDensity)
{
GenerateCoordinateSystem(Direction, XAxis, YAxis);
SceneBounds = InSceneBounds;
ImportanceBounds = InImportanceBounds;
// Vector through the scene bound's origin, along the direction of the light
const FVector4 SceneAxis = (SceneBounds.Origin + Direction * SceneBounds.SphereRadius) - (SceneBounds.Origin - Direction * SceneBounds.SphereRadius);
const float SceneAxisLength = SceneBounds.SphereRadius * 2.0f;
const FVector4 DirectionalLightOriginToImportanceOrigin = ImportanceBounds.Origin - (SceneBounds.Origin - Direction * SceneBounds.SphereRadius);
// Find the closest point on the scene's axis to the importance volume's origin by projecting DirectionalLightOriginToImportanceOrigin onto SceneAxis.
// This gives the offset in the directional light's disk from the scene bound's origin.
const FVector4 ClosestPositionOnAxis = Dot3(SceneAxis, DirectionalLightOriginToImportanceOrigin) / (SceneAxisLength * SceneAxisLength) * SceneAxis + SceneBounds.Origin - Direction * SceneBounds.SphereRadius;
// Find the disk offset from the world space origin and transform into the [-1,1] space of the directional light's disk, still in 3d.
const FVector4 DiskOffset = (ImportanceBounds.Origin - ClosestPositionOnAxis) / SceneBounds.SphereRadius;
const float DebugLength = (ImportanceBounds.Origin - ClosestPositionOnAxis).Size();
const float DebugDot = ((ImportanceBounds.Origin - ClosestPositionOnAxis) / DebugLength) | Direction;
// Verify that ImportanceBounds.Origin is either on the scene's axis or the vector between it and ClosestPositionOnAxis is orthogonal to the light's direction
//checkSlow(DebugLength < KINDA_SMALL_NUMBER * 10.0f || FMath::Abs(DebugDot) < DELTA * 10.0f);
// Decompose DiskOffset into it's corresponding parts along XAxis and YAxis
const FVector4 XAxisProjection = Dot3(XAxis, DiskOffset) * XAxis;
const FVector4 YAxisProjection = Dot3(YAxis, DiskOffset) * YAxis;
ImportanceDiskOrigin = FVector2D(Dot3(XAxisProjection, XAxis), Dot3(YAxisProjection, YAxis));
// Transform the importance volume's radius into the [-1,1] space of the directional light's disk
LightSpaceImportanceDiskRadius = ImportanceBounds.SphereRadius / SceneBounds.SphereRadius;
const FVector4 DebugPosition = (ImportanceDiskOrigin.X * XAxis + ImportanceDiskOrigin.Y * YAxis);
const float DebugLength2 = (DiskOffset - DebugPosition).Size3();
// Verify that DiskOffset was decomposed correctly by reconstructing it
checkSlow(DebugLength2 < KINDA_SMALL_NUMBER);
IndirectDiskRadius = InIndirectDiskRadius;
GridSize = InGridSize;
OutsideImportanceVolumeDensity = InOutsideImportanceVolumeDensity;
const float ImportanceDiskAreaMillions = (float)PI * FMath::Square(ImportanceBounds.SphereRadius) / 1000000.0f;
checkSlow(SceneBounds.SphereRadius >= ImportanceBounds.SphereRadius);
const float OutsideImportanceDiskAreaMillions = (float)PI * (FMath::Square(SceneBounds.SphereRadius) - FMath::Square(ImportanceBounds.SphereRadius)) / 1000000.0f;
// Calculate the probability that a generated sample will be in the importance volume,
// Based on the fraction of total photons that should be gathered in the importance volume.
ImportanceBoundsSampleProbability = ImportanceDiskAreaMillions * InDirectPhotonDensity
/ (ImportanceDiskAreaMillions * InDirectPhotonDensity + OutsideImportanceDiskAreaMillions * OutsideImportanceVolumeDensity);
// Calculate the size of the directional light source using Tangent(LightSourceAngle) = LightSourceRadius / DistanceToReceiver
LightSourceRadius = 2.0f * SceneBounds.SphereRadius * FMath::Tan(LightSourceAngle);
if (!bInEmitPhotonsOutsideImportanceVolume && ImportanceBounds.SphereRadius > DELTA)
{
// Always sample inside the importance volume
ImportanceBoundsSampleProbability = 1.0f;
OutsideImportanceVolumeDensity = 0.0f;
}
}
/** Returns the number of direct photons to gather required by this light. */
int32 FDirectionalLight::GetNumDirectPhotons(float DirectPhotonDensity) const
{
int32 NumDirectPhotons = 0;
if (ImportanceBounds.SphereRadius > DELTA)
{
// The importance volume is valid, so only gather enough direct photons to meet DirectPhotonDensity inside the importance volume
const float ImportanceDiskAreaMillions = (float)PI * FMath::Square(ImportanceBounds.SphereRadius) / 1000000.0f;
checkSlow(SceneBounds.SphereRadius > ImportanceBounds.SphereRadius);
const float OutsideImportanceDiskAreaMillions = (float)PI * (FMath::Square(SceneBounds.SphereRadius) - FMath::Square(ImportanceBounds.SphereRadius)) / 1000000.0f;
NumDirectPhotons = FMath::TruncToInt(ImportanceDiskAreaMillions * DirectPhotonDensity + OutsideImportanceDiskAreaMillions * OutsideImportanceVolumeDensity);
}
else
{
// Gather enough photons to meet DirectPhotonDensity everywhere in the scene
const float SceneDiskAreaMillions = (float)PI * FMath::Square(SceneBounds.SphereRadius) / 1000000.0f;
NumDirectPhotons = FMath::TruncToInt(SceneDiskAreaMillions * DirectPhotonDensity);
}
return NumDirectPhotons == appTruncErrorCode ? INT_MAX : NumDirectPhotons;
}
/** Generates a direction sample from the light's domain */
void FDirectionalLight::SampleDirection(FLMRandomStream& RandomStream, FLightRay& SampleRay, FVector4& LightSourceNormal, FVector2D& LightSurfacePosition, float& RayPDF, FLinearColor& Power) const
{
FVector4 DiskPosition3D;
// If the importance volume is valid, generate samples in the importance volume with a probability of ImportanceBoundsSampleProbability
if (ImportanceBounds.SphereRadius > DELTA
&& RandomStream.GetFraction() < ImportanceBoundsSampleProbability)
{
const FVector2D DiskPosition2D = GetUniformUnitDiskPosition(RandomStream);
LightSurfacePosition = ImportanceDiskOrigin + DiskPosition2D * LightSpaceImportanceDiskRadius;
DiskPosition3D = SceneBounds.Origin + SceneBounds.SphereRadius * (LightSurfacePosition.X * XAxis + LightSurfacePosition.Y * YAxis);
RayPDF = ImportanceBoundsSampleProbability / ((float)PI * FMath::Square(ImportanceBounds.SphereRadius));
}
else
{
float DistanceToImportanceDiskOriginSq;
do
{
LightSurfacePosition = GetUniformUnitDiskPosition(RandomStream);
DistanceToImportanceDiskOriginSq = (LightSurfacePosition - ImportanceDiskOrigin).SizeSquared();
}
// Use rejection sampling to prevent any samples from being generated inside the importance volume
while (DistanceToImportanceDiskOriginSq < FMath::Square(LightSpaceImportanceDiskRadius));
// Create the ray using a disk centered at the scene's origin, whose radius is the size of the scene
DiskPosition3D = SceneBounds.Origin + SceneBounds.SphereRadius * (LightSurfacePosition.X * XAxis + LightSurfacePosition.Y * YAxis);
// Calculate the probability of generating a uniform disk sample in the scene, minus the importance volume's disk
RayPDF = (1.0f - ImportanceBoundsSampleProbability) / ((float)PI * (FMath::Square(SceneBounds.SphereRadius) - FMath::Square(ImportanceBounds.SphereRadius)));
}
//@todo - take light source radius into account
SampleRay = FLightRay(
DiskPosition3D - SceneBounds.SphereRadius * Direction,
DiskPosition3D + SceneBounds.SphereRadius * Direction,
NULL,
this
);
LightSourceNormal = Direction;
checkSlow(RayPDF > 0);
Power = IndirectColor * Brightness;
}
/** Gives the light an opportunity to precalculate information about the indirect path rays that will be used to generate new directions. */
void FDirectionalLight::CachePathRays(const TArray<FIndirectPathRay>& IndirectPathRays)
{
if (IndirectPathRays.Num() == 0)
{
return;
}
// The indirect disk radius in the [-1, 1] space of the directional light's disk
const float LightSpaceIndirectDiskRadius = IndirectDiskRadius / SceneBounds.SphereRadius;
// Find the minimum and maximum position in the [-1, 1] space of the directional light's disk
// That a position can be generated from in FDirectionalLight::SampleDirection
FVector2D GridMin(1.0f, 1.0f);
FVector2D GridMax(-1.0f, -1.0f);
for (int32 RayIndex = 0; RayIndex < IndirectPathRays.Num(); RayIndex++)
{
const FIndirectPathRay& CurrentRay = IndirectPathRays[RayIndex];
GridMin.X = FMath::Min(GridMin.X, CurrentRay.LightSurfacePosition.X - LightSpaceIndirectDiskRadius);
GridMin.Y = FMath::Min(GridMin.Y, CurrentRay.LightSurfacePosition.Y - LightSpaceIndirectDiskRadius);
GridMax.X = FMath::Max(GridMax.X, CurrentRay.LightSurfacePosition.X + LightSpaceIndirectDiskRadius);
GridMax.Y = FMath::Max(GridMax.Y, CurrentRay.LightSurfacePosition.Y + LightSpaceIndirectDiskRadius);
}
GridMin.X = FMath::Min(GridMin.X, 1.0f);
GridMin.Y = FMath::Min(GridMin.Y, 1.0f);
GridMax.X = FMath::Max(GridMax.X, -1.0f);
GridMax.Y = FMath::Max(GridMax.Y, -1.0f);
checkSlow(GridMax > GridMin);
const FVector2D GridExtent2D = 0.5f * (GridMax - GridMin);
// Keep the grid space square to simplify logic
GridExtent = FMath::Max(GridExtent2D.X, GridExtent2D.Y);
GridCenter = 0.5f * (GridMin + GridMax);
// Allocate the grid
PathRayGrid.Empty(GridSize * GridSize);
PathRayGrid.AddZeroed(GridSize * GridSize);
const float GridSpaceIndirectDiskRadius = IndirectDiskRadius * GridExtent / SceneBounds.SphereRadius;
const float InvGridSize = 1.0f / (float)GridSize;
// For each grid cell, store the indices into IndirectPathRays of the path rays that affect the grid cell
for (int32 Y = 0; Y < GridSize; Y++)
{
for (int32 X = 0; X < GridSize; X++)
{
// Center and Extent of the cell in the [0, 1] grid space
const FVector2D BoxCenter((X + .5f) * InvGridSize, (Y + .5f) * InvGridSize);
const float BoxExtent = .5f * InvGridSize;
// Corners of the cell
const int32 NumBoxCorners = 4;
FVector2D BoxCorners[NumBoxCorners];
BoxCorners[0] = BoxCenter + FVector2D(BoxExtent, BoxExtent);
BoxCorners[1] = BoxCenter + FVector2D(-BoxExtent, BoxExtent);
BoxCorners[2] = BoxCenter + FVector2D(BoxExtent, -BoxExtent);
BoxCorners[3] = BoxCenter + FVector2D(-BoxExtent, -BoxExtent);
// Calculate the world space positions of each corner of the cell
FVector4 WorldBoxCorners[NumBoxCorners];
for (int32 i = 0; i < NumBoxCorners; i++)
{
// Transform the cell corner from [0, 1] grid space to [-1, 1] in the directional light's disk
const FVector2D LightBoxCorner(2.0f * GridExtent * BoxCorners[i] + GridCenter - FVector2D(GridExtent, GridExtent));
// Calculate the world position of the cell corner
WorldBoxCorners[i] = SceneBounds.Origin + SceneBounds.SphereRadius * (LightBoxCorner.X * XAxis + LightBoxCorner.Y * YAxis) - SceneBounds.SphereRadius * Direction;
}
// Calculate the world space distance along the diagonal of the box
const float DiagonalBoxDistance = (WorldBoxCorners[0] - WorldBoxCorners[3]).Size3();
const float DiagonalBoxDistanceAndRadiusSquared = FMath::Square(DiagonalBoxDistance + IndirectDiskRadius);
for (int32 RayIndex = 0; RayIndex < IndirectPathRays.Num(); RayIndex++)
{
const FIndirectPathRay& CurrentRay = IndirectPathRays[RayIndex];
bool bAnyCornerInCircle = false;
bool bWithinDiagonalDistance = true;
// If any of the box corners lie within the disk around the current path ray, then they intersect
for (int32 i = 0; i < NumBoxCorners; i++)
{
const float SampleDistanceSquared = (WorldBoxCorners[i] - CurrentRay.Start).SizeSquared3();
bWithinDiagonalDistance = bWithinDiagonalDistance && SampleDistanceSquared < DiagonalBoxDistanceAndRadiusSquared;
if (SampleDistanceSquared < IndirectDiskRadius * IndirectDiskRadius)
{
bAnyCornerInCircle = true;
PathRayGrid[Y * GridSize + X].Add(RayIndex);
break;
}
}
// If none of the box corners lie within the disk but the disk is less than the diagonal + the disk radius, treat them as intersecting.
// This is a conservative test, they might not actually intersect.
if (!bAnyCornerInCircle && bWithinDiagonalDistance)
{
PathRayGrid[Y * GridSize + X].Add(RayIndex);
}
}
}
}
}
/** Generates a direction sample from the light based on the given rays */
void FDirectionalLight::SampleDirection(
const TArray<FIndirectPathRay>& IndirectPathRays,
FLMRandomStream& RandomStream,
FLightRay& SampleRay,
float& RayPDF,
FLinearColor& Power) const
{
checkSlow(IndirectPathRays.Num() > 0);
const FVector2D DiskPosition2D = GetUniformUnitDiskPosition(RandomStream);
const int32 RayIndex = FMath::TruncToInt(RandomStream.GetFraction() * IndirectPathRays.Num());
checkSlow(RayIndex >= 0 && RayIndex < IndirectPathRays.Num());
// Create the ray using a disk centered at the scene's origin, whose radius is the size of the scene
const FVector4 DiskPosition3D = IndirectPathRays[RayIndex].Start + IndirectDiskRadius * (DiskPosition2D.X * XAxis + DiskPosition2D.Y * YAxis);
SampleRay = FLightRay(
DiskPosition3D,
DiskPosition3D + 2.0f * SceneBounds.SphereRadius * Direction,
NULL,
this
);
const float DiskPDF = 1.0f / ((float)PI * IndirectDiskRadius * IndirectDiskRadius);
const float LightSpaceIndirectDiskRadius = IndirectDiskRadius / SceneBounds.SphereRadius;
FVector2D SampleLightSurfacePosition;
// Clamp the generated position to lie within the [-1, 1] space of the directional light's disk
SampleLightSurfacePosition.X = FMath::Clamp(DiskPosition2D.X * LightSpaceIndirectDiskRadius + IndirectPathRays[RayIndex].LightSurfacePosition.X, -1.0f, 1.0f - DELTA);
SampleLightSurfacePosition.Y = FMath::Clamp(DiskPosition2D.Y * LightSpaceIndirectDiskRadius + IndirectPathRays[RayIndex].LightSurfacePosition.Y, -1.0f, 1.0f - DELTA);
checkSlow(SampleLightSurfacePosition.X >= GridCenter.X - GridExtent && SampleLightSurfacePosition.X <= GridCenter.X + GridExtent);
checkSlow(SampleLightSurfacePosition.Y >= GridCenter.Y - GridExtent && SampleLightSurfacePosition.Y <= GridCenter.Y + GridExtent);
// Calculate the cell indices that the generated position falls into
const int32 CellX = FMath::Clamp(FMath::TruncToInt(GridSize * (SampleLightSurfacePosition.X - GridCenter.X + GridExtent) / (2.0f * GridExtent)), 0, GridSize - 1);
const int32 CellY = FMath::Clamp(FMath::TruncToInt(GridSize * (SampleLightSurfacePosition.Y - GridCenter.Y + GridExtent) / (2.0f * GridExtent)), 0, GridSize - 1);
const TArray<int32>& CurrentGridCell = PathRayGrid[CellY * GridSize + CellX];
// The cell containing the sample position must contain at least the index of the path used to generate this sample position
checkSlow(CurrentGridCell.Num() > 0);
// Initialize the total PDF to the PDF contribution from the path used to generate this sample position
RayPDF = DiskPDF;
// Calculate the probability that this sample was chosen by other paths
// Iterating only over paths that affect the sample position's cell as an optimization
for (int32 OtherRayIndex = 0; OtherRayIndex < CurrentGridCell.Num(); OtherRayIndex++)
{
const int32 CurrentPathIndex = CurrentGridCell[OtherRayIndex];
const FIndirectPathRay& CurrentPath = IndirectPathRays[CurrentPathIndex];
const float SampleDistanceSquared = (DiskPosition3D - CurrentPath.Start).SizeSquared3();
// Accumulate the disk probability for all the disks which contain the sample position
if (SampleDistanceSquared < IndirectDiskRadius * IndirectDiskRadius
// The path that was used to generate the sample has already been counted
&& CurrentPathIndex != RayIndex)
{
RayPDF += DiskPDF;
}
}
RayPDF /= IndirectPathRays.Num();
check(RayPDF > 0);
Power = IndirectColor * Brightness;
}
/** Returns the light's radiant power. */
float FDirectionalLight::Power() const
{
const float EffectiveRadius = ImportanceBounds.SphereRadius > DELTA ? ImportanceBounds.SphereRadius : SceneBounds.SphereRadius;
const FLinearColor LightPower = GetDirectIntensity(FVector4(0,0,0), false) * IndirectLightingScale * (float)PI * EffectiveRadius * EffectiveRadius;
return FLinearColorUtils::LinearRGBToXYZ(LightPower).G;
}
/** Validates a surface sample given the position that sample is affecting. */
void FDirectionalLight::ValidateSurfaceSample(const FVector4& Point, FLightSurfaceSample& Sample) const
{
// Directional light samples are generated on a disk the size of the light source radius, centered on the origin
// Move the disk to the other side of the scene along the light's reverse direction
Sample.Position += Point - Direction * 2.0f * SceneBounds.SphereRadius;
}
/** Gets a single position which represents the center of the area light source from the ReceivingPosition's point of view. */
FVector4 FDirectionalLight::LightCenterPosition(const FVector4& ReceivingPosition, const FVector4& ReceivingNormal) const
{
return ReceivingPosition - Direction * 2.0f * SceneBounds.SphereRadius;
}
/** Returns true if all parts of the light are behind the surface being tested. */
bool FDirectionalLight::BehindSurface(const FVector4& TrianglePoint, const FVector4& TriangleNormal) const
{
const float NormalDotLight = Dot3(TriangleNormal, FDirectionalLight::GetDirectLightingDirection(TrianglePoint, TriangleNormal));
return NormalDotLight < 0.0f;
}
/** Gets a single direction to use for direct lighting that is representative of the whole area light. */
FVector4 FDirectionalLight::GetDirectLightingDirection(const FVector4& Point, const FVector4& PointNormal) const
{
// The position on the directional light surface disk that will first be visible to a triangle rotating toward the light
const FVector4 FirstVisibleLightPoint = Point - Direction * 2.0f * SceneBounds.SphereRadius + PointNormal * LightSourceRadius;
return FirstVisibleLightPoint - Point;
}
/** Generates a sample on the light's surface. */
void FDirectionalLight::SampleLightSurface(FLMRandomStream& RandomStream, FLightSurfaceSample& Sample) const
{
// Create samples on a disk the size of the light source radius, centered at the origin
// This disk will be moved based on the receiver position
//@todo - stratify
Sample.DiskPosition = GetUniformUnitDiskPosition(RandomStream);
Sample.Position = LightSourceRadius * (Sample.DiskPosition.X * XAxis + Sample.DiskPosition.Y * YAxis);
Sample.Normal = Direction;
Sample.PDF = 1.0f / ((float)PI * LightSourceRadius * LightSourceRadius);
}
//----------------------------------------------------------------------------
// Point light class
//----------------------------------------------------------------------------
void FPointLight::Import( FLightmassImporter& Importer )
{
FLight::Import( Importer );
Importer.ImportData( (FPointLightData*)this );
}
void FPointLight::Initialize(float InIndirectPhotonEmitConeAngle)
{
CosIndirectPhotonEmitConeAngle = FMath::Cos(InIndirectPhotonEmitConeAngle);
}
/** Returns the number of direct photons to gather required by this light. */
int32 FPointLight::GetNumDirectPhotons(float DirectPhotonDensity) const
{
// Gather enough photons to meet DirectPhotonDensity at the influence radius of the point light.
const float InfluenceSphereSurfaceAreaMillions = 4.0f * (float)PI * FMath::Square(Radius) / 1000000.0f;
const int32 NumDirectPhotons = FMath::TruncToInt(InfluenceSphereSurfaceAreaMillions * DirectPhotonDensity);
return NumDirectPhotons == appTruncErrorCode ? INT_MAX : NumDirectPhotons;
}
/**
* Tests whether the light affects the given bounding volume.
* @param Bounds - The bounding volume to test.
* @return True if the light affects the bounding volume
*/
bool FPointLight::AffectsBounds(const FBoxSphereBounds& Bounds) const
{
if((Bounds.Origin - Position).SizeSquared() > FMath::Square(Radius + Bounds.SphereRadius))
{
return false;
}
if(!FLight::AffectsBounds(Bounds))
{
return false;
}
return true;
}
/**
* Computes the intensity of the direct lighting from this light on a specific point.
*/
FLinearColor FPointLight::GetDirectIntensity(const FVector4& Point, bool bCalculateForIndirectLighting) const
{
if (LightFlags & GI_LIGHT_INVERSE_SQUARED)
{
FVector4 ToLight = Position - Point;
float DistanceSqr = ToLight.SizeSquared3();
float DistanceAttenuation = 0.0f;
if( LightSourceLength > 0.0f )
{
// Line segment irradiance
FVector4 L01 = Direction * LightSourceLength;
FVector4 L0 = ToLight - 0.5f * L01;
FVector4 L1 = ToLight + 0.5f * L01;
float LengthL0 = L0.Size3();
float LengthL1 = L1.Size3();
DistanceAttenuation = 1.0f / ( ( LengthL0 * LengthL1 + Dot3( L0, L1 ) ) * 0.5f + 1.0f );
DistanceAttenuation *= 0.5 * ( L0 / LengthL0 + L1 / LengthL1 ).Size3();
}
else
{
// Sphere irradiance (technically just 1/d^2 but this avoids inf)
DistanceAttenuation = 1.0f / ( DistanceSqr + 1.0f );
}
// lumens
DistanceAttenuation *= 16.0f;
float LightRadiusMask = FMath::Square(FMath::Max(0.0f, 1.0f - FMath::Square(DistanceSqr / (Radius * Radius))));
DistanceAttenuation *= LightRadiusMask;
return FLight::GetDirectIntensity(Point, bCalculateForIndirectLighting) * DistanceAttenuation;
}
else
{
float RadialAttenuation = FMath::Pow(FMath::Max(1.0f - ((Position - Point) / Radius).SizeSquared3(), 0.0f), FalloffExponent);
return FLight::GetDirectIntensity(Point, bCalculateForIndirectLighting) * RadialAttenuation;
}
}
/** Returns an intensity scale based on the receiving point. */
float FPointLight::CustomAttenuation(const FVector4& Point, FLMRandomStream& RandomStream) const
{
// Remove the physical attenuation, then attenuation using Unreal point light radial falloff
const float PointDistanceSquared = (Position - Point).SizeSquared3();
const float PhysicalAttenuation = 1.0f / ( PointDistanceSquared + 0.0001f );
float UnrealAttenuation = 1.0f;
if( LightFlags & GI_LIGHT_INVERSE_SQUARED )
{
const float LightRadiusMask = FMath::Square( FMath::Max( 0.0f, 1.0f - FMath::Square( PointDistanceSquared / (Radius * Radius) ) ) );
UnrealAttenuation = 16.0f * PhysicalAttenuation * LightRadiusMask;
}
else
{
UnrealAttenuation = FMath::Pow(FMath::Max(1.0f - ((Position - Point) / Radius).SizeSquared3(), 0.0f), FalloffExponent);
}
// light profile (IES)
{
FVector4 NegLightVector = (Position - Point).GetSafeNormal();
UnrealAttenuation *= ComputeLightProfileMultiplier(Dot3(NegLightVector, Direction));
}
// Thin out photons near the light source.
// This is partly an optimization since the photon density near light sources doesn't need to be high, and the natural 1 / R^2 density is overkill,
// But this also improves quality since we are doing a nearest N photon neighbor search when calculating irradiance.
// If the photon map has a high density of low power photons near light sources,
// Combined with sparse, high power photons from other light sources (directional lights for example), the result will be very splotchy.
const float FullProbabilityDistance = .5f * Radius;
const float DepositProbability = FMath::Clamp(PointDistanceSquared / (FullProbabilityDistance * FullProbabilityDistance), 0.0f, 1.0f);
if (RandomStream.GetFraction() < DepositProbability)
{
// Re-weight the photon since it survived the thinning based on the probability of being deposited
return UnrealAttenuation / (PhysicalAttenuation * DepositProbability);
}
else
{
return 0.0f;
}
}
// Fudge factor to get point light photon intensities to match direct lighting more closely.
static const float PointLightIntensityScale = 1.5f;
/** Generates a direction sample from the light's domain */
void FPointLight::SampleDirection(FLMRandomStream& RandomStream, FLightRay& SampleRay, FVector4& LightSourceNormal, FVector2D& LightSurfacePosition, float& RayPDF, FLinearColor& Power) const
{
const FVector4 RandomDirection = GetUnitVector(RandomStream);
FLightSurfaceSample SurfaceSample;
SampleLightSurface(RandomStream, SurfaceSample);
const float SurfacePositionDotDirection = Dot3((SurfaceSample.Position - Position), RandomDirection);
if (SurfacePositionDotDirection < 0.0f)
{
// Reflect the surface position about the origin so that it lies in the same hemisphere as the RandomDirection
const FVector4 LocalSamplePosition = SurfaceSample.Position - Position;
SurfaceSample.Position = -LocalSamplePosition + Position;
}
SampleRay = FLightRay(
SurfaceSample.Position,
SurfaceSample.Position + RandomDirection * FMath::Max((Radius - LightSourceRadius), 0.0f),
NULL,
this
);
LightSourceNormal = (SurfaceSample.Position - Position).GetSafeNormal();
// Approximate the probability of generating this direction as uniform over all the solid angles
// This diverges from the actual probability for positions inside the light source radius
RayPDF = 1.0f / (4.0f * (float)PI);
Power = IndirectColor * Brightness * PointLightIntensityScale;
}
/** Generates a direction sample from the light based on the given rays */
void FPointLight::SampleDirection(
const TArray<FIndirectPathRay>& IndirectPathRays,
FLMRandomStream& RandomStream,
FLightRay& SampleRay,
float& RayPDF,
FLinearColor& Power) const
{
checkSlow(IndirectPathRays.Num() > 0);
// Pick an indirect path ray with uniform probability
const int32 RayIndex = FMath::TruncToInt(RandomStream.GetFraction() * IndirectPathRays.Num());
checkSlow(RayIndex >= 0 && RayIndex < IndirectPathRays.Num());
const FVector4 PathRayDirection = IndirectPathRays[RayIndex].UnitDirection;
FVector4 XAxis(0,0,0);
FVector4 YAxis(0,0,0);
GenerateCoordinateSystem(PathRayDirection, XAxis, YAxis);
// Generate a sample direction within a cone about the indirect path
const FVector4 ConeSampleDirection = UniformSampleCone(RandomStream, CosIndirectPhotonEmitConeAngle, XAxis, YAxis, PathRayDirection);
FLightSurfaceSample SurfaceSample;
// Generate a surface sample, not taking the indirect path into account
SampleLightSurface(RandomStream, SurfaceSample);
const float SurfacePositionDotDirection = Dot3((SurfaceSample.Position - Position), ConeSampleDirection);
if (SurfacePositionDotDirection < 0.0f)
{
// Reflect the surface position about the origin so that it lies in the same hemisphere as the ConeSampleDirection
const FVector4 LocalSamplePosition = SurfaceSample.Position - Position;
SurfaceSample.Position = -LocalSamplePosition + Position;
}
SampleRay = FLightRay(
SurfaceSample.Position,
SurfaceSample.Position + ConeSampleDirection * FMath::Max((Radius - LightSourceRadius), 0.0f),
NULL,
this
);
const float ConePDF = UniformConePDF(CosIndirectPhotonEmitConeAngle);
RayPDF = 0.0f;
// Calculate the probability that this direction was chosen
for (int32 OtherRayIndex = 0; OtherRayIndex < IndirectPathRays.Num(); OtherRayIndex++)
{
// Accumulate the disk probability for all the disks which contain the sample position
if (Dot3(IndirectPathRays[OtherRayIndex].UnitDirection, ConeSampleDirection) > (1.0f - DELTA) * CosIndirectPhotonEmitConeAngle)
{
RayPDF += ConePDF;
}
}
RayPDF /= IndirectPathRays.Num();
checkSlow(RayPDF > 0);
Power = IndirectColor * Brightness * PointLightIntensityScale;
}
/** Validates a surface sample given the position that sample is affecting. */
void FPointLight::ValidateSurfaceSample(const FVector4& Point, FLightSurfaceSample& Sample) const
{
// Only attempt to fixup sphere light source sample positions as the light source is radially symmetric
if (LightSourceLength <= 0)
{
const FVector4 LightToPoint = Point - Position;
const float LightToPointDistanceSquared = LightToPoint.SizeSquared3();
if (LightToPointDistanceSquared < FMath::Square(LightSourceRadius * 2.0f))
{
// Point is inside the light source radius * 2
FVector4 LocalSamplePosition = Sample.Position - Position;
// Reposition the light surface sample on a sphere whose radius is half of the distance from the light to Point
LocalSamplePosition *= FMath::Sqrt(LightToPointDistanceSquared) / (2.0f * LightSourceRadius);
Sample.Position = LocalSamplePosition + Position;
}
const float SurfacePositionDotDirection = Dot3((Sample.Position - Position), LightToPoint);
if (SurfacePositionDotDirection < 0.0f)
{
// Reflect the surface position about the origin so that it lies in the hemisphere facing Point
// The sample's PDF is unchanged
const FVector4 LocalSamplePosition = Sample.Position - Position;
Sample.Position = -LocalSamplePosition + Position;
}
}
}
/** Returns the light's radiant power. */
float FPointLight::Power() const
{
FLinearColor IncidentPower = FLinearColor(Color) * Brightness * IndirectLightingScale;
// Approximate power of the light by the total amount of light passing through a sphere at half the light's radius
const float RadiusFraction = .5f;
const float DistanceToEvaluate = RadiusFraction * Radius;
if (LightFlags & GI_LIGHT_INVERSE_SQUARED)
{
IncidentPower = IncidentPower * 16 / (DistanceToEvaluate * DistanceToEvaluate);
}
else
{
float UnrealAttenuation = FMath::Pow(FMath::Max(1.0f - RadiusFraction * RadiusFraction, 0.0f), FalloffExponent);
// Point light power is proportional to its radius squared
IncidentPower = IncidentPower * UnrealAttenuation;
}
const FLinearColor LightPower = IncidentPower * 4.f * PI * DistanceToEvaluate * DistanceToEvaluate;
return FLinearColorUtils::LinearRGBToXYZ(LightPower).G;
}
FVector4 FPointLight::LightCenterPosition(const FVector4& ReceivingPosition, const FVector4& ReceivingNormal) const
{
if( LightSourceLength > 0 )
{
FVector4 ToLight = Position - ReceivingPosition;
FVector4 Dir = Direction;
if( Dot3( ReceivingNormal, Direction ) < 0.0f )
{
Dir = -Direction;
}
// Clip to hemisphere
float Proj = FMath::Min( Dot3( ToLight, Dir ), Dot3( ReceivingNormal, ToLight) / Dot3( ReceivingNormal, Dir ) );
// Point on line segment closest to Point
return Position - Dir * FMath::Clamp( Proj, -0.5f * LightSourceLength, 0.5f * LightSourceLength );
}
else
{
return Position;
}
}
/** Returns true if all parts of the light are behind the surface being tested. */
bool FPointLight::BehindSurface(const FVector4& TrianglePoint, const FVector4& TriangleNormal) const
{
const float NormalDotLight = Dot3(TriangleNormal, FPointLight::GetDirectLightingDirection(TrianglePoint, TriangleNormal));
return NormalDotLight < 0.0f;
}
/** Gets a single direction to use for direct lighting that is representative of the whole area light. */
FVector4 FPointLight::GetDirectLightingDirection(const FVector4& Point, const FVector4& PointNormal) const
{
FVector4 LightPosition = Position;
if( LightSourceLength > 0 )
{
FVector4 ToLight = Position - Point;
FVector4 L01 = Direction * LightSourceLength;
FVector4 L0 = ToLight - 0.5 * L01;
FVector4 L1 = ToLight + 0.5 * L01;
#if 0
// Point on line segment with smallest angle to normal
float A = LightSourceLength * LightSourceLength;
float B = 2.0f * Dot3( L0, L01 );
float C = Dot3( L0, L0 );
float D = Dot3( PointNormal, L0 );
float E = Dot3( PointNormal, L01 );
float t = FMath::Clamp( (B*D - 2.0f * C*E) / (B*E - 2.0f * A*D), 0.0f, 1.0f );
return L0 + t * L01;
#else
// Line segment irradiance
float LengthL0 = L0.Size3();
float LengthL1 = L1.Size3();
return ( L0 * LengthL1 + L1 * LengthL0 ) / ( LengthL0 + LengthL1 );
#endif
}
else
{
// The position on the point light surface sphere that will first be visible to a triangle rotating toward the light
const FVector4 FirstVisibleLightPoint = LightPosition + PointNormal * LightSourceRadius;
return FirstVisibleLightPoint - Point;
}
}
/** Generates a sample on the light's surface. */
void FPointLight::SampleLightSurface(FLMRandomStream& RandomStream, FLightSurfaceSample& Sample) const
{
Sample.DiskPosition = FVector2D(0, 0);
if (LightSourceLength <= 0)
{
// Generate a sample on the surface of the sphere with uniform density over the surface area of the sphere
//@todo - stratify
const FVector4 UnitSpherePosition = GetUnitVector(RandomStream);
Sample.Position = UnitSpherePosition * LightSourceRadius + Position;
Sample.Normal = UnitSpherePosition;
// Probability of generating this surface position is 1 / SurfaceArea
Sample.PDF = 1.0f / (4.0f * (float)PI * LightSourceRadius * LightSourceRadius);
}
else
{
float CylinderSurfaceArea = 2.0f * (float)PI * LightSourceRadius * LightSourceLength;
float SphereSurfaceArea = 4.0f * (float)PI * LightSourceRadius * LightSourceRadius;
float TotalSurfaceArea = CylinderSurfaceArea + SphereSurfaceArea;
// Cylinder End caps
// The chance of calculating a point on the end sphere is equal to it's percentage of total surface area
if (RandomStream.GetFraction() < SphereSurfaceArea / TotalSurfaceArea)
{
// Generate a sample on the surface of the sphere with uniform density over the surface area of the sphere
//@todo - stratify
const FVector4 UnitSpherePosition = GetUnitVector(RandomStream);
Sample.Position = UnitSpherePosition * LightSourceRadius + Position;
if (Dot3(UnitSpherePosition, Direction) > 0)
{
Sample.Position += Direction * (LightSourceLength * 0.5f);
}
else
{
Sample.Position += -Direction * (LightSourceLength * 0.5f);
}
Sample.Normal = UnitSpherePosition;
}
// Cylinder body
else
{
// Get point along center line
FVector4 CentreLinePosition = Position + Direction * LightSourceLength * (RandomStream.GetFraction() - 0.5f);
// Get point radius away from center line at random angle
float Theta = 2.0f * (float)PI * RandomStream.GetFraction();
FVector4 CylEdgePos = FVector4(0, FMath::Cos(Theta), FMath::Sin(Theta), 1);
CylEdgePos = FRotationMatrix::MakeFromZ( Direction ).TransformVector( CylEdgePos );
Sample.Position = CylEdgePos * LightSourceRadius + CentreLinePosition;
Sample.Normal = CylEdgePos;
}
// Probability of generating this surface position is 1 / SurfaceArea
Sample.PDF = 1.0f / TotalSurfaceArea;
}
}
//----------------------------------------------------------------------------
// Spot light class
//----------------------------------------------------------------------------
void FSpotLight::Import( FLightmassImporter& Importer )
{
FPointLight::Import( Importer );
Importer.ImportData( (FSpotLightData*)this );
}
/**
* Tests whether the light affects the given bounding volume.
* @param Bounds - The bounding volume to test.
* @return True if the light affects the bounding volume
*/
bool FSpotLight::AffectsBounds(const FBoxSphereBounds& Bounds) const
{
if(!FLight::AffectsBounds(Bounds))
{
return false;
}
// Radial check
if((Bounds.Origin - Position).SizeSquared() > FMath::Square(Radius + Bounds.SphereRadius))
{
return false;
}
// Cone check
float ClampedInnerConeAngle = FMath::Clamp(InnerConeAngle,0.0f,89.0f) * (float)PI / 180.0f,
ClampedOuterConeAngle = FMath::Clamp(OuterConeAngle * (float)PI / 180.0f,ClampedInnerConeAngle + 0.001f,89.0f * (float)PI / 180.0f + 0.001f);
float Sin = FMath::Sin(ClampedOuterConeAngle),
Cos = FMath::Cos(ClampedOuterConeAngle);
FVector4 U = Position - (Bounds.SphereRadius / Sin) * Direction,
D = Bounds.Origin - U;
float dsqr = Dot3(D, D),
E = Dot3(Direction, D);
if(E > 0.0f && E * E >= dsqr * FMath::Square(Cos))
{
D = Bounds.Origin - Position;
dsqr = Dot3(D, D);
E = -Dot3(Direction, D);
if(E > 0.0f && E * E >= dsqr * FMath::Square(Sin))
return dsqr <= FMath::Square(Bounds.SphereRadius);
else
return true;
}
return false;
}
/**
* Computes the intensity of the direct lighting from this light on a specific point.
*/
FLinearColor FSpotLight::GetDirectIntensity(const FVector4& Point, bool bCalculateForIndirectLighting) const
{
float ClampedInnerConeAngle = FMath::Clamp(InnerConeAngle,0.0f,89.0f) * (float)PI / 180.0f,
ClampedOuterConeAngle = FMath::Clamp(OuterConeAngle * (float)PI / 180.0f,ClampedInnerConeAngle + 0.001f,89.0f * (float)PI / 180.0f + 0.001f),
OuterCone = FMath::Cos(ClampedOuterConeAngle),
InnerCone = FMath::Cos(ClampedInnerConeAngle);
FVector4 LightVector = (Point - Position).GetSafeNormal();
float SpotAttenuation = FMath::Square(FMath::Clamp<float>((Dot3(LightVector, Direction) - OuterCone) / (InnerCone - OuterCone),0.0f,1.0f));
if( LightFlags & GI_LIGHT_INVERSE_SQUARED )
{
FVector4 ToLight = Position - Point;
float DistanceSqr = ToLight.SizeSquared3();
float DistanceAttenuation = 0.0f;
if( LightSourceLength > 0.0f )
{
// Line segment irradiance
FVector4 L01 = Direction * LightSourceLength;
FVector4 L0 = ToLight - 0.5 * L01;
FVector4 L1 = ToLight + 0.5 * L01;
float LengthL0 = L0.Size3();
float LengthL1 = L1.Size3();
DistanceAttenuation = 1.0f / ( ( LengthL0 * LengthL1 + Dot3( L0, L1 ) ) * 0.5f + 1.0f );
}
else
{
// Sphere irradiance (technically just 1/d^2 but this avoids inf)
DistanceAttenuation = 1.0f / ( DistanceSqr + 1.0f );
}
// lumens
DistanceAttenuation *= 16.0f;
float LightRadiusMask = FMath::Square( FMath::Max( 0.0f, 1.0f - FMath::Square( DistanceSqr / (Radius * Radius) ) ) );
DistanceAttenuation *= LightRadiusMask;
return FLight::GetDirectIntensity(Point, bCalculateForIndirectLighting) * DistanceAttenuation * SpotAttenuation;
}
else
{
float RadialAttenuation = FMath::Pow( FMath::Max(1.0f - ((Position - Point) / Radius).SizeSquared3(),0.0f), FalloffExponent );
return FLight::GetDirectIntensity(Point, bCalculateForIndirectLighting) * RadialAttenuation * SpotAttenuation;
}
}
/** Returns the number of direct photons to gather required by this light. */
int32 FSpotLight::GetNumDirectPhotons(float DirectPhotonDensity) const
{
const float InfluenceSphereSurfaceAreaMillions = 4.0f * (float)PI * FMath::Square(Radius) / 1000000.0f;
const float ConeSolidAngle = 2.0f * float(PI) * (1.0f - FMath::Cos(OuterConeAngle * (float)PI / 180.0f));
// Find the fraction of the sphere's surface area that is inside the cone
const float ConeSurfaceAreaSphereFraction = ConeSolidAngle / (4.0f * (float)PI);
// Gather enough photons to meet DirectPhotonDensity on the spherical cap at the influence radius of the spot light.
const int32 NumDirectPhotons = FMath::TruncToInt(InfluenceSphereSurfaceAreaMillions * ConeSurfaceAreaSphereFraction * DirectPhotonDensity);
return NumDirectPhotons == appTruncErrorCode ? INT_MAX : NumDirectPhotons;
}
/** Generates a direction sample from the light's domain */
void FSpotLight::SampleDirection(FLMRandomStream& RandomStream, FLightRay& SampleRay, FVector4& LightSourceNormal, FVector2D& LightSurfacePosition, float& RayPDF, FLinearColor& Power) const
{
FVector4 XAxis(0,0,0);
FVector4 YAxis(0,0,0);
GenerateCoordinateSystem(Direction, XAxis, YAxis);
const float CosOuterConeAngle = FMath::Cos(OuterConeAngle * (float)PI / 180.0f);
//@todo - the PDF should be affected by inner cone angle too
const FVector4 ConeSampleDirection = UniformSampleCone(RandomStream, CosOuterConeAngle, XAxis, YAxis, Direction);
//@todo - take light source radius into account
SampleRay = FLightRay(
Position,
Position + ConeSampleDirection * Radius,
NULL,
this
);
LightSourceNormal = Direction;
RayPDF = UniformConePDF(CosOuterConeAngle);
checkSlow(RayPDF > 0.0f);
Power = IndirectColor * Brightness * PointLightIntensityScale;
}
//----------------------------------------------------------------------------
// Sky light class
//----------------------------------------------------------------------------
void FSkyLight::Import( FLightmassImporter& Importer )
{
FLight::Import( Importer );
Importer.ImportData( (FSkyLightData*)this );
}
void FMeshLightPrimitive::AddSubPrimitive(const FTexelToCorners& TexelToCorners, const FIntPoint& Coordinates, const FLinearColor& InTexelPower, float NormalOffset)
{
const FVector4 FirstTriangleNormal = (TexelToCorners.Corners[0].WorldPosition - TexelToCorners.Corners[1].WorldPosition) ^ (TexelToCorners.Corners[2].WorldPosition - TexelToCorners.Corners[1].WorldPosition);
const float FirstTriangleArea = .5f * FirstTriangleNormal.Size3();
const FVector4 SecondTriangleNormal = (TexelToCorners.Corners[2].WorldPosition - TexelToCorners.Corners[1].WorldPosition) ^ (TexelToCorners.Corners[2].WorldPosition - TexelToCorners.Corners[3].WorldPosition);
const float SecondTriangleArea = .5f * SecondTriangleNormal.Size3();
const float SubPrimitiveSurfaceArea = FirstTriangleArea + SecondTriangleArea;
// Convert power per texel into power per texel surface area
const FLinearColor SubPrimitivePower = InTexelPower * SubPrimitiveSurfaceArea;
// If this is the first sub primitive, initialize
if (NumSubPrimitives == 0)
{
SurfaceNormal = TexelToCorners.WorldTangentZ;
const FVector4 OffsetAmount = NormalOffset * TexelToCorners.WorldTangentZ;
for (int32 CornerIndex = 0; CornerIndex < NumTexelCorners; CornerIndex++)
{
Corners[CornerIndex].WorldPosition = TexelToCorners.Corners[CornerIndex].WorldPosition + OffsetAmount;
Corners[CornerIndex].FurthestCoordinates = Coordinates;
}
SurfaceArea = SubPrimitiveSurfaceArea;
Power = SubPrimitivePower;
}
else
{
// Average sub primitive normals
SurfaceNormal += TexelToCorners.WorldTangentZ;
// Directions corresponding to CornerOffsets in FStaticLightingSystem::CalculateTexelCorners
static const FIntPoint CornerDirections[NumTexelCorners] =
{
FIntPoint(-1, -1),
FIntPoint(1, -1),
FIntPoint(-1, 1),
FIntPoint(1, 1)
};
const FVector4 OffsetAmount = NormalOffset * TexelToCorners.WorldTangentZ;
for (int32 CornerIndex = 0; CornerIndex < NumTexelCorners; CornerIndex++)
{
const FIntPoint& ExistingFurthestCoordinates = Corners[CornerIndex].FurthestCoordinates;
// Store the new position if this coordinate is greater or equal to the previous coordinate for this corner in texture space, in the direction of the corner.
if (CornerDirections[CornerIndex].X * (Coordinates.X - ExistingFurthestCoordinates.X) >=0
&& CornerDirections[CornerIndex].Y * (Coordinates.Y - ExistingFurthestCoordinates.Y) >=0)
{
Corners[CornerIndex].WorldPosition = TexelToCorners.Corners[CornerIndex].WorldPosition + OffsetAmount;
Corners[CornerIndex].FurthestCoordinates = Coordinates;
}
}
// Accumulate the area and power that this simplified primitive represents
SurfaceArea += SubPrimitiveSurfaceArea;
Power += SubPrimitivePower;
}
NumSubPrimitives++;
}
void FMeshLightPrimitive::Finalize()
{
SurfaceNormal = SurfaceNormal.SizeSquared3() > SMALL_NUMBER ? SurfaceNormal.GetUnsafeNormal3() : FVector4(0, 0, 1);
}
//----------------------------------------------------------------------------
// Mesh Area Light class
//----------------------------------------------------------------------------
void FMeshAreaLight::Initialize(float InIndirectPhotonEmitConeAngle, const FBoxSphereBounds& InImportanceBounds)
{
CosIndirectPhotonEmitConeAngle = FMath::Cos(InIndirectPhotonEmitConeAngle);
ImportanceBounds = InImportanceBounds;
}
/** Returns the number of direct photons to gather required by this light. */
int32 FMeshAreaLight::GetNumDirectPhotons(float DirectPhotonDensity) const
{
// Gather enough photons to meet DirectPhotonDensity at the influence radius of the mesh area light.
// Clamp the influence radius to the importance or scene radius for the purposes of emitting photons
// This prevents huge mesh area lights from emitting more photons than are needed
const float InfluenceSphereSurfaceAreaMillions = 4.0f * (float)PI * FMath::Square(FMath::Min(ImportanceBounds.SphereRadius, InfluenceRadius)) / 1000000.0f;
const int32 NumDirectPhotons = FMath::TruncToInt(InfluenceSphereSurfaceAreaMillions * DirectPhotonDensity);
return NumDirectPhotons == appTruncErrorCode ? INT_MAX : NumDirectPhotons;
}
/** Initializes the mesh area light with primitives */
void FMeshAreaLight::SetPrimitives(
const TArray<FMeshLightPrimitive>& InPrimitives,
float EmissiveLightFalloffExponent,
float EmissiveLightExplicitInfluenceRadius,
int32 InMeshAreaLightGridSize,
FGuid InLevelGuid)
{
check(InPrimitives.Num() > 0);
Primitives = InPrimitives;
MeshAreaLightGridSize = InMeshAreaLightGridSize;
LevelGuid = InLevelGuid;
TotalSurfaceArea = 0.0f;
TotalPower = FLinearColor::Black;
Position = FVector4(0,0,0);
FBox Bounds(ForceInit);
CachedPrimitiveNormals.Empty(MeshAreaLightGridSize * MeshAreaLightGridSize);
CachedPrimitiveNormals.AddZeroed(MeshAreaLightGridSize * MeshAreaLightGridSize);
PrimitivePDFs.Empty(Primitives.Num());
for (int32 PrimitiveIndex = 0; PrimitiveIndex < Primitives.Num(); PrimitiveIndex++)
{
const FMeshLightPrimitive& CurrentPrimitive = Primitives[PrimitiveIndex];
TotalSurfaceArea += CurrentPrimitive.SurfaceArea;
TotalPower += CurrentPrimitive.Power;
PrimitivePDFs.Add(CurrentPrimitive.SurfaceArea);
for (int32 CornerIndex = 0; CornerIndex < NumTexelCorners; CornerIndex++)
{
Bounds += CurrentPrimitive.Corners[CornerIndex].WorldPosition;
}
const FVector2D SphericalCoordinates = FVector(CurrentPrimitive.SurfaceNormal).UnitCartesianToSpherical();
// Determine grid cell the primitive's normal falls into based on spherical coordinates
const int32 CacheX = FMath::Clamp(FMath::TruncToInt(SphericalCoordinates.X / (float)PI * MeshAreaLightGridSize), 0, MeshAreaLightGridSize - 1);
const int32 CacheY = FMath::Clamp(FMath::TruncToInt((SphericalCoordinates.Y + (float)PI) / (2 * (float)PI) * MeshAreaLightGridSize), 0, MeshAreaLightGridSize - 1);
CachedPrimitiveNormals[CacheY * MeshAreaLightGridSize + CacheX].Add(CurrentPrimitive.SurfaceNormal);
}
for (int32 PhiStep = 0; PhiStep < MeshAreaLightGridSize; PhiStep++)
{
for (int32 ThetaStep = 0; ThetaStep < MeshAreaLightGridSize; ThetaStep++)
{
const TArray<FVector4>& CurrentCachedNormals = CachedPrimitiveNormals[PhiStep * MeshAreaLightGridSize + ThetaStep];
if (CurrentCachedNormals.Num() > 0)
{
OccupiedCachedPrimitiveNormalCells.Add(FIntPoint(ThetaStep, PhiStep));
}
}
}
// Compute the Cumulative Distribution Function for our step function of primitive surface areas
CalculateStep1dCDF(PrimitivePDFs, PrimitiveCDFs, UnnormalizedIntegral);
SourceBounds = FBoxSphereBounds(Bounds);
Position = SourceBounds.Origin;
Position.W = 1.0f;
check(TotalSurfaceArea > 0.0f);
check(TotalPower.R > 0.0f || TotalPower.G > 0.0f || TotalPower.B > 0.0f);
// The irradiance value at which to place the light's influence radius
const float IrradianceCutoff = .002f;
// If EmissiveLightExplicitInfluenceRadius is 0, automatically generate the influence radius based on the light's power
// Solve Irradiance = Power / Distance ^2 for Radius
//@todo - should the SourceBounds also factor into the InfluenceRadius calculation?
InfluenceRadius = EmissiveLightExplicitInfluenceRadius > DELTA ? EmissiveLightExplicitInfluenceRadius : FMath::Sqrt(FLinearColorUtils::LinearRGBToXYZ(TotalPower).G / IrradianceCutoff);
FalloffExponent = EmissiveLightFalloffExponent;
// Using the default for point lights
ShadowExponent = 2.0f;
}
/**
* Tests whether the light affects the given bounding volume.
* @param Bounds - The bounding volume to test.
* @return True if the light affects the bounding volume
*/
bool FMeshAreaLight::AffectsBounds(const FBoxSphereBounds& Bounds) const
{
if((Bounds.Origin - Position).SizeSquared() > FMath::Square(InfluenceRadius + Bounds.SphereRadius + SourceBounds.SphereRadius))
{
return false;
}
if(!FLight::AffectsBounds(Bounds))
{
return false;
}
return true;
}
/**
* Computes the intensity of the direct lighting from this light on a specific point.
*/
FLinearColor FMeshAreaLight::GetDirectIntensity(const FVector4& Point, bool bCalculateForIndirectLighting) const
{
FLinearColor AccumulatedPower(ForceInit);
float AccumulatedSurfaceArea = 0.0f;
for (int32 PrimitiveIndex = 0; PrimitiveIndex < Primitives.Num(); PrimitiveIndex++)
{
const FMeshLightPrimitive& CurrentPrimitive = Primitives[PrimitiveIndex];
FVector4 PrimitiveCenter(0,0,0);
for (int32 CornerIndex = 0; CornerIndex < NumTexelCorners; CornerIndex++)
{
PrimitiveCenter += CurrentPrimitive.Corners[CornerIndex].WorldPosition / 4.0f;
}
const FVector4 LightVector = (Point - PrimitiveCenter).GetSafeNormal();
const float NDotL = Dot3(LightVector, CurrentPrimitive.SurfaceNormal);
if (NDotL >= 0)
{
// Using standard Unreal attenuation for point lights for each primitive
const float RadialAttenuation = FMath::Pow(FMath::Max(1.0f - ((PrimitiveCenter - Point) / InfluenceRadius).SizeSquared3(), 0.0f), FalloffExponent);
// Weight exitant power by the distance attenuation to this primitive and the light's cosine distribution around the primitive's normal
//@todo - photon emitting does not take the cosine distribution into account
AccumulatedPower += CurrentPrimitive.Power * RadialAttenuation * NDotL;
}
}
return AccumulatedPower / TotalSurfaceArea * (bCalculateForIndirectLighting ? IndirectLightingScale : 1.0f);
}
/** Returns an intensity scale based on the receiving point. */
float FMeshAreaLight::CustomAttenuation(const FVector4& Point, FLMRandomStream& RandomStream) const
{
const float FullProbabilityDistance = .5f * InfluenceRadius;
float PowerWeightedAttenuation = 0.0f;
float PowerWeightedPhysicalAttenuation = 0.0f;
float DepositProbability = 0.0f;
for (int32 PrimitiveIndex = 0; PrimitiveIndex < Primitives.Num(); PrimitiveIndex++)
{
const FMeshLightPrimitive& CurrentPrimitive = Primitives[PrimitiveIndex];
FVector4 PrimitiveCenter(0,0,0);
for (int32 CornerIndex = 0; CornerIndex < NumTexelCorners; CornerIndex++)
{
PrimitiveCenter += CurrentPrimitive.Corners[CornerIndex].WorldPosition / 4.0f;
}
const float NDotL = Dot3((Point - PrimitiveCenter), CurrentPrimitive.SurfaceNormal);
if (NDotL >= 0)
{
const float RadialAttenuation = FMath::Pow(FMath::Max(1.0f - ((PrimitiveCenter - Point) / InfluenceRadius).SizeSquared3(), 0.0f), FalloffExponent);
const float PowerWeight = FLinearColorUtils::LinearRGBToXYZ(CurrentPrimitive.Power).G;
// Weight the attenuation factors by how much power this primitive emits, and its distance attenuation
PowerWeightedAttenuation += PowerWeight * RadialAttenuation;
// Also accumulate physical attenuation
const float DistanceSquared = (PrimitiveCenter - Point).SizeSquared3();
PowerWeightedPhysicalAttenuation += PowerWeight / DistanceSquared;
DepositProbability += CurrentPrimitive.SurfaceArea / TotalSurfaceArea * FMath::Min(DistanceSquared / (FullProbabilityDistance * FullProbabilityDistance), 1.0f);
}
}
DepositProbability = FMath::Clamp(DepositProbability, 0.0f, 1.0f);
// Thin out photons near the light source.
// This is partly an optimization since the photon density near light sources doesn't need to be high, and the natural 1 / R^2 density is overkill,
// But this also improves quality since we are doing a nearest N photon neighbor search when calculating irradiance.
// If the photon map has a high density of low power photons near light sources,
// Combined with sparse, high power photons from other light sources (directional lights for example), the result will be very splotchy.
if (RandomStream.GetFraction() < DepositProbability)
{
// Remove physical attenuation, apply standard Unreal point light attenuation from each primitive
return PowerWeightedAttenuation / (PowerWeightedPhysicalAttenuation * DepositProbability);
}
else
{
return 0.0f;
}
}
// Fudge factor to get mesh area light photon intensities to match direct lighting more closely.
static const float MeshAreaLightIntensityScale = 2.5f;
/** Generates a direction sample from the light's domain */
void FMeshAreaLight::SampleDirection(FLMRandomStream& RandomStream, FLightRay& SampleRay, FVector4& LightSourceNormal, FVector2D& LightSurfacePosition, float& RayPDF, FLinearColor& Power) const
{
FLightSurfaceSample SurfaceSample;
FMeshAreaLight::SampleLightSurface(RandomStream, SurfaceSample);
const float DistanceFromCenter = (SurfaceSample.Position - Position).Size3();
// Generate a sample direction from a distribution that is uniform over all directions
FVector4 SampleDir;
do
{
SampleDir = GetUnitVector(RandomStream);
}
// Regenerate the direction vector until it is less than .1 of a degree from perpendicular to the light's surface normal
// This prevents generating directions that are deemed outside of the light source primitive's hemisphere by later calculations due to fp imprecision
while(FMath::Abs(Dot3(SampleDir, SurfaceSample.Normal)) < .0017);
if (Dot3(SampleDir, SurfaceSample.Normal) < 0.0f)
{
// Reflect the sample direction across the origin so that it lies in the same hemisphere as the primitive normal
SampleDir *= -1.0f;
}
SampleRay = FLightRay(
SurfaceSample.Position,
SurfaceSample.Position + SampleDir * FMath::Max(InfluenceRadius - DistanceFromCenter, 0.0f),
NULL,
this
);
LightSourceNormal = SurfaceSample.Normal;
// The probability of selecting any direction in a hemisphere defined by each primitive normal
const float HemispherePDF = 1.0f / (2.0f * (float)PI);
RayPDF = 0.0f;
const FIntPoint Corners[] =
{
FIntPoint(0,0),
FIntPoint(0,1),
FIntPoint(1,0),
FIntPoint(1,1)
};
// Use a grid which contains cached primitive normals to accelerate PDF calculation
// This prevents the need to iterate over all of the mesh area light's primitives, of which there may be thousands
for (int32 OccupiedCellIndex = 0; OccupiedCellIndex < OccupiedCachedPrimitiveNormalCells.Num(); OccupiedCellIndex++)
{
const int32 ThetaStep = OccupiedCachedPrimitiveNormalCells[OccupiedCellIndex].X;
const int32 PhiStep = OccupiedCachedPrimitiveNormalCells[OccupiedCellIndex].Y;
const TArray<FVector4>& CurrentCachedNormals = CachedPrimitiveNormals[PhiStep * MeshAreaLightGridSize + ThetaStep];
if (CurrentCachedNormals.Num() > 0)
{
bool bAllCornersInSameHemisphere = true;
bool bAllCornersInOppositeHemisphere = true;
// Determine whether the cell is completely in the same hemisphere as the sample direction, completely on the other side or spanning the terminator
// This is done by checking each cell's corners
for (int32 CornerIndex = 0; CornerIndex < ARRAY_COUNT(Corners); CornerIndex++)
{
const float Theta = (ThetaStep + Corners[CornerIndex].X) / (float)MeshAreaLightGridSize * (float)PI;
const float Phi = (PhiStep + Corners[CornerIndex].Y) / (float)MeshAreaLightGridSize * 2 * (float)PI - (float)PI;
// Calculate the cartesian unit direction corresponding to this corner
const FVector4 CurrentCornerDirection = FVector2D(Theta, Phi).SphericalToUnitCartesian();
bAllCornersInSameHemisphere = bAllCornersInSameHemisphere && Dot3(CurrentCornerDirection, SampleDir) > 0.0f;
bAllCornersInOppositeHemisphere = bAllCornersInOppositeHemisphere && Dot3(CurrentCornerDirection, SampleDir) < 0.0f;
}
if (bAllCornersInSameHemisphere)
{
// If the entire cell is in the same hemisphere as the sample direction, the sample could have been generated from any of them
RayPDF += CurrentCachedNormals.Num() * HemispherePDF;
}
else if (!bAllCornersInOppositeHemisphere)
{
// If the cell spans both hemispheres, we have to test each normal individually
for (int32 CachedNormalIndex = 0; CachedNormalIndex < CurrentCachedNormals.Num(); CachedNormalIndex++)
{
if (Dot3(CurrentCachedNormals[CachedNormalIndex], SampleDir) > 0.0f)
{
// Accumulate the probability that this direction was generated by each primitive
RayPDF += HemispherePDF;
}
}
}
}
}
RayPDF /= Primitives.Num();
checkSlow(RayPDF > 0.0f);
Power = TotalPower / TotalSurfaceArea * MeshAreaLightIntensityScale;
}
/** Generates a direction sample from the light based on the given rays */
void FMeshAreaLight::SampleDirection(
const TArray<FIndirectPathRay>& IndirectPathRays,
FLMRandomStream& RandomStream,
FLightRay& SampleRay,
float& RayPDF,
FLinearColor& Power) const
{
checkSlow(IndirectPathRays.Num() > 0);
// Pick an indirect path ray with uniform probability
const int32 RayIndex = FMath::TruncToInt(RandomStream.GetFraction() * IndirectPathRays.Num());
checkSlow(RayIndex >= 0 && RayIndex < IndirectPathRays.Num());
const FIndirectPathRay& ChosenPathRay = IndirectPathRays[RayIndex];
const FVector4 PathRayDirection = ChosenPathRay.UnitDirection;
FVector4 XAxis(0,0,0);
FVector4 YAxis(0,0,0);
GenerateCoordinateSystem(PathRayDirection, XAxis, YAxis);
// Calculate Cos of the angle between the direction and the light source normal.
// This is also the Sin of the angle between the direction and the plane perpendicular to the normal.
const float DirectionDotLightNormal = Dot3(PathRayDirection, ChosenPathRay.LightSourceNormal);
checkSlow(DirectionDotLightNormal > 0.0f);
// Calculate Cos of the angle between the direction and the plane perpendicular to the normal using cos^2 + sin^2 = 1
const float CosDirectionNormalPlaneAngle = FMath::Sqrt(1.0f - DirectionDotLightNormal * DirectionDotLightNormal);
// Clamp the cone angle to CosDirectionNormalPlaneAngle so that any direction generated from the cone lies in the same hemisphere
// As the light source normal that was used to generate that direction.
// This is necessary to make sure we only generate directions that the light actually emits in.
// Within the range [0, PI / 2], smaller angles have a larger cosine
// The DELTA bias is to avoid generating directions that are so close to being perpendicular to the normal that their dot product is negative due to fp imprecision.
const float CosEmitConeAngle = FMath::Max(CosIndirectPhotonEmitConeAngle, FMath::Min(CosDirectionNormalPlaneAngle + DELTA, 1.0f));
// Generate a sample direction within a cone about the indirect path
const FVector4 ConeSampleDirection = UniformSampleCone(RandomStream, CosEmitConeAngle, XAxis, YAxis, PathRayDirection);
FLightSurfaceSample SurfaceSample;
float NormalDotSampleDirection = 0.0f;
do
{
// Generate a surface sample
FMeshAreaLight::SampleLightSurface(RandomStream, SurfaceSample);
NormalDotSampleDirection = Dot3(SurfaceSample.Normal, ConeSampleDirection);
}
// Use rejection sampling to find a surface position that is valid for ConeSampleDirection
while(NormalDotSampleDirection < 0.0f);
const float DistanceFromCenter = (SurfaceSample.Position - Position).Size3();
SampleRay = FLightRay(
SurfaceSample.Position,
SurfaceSample.Position + ConeSampleDirection * FMath::Max(InfluenceRadius - DistanceFromCenter, 0.0f),
NULL,
this
);
const float ConePDF = UniformConePDF(CosEmitConeAngle);
RayPDF = 0.0f;
// Calculate the probability that this direction was chosen
for (int32 OtherRayIndex = 0; OtherRayIndex < IndirectPathRays.Num(); OtherRayIndex++)
{
// Accumulate the cone probability for all the cones which contain the sample position
if (Dot3(IndirectPathRays[OtherRayIndex].UnitDirection, ConeSampleDirection) > (1.0f - DELTA) * CosEmitConeAngle)
{
RayPDF += ConePDF;
}
}
RayPDF /= IndirectPathRays.Num();
checkSlow(RayPDF > 0);
Power = TotalPower / TotalSurfaceArea * MeshAreaLightIntensityScale;
}
/** Validates a surface sample given the position that sample is affecting. */
void FMeshAreaLight::ValidateSurfaceSample(const FVector4& Point, FLightSurfaceSample& Sample) const
{
}
/** Returns the light's radiant power. */
float FMeshAreaLight::Power() const
{
const FLinearColor LightPower = TotalPower / TotalSurfaceArea * 2.0f * (float)PI * InfluenceRadius * InfluenceRadius;
return FLinearColorUtils::LinearRGBToXYZ(LightPower).G;
}
/** Generates a sample on the light's surface. */
void FMeshAreaLight::SampleLightSurface(FLMRandomStream& RandomStream, FLightSurfaceSample& Sample) const
{
float PrimitivePDF;
float FloatPrimitiveIndex;
// Pick a primitive with probability proportional to the primitive's fraction of the light's total surface area
Sample1dCDF(PrimitivePDFs, PrimitiveCDFs, UnnormalizedIntegral, RandomStream, PrimitivePDF, FloatPrimitiveIndex);
const int32 PrimitiveIndex = FMath::TruncToInt(FloatPrimitiveIndex * Primitives.Num());
check(PrimitiveIndex >= 0 && PrimitiveIndex < Primitives.Num());
const FMeshLightPrimitive& SelectedPrimitive = Primitives[PrimitiveIndex];
// Approximate the primitive as a coplanar square, and sample uniformly by area
const float Alpha1 = RandomStream.GetFraction();
const FVector4 InterpolatedPosition1 = FMath::Lerp(SelectedPrimitive.Corners[0].WorldPosition, SelectedPrimitive.Corners[1].WorldPosition, Alpha1);
const FVector4 InterpolatedPosition2 = FMath::Lerp(SelectedPrimitive.Corners[2].WorldPosition, SelectedPrimitive.Corners[3].WorldPosition, Alpha1);
const float Alpha2 = RandomStream.GetFraction();
const FVector4 SamplePosition = FMath::Lerp(InterpolatedPosition1, InterpolatedPosition2, Alpha2);
const float SamplePDF = PrimitivePDF / SelectedPrimitive.SurfaceArea;
Sample = FLightSurfaceSample(SamplePosition, SelectedPrimitive.SurfaceNormal, FVector2D(0,0), SamplePDF);
}
/** Returns true if all parts of the light are behind the surface being tested. */
bool FMeshAreaLight::BehindSurface(const FVector4& TrianglePoint, const FVector4& TriangleNormal) const
{
const float NormalDotLight = Dot3(TriangleNormal, FMeshAreaLight::GetDirectLightingDirection(TrianglePoint, TriangleNormal));
return NormalDotLight < 0.0f;
}
/** Gets a single direction to use for direct lighting that is representative of the whole area light. */
FVector4 FMeshAreaLight::GetDirectLightingDirection(const FVector4& Point, const FVector4& PointNormal) const
{
// The position on a sphere approximating the area light surface that will first be visible to a triangle rotating toward the light
const FVector4 FirstVisibleLightPoint = Position + PointNormal * SourceBounds.SphereRadius;
return FirstVisibleLightPoint - Point;
}
}