/// <summary> /// 根据设置更换合适的shader 和设置响应的阴影设置 /// </summary> /// <param name="sRender"></param> public static void ChangeShader(Renderer sRender) { if (sRender == null || GameScene.mainScene == null) { return; } bool flag = false; if (sRender.gameObject.layer == GameLayer.Layer_Ground) //只有地面接受阴影哦 { sRender.receiveShadows = true; } else { sRender.receiveShadows = false; if (sRender.gameObject.layer == GameLayer.Layer_UIEffect) { flag = true; } } for (int i = 0; i < sRender.materials.Length; i++) { Material material = sRender.materials[i]; if (!(material == null)) { string a = material.shader.name; if (!GameScene.mainScene.terrainConfig.enablePointLight) { if (a == GameObjectUnit.diffuseShaderName || a == GameObjectUnit.snailDiffusePointShaderName) { material.shader = GameObjectUnit.snailDiffuseShader; } if (a == GameObjectUnit.diffuseCutoutShaderName || a == GameObjectUnit.snailDiffusePointCutoutShaderName) { material.shader = GameObjectUnit.snailDiffuseCutoutShader; } } else { if (a == GameObjectUnit.diffuseShaderName || a == GameObjectUnit.snailDiffuseShaderName) { material.shader = GameObjectUnit.snailDiffusePointShader; } if (a == GameObjectUnit.diffuseCutoutShaderName || a == GameObjectUnit.snailDiffuseCutoutShaderName) { material.shader = GameObjectUnit.snailDiffusePointCutoutShader; } } if (flag) { GameObjectUnit.ChangeShader(material, new string[] { "Snail/Bumped Specular Point Light2", "Snail/Bumped Specular Point Light Cutout2", "Snail/Bumped Specular Point Light UV Anima2" }); } } } }
public void CombineRender(MaterialAsset oOtherAsset) { if (this.oSelfAsset == null) { return; } MeshRenderer component = base.GetComponent <MeshRenderer>(); if (component != null) { component.material = oOtherAsset.material; GameObjectUnit.ChangeShader(component); } MeshFilter component2 = base.GetComponent <MeshFilter>(); if (component2 != null) { component2.mesh = this.oSelfAsset.mesh; } if (this.matchMono != null) { UnityEngine.Object.Destroy(this.matchMono); } UnityEngine.Object.Destroy(this); }
/// <summary> /// 从unit映射中移除指定unit对象 /// </summary> /// <param name="unit"></param> public void RemoveUnit(GameObjectUnit unit) { if (this.unitsMap.ContainsKey(unit.createID)) { this.unitsMap.Remove(unit.createID); this.units.Remove(unit); this.unitCount--; } }
/// <summary> /// 添加指定的unit到tile的unit映射中 /// </summary> /// <param name="unit"></param> public void AddUnit(GameObjectUnit unit) { if (this.unitsMap.ContainsKey(unit.createID)) { return; } this.unitsMap.Add(unit.createID, unit); this.units.Add(unit); this.unitCount++; }
public void CombineRender(MeshAsset oOtherAsset) { if (this.oSelfAsset == null) { return; } if (this.matchMono is SkinMeshLoader) { SkinnedMeshRenderer component = base.GetComponent <SkinnedMeshRenderer>(); if (component != null) { component.material = this.oSelfAsset.material; component.sharedMesh = oOtherAsset.mesh; GameObjectUnit.ChangeShader(component); } } else if (this.matchMono is MeshLoader) { MeshRenderer component2 = base.GetComponent <MeshRenderer>(); if (component2 != null) { component2.material = this.oSelfAsset.material; GameObjectUnit.ChangeShader(component2); } MeshFilter component3 = base.GetComponent <MeshFilter>(); if (component3 != null) { component3.mesh = oOtherAsset.mesh; } } else if (this.matchMono == null) { MeshRenderer component4 = base.GetComponent <MeshRenderer>(); if (component4 != null) { component4.material = this.oSelfAsset.material; GameObjectUnit.ChangeShader(component4); } else { SkinnedMeshRenderer component5 = base.GetComponent <SkinnedMeshRenderer>(); if (component5 != null) { component5.material = this.oSelfAsset.material; GameObjectUnit.ChangeShader(component5); } } } if (this.matchMono != null) { UnityEngine.Object.Destroy(this.matchMono); } UnityEngine.Object.Destroy(this); }
/// <summary> /// 创建unit游戏对象 /// </summary> /// <param name="scene"></param> /// <param name="pos"></param> /// <param name="createID"></param> /// <param name="prePath"></param> /// <returns></returns> public static GameObjectUnit Create(GameScene scene, Vector3 pos, int createID, string prePath) { GameObjectUnit gameObjectUnit = new GameObjectUnit(createID); gameObjectUnit.scene = scene; gameObjectUnit.position = pos; gameObjectUnit.center = pos; gameObjectUnit.prePath = prePath; gameObjectUnit.UpdateViewRange(); return(gameObjectUnit); }
/// <summary> /// 开始显示gameobject对象 /// </summary> public void Visible() { if (this.destroyed || this.willRemoved) //销毁或待移除,没法可视 { return; } if (!this.visible) { if (this.ins == null) { if (this.prePath != string.Empty) //需要预先加载 { if (GameObjectUnit.thridPardResourManager != null && this.scene.loadFromAssetBund) { if (this.pre != null) { this.Initialize(); } else { GameObjectUnit.thridPardResourManager(this.prePath, new AssetCallBack(this.OnThridPartAssetLoaded)); } } else { Asset asset = AssetLibrary.Load(this.prePath, AssetType.Prefab, LoadType.Type_Resources); if (!asset.loaded) { this.pre = null; } else { this.pre = asset.gameObject; } if (this.pre != null) { this.Initialize(); } } } } else { this.ins.SetActive(true); } if (!this.isStatic && this.ins != null) //如果不是静态,激活动态unit { this.ActiveDynaUnit(); } } this.visible = true; }
private void Update() { GameObjectUnit gameObjectUnit = GameScene.mainScene.FindUnit(base.gameObject.name); if (Application.isEditor) { gameObjectUnit.localScale = new Vector3(this.radius, this.radius, this.radius); if (gameObjectUnit.ins) { gameObjectUnit.ins.transform.localScale = new Vector3(this.radius, this.radius, this.radius); } } }
/// <summary> /// 根据unitid获取gameobjecunit对象 /// </summary> /// <param name="createID"></param> /// <returns></returns> public GameObjectUnit FindUint(int createID) { int num = this.tiles.Length; for (int i = 0; i < num; i++) { if (this.tiles[i] != null) { GameObjectUnit gameObjectUnit = this.tiles[i].FindUnit(createID); if (gameObjectUnit != null) { return(gameObjectUnit); } } } return(null); }
public void Update(GameObjectUnit target) { if (this.target == null) { this.target = target; Vector3 eulerAngles = base.transform.eulerAngles; this.x = eulerAngles.y; this.y = eulerAngles.x; this.CalDistance(); this.RotateCamera(true); } if (this.target != null) { this.ScrollMouse(); this.RotateCamera(false); } }
/// <summary> /// 拆分组合的unit /// </summary> /// <returns></returns> public bool DeCombineUnits() { if (this.combinUnitIDs.Count < 1) { return(false); } for (int i = 0; i < this.combinUnitIDs.Count; i++) { GameObjectUnit gameObjectUnit = GameScene.mainScene.FindUnitInTiles(this.combinUnitIDs[i]); if (gameObjectUnit == null) { return(false); } gameObjectUnit.combineParentUnitID = -1; } this.combinUnitIDs.Clear(); return(true); }
private void LateUpdate() { if (this.target == null) { if (GameScene.mainScene != null) { this.target = GameScene.mainScene.mainUnit; } if (this.target != null) { Vector3 eulerAngles = base.transform.eulerAngles; this.x = eulerAngles.y; this.y = eulerAngles.x; this.CalDistance(); this.RotateCamera(true); } } if (this.target != null) { this.ScrollMouse(); this.RotateCamera(false); } }
/// <summary> /// /// </summary> /// <param name="resolution">网格分辨率</param> /// <param name="mask">射线检测layermask</param> /// <param name="occlusionMask">计算检测剔除的层的layermask</param> /// <param name="selectObject"></param> public void ComputeHeights(int resolution, int mask, int occlusionMask, GameObjectUnit selectObject = null) { TerrainConfig terrainConfig = this.region.scene.terrainConfig; int mask_Ground = GameLayer.Mask_Ground; Ray ray = default(Ray); Vector3 vector = new Vector3(0f, 500f, 0f); Vector3 vector2 = new Vector3(1f, 500f, 0f); Vector3 vector3 = new Vector3(0f, 500f, 1f); Vector3 vector4 = new Vector3(1f, 500f, 1f); Vector3 vector5 = new Vector3(0.5f, 500f, 0.5f); Vector3 origin = new Vector3(-0.5f, 500f, -0.5f); Vector3[] array = new Vector3[] { vector, vector2, vector3, vector4, vector5 }; ray.direction = Vector3.down; float num = (float)terrainConfig.tileSize / (float)resolution; this.heights = new float[resolution, resolution]; this.grids = new int[resolution, resolution]; for (int i = 0; i < resolution; i++) { for (int j = 0; j < resolution; j++) { this.heights[i, j] = 0f; float num2 = 100000f; float num3 = -100000f; int num4 = 0; bool flag = false; for (int k = 0; k < array.Length; k++) { //计算每个resolution block的坐标 origin.x = (float)i * num + this.position.x + array[k].x - (float)terrainConfig.tileSize * 0.5f; origin.z = (float)j * num + this.position.z + array[k].z - (float)terrainConfig.tileSize * 0.5f; ray.origin = origin; RaycastHit raycastHit; Physics.Raycast(ray, out raycastHit, 2000f, mask); if (raycastHit.transform != null) { if (num4 == 0 && (1 << raycastHit.transform.gameObject.layer & occlusionMask) >= 1) //如果射线检测的游戏对象和剔除层有交集,则该网格阻塞,不可通过 { num4 = 1; } if (num2 > raycastHit.point.y) { num2 = raycastHit.point.y; } if (num3 < raycastHit.point.y) { num3 = raycastHit.point.y; } if (selectObject != null && selectObject.ins != null && raycastHit.transform.gameObject.name == selectObject.ins.name) //射线检测到的游戏对象为待测试游戏对象 { flag = true; } } } this.grids[i, j] = num4; if (num4 == 0) { if (num3 - num2 > terrainConfig.blockHeight) //如果一个resolution block 五点检测的最大高度度和最小高度差值大于阻塞高度,则改网格为阻塞,不可通过,否则可以 { this.grids[i, j] = 1; } else { this.grids[i, j] = 0; } } this.heights[i, j] = 0f; num2 = 100000f; num3 = -100000f; for (int l = 0; l < array.Length; l++) { origin.x = (float)i * num + this.position.x + array[l].x - (float)terrainConfig.tileSize * 0.5f; origin.z = (float)j * num + this.position.z + array[l].z - (float)terrainConfig.tileSize * 0.5f; ray.origin = origin; RaycastHit raycastHit; Physics.Raycast(ray, out raycastHit, 2000f, mask_Ground); if (raycastHit.transform != null) { if (num2 > raycastHit.point.y) { num2 = raycastHit.point.y; } if (num3 < raycastHit.point.y) { num3 = raycastHit.point.y; } } } if (num3 > terrainConfig.maxReachTerrainHeight) //如果网格点高度,大于最大高度,则该网格点不可通过 { this.grids[i, j] = 1; } this.heights[i, j] = num3; //计算保存网格点的高度值 if (flag && this.grids[i, j] == 1) //拣选到待测试对象,并且该网格阻塞,则将该网格坐标加入待测试unit网格列表中 { float num5 = (float)i * num + this.position.x + array[4].x - (float)terrainConfig.tileSize * 0.5f; float num6 = (float)j * num + this.position.z + array[4].z - (float)terrainConfig.tileSize * 0.5f; int gridX = Mathf.FloorToInt(num5 / terrainConfig.gridSize); int gridY = Mathf.FloorToInt(num6 / terrainConfig.gridSize); selectObject.AppendGrid(gridX, gridY); } } } }
/// <summary> /// 销毁回收 /// </summary> public void Destroy() { if (GameScene.isPlaying) { if (this.waterData != null) { this.waterData.Release(); } if (this.terrain != null) { this.terrain.Destroy(); DelegateProxy.GameDestory(this.terrain); DelegateProxy.GameDestory(this.terrain.gameObject); this.terrain = null; } if (this.water != null) { this.water.Destroy(); DelegateProxy.GameDestory(this.water); DelegateProxy.GameDestory(this.water.gameObject); this.water = null; } } else { if (this.terrain != null) { DelegateProxy.DestroyObjectImmediate(this.terrain.gameObject); } if (this.water != null) { DelegateProxy.DestroyObjectImmediate(this.water.gameObject); } } //同时销毁tile上的(静态)unit if (this.units != null) { while (this.units.Count > 0) { GameObjectUnit gameObjectUnit = this.units[0]; //进行destory,放入cache待重用 if (this.scene.ContainUnit(gameObjectUnit)) { this.scene.RemoveUnit(gameObjectUnit, true, true); } else { //场景不包含的unit,销毁,并加入静态缓存待重用 gameObjectUnit.Destroy(); this.scene.RemoveEmptyUnit(gameObjectUnit); } } this.units.Clear(); this.units = null; this.unitsMap.Clear(); this.unitsMap = null; } this.left = null; this.right = null; this.top = null; this.bot = null; this.top_left = null; this.top_right = null; this.bot_left = null; this.bot_right = null; this.region = null; this.scene = null; this._lightmapPrototype = null; this.waterData = null; this.heights = new float[0, 0]; this.grids = new int[0, 0]; this.heights = null; this.grids = null; }
/// <summary> /// 根据视点更新瓦片状态 ,如X宫格相关的tile,水体效果,unit列表状态等 /// </summary> /// <param name="eyePos"></param> public void Update(Vector3 eyePos) { if (this.units == null || this.units.Count < 1) //瓦片上没有unit,不进行更新 { return; } if (!GameScene.isPlaying && this.terrain != null) //当场景没有开始,地形对象非空时 ,更新下周围9宫格tile状态 { if (this.left == null) { this.left = this.scene.GetNeighborTile(this, -1, 0); } if (this.right == null) { this.right = this.scene.GetNeighborTile(this, 1, 0); } if (this.top == null) { this.top = this.scene.GetNeighborTile(this, 0, 1); } if (this.bot == null) { this.bot = this.scene.GetNeighborTile(this, 0, -1); } if (this.top_left == null) { this.top_left = this.scene.GetNeighborTile(this, -1, 1); } if (this.top_right == null) { this.top_right = this.scene.GetNeighborTile(this, 1, 1); } if (this.bot_left == null) { this.bot_left = this.scene.GetNeighborTile(this, -1, -1); } if (this.bot_right == null) { this.bot_right = this.scene.GetNeighborTile(this, 1, -1); } if (this.terrain.left == null && this.left != null) { this.terrain.left = this.left.terrain; } if (this.terrain.right == null && this.right != null) { this.terrain.right = this.right.terrain; } if (this.terrain.top == null && this.top != null) { this.terrain.top = this.top.terrain; } if (this.terrain.bot == null && this.bot != null) { this.terrain.bot = this.bot.terrain; } if (this.terrain.top_left == null && this.top_left != null) { this.terrain.top_left = this.top_left.terrain; } if (this.terrain.top_right == null && this.top_right != null) { this.terrain.top_right = this.top_right.terrain; } if (this.terrain.bot_left == null && this.bot_left != null) { this.terrain.bot_left = this.bot_left.terrain; } if (this.terrain.bot_right == null && this.bot_right != null) { this.terrain.bot_right = this.bot_right.terrain; } } if (!GameScene.isPlaying && this.water != null) //场景没有运行,但是水体已创建,强制更新 { this.water.ForcedUpdate(); } if (this.tick % 2 == 0) //两次进行一次 { this.computeCount = 0; int num = this.unitCount - 1; this.unitInd = this.lastInd; while (this.unitInd < this.unitCount) { if (this.computeCount > this.maxComputeCount) { this.lastInd = this.unitInd; return; } GameObjectUnit gameObjectUnit = this.units[this.unitInd]; if (!gameObjectUnit.active && (gameObjectUnit.combineParentUnitID < 0 || GameScene.SampleMode)) //如果unit没有激活并且组合父unitid为空或者游戏场景为采用模式 { this.dx = gameObjectUnit.center.x - eyePos.x; this.dz = gameObjectUnit.center.z - eyePos.z; gameObjectUnit.viewDistance = Mathf.Sqrt(this.dx * this.dx + this.dz * this.dz); if (gameObjectUnit.viewDistance < gameObjectUnit.near) //小于近视距离,加入场景tile列表 { this.scene.AddUnit(gameObjectUnit); } } if (this.unitInd >= num) { this.lastInd = 0; } this.computeCount++; this.unitInd++; } } this.tick++; }
/// <summary> /// 读取瓦片数据信息 /// </summary> /// <param name="br"></param> public void Read(BinaryReader br) { TerrainConfig terrainConfig = this.region.scene.terrainConfig; this.tileX = br.ReadInt32(); this.tileY = br.ReadInt32(); this.key = string.Concat(new object[] { this.region.regionX, "_", this.region.regionY, "_", this.tileX, "_", this.tileY }); this.far = terrainConfig.tileCullingDistance; br.ReadSingle(); br.ReadSingle(); br.ReadSingle(); bool flag = br.ReadBoolean(); long num = br.BaseStream.Position; int num2 = terrainConfig.heightmapResolution; if (br.ReadInt32() == 10001) { num2 = terrainConfig.heightmapResolution; } else { num2 = terrainConfig.heightmapResolution + 1; br.BaseStream.Position = num; } if (GameScene.isPlaying) { br.BaseStream.Position += 8196L; } else { if (flag) { for (int i = 0; i < num2; i++) { for (int j = 0; j < num2; j++) { this.heights = new float[num2, num2]; this.heights[j, i] = br.ReadSingle(); } } } num = br.BaseStream.Position; if (br.ReadInt32() == 10002) { for (int i = 0; i < num2; i++) { for (int j = 0; j < num2; j++) { this.grids = new int[num2, num2]; this.grids[j, i] = br.ReadInt32(); } } } else { br.BaseStream.Position = num; } } this.position.x = (float)(this.tileX * terrainConfig.tileSize) + this.region.actualX; this.position.y = terrainConfig.defaultTerrainHeight; this.position.z = (float)(this.tileY * terrainConfig.tileSize) + this.region.actualY; int num3 = br.ReadInt32(); int num4 = 0; if (num3 > 0) { //循环读取unit对象 while (br.ReadString() == "GameObject") { int num5 = br.ReadInt32(); GameObjectUnit gameObjectUnit = null; if (this.scene.curSceneUnitsMap.ContainsKey(num5)) { gameObjectUnit = this.scene.curSceneUnitsMap[num5]; } if (gameObjectUnit == null) { gameObjectUnit = this.scene.CreateEmptyUnit(num5); } gameObjectUnit.scene = this.region.scene; gameObjectUnit.Read(br, num5); if (GameScene.SampleMode) { this.AddUnit(gameObjectUnit); gameObjectUnit.tiles.Add(this); if (!this.scene.curSceneUnitsMap.ContainsKey(num5)) { this.scene.curSceneUnitsMap.Add(num5, gameObjectUnit); } } else if ((gameObjectUnit.combineParentUnitID < 0 && gameObjectUnit.type != UnitType.UnitType_Light) || !GameScene.isPlaying) { this.AddUnit(gameObjectUnit); gameObjectUnit.tiles.Add(this); if (!this.scene.curSceneUnitsMap.ContainsKey(num5)) { this.scene.curSceneUnitsMap.Add(num5, gameObjectUnit); } } else if (gameObjectUnit != null) { gameObjectUnit.Destroy(); this.scene.RemoveEmptyUnit(gameObjectUnit); } //将unit,tile,scene关联映射 num4++; if (num4 >= num3) { break; } } } //读取水体数据 if (br.BaseStream.Position < br.BaseStream.Length) { num = br.BaseStream.Position; if (br.ReadInt32() == 10011) { if (br.ReadInt32() == 1) { this.waterData = new WaterData(); this.waterData.Read(br); } } else { br.BaseStream.Position = num; } } //读取光照贴图原型数据属性 bool flag2 = br.ReadBoolean(); if (flag2) { this._lightmapPrototype.lightmapIndex = br.ReadInt32(); num = br.BaseStream.Position; if (br.ReadInt32() == 10007) { this._lightmapPrototype.scale = br.ReadSingle(); } else { br.BaseStream.Position = num; } this._lightmapPrototype.lightmapTilingOffset.x = br.ReadSingle(); this._lightmapPrototype.lightmapTilingOffset.y = br.ReadSingle(); this._lightmapPrototype.lightmapTilingOffset.z = br.ReadSingle(); this._lightmapPrototype.lightmapTilingOffset.w = br.ReadSingle(); } //瓦片包围盒 this.bounds = default(Bounds); this.bounds.min = new Vector3(this.position.x - (float)terrainConfig.tileSize * 0.5f, 0f, this.position.z - (float)terrainConfig.tileSize * 0.5f); this.bounds.max = new Vector3(this.position.x + (float)terrainConfig.tileSize * 0.5f, 300f, this.position.z + (float)terrainConfig.tileSize * 0.5f); //tile资源路径 this.path = string.Concat(new object[] { "Scenes/", this.region.scene.sceneID, "/", this.region.regionX, "_", this.region.regionY, "/", this.tileX, "_", this.tileY }); }
/// <summary> /// tile是否包含指定unit映射 /// </summary> /// <param name="unit"></param> /// <returns></returns> public bool ContainUnit(GameObjectUnit unit) { return(this.unitsMap.ContainsKey(unit.createID)); }
public virtual void Destroy() { this.unit = null; }