List <float> ListFlattenUpdateData(RaycastSound sound) { return(new List <float> { sound.attenuation, sound.position.x, sound.position.z }); }
List <float> ListFlattenData(RaycastSound sound) { return(new List <float> { 0f, sound.time, sound.occlusion, sound.absorption, sound.roughness, sound.volume, sound.position.x, sound.position.z }); }
string FlattenSoundData(RaycastSound sound) { return(string.Format( "{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}", 1f, sound.time, sound.occlusion, sound.absorption, sound.roughness, sound.volume, sound.position.x, sound.position.z)); }
void Awake() { layerMask = 1 << LayerMask.NameToLayer("Sound"); audio = GetComponent <AudioSource>(); listener = Camera.main.GetComponent <AudioListener>().transform; var position = listener.transform.position - transform.position; var distance = Vector3.Distance(listener.position, transform.position); sound = new RaycastSound( name: name, time: distance / speedOfSound, volume: audio.volume, absorption: 0f, roughness: 0f, occlusion: 0f, attenuation: 0f, position: position); }
IEnumerator Start() { var layerMask = ~(1 << LayerMask.NameToLayer("Sound")); var lowpass = audio.outputAudioMixerGroup.audioMixer.FindSnapshot("Occlusion"); var snapshot = audio.outputAudioMixerGroup.audioMixer.FindSnapshot("Snapshot"); var updateInstruction = new WaitForFixedUpdate(); var position = listener.position - transform.position; var distance = Vector3.Distance(listener.position, transform.position); var ray = new Ray(transform.position, position); var hits = Physics.RaycastAll(ray, distance, layerMask); var attenuation = 0f; var occlusion = 0.3f; while (true) { yield return(updateInstruction); position = listener.position - transform.position; distance = Vector3.Distance(listener.position, transform.position); ray = new Ray(transform.position, position); hits = Physics.RaycastAll(ray, distance, layerMask); attenuation = hits.Length / 10f; if (0 < hits.Length) { lowpass.TransitionTo(occlusion); } else { snapshot.TransitionTo(occlusion); } //print(hits.Length); sound = new RaycastSound( name: name, time: distance / speedOfSound, volume: audio.volume, absorption: 0f, roughness: 0f, occlusion: occlusion, attenuation: attenuation, position: position); } }
void FixedUpdate() { var position = listener.position - transform.position; var distance = Vector3.Distance(listener.position, transform.position); var ray = new Ray(transform.position, position); var hits = Physics.RaycastAll(ray, distance, layerMask); var attenuation = (hits.Length > 1)?1f:0f; sound = new RaycastSound( name: name, time: distance / speedOfSound, volume: audio.volume, absorption: 0f, roughness: 0f, occlusion: 0f, attenuation: attenuation, position: position); #if _DEBUG var nearest = ray.GetPoint(distance); DebugLine.DrawLine(ray.origin, nearest, Color.red, Color.red, 10f, 1f); #endif }