private void Update() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); float3 dir = ray.direction; float3 pos = ray.origin; raycast_result result; if (Input.GetKeyDown(KeyCode.Mouse1)) { System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew(); Raycast.RaycastJob(ref gameWorld, pos, dir, 1000, out result); sw.Stop(); Debug.Log($"Raycast took {sw.ElapsedMilliseconds}ms and hit {result.hitPos.x}, {result.hitPos.y}, {result.hitPos.z}"); GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere); go.transform.localScale = new Vector3(.35f, .25f, .25f); go.transform.position = result.hitPos; Debug.DrawLine(pos, result.hitPos, new Color32(122, 0, 122, 255), 10000); } float boxDimWithPadding = (gameWorld.entityCount * .5f) + gameWorld.entityCount; Bounds b = new Bounds(new Vector3(boxDimWithPadding / 2, 0, boxDimWithPadding / 2), new Vector3(boxDimWithPadding, 100, boxDimWithPadding)); //positionBuffer.SetData(positions); //material.SetBuffer("positionBuffer", positionBuffer); Graphics.DrawMeshInstancedIndirect(boxMesh, 0, material, b, argsBuffer); }
private void Update() { if (Input.GetKeyDown(KeyCode.Mouse1)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); float3 dir = ray.direction; float3 pos = ray.origin; raycast_result result; System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew(); Raycast.RaycastJob(ref gameWorld, pos, dir, 1000, out result); //Raycast.SlowRayCast(gameWorld, pos, dir, 1000, out result); sw.Stop(); string hitStr = $"hit {gameWorld.entities[result.hitEntityIndex].name} entity as position {result.hitPos}"; Color drawColor = new Color32(122, 0, 122, 255); selectedEntityIndex = result.hitEntityIndex; if (!result.didHit) { hitStr = "did not hit"; drawColor = new Color32(255, 0, 0, 255); selectedEntityIndex = -1; } Debug.Log($"Raycast took {sw.ElapsedMilliseconds}ms and {hitStr}"); Debug.DrawLine(pos, result.hitPos, drawColor, 10000); } if (selectedEntityIndex >= 0) { entity selectedEntity = gameWorld.entities[selectedEntityIndex]; if (GameWorld.HasTag(ref selectedEntity, global::tag.BUILDING)) { selectedBuildingTransform.position = selectedEntity.position; buildingUI.GetComponentInChildren <TMPro.TMP_Text>().text = selectedEntity.name; UIHelpers.WorldSpaceToScreenSpace(ref selectedBuildingTransform, ref buildingUI, ref dynamicBuildingCanvas); } } if (debugRender) { Camera cam = Camera.main; Quaternion rot = Quaternion.Euler(rotation); for (int i = 0; i < gameWorld.entityCount; ++i) { Graphics.DrawMesh(debugMeshes[i], gameWorld.entities[i].position, rot, debugMat, 1, cam); } } #if false //Draw the ground entity. Graphics.DrawMesh(groundMesh, groundPos, Quaternion.identity, debugMat, 1, Camera.main); #endif }
private void Start() { mainCam = Camera.main; gameWorld = new world(30000); positions = new Vector4[gameWorld.maxEntities]; positionBuffer = new ComputeBuffer(gameWorld.maxEntities, 16); argsBuffer = new ComputeBuffer(1, args.Length * sizeof(uint), ComputeBufferType.IndirectArguments); #if true SpawnLotsOfCubes(ref gameWorld, gameWorld.maxEntities); float3 start = new float3(50, 0, -50); System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew(); raycast_result hitResult; Raycast.RaycastJob(ref gameWorld, new float3(50, 0, -50), forward, 10000, out hitResult); sw.Stop(); Debug.Log($"Raycast took {sw.ElapsedMilliseconds}ms"); #endif #if false SpawnLotsOfGameObjects(gameWorld.maxEntities); System.Diagnostics.Stopwatch sw2 = System.Diagnostics.Stopwatch.StartNew(); Physics.Raycast(new Vector3(50, 0, -5), Vector3.forward, 10000); sw2.Stop(); Debug.Log($"Unity Raycast took {sw2.ElapsedMilliseconds}ms"); #endif #if true RayCastAlongSphere(ref gameWorld, new float3(0, 0, 0), 20f); #endif #if false BoundsTests(ref test); #endif #if false float3CreationTests(); #endif #if false ProjectionTests(); #endif }
private static void RayCastAlongSphere(ref world _w, float3 _origin, float _radius) { System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew(); float diameter = _radius * 2; float yStep = _radius / 25; float cStep = 360 / 25; raycast_result hitResult; int count = 0; for (float y = -_radius; y <= _radius; y += yStep) { float absY = Mathf.Abs(y); float ratio = (_radius - absY); for (float c = 0; c < 360; c += cStep) { float x = Mathf.Sin(c) * ratio; float z = Mathf.Cos(c) * ratio; float3 start = new float3(x, y, z); start = Math.Float3Normalize(start); start = start * _radius; float3 dir = Math.Float3Normalize(new float3(0, 0, 0) - start); if (Raycast.RaycastJob(ref _w, start, dir, _radius, out hitResult)) { raycast_result result2; float3 newStart = start + Math.Float3FromDirAndMag(start, dir * 1, _radius * 2); //newStart = float3Addfloat3(hitResult.hitPos, float3FromDirAndMag(hitResult.hitPos, dir, 1)); if (Raycast.RaycastJob(ref _w, newStart, dir * 1, _radius * 2, out result2)) { Debug.DrawLine(hitResult.hitPos, result2.hitPos, Color.green, Mathf.Infinity); } count++; } count++; } } sw.Stop(); Debug.Log($"ElapsedTime: {sw.ElapsedMilliseconds}ms for {count} raycasts"); }