//public CustomDropdown dropdownStandtaskList; void Awake() { webSocketManager = FindObjectOfType<WebSocketManager>(); messageManager = FindObjectOfType<MessageManager>(); rayCastManager = FindObjectOfType<RayCastManager>(); Finished = true; }
public static RayCastManager GetInstance() { if (instance == null) { instance = new RayCastManager(); } return(instance); }
public void UpdateLine(bool throughinsidewall, float width) { Vector3 vector = (new Vector3(MathDxx.Sin(base.transform.eulerAngles.y + 90f), 0f, MathDxx.Cos(base.transform.eulerAngles.y + 90f)) * width) / 2f; RayCastManager.CastMinDistance(base.transform.position + vector, base.transform.eulerAngles.y, throughinsidewall, out float num); RayCastManager.CastMinDistance(base.transform.position - vector, base.transform.eulerAngles.y, throughinsidewall, out float num2); float length = (num >= num2) ? num2 : num; this.SetLine(true, length); }
void Update() { //Set the position of the hexagonFrame to the selected Hexagon if ((CameraController.instance.isMoving) || (mousePosition.x != Input.mousePosition.x) || (mousePosition.y != Input.mousePosition.y)) { //If the player selects no hexagon the position is RayCastManager.noResult hexagonFrame.transform.position = RayCastManager.getWorldCoordsRaycast("Hexagon"); selectedPosition = hexagonFrame.transform.position; } mousePosition = Input.mousePosition; }
protected override void OnInstall() { base.AttackEffect = "WeaponHand1066Effect"; this.redline = Object.Instantiate <GameObject>(ResourceManager.Load <GameObject>("Game/Bullet/Bullet1066_RedLine")); this.redline.SetParentNormal(base.m_Entity.m_Body.transform); this.ctrl = this.redline.GetComponent <BulletRedLineCtrl>(); this.ctrl.SetLine(true, 0f); this.time = 0f; Vector3 vector = new Vector3(MathDxx.Sin(base.m_Entity.eulerAngles.y + 90f), 0f, MathDxx.Cos(base.m_Entity.eulerAngles.y + 90f)) * 0.5f; RayCastManager.CastMinDistance(base.m_Entity.m_Body.transform.position + vector, base.m_Entity.eulerAngles.y, false, out float num); RayCastManager.CastMinDistance(base.m_Entity.m_Body.transform.position - vector, base.m_Entity.eulerAngles.y, false, out float num2); this.mindis = (num >= num2) ? num2 : num; Updater.AddUpdate("Weapon1066", new Action <float>(this.OnUpdate), false); base.OnInstall(); }
private void UpdateLineLength(int index) { this.RedLineList[index].transform.position = new Vector3(this.resultpos.x, this.m_Entity.m_Body.LeftBullet.transform.position.y, this.resultpos.z); this.RedLineList[index].transform.rotation = Quaternion.Euler(0f, this.resultangle, 0f); Vector3 vector2 = this.nextpos - this.resultpos; RayCastManager.CastMinDistance(this.RedLineList[index].transform.position, vector2.normalized, this.bThroughWall, out float num, out this.resultpos, out Collider collider); this.lineCtrlList[index].SetLine(index == (this.RedLineList.Count - 1), num); if (collider != null) { this.resultangle = Utils.ExcuteReboundWallRedLine(this.RedLineList[index].transform, collider); } float x = MathDxx.Sin(this.resultangle); float z = MathDxx.Cos(this.resultangle); this.nextpos = this.resultpos + (new Vector3(x, 0f, z) * 40f); }
static float GetDistanceToGround(RayCastManager scene, Vector3 pos, int height) { VecInt3 g = pos; Vector3 localPos = pos - g; if (scene.isWalkable(g.x, g.y - 1, g.z, height)) { return(1 + localPos.y); } else if (scene.isWalkable(g.x, g.y, g.z, height)) { return(localPos.y); } else if (!scene.isWalkable(g.x, g.y, g.z, height)) { return(localPos.y - 1); } return(1); }
public void Init() { Debug.Log("VoxelMesh.Init"); //center = new Vector3(CenterX, CenterY, CenterZ); blockManager = new BlockManager(); blockManager.create(SpaceX * Const.ChunkSize, SpaceY * Const.ChunkSize, SpaceZ * Const.ChunkSize, new BlockTypeFun()); if (model == null) { model = new VoxelModel(); } else { for (int i = 0; i < model.sizeX; i++) { for (int j = 0; j < model.sizeY; j++) { for (int k = 0; k < model.sizeZ; k++) { short b = model.blocks[k * model.sizeX * model.sizeY + j * model.sizeX + i]; if (b != (short)BlockTypeEnum.Air) { blockManager.setBlock(i + model.originX, j + model.originY, k + model.originZ, b); } } } } } rtm = new RayCastManager(); rtm.create(blockManager.SizeX, blockManager.SizeY, blockManager.SizeZ); for (int x = 0; x < SpaceX * Const.ChunkSize; x++) { for (int y = 0; y < SpaceY * Const.ChunkSize; y++) { for (int z = 0; z < SpaceZ * Const.ChunkSize; z++) { rtm.setBlock(x, y, z, blockManager.getBlock(x, y, z) != 0 ? RayCastBlockType.All : RayCastBlockType.Nothing); } } } bDirty = true; curOperator = Operator.DeleteBlock; }
public void Init(EntityBase entity, int count, float[] angles) { this.Deinit(); this.time = 0f; for (int i = 0; i < count; i++) { GameObject child = Object.Instantiate <GameObject>(ResourceManager.Load <GameObject>("Game/Bullet/Bullet1066_RedLine")); child.SetParentNormal(entity.m_Body.transform); child.transform.localRotation = Quaternion.Euler(0f, angles[i], 0f); BulletRedLineCtrl component = child.GetComponent <BulletRedLineCtrl>(); component.SetLine(true, 0f); Vector3 vector = new Vector3(MathDxx.Sin(angles[i] + 90f), 0f, MathDxx.Cos(angles[i] + 90f)) * 0.5f; RayCastManager.CastMinDistance(entity.m_Body.transform.position + vector, entity.eulerAngles.y, false, out float num2); RayCastManager.CastMinDistance(entity.m_Body.transform.position - vector, entity.eulerAngles.y, false, out float num3); this.mindiss.Add((num2 >= num3) ? num3 : num2); this.list.Add(component); } Updater.AddUpdate("Weapon1066", new Action <float>(this.OnUpdate), false); }
static Vector3 GetNearestWalkablePos(RayCastManager scene, Vector3 pos, int height) { float r = 0.3f; VecInt3 g = pos; Vector3 localPos = pos - g; VecInt3 step;//移动的方向,-1,0,1 step.x = localPos.x < r ? -1 : (localPos.x > (1 - r) ? 1 : 0); step.z = localPos.z < r ? -1 : (localPos.z > (1 - r) ? 1 : 0); Vector3 offset;//退回到正确的位置需要移动的量 offset.x = step.x == -1 ? r - localPos.x : (step.x == 1 ? ((1 - r) - localPos.x) : 0); offset.z = step.z == -1 ? r - localPos.z : (step.z == 1 ? ((1 - r) - localPos.z) : 0); bool bCanMoveX = step.x != 0 ? scene.isWalkable(g.x + step.x, g.y, g.z, height) : true; bool bCanMoveZ = step.z != 0 ? scene.isWalkable(g.x, g.y, g.z + step.z, height) : true; bool bCanMoveXZ = (step.x != 0 && step.z != 0) ? scene.isWalkable(g.x + step.x, g.y, g.z + step.z, height) : true; bool bCanUpX = scene.isWalkable(g.x + step.x, g.y + 1, g.z, height); bool bCanUpZ = scene.isWalkable(g.x, g.y + 1, g.z + step.z, height); bool bCanUpXZ = scene.isWalkable(g.x + step.x, g.y + 1, g.z + step.z, height); bCanMoveX |= bCanUpX; bCanMoveZ |= bCanUpZ; bCanMoveXZ |= bCanUpXZ; if (!bCanMoveXZ && bCanMoveX && bCanMoveZ) { if (Mathf.Abs(offset.x) < Mathf.Abs(offset.z)) { bCanMoveX = false; } else { bCanMoveZ = false; } } if (!bCanMoveX) { pos.x += offset.x; } if (!bCanMoveZ) { pos.z += offset.z; } //if ((!bCanMoveX) || (!bCanMoveZ)) { // bool bCanStepUp = scene.testBlock(g.x + step.x, g.y + 1, g.z + step.z, RayCastBlockType.Walkable); // if (bCanStepUp) { // pos.y = g.y + 1; // } // else { // // } //} return(pos); }
static bool IsWalkable(RayCastManager scene, Vector3 pos, int height) { VecInt3 g = pos; return(scene.isWalkable(g.x, g.y, g.z, height)); }
IEnumerator InitWorld() { Block.BlockManager bm = new Block.BlockManager(); for (int i = 0; i < (int)Game.BlockType.Num; i++) { for (int f = 0; f < 6; f++) { Game.BlockType block = (Game.BlockType)i; texturePacker.AddTexture(block, f, TextureNameConfig.GetTextureName(block, f)); } } texturePacker.Pack(); packedTexture = texturePacker.GetPackedTexture(); BlockTypeFun blockTypeFun = new BlockTypeFun(); blockTypeFun.texturePacker = texturePacker; //float startTime = Time.realtimeSinceStartup; int WorldSizeX = 8; int WorldSizeY = 2; int WorldSizeZ = 8; //申请内存 bm.create(WorldSizeX * Const.ChunkSize, WorldSizeY * Const.ChunkSize, WorldSizeZ * Const.ChunkSize, blockTypeFun); DebugTool.Log("申请内存"); yield return(null); // TerrainTool.createTerrain(bm, WorldSizeX, WorldSizeY, WorldSizeZ); DebugTool.Log("创建地形"); yield return(null); TerrainTool.createTree(bm, WorldSizeX, WorldSizeY, WorldSizeZ); DebugTool.Log("创建树"); yield return(null); List <HouseItem> houseItems = new List <HouseItem>(); TerrainTool.createBuildings(bm, ref houseItems); DebugTool.Log("创建建筑完成"); yield return(null); //光线追踪初始化 RayCastManager rtm = new RayCastManager(); rtm.create(bm.SizeX, bm.SizeY, bm.SizeZ); for (int x = 0; x < rtm.getSizeX(); x++) { for (int z = 0; z < rtm.getSizeZ(); z++) { for (int y = 0; y < rtm.getSizeY(); y++) { short block = bm.getBlock(x, y, z); RayCastBlockType rlt = 0; rlt |= blockTypeFun.isCollider(block) ? RayCastBlockType.Collider : RayCastBlockType.Nothing; rlt |= blockTypeFun.isOpacity(block) ? RayCastBlockType.Opacity : RayCastBlockType.Nothing; rtm.setBlock(x, y, z, rlt); } rtm.updateInSun(x, z); } } //预处理光线 Vector3[][] rays = new Vector3[6][]; { for (int i = 0; i < 6; i++) { rays[i] = TerrainTool.getRandomRays(9, i); } } DebugTool.Log("预处理光线追踪完成"); yield return(null); rtm.updateAllLight(); DebugTool.Log("扩散光照计算完毕"); yield return(null); //int[] lightCount = new int[Const.MaxLightIteration + 1]; //for (int i = 0; i < WorldSizeX * Const.ChunkSize; i++) { // for (int k = 0; k < WorldSizeZ * Const.ChunkSize; k++) { // for (int j = 0; j < WorldSizeY * Const.ChunkSize; j++) { // lightCount[rtm.getLight(i, j, k)]++; // } // } //} //for (int i = 0; i < lightCount.Length; i++) { // Debug.Log("light (" + i + ") = " + lightCount[i]); //} Texture2D lightMap = new Texture2D(256, 256, TextureFormat.RGBAHalf, false); Color[] colors = new Color[256 * 256]; for (int i = 0; i < 256; i++) { for (int j = 0; j < 256; j++) { colors[j * 256 + i] = new Color(i / 256.0f, i / 256.0f, i / 256.0f, 1) * 2; } } lightMap.SetPixels(colors); lightMap.Apply(); LightmapData[] lightmaps = new LightmapData[1]; lightmaps[0] = new LightmapData(); lightmaps[0].lightmapFar = lightMap; lightmaps[0].lightmapNear = lightMap; LightmapSettings.lightmaps = lightmaps; GameObject itemsRoot = new GameObject("items"); for (int i = 0; i < houseItems.Count; i++) { HouseItem item = houseItems[i]; string path = HouseItemGenerator.GetItemPrefabPath(item.item); GameObject prefab = GlobalResources.loadPrefab(path); if (prefab != null) { GameObject inst = GameObject.Instantiate <GameObject>(prefab); inst.name = houseItems[i].item.ToString(); inst.transform.parent = itemsRoot.transform; inst.transform.position = houseItems[i].pos + new Vector3(0.5f, 0f, 0.5f); MeshRenderer renderer = inst.GetComponentInChildren <MeshRenderer>(); renderer.lightmapIndex = 0; inst.isStatic = true; float light = rtm.getLight(item.pos.x, item.pos.y, item.pos.z) / (float)Const.MaxLightIteration; light = Mathf.Lerp(0.0f, 0.6f, light); renderer.lightmapScaleOffset = new Vector4(0, 0, light, 0f); //MeshFilter meshFilter = inst.GetComponentInChildren<MeshFilter>(); //Mesh mesh = meshFilter.mesh; //Color32[] colors = new Color32[mesh.colors.Length]; //byte c = (byte)(rtm.getLight(item.pos.x, item.pos.y, item.pos.z) * 255); //ArrayTool.SetAll(colors, new Color32(c,c,c,1)); //mesh.colors32 = colors; } else { int a = 0; } } //初始化AO计算 rma = new RayMarchingAo(); rma.Init(rtm); //ViewVoxel viewVoxel = GameObject.FindObjectOfType<ViewVoxel>(); //if (viewVoxel) { // Texture3D voxelTex3D = rma.GetVoxelTexture(); // //OpenGLLibrary.glEnable(OpenGL.GL_TEXTURE_3D); // //OpenGLLibrary.glBindTexture(OpenGL.GL_TEXTURE_3D, voxelTex3D.GetNativeTexturePtr()); // //byte[] data = new byte[voxelTex3D.width * voxelTex3D.height * voxelTex3D.depth * 4]; // //for (int i = 0; i < data.Length; i++ ){ // // data[i] = 0x56; // //} // //OpenGL.TexSubImage3D(OpenGL.GL_TEXTURE_3D, 0, 0, 0, 0, 32, 32, 32, OpenGL.GL_RGBA, OpenGL.GL_UNSIGNED_BYTE, ref data); // //OpenGL.TextureSubImage3D(voxelTex3D, 0, 0, 0, 0, 1, 1, 1, OpenGL.GL_RED, OpenGL.GL_UNSIGNED_BYTE, ref data); // viewVoxel.SetVexelTex(voxelTex3D); //} //计算光照 Vector3 sunDir = new Vector3(1, -3, 1); sunDir.Normalize(); { //TerrainTool.calcLight(bm, rtm, rays, sunDir); //TerrainTool.calcLight2(bm,32); } DebugTool.Log("计算光照完成"); yield return(null); Material mat = GlobalResources.getBlockMaterial(); mat.mainTexture = texturePacker.GetPackedTexture(); GameObject root = GameObject.Find("Root"); if (root == null) { root = new GameObject("Root"); } Level level = GameObject.FindObjectOfType <Level>(); if (level) { level.scene = rtm; } //创建网格 for (int i = 0; i < WorldSizeX; i++) { for (int k = 0; k < WorldSizeZ; k++) { for (int j = 0; j < WorldSizeY; j++) { int baseX = i * Const.ChunkSize; int baseY = j * Const.ChunkSize; int baseZ = k * Const.ChunkSize; BlockChunk chunk = new BlockChunk(bm, baseX, baseY, baseZ); Vector3 chunkPos = new Vector3(i * Const.ChunkSize * Const.BlockSize, j * Const.ChunkSize * Const.BlockSize, k * Const.ChunkSize * Const.BlockSize); //bool[,,] visibleBlocks = new bool[Const.ChunkSize,Const.ChunkSize,Const.ChunkSize];// = MeshTool.GetVisibleBlocks(chunk, blockTypeFun); //TerrainTool.calcChunkLight(chunk, i, j, k, rtm, rays, sunDir); //Mesh mesh = MeshTool.createMesh(chunk, blockTypeFun, 0, 0, 0); for (int f = 0; f < 6; f++) { List <MeshTool.BlockSurface> surface = MeshTool.getChunkSurface(chunk, blockTypeFun, f); //for (int s = 0; s < surface.Count; s++) { // visibleBlocks[surface[s].pos.x, surface[s].pos.y, surface[s].pos.z] = true; //} Texture2D texSurface = MeshTool.SurfacePointsToTexture(surface, f); RenderTexture targetAoResult = rma.RenderByCalcShader(texSurface, new Vector3(i, j, k) * Const.ChunkSize, f); //回读亮度数据 RenderTexture.active = targetAoResult; Texture2D readback = new Texture2D(targetAoResult.width, targetAoResult.height); readback.ReadPixels(new Rect(0, 0, targetAoResult.width, targetAoResult.width), 0, 0); MeshTool.SetRaytraceAo(surface, readback); MeshTool.SetExtendAo(surface, rtm, f, new VecInt3(i * Const.ChunkSize, j * Const.ChunkSize, k * Const.ChunkSize)); //将可行走区域标记黑色 //if (f == (int)BlockFaceIndex.BFI_y1) { // for (int s = 0; s < surface.Count; s++) { // int gx = baseX + surface[s].pos.x; // int gy = baseY + surface[s].pos.y; // int gz = baseZ + surface[s].pos.z; // if (rtm.testBlock(gx, gy, gz, (byte)RayTraceBlockType.Walkable)) { // for (int v = 0; v < 4; v++) { // surface[s].raytraceAo[v] *= 0.1f; // } // } // } //} Mesh mesh = MeshTool.createMesh2(surface, f, blockTypeFun); if (mesh != null) { GameObject obj = new GameObject("Chunk", typeof(MeshRenderer), typeof(MeshFilter)); obj.isStatic = true; obj.GetComponent <Renderer>().material = mat; obj.GetComponent <MeshFilter>().mesh = mesh; obj.transform.SetParent(root.transform); obj.transform.position = chunkPos; } } //physics.AddChunk(visibleBlocks, chunkPos); yield return(null); } } } StaticBatchingUtility.Combine(root); StaticBatchingUtility.Combine(itemsRoot); DebugTool.Log("生成网格"); yield return(null); }
public void Init(Camera cam, EditorCommandManager commandManager) { //计算格子 List <Vector3> tempLines = new List <Vector3>(); for (int i = 0; i <= SpaceX * Const.ChunkSize; i++) { tempLines.Add(new Vector3(i, halfSpaceSize.y, 0) - halfSpaceSize); tempLines.Add(new Vector3(i, halfSpaceSize.y, SpaceY * Const.ChunkSize) - halfSpaceSize); } for (int j = 0; j <= SpaceY * Const.ChunkSize; j++) { tempLines.Add(new Vector3(0, halfSpaceSize.y, j) - halfSpaceSize); tempLines.Add(new Vector3(SpaceX * Const.ChunkSize, halfSpaceSize.y, j) - halfSpaceSize); } gridLines = tempLines.ToArray(); //预处理光线 rays = new Vector3[6][]; { for (int i = 0; i < 6; i++) { rays[i] = TerrainTool.getRandomRays(9, i); } } DebugTool.Log("预处理光线追踪"); camera = cam; this.commandManager = commandManager; blockManager = new BlockManager(); blockManager.create(SpaceX * Const.ChunkSize, SpaceY * Const.ChunkSize, SpaceZ * Const.ChunkSize, new BlockTypeFun()); //blockManager.forEachChunk((BlockChunk chunk, int i, int j, int k) => { // for (int x = 0; x < Const.ChunkSize; x++) { // for (int y = 0; y < Const.ChunkSize; y++) { // for (int z = 0; z < Const.ChunkSize; z++) { // int gx = i * Const.ChunkSize + x; // int gy = j * Const.ChunkSize + y; // int gz = k * Const.ChunkSize + z; // if ((new Vector3(gx, gy, gz) - new Vector3(SpaceX / 2.0f, SpaceY / 2.0f, SpaceZ / 2.0f) * Const.ChunkSize).magnitude < 10) { // chunk.setBlock(x, y, z, (int)BlockTypeEnum.Sand); // } // } // } // } //}); rtm = new RayCastManager(); rtm.create(blockManager.SizeX, blockManager.SizeY, blockManager.SizeZ); //rtm.moveTo(0, 0, 0, (int gx, int gy, int gz) => { // return blockManager.getBlock(gx, gy, gz) != 0 ? RayCastBlockType.All : RayCastBlockType.Nothing; //}); for (int x = 0; x < SpaceX * Const.ChunkSize; x++) { for (int y = 0; y < SpaceY * Const.ChunkSize; y++) { for (int z = 0; z < SpaceZ * Const.ChunkSize; z++) { rtm.setBlock(x, y, z, blockManager.getBlock(x, y, z) != 0 ? RayCastBlockType.All : RayCastBlockType.Nothing); } } } bDirty = true; previewMesh = GLRender.createCubeMesh(0.5f, Color.white); }
// Use this for initialization void Start() { SFServer = SmartFoxConnection.Connection; ourGWM = GameObject.Find("SceneScriptsObject").GetComponent<GameWorldManager>(); ourRCM = GameObject.Find("SceneScriptsObject").GetComponent<RayCastManager>(); ChatTB = GameObject.Find("ChatTB").GetComponent<InputField>(); ChatContent = GameObject.Find("ChatContent"); ChatTextLabel = new List<GameObject>(); rayCastLabel = GameObject.Find("RayCastLabel"); rayCastLabel.SetActive(false); ChatTBisFocused = false; inventoryOpen = false; }