public GameObjectState(GameObjectController controller) { _controller = controller; _curPos = Terrain.GetWorldPos(_controller.transform.position); CheckAndResetPos(); Chunk chunk = World.world.GetChunk(_curPos.x, 0, _curPos.z); AttachChunk(chunk); WorldPos chunkPos = chunk.worldPos; _inBlock = _attachChunk.GetBlock(_curPos.x - chunkPos.x, _curPos.y - chunkPos.y, _curPos.z - chunkPos.z, true); _standBlock = _attachChunk.GetBlock(_curPos.x - chunkPos.x, _curPos.y - chunkPos.y - 1, _curPos.z - chunkPos.z, true); _blockLightLevel = _attachChunk.GetBlockLight(_curPos.x - chunkPos.x, _curPos.y - chunkPos.y, _curPos.z - chunkPos.z, true); _sunLightLevel = _attachChunk.GetSunLight(_curPos.x - chunkPos.x, _curPos.y - chunkPos.y, _curPos.z - chunkPos.z, true); InitRenderLight(_blockLightLevel, _sunLightLevel); }
private void SpreadInPos(int x, int y, int z, Chunk chunk, int curLightLevel) { //光照强度小于等于1时不再传播 if (curLightLevel < 2) { return; } Block b = chunk.GetBlock(x, y, z, true); BlockAttributeCalculator calculator = BlockAttributeCalculatorFactory.GetCalculator(b.BlockType); int lightDamp = calculator.LightDamp(b.ExtendId); if (lightDamp < WorldConfig.Instance.maxLightLevel) { int lightLevel = chunk.GetBlockLight(x, y, z); int nextLightLevel = curLightLevel - lightDamp - 1; if (nextLightLevel > lightLevel) { int curBlockLightLevel = calculator.LightLevel(b.ExtendId); if (curBlockLightLevel > nextLightLevel) { nextLightLevel = curBlockLightLevel; } SetLight(chunk, x, y, z, nextLightLevel); } } }
public int GetBlockLight(int x, int y, int z, bool isInRange = false) { if (!isInRange && !IsInRange(x, y, z)) { return(chunk.GetBlockLight(x, y + chunkOffsetY, z)); } return(blockLight.GetData(x, y, z)); }
protected virtual void AddSpecialColor(Chunk chunk, int x, int y, int z, MeshData meshData) { int sunLight = chunk.GetSunLight(x, y, z, true); int blockLight = chunk.GetBlockLight(x, y, z, true); meshData.AddColor(MeshBaseDataCache.Instance.GetColor(LightConst.lightColor[sunLight], LightConst.lightColor[blockLight], 0, 2)); meshData.AddColor(MeshBaseDataCache.Instance.GetColor(LightConst.lightColor[sunLight], LightConst.lightColor[blockLight], 0, 2)); meshData.AddColor(MeshBaseDataCache.Instance.GetColor(LightConst.lightColor[sunLight], LightConst.lightColor[blockLight], 0, 2)); meshData.AddColor(MeshBaseDataCache.Instance.GetColor(LightConst.lightColor[sunLight], LightConst.lightColor[blockLight], 0, 2)); }
protected void AddSpecialColorAndSpeed(Chunk chunk, int x, int y, int z, MeshData meshData, float speed = 0.5f) { int sunLight = chunk.GetSunLight(x, y, z); int blockLight = chunk.GetBlockLight(x, y, z); meshData.AddColor(MeshBaseDataCache.Instance.GetColor(LightConst.lightColor[sunLight], LightConst.lightColor[blockLight], speed, 2)); meshData.AddColor(MeshBaseDataCache.Instance.GetColor(LightConst.lightColor[sunLight], LightConst.lightColor[blockLight], speed, 2)); meshData.AddColor(MeshBaseDataCache.Instance.GetColor(LightConst.lightColor[sunLight], LightConst.lightColor[blockLight], speed, 2)); meshData.AddColor(MeshBaseDataCache.Instance.GetColor(LightConst.lightColor[sunLight], LightConst.lightColor[blockLight], speed, 2)); }
public int GetBlockLight(int x, int y, int z) { Chunk chunk = GetChunk(x, 0, z); if (chunk != null && chunk.isPopulationDataPrepared) { return(chunk.GetBlockLight(x - chunk.worldPos.x, y - chunk.worldPos.y, z - chunk.worldPos.z, true)); } return(0); }
private bool CheckLightCondition(Chunk chunk, int condition, int x, int y, int z) { int light = chunk.GetBlockLight(x, y + 1, z); if (light >= condition) { return(true); } return(false); }
public override void CalculateSpecialMesh(Chunk chunk, int x, int y, int z, MeshData meshData, Block self, Block other, BlockAttributeCalculator otherCalculator, Direction direction) { //因为会计算六次 if (direction != Direction.right) { return; } if (!IsModelCenter(self.ExtendId)) { return; } ModelData modelData = ModelDataManager.Instance.GetModelData((int)self.BlockType); Direction face = GetFaceDirection(self.ExtendId); Vector2 rotate = GetRotateSinAndCos(face); int sunLight = chunk.GetSunLight(x, y, z, true); int blockLight = chunk.GetBlockLight(x, y, z, true); Rect rect = GetUVRect(self.ExtendId, direction); meshData.useRenderDataForCol = true; int verticesIndex = meshData.GetCurVerticesIndex(); int colVerticesIndex = meshData.GetCurColVerticesIndex(); for (int i = 0; i < modelData.vertices.Length; i++) { Vector3[] vertices = modelData.vertices; Vector2[] uvs = modelData.uvs; float verX = vertices[i].x * rotate.y + vertices[i].z * rotate.x; float verZ = -vertices[i].x * rotate.x + vertices[i].z * rotate.y; float realX = x + 0.5f + verX; float realY = y + vertices[i].y; float realZ = z + 0.5f + verZ; //添加渲染网格 meshData.AddVertice(MeshBaseDataCache.Instance.GetVector3(realX, realY, realZ)); meshData.AddColor(MeshBaseDataCache.Instance.GetColor(LightConst.lightColor[sunLight], LightConst.lightColor[blockLight], 0, 2)); //这里要转换uv坐标 meshData.AddUV(MeshBaseDataCache.Instance.GetVector2(rect.x + uvs[i].x * rect.width, rect.y + uvs[i].y * rect.height)); //添加碰撞网格 meshData.AddColVertice(MeshBaseDataCache.Instance.GetVector3(realX, realY, realZ)); } for (int i = 0; i < modelData.triangles.Length; i++) { meshData.AddTriangle(verticesIndex + modelData.triangles[i]); meshData.AddColTriangle(colVerticesIndex + modelData.triangles[i]); } meshData.useRenderDataForCol = MeshData.DefaultUseRenderDataForCol; }
private void ShrinkInPos(int x, int y, int z, Chunk chunk, int prevLightLevel, int curLightLevel) { //当前光照强度为最大值时不收缩 if (curLightLevel >= WorldConfig.Instance.maxLightLevel) { return; } Block b = chunk.GetBlock(x, y, z, true); BlockAttributeCalculator calculator = BlockAttributeCalculatorFactory.GetCalculator(b.BlockType); int lightDamp = calculator.LightDamp(b.ExtendId); if (lightDamp < WorldConfig.Instance.maxLightLevel) { int lightLevel = chunk.GetBlockLight(x, y, z); if (lightLevel > 0) { int temp = prevLightLevel - lightDamp; //如果前一个物块比当前物块的太阳光亮,那么减弱当前物块的亮度 if (temp > lightLevel) { int nextLightLevel = curLightLevel - lightDamp - 1; if (nextLightLevel < 0) { nextLightLevel = 0; } //如果最终结果没有发生改变,那么不收缩 if (nextLightLevel == lightLevel) { return; } chunk.SetBlockLight(x, y, z, nextLightLevel, true); if (!_changedList.Contains(chunk)) { _changedList.Add(chunk); } int nextIndex = (x * Chunk.chunkDepth + z) * Chunk.chunkHeight + y; _lightBfsQueue.Enqueue(NodeCache.Instance.GetShrinkNode(nextIndex, lightLevel, nextLightLevel, chunk)); } //如果前一个物块比当前物块的太阳光暗,那么增强前一个物块的亮度 else if (temp < lightLevel) { int nextIndex = (x * Chunk.chunkDepth + z) * Chunk.chunkHeight + y; _lightSpread.AddSpreadNode(NodeCache.Instance.GetSpreadNode(nextIndex, lightLevel, chunk)); } } } }
private List <Chunk> GetBlockLightChangeChunks(Chunk chunk, int x, int y, int z, Block b, List <Chunk> list) { BlockAttributeCalculator calculator = BlockAttributeCalculatorFactory.GetCalculator(b.BlockType); int curBlockLightLevel = calculator.LightLevel(b.ExtendId); int blockLightLevel = chunk.GetBlockLight(x, y, z, true); if (curBlockLightLevel > blockLightLevel) { int index = (x * Chunk.chunkDepth + z) * Chunk.chunkHeight + y; LightSpreadNode node = NodeCache.Instance.GetSpreadNode(index, curBlockLightLevel, chunk); _blockLightSpread.AddSpreadNode(node); chunk.SetBlockLight(x, y, z, curBlockLightLevel, true); } else if (curBlockLightLevel < blockLightLevel) { int index = (x * Chunk.chunkDepth + z) * Chunk.chunkHeight + y; LightShrinkNode node = NodeCache.Instance.GetShrinkNode(index, blockLightLevel, curBlockLightLevel, chunk); _blockLightShrink.AddShrinkNode(node); chunk.SetBlockLight(x, y, z, curBlockLightLevel, true); } List <Chunk> spreadList = _blockLightSpread.SpreadInChunk(chunk); List <Chunk> shrinkList = _blockLightShrink.ShrinkInChunk(chunk); for (int i = 0; i < spreadList.Count; i++) { if (!list.Contains(spreadList[i])) { list.Add(spreadList[i]); } } for (int i = 0; i < shrinkList.Count; i++) { if (!list.Contains(shrinkList[i])) { list.Add(shrinkList[i]); } } return(list); }