bool GetCurShadowLightInfo(ShadowHelper.Shadower shadower, out Matrix shadowerTransform, out float intensity, out Vector3 lightPos, out Vector3 lightDir, out bool bDirectional) { Vector4 col0, col1, col2; if (shadower.mContext is StaticMeshComp) { StaticMeshComp smc = shadower.mContext as StaticMeshComp; shadowerTransform = smc.mPO.GetMatrix(); if (!mVisibleSMC.Contains(smc)) { intensity = 0f; lightPos = lightDir = Vector3.Zero; bDirectional = true; return(false); } MeshLighting ml = smc.mOwner.GetComponent( typeof(MeshLighting)) as MeshLighting; if (ml == null || !ml.NeedsShadow()) { intensity = 0f; lightPos = lightDir = Vector3.Zero; bDirectional = true; return(false); } ml.GetCurrentValues(out col0, out col1, out col2, out intensity, out lightPos, out lightDir, out bDirectional); return(true); } if (shadower.mChar == mPChar) { shadowerTransform = mPChar.GetTransform(); } else { intensity = 0f; lightPos = Vector3.Zero; lightDir = Vector3.Zero; bDirectional = false; shadowerTransform = Matrix.Identity; return(false); } return(mPMeshLighting.GetCurrentValues(out col0, out col1, out col2, out intensity, out lightPos, out lightDir, out bDirectional)); }
//called once before render with accumulated delta //do all once per render style updates in here internal void RenderUpdate(float msDelta) { if (msDelta <= 0f) { return; //can happen if fixed time and no remainder } //check pvs against entities mVisibleSMC.Clear(); foreach (StaticMeshComp smc in mStaticComps) { Vector3 smPos = smc.mPO.GetPosition(); if (mZone.IsVisibleFrom(mGD.GCam.Position, smPos)) { mVisibleSMC.Add(smc); } } //update meshlighting for visible foreach (StaticMeshComp smc in mVisibleSMC) { MeshLighting ml = smc.mOwner.GetComponent(typeof(MeshLighting)) as MeshLighting; ConvexVolume cv = smc.mOwner.GetComponent(typeof(ConvexVolume)) as ConvexVolume; ml.Update(msDelta / 1000f, smc.mPO.GetPosition() + cv.SizeY * 0.5f); } mZoneDraw.Update(msDelta); mZoneMats.UpdateWVP(Matrix.Identity, mGD.GCam.View, mGD.GCam.Projection, mGD.GCam.Position); if (mStaticMats != null) { mStaticMats.UpdateWVP(Matrix.Identity, mGD.GCam.View, mGD.GCam.Projection, mGD.GCam.Position); } if (mPMats != null) { mPMats.UpdateWVP(Matrix.Identity, mGD.GCam.View, mGD.GCam.Projection, mGD.GCam.Position); } mShadKeeper.StartNewFrame(); mShadKeeper.ComputeBatch(mPMob.GetEyePos()); }
void SetTriLightForSMC(StaticMeshComp smc) { MeshLighting ml = smc.mOwner.GetComponent( typeof(MeshLighting)) as MeshLighting; StaticMesh sm = smc.mDrawObject as StaticMesh; if (ml == null || sm == null) { return; } Vector4 lightCol0, lightCol1, lightCol2; Vector3 lightPos, lightDir; bool bDir; float intensity; ml.GetCurrentValues( out lightCol0, out lightCol1, out lightCol2, out intensity, out lightPos, out lightDir, out bDir); sm.SetTriLightValues(lightCol0, lightCol1, lightCol2, lightDir); }
internal MapLoop(GraphicsDevice gd, string gameRootDir) { mGD = gd; mGameRootDir = gameRootDir; mResX = gd.RendForm.ClientRectangle.Width; mResY = gd.RendForm.ClientRectangle.Height; mSKeeper = new StuffKeeper(); mSKeeper.eCompileNeeded += SharedForms.ShaderCompileHelper.CompileNeededHandler; mSKeeper.eCompileDone += SharedForms.ShaderCompileHelper.CompileDoneHandler; mSKeeper.Init(mGD, gameRootDir); mZoneMats = new MatLib(gd, mSKeeper); mZone = new Zone(); mZoneDraw = new MeshLib.IndoorMesh(gd, mZoneMats); mPartMats = new MatLib(mGD, mSKeeper); mPB = new ParticleBoss(gd.GD, mPartMats); mFontMats = new MatLib(gd, mSKeeper); mFontMats.CreateMaterial("Text"); mFontMats.SetMaterialEffect("Text", "2D.fx"); mFontMats.SetMaterialTechnique("Text", "Text"); mFonts = mSKeeper.GetFontList(); mST = new ScreenText(gd.GD, mFontMats, mFonts[0], 1000); mTextProj = Matrix.OrthoOffCenterLH(0, mResX, mResY, 0, 0.1f, 5f); //grab two UI textures to show how to do gumpery List <string> texs = mSKeeper.GetTexture2DList(); List <string> uiTex = new List <string>(); foreach (string tex in texs) { if (tex.StartsWith("UI")) { uiTex.Add(tex); } } Vector4 color = Vector4.UnitY + (Vector4.UnitW * 0.15f); //string indicators for various statusy things mST.AddString(mFonts[0], "Stuffs", "ClimbStatus", color, Vector2.UnitX * 20f + Vector2.UnitY * 600f, Vector2.One); mST.AddString(mFonts[0], "Stuffs", "LevelStatus", color, Vector2.UnitX * 20f + Vector2.UnitY * 620f, Vector2.One); mST.AddString(mFonts[0], "Stuffs", "PosStatus", color, Vector2.UnitX * 20f + Vector2.UnitY * 640f, Vector2.One); mST.AddString(mFonts[0], "(G), (H) to clear: Dynamic Lights: 0", "DynStatus", color, Vector2.UnitX * 20f + Vector2.UnitY * 660f, Vector2.One); mZoneMats.InitCelShading(1); // mZoneMats.GenerateCelTexturePreset(gd.GD, // gd.GD.FeatureLevel == FeatureLevel.Level_9_3, false, 0); // mZoneMats.SetCelTexture(0); float [] thresholds = new float[3 - 1]; float [] levels = new float[3]; thresholds[0] = 0.7f; thresholds[1] = 0.3f; levels[0] = 1; levels[1] = 0.8f; levels[2] = 0.5f; mZoneMats.GenerateCelTexture(gd.GD, gd.GD.FeatureLevel == SharpDX.Direct3D.FeatureLevel.Level_9_3, 0, 256, thresholds, levels); mZoneMats.SetCelTexture(0); mZoneDraw = new IndoorMesh(gd, mZoneMats); mAudio.LoadAllSounds(mGameRootDir + "\\Audio\\SoundFX"); //set up post processing module mPost = new PostProcess(gd, mZoneMats, "Post.fx"); #if true mPost.MakePostTarget(gd, "SceneColor", mResX, mResY, Format.R16G16B16A16_Float); mPost.MakePostDepth(gd, "SceneDepth", mResX, mResY, (gd.GD.FeatureLevel != FeatureLevel.Level_9_3)? Format.D32_Float_S8X24_UInt : Format.D24_UNorm_S8_UInt); mPost.MakePostTarget(gd, "SceneDepthMatNorm", mResX, mResY, Format.R16G16B16A16_Float); mPost.MakePostTarget(gd, "Bleach", mResX, mResY, Format.R16G16B16A16_Float); mPost.MakePostTarget(gd, "Outline", mResX, mResY, Format.R16G16B16A16_Float); mPost.MakePostTargetHalfRes(gd, "Bloom1", mResX / 2, mResY / 2, Format.R16G16B16A16_Float); mPost.MakePostTargetHalfRes(gd, "Bloom2", mResX / 2, mResY / 2, Format.R16G16B16A16_Float); #elif ThirtyTwo mPost.MakePostTarget(gd, "SceneColor", mResX, mResY, Format.R8G8B8A8_UNorm); mPost.MakePostDepth(gd, "SceneDepth", mResX, mResY, (gd.GD.FeatureLevel != FeatureLevel.Level_9_3)? Format.D32_Float_S8X24_UInt : Format.D24_UNorm_S8_UInt); mPost.MakePostTarget(gd, "SceneDepthMatNorm", mResX, mResY, Format.R16G16B16A16_Float); mPost.MakePostTarget(gd, "Bleach", mResX, mResY, Format.R8G8B8A8_UNorm); mPost.MakePostTarget(gd, "Outline", mResX, mResY, Format.R8G8B8A8_UNorm); mPost.MakePostTarget(gd, "Bloom1", mResX / 2, mResY / 2, Format.R8G8B8A8_UNorm); mPost.MakePostTarget(gd, "Bloom2", mResX / 2, mResY / 2, Format.R8G8B8A8_UNorm); #else mPost.MakePostTarget(gd, "SceneColor", mResX, mResY, Format.B5G5R5A1_UNorm); mPost.MakePostDepth(gd, "SceneDepth", mResX, mResY, (gd.GD.FeatureLevel != FeatureLevel.Level_9_3)? Format.D32_Float_S8X24_UInt : Format.D24_UNorm_S8_UInt); mPost.MakePostTarget(gd, "SceneDepthMatNorm", mResX, mResY, Format.R16G16B16A16_Float); mPost.MakePostTarget(gd, "Bleach", mResX, mResY, Format.B5G5R5A1_UNorm); mPost.MakePostTarget(gd, "Outline", mResX, mResY, Format.B5G5R5A1_UNorm); mPost.MakePostTarget(gd, "Bloom1", mResX / 2, mResY / 2, Format.B5G5R5A1_UNorm); mPost.MakePostTarget(gd, "Bloom2", mResX / 2, mResY / 2, Format.B5G5R5A1_UNorm); #endif if (gd.GD.FeatureLevel != FeatureLevel.Level_9_3) { mDynLights = new DynamicLights(mGD, mZoneMats, "BSP.fx"); } //see if any static stuff if (Directory.Exists(mGameRootDir + "/Statics")) { DirectoryInfo di = new DirectoryInfo(mGameRootDir + "/Statics"); FileInfo[] fi = di.GetFiles("*.MatLib", SearchOption.TopDirectoryOnly); if (fi.Length > 0) { mStaticMats = new MatLib(gd, mSKeeper); mStaticMats.ReadFromFile(fi[0].DirectoryName + "\\" + fi[0].Name); mStaticMats.InitCelShading(1); mStaticMats.GenerateCelTexturePreset(gd.GD, (gd.GD.FeatureLevel == FeatureLevel.Level_9_3), true, 0); mStaticMats.SetCelTexture(0); } mStatics = Mesh.LoadAllStaticMeshes(mGameRootDir + "\\Statics", gd.GD); //gen bounds, they don't seem to save correctly foreach (KeyValuePair <string, IArch> ia in mStatics) { ia.Value.UpdateBounds(); } } //load character stuff if any around if (Directory.Exists(mGameRootDir + "/Characters")) { DirectoryInfo di = new DirectoryInfo(mGameRootDir + "/Characters"); FileInfo[] fi = di.GetFiles("*.AnimLib", SearchOption.TopDirectoryOnly); if (fi.Length > 0) { mPAnims = new AnimLib(); mPAnims.ReadFromFile(fi[0].DirectoryName + "\\" + fi[0].Name); List <Anim> anims = mPAnims.GetAnims(); foreach (Anim a in anims) { mAnims.Add(a.Name); } } fi = di.GetFiles("*.MatLib", SearchOption.TopDirectoryOnly); if (fi.Length > 0) { mPMats = new MatLib(mGD, mSKeeper); mPMats.ReadFromFile(fi[0].DirectoryName + "\\" + fi[0].Name); mPMats.InitCelShading(1); mPMats.GenerateCelTexturePreset(gd.GD, gd.GD.FeatureLevel == FeatureLevel.Level_9_3, false, 0); mPMats.SetCelTexture(0); } fi = di.GetFiles("*.Character", SearchOption.TopDirectoryOnly); if (fi.Length > 0) { mPArch = new CharacterArch(); mPArch.ReadFromFile(fi[0].DirectoryName + "\\" + fi[0].Name, mGD.GD, false); } fi = di.GetFiles("*.CharacterInstance", SearchOption.TopDirectoryOnly); if (fi.Length > 0) { mPChar = new Character(mPArch, mPAnims); mPChar.ReadFromFile(fi[0].DirectoryName + "\\" + fi[0].Name); mPChar.SetMatLib(mPMats); mPShad = new ShadowHelper.Shadower(); mPShad.mChar = mPChar; mPShad.mContext = this; mPEntity = new Entity(true, mEBoss); mPMeshLighting = new MeshLighting(mPEntity, mZone, PlayerBoxStanding / 2f, mZoneDraw.GetStyleStrength); mPEntity.AddComponent(mPMeshLighting); } } mPMob = new Mobile(mPChar, PlayerBoxWidth, PlayerBoxStanding, PlayerEyeStanding, true); mPCamMob = new Mobile(mPChar, PlayerBoxWidth, PlayerBoxStanding, PlayerEyeStanding, true); mFatBox = Misc.MakeBox(PlayerBoxWidth + 1, PlayerBoxStanding); mKeeper.AddLib(mZoneMats); if (mStaticMats != null) { mKeeper.AddLib(mStaticMats); } if (mPMats != null) { mKeeper.AddLib(mPMats); } //example material groups //these treat all materials in the group //as a single material for the purposes //of drawing cartoony outlines around them List <string> skinMats = new List <string>(); skinMats.Add("Face"); skinMats.Add("Skin"); skinMats.Add("EyeWhite"); skinMats.Add("EyeLiner"); skinMats.Add("LeftIris"); skinMats.Add("LeftPupil"); skinMats.Add("RightIris"); skinMats.Add("RightPupil"); // skinMats.Add("Nails"); mKeeper.AddMaterialGroup("SkinGroup", skinMats); if (Directory.Exists(mGameRootDir + "/Levels")) { DirectoryInfo di = new DirectoryInfo(mGameRootDir + "/Levels"); FileInfo[] fi = di.GetFiles("*.Zone", SearchOption.TopDirectoryOnly); foreach (FileInfo f in fi) { mLevels.Add(f.Name.Substring(0, f.Name.Length - 5)); } } //if debugger lands here, levels are sort of needed //otherwise there's not much point for this test prog ChangeLevel(mLevels[mCurLevel]); mST.ModifyStringText(mFonts[0], "(L) CurLevel: " + mLevels[mCurLevel], "LevelStatus"); }
void ChangeLevel(string level) { UpdateTimer fakeUT = new UpdateTimer(false, false); fakeUT.Stamp(); string lev = mGameRootDir + "/Levels/" + level; FreeLevelData(); mZone = new Zone(); mZoneMats.ReadFromFile(lev + ".MatLib"); mZone.Read(lev + ".Zone", false); mZoneDraw.Read(mGD, mSKeeper, lev + ".ZoneDraw", false); //for less state changes mZoneMats.FinalizeMaterials(); mZoneMats.SetLightMapsToAtlas(); QuakeTranslator qtrans = new QuakeTranslator(); qtrans.TranslateModels(mEBoss, mZone); qtrans.TranslateTriggers(mEBoss, mZone); qtrans.TranslateLights(mEBoss, mZone, mZoneDraw.SwitchLight); qtrans.TranslateItems(mEBoss, mZone, GetDrawObject); qtrans.TranslateWeapons(mEBoss, mZone, GetDrawObject); fakeUT.Stamp(); //grab the model movers mBModelMovers = mEBoss.GetEntityComponents(typeof(BModelMover)); //grab triggers mTriggers = mEBoss.GetEntityComponents(typeof(Trigger)); //grab convex volumes for pickups mPickUpCVs = mEBoss.GetEntityComponents(typeof(ConvexVolume)); //grab pickups mPickUps = mEBoss.GetEntityComponents(typeof(PickUp)); mStaticComps = mEBoss.GetEntityComponents(typeof(StaticMeshComp)); //make meshlighting for statics foreach (StaticMeshComp smc in mStaticComps) { StaticMesh sm = smc.mDrawObject as StaticMesh; float baseToMiddle = sm.GetBoxBound().Height / 2; MeshLighting ml = new MeshLighting(smc.mOwner, mZone, baseToMiddle, mZoneDraw.GetStyleStrength); smc.mOwner.AddComponent(ml); } mPHelper.Initialize(mZone, mPB); mIMHelper.Initialize(mZone); List <ZoneEntity> wEnt = mZone.GetEntities("worldspawn"); Debug.Assert(wEnt.Count == 1); float mDirShadowAtten; string ssa = wEnt[0].GetValue("SunShadowAtten"); if (!Single.TryParse(ssa, out mDirShadowAtten)) { mDirShadowAtten = 200f; //default } mShadowHelper.Initialize(mGD, 512, mDirShadowAtten, mZoneMats, mPost, GetCurShadowLightInfo, GetTransdBounds); mShadKeeper.Initialize(mGD, 512); List <Component> lights = mEBoss.GetEntityComponents(typeof(Light)); foreach (Component c in lights) { Light lt = c as Light; if (lt == null) { continue; } mShadKeeper.AddShadowCastingLight(lt.mPosition, lt.GetStrength()); } // mGraph.Load(lev + ".Pathing"); // mGraph.GenerateGraph(mZone.GetWalkableFaces, 32, 18f, CanPathReach); // mGraph.Save(mLevels[index] + ".Pathing"); // mGraph.BuildDrawInfo(gd); // mPathMobile.SetZone(mZone); mPMob.SetZone(mZone); mPCamMob.SetZone(mZone); MakeStaticShadowers(); float ang; Vector3 gpos = mZone.GetPlayerStartPos(out ang); mPMob.SetGroundPos(gpos); mPCamMob.SetGroundPos(gpos); mKeeper.Scan(); mKeeper.AssignIDsToEffectMaterials("BSP.fx"); if (mPChar != null) { mShadowHelper.RegisterShadower(mPShad, mPMats); mPChar.AssignMaterialIDs(mKeeper); } foreach (StaticMeshComp smc in mStaticComps) { StaticMesh sm = smc.mDrawObject as StaticMesh; if (sm == null) { continue; } sm.AssignMaterialIDs(mKeeper); } //update entities once //holy crap this is slow mEBoss.Update(fakeUT); }