| | 3637 | protected virtual void EnsureShadowTexturesCreated() |
| | 3638 | { |
| | 3639 | if ( shadowTextureConfigDirty ) |
| | 3640 | { |
| | 3641 | DestroyShadowTextures(); |
| | 3642 | ShadowTextureManager.Instance.GetShadowTextures( shadowTextureConfigList, shadowTextures ); |
| | 3643 | |
| | 3644 | // clear shadow cam - light mapping |
| | 3645 | shadowCameraLightMapping.Clear(); |
| | 3646 | |
| | 3647 | |
| | 3648 | // Recreate shadow textures |
| | 3649 | foreach ( Texture shadowTexture in shadowTextures ) |
| | 3650 | { |
| | 3651 | // Camera names are local to SM |
| | 3652 | String camName = shadowTexture.Name + "Cam"; |
| | 3653 | // Material names are global to SM, make specific |
| | 3654 | String matName = shadowTexture.Name + "Mat" + this.Name; |
| | 3655 | |
| | 3656 | RenderTexture shadowRTT = shadowTexture.GetBuffer().GetRenderTarget(); |
| | 3657 | |
| | 3658 | // Create camera for this texture, but note that we have to rebind |
| | 3659 | // in PrepareShadowTextures to coexist with multiple SMs |
| | 3660 | Camera cam = CreateCamera( camName ); |
| | 3661 | cam.AspectRatio = shadowTexture.Width / (Real)shadowTexture.Height; |
| | 3662 | shadowTextureCameras.Add( cam ); |
| | 3663 | |
| | 3664 | // Create a viewport, if not there already |
| | 3665 | if ( shadowRTT.ViewportCount == 0 ) |
| | 3666 | { |
| | 3667 | // Note camera assignment is transient when multiple SMs |
| | 3668 | Viewport v = shadowRTT.AddViewport( cam ); |
| | 3669 | v.ClearEveryFrame = true; |
| | 3670 | // remove overlays |
| | 3671 | v.ShowOverlays = false; |
| | 3672 | } |
| | 3673 | |
| | 3674 | // Don't update automatically - we'll do it when required |
| | 3675 | shadowRTT.IsAutoUpdated = false; |
| | 3676 | |
| | 3677 | // Also create corresponding Material used for rendering this shadow |
| | 3678 | Material mat = (Material)MaterialManager.Instance[ matName ]; |
| | 3679 | if ( mat == null ) |
| | 3680 | { |
| | 3681 | mat = (Material)MaterialManager.Instance.Create( matName, ResourceGroupManager.InternalResourceGroupName ); |
| | 3682 | } |
| | 3683 | Pass p = mat.GetTechnique( 0 ).GetPass( 0 ); |
| | 3684 | if ( p.TextureUnitStageCount != 1 /* || |
| | 3685 | p.GetTextureUnitState( 0 ).GetTexture( 0 ) != shadowTexture */ ) |
| | 3686 | { |
| | 3687 | mat.GetTechnique( 0 ).GetPass( 0 ).RemoveAllTextureUnitStates(); |
| | 3688 | // create texture unit referring to render target texture |
| | 3689 | TextureUnitState texUnit = p.CreateTextureUnitState( shadowTexture.Name ); |
| | 3690 | // set projective based on camera |
| | 3691 | texUnit.SetProjectiveTexturing( !p.HasVertexProgram, cam ); |
| | 3692 | // clamp to border colour |
| | 3693 | texUnit.TextureAddressing = TextureAddressing.Border; |
| | 3694 | texUnit.TextureBorderColor = ColorEx.White; |
| | 3695 | mat.Touch(); |
| | 3696 | |
| | 3697 | } |
| | 3698 | |
| | 3699 | // insert dummy camera-light combination |
| | 3700 | shadowCameraLightMapping.Add( cam, null ); |
| | 3701 | |
| | 3702 | // Get null shadow texture |
| | 3703 | nullShadowTexture = shadowTextureConfigList.Count == 0 ? |
| | 3704 | null : |
| | 3705 | ShadowTextureManager.Instance.GetNullShadowTexture( shadowTextureConfigList[ 0 ].format ); |
| | 3706 | } |
| | 3707 | shadowTextureConfigDirty = false; |
| | 3708 | } |
| | 3709 | } |
| | 3710 | |