private void OnSceneGUI() { Event guiEvent = Event.current; //get the position of the mouse //guiEvent.mousePosition is gui coordinate, we need to convert it to world position Ray mouseRay = HandleUtility.GUIPointToWorldRay(guiEvent.mousePosition); float drawPlaneHeight = diffracPoint.transform.position.y; //user defined height for the plane float distToDrawPlane = (drawPlaneHeight - mouseRay.origin.y) / mouseRay.direction.y; //equation: origin.y + dir.y * distance = h Vector3 mousePosition = mouseRay.GetPoint(distToDrawPlane); if (diffracPoint.edgeVector_1 == Vector3.zero || diffracPoint.edgeVector_2 == Vector3.zero) { diffracPoint.edgeVector_1 = UtilityMethod.RotateAroundY(30, diffracPoint.transform.forward); diffracPoint.edgeVector_2 = UtilityMethod.RotateAroundY(-30, diffracPoint.transform.forward); } if (guiEvent.type == EventType.Layout) { HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Keyboard)); } else { HandleInput(guiEvent, mousePosition); DrawHandles(guiEvent, mousePosition); } }
private void DiffractSoundRay(Vector3 diffractionPoint, Vector3 mainDir, Vector3 edgeDir, float angle, SoundSegment segment, float remainVol, float triggerRadius) { float diffractionAngle = angle * SystemController.Instance.diffractionAngleRatio; int numOfRays = Mathf.RoundToInt((float)rayFrequency / 360 * Mathf.Abs(diffractionAngle)); float delta = diffractionAngle / numOfRays; Vector3 dir = mainDir; Vector3 origin = new Vector3(diffractionPoint.x, transform.position.y, diffractionPoint.z); for (int i = 0; i <= numOfRays; i++) { //if raycast inside the diffraction trigger, the ray will first hit the trigger from inside, so to prevent that we need to manually set the origin outside the trigger radius soundSegmentQueue.Enqueue(new SoundSegment(segment._id, origin + dir * triggerRadius, dir, remainVol, segment._type, segment._reflectNum)); dir = UtilityMethod.RotateAroundY(delta, dir); } }
public void Calculate() { if (soundQueue.Count <= 0 && soundSegmentQueue.Count <= 0) { return; } if (segmentId > 10000) { segmentId = 0; } //reset segment id //segmentId = 0; diffractedSegments.Clear(); mapKeyPoints = new List <MapPointData>(); soundSegmentQueue_nextFrame = new Queue <SoundSegment>(); int numOfSoundForOneRound = soundQueue.Count; for (int q = 0; q < numOfSoundForOneRound; q++) { Sound sound = soundQueue.Dequeue(); if (!sound.IsOver()) { soundQueue.Enqueue(sound); float volume = sound.volume; // rayFrequency; float steps = sound.volume / (system.fadingSpeed + float.Epsilon); float spreadDistance = steps * system.stepDistance; //mapKeyPoints.Add(new MapPointData(sound.sid, sound.producedPos, sound.volume, fadingSpeed, stepDistance)); Transform target = sound.producer.transform; Vector3 origin = target.position; Vector3 forward = target.forward; //rotate clockwise and counter-clockwise to find the bondary vectors float halfRange = sound.range / 2; Vector3 bound_CCW = UtilityMethod.RotateAroundY(halfRange, forward); Vector3 bound_CW = UtilityMethod.RotateAroundY(-halfRange, forward); //Debug.DrawRay(agent.transform.position, bound_CCW * 10, Color.cyan); //Debug.DrawRay(agent.transform.position, bound_CW * 10, Color.green); float delta = 360f / rayFrequency; int rayNum = (int)(sound.range / delta); Vector3 dir = bound_CW; for (int i = 0; i <= rayNum; i++) // <= because we enqueue the segement first then increment the angle, so we need extra 1 time for the last direction { //raycast soundSegmentQueue.Enqueue(new SoundSegment(segmentId, origin, dir, sound.volume, sound.type, 0)); dir = UtilityMethod.RotateAroundY(delta, dir); } } } while (soundSegmentQueue.Count > 0) { SoundSegment segment = soundSegmentQueue.Dequeue(); RayCastFromPoint(segment); } soundSegmentQueue = soundSegmentQueue_nextFrame; }