带有3D声音的太阳系

操作系统 云服务/平台 技术难度 关注领域
Android   Intermediate 3D Audio Gaming

任务目标

太阳系看起来非常漂亮和有吸引力,我希望听到来自行星的逼真的声音,Qualcomm 3D音频插件是一个有用的工具,让我更方便地实现它。

 

这是一张漂亮的背景图片,当apk运行时你可以看到它。

地球贴图。

太阳贴图。

海王星贴图。

主场景,主摄像头放置在太阳系模型的上面。

海王星场景,将主摄像头放置在海王星轨道上

所需材料/所需清单/工具

  • Unity 2018.3.9f1 Personal

  • 3D Audio Plugin for Unity

  • Snapdragon Profiler

源码/示例/可执行的应用程序

  • Source Code

附加资料

  • 带有3D声音的太阳系

搭建/在组装说明

使用的零件

以下展示了在这个项目中使用到的部分。

1. 在带有骁龙845处理器的安卓设备上安装了unity编译的apk,主要用来运行apk并查看太阳系的效果。

2.windows 7。

3.type-c 数据线。

4.Unity 2018.3.9f1个人版,所有的开发都是基于Unity。

5.骁龙Profiler,一个用来查看apk实时性能指标的工具。

6.3D音频插件,需要导入到Unity中。

 

部署项目

1.从链接处下载,安装PC。

2.https://developer.qualcomm.com/software/snapdragon-profiler, and install it to PC.

3.https://developer.qualcomm.com/software/3d-audio-plugin-unity, and import into Unity.

4.寻找行星贴图,放到Asset子目录下。

5.收集行星音效,放到Assets目录下。

6.一步一步实现需要的功能。

7.根据高通提供的步骤试着添加音效。

8.选择android平台,连接设备,然后编译,如果没有问题,将会生成apk并自动安装。

9.打开apk,欣赏它。

10.如果你关注性能指标,你可以使用骁龙Profiler 来查看。

11.如果没有问题,上传代码到github.

 

工作流程

如何实现行星自传和公转?

为了实现这个需求,在Solar-System-With-3D-Sound/Assets/Scripts/创建脚本PlanetMove.cs

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

 

public class PlanetMove : MonoBehaviour

{

       public Transform origin;    //Rotation center

public float gspeed;          //Revolute speed

public float zspeed;          //Rotate speed

public float ry, rz;             //to ensure eccentricity

 

void Start()

{

         //

}

 

void Update ()

{

 

         Vector3 axis = new Vector3 (0, ry, rz);    

         this.transform.RotateAround (origin.position, axis, gspeed * Time.deltaTime);  

         this.transform.Rotate (Vector3.up * zspeed * Time.deltaTime);      

}

}

这个脚本很容易理解,而且应该把它拖到星体上,在这里你可以看家5个参数,意味着你必须在运行之前填写它们。(脚本复用性很高。)

2.如何实现同通过按钮切换到不同场景?

为实现这个需求,我在如下目录下添加了脚本SwitchToMain.cs。

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;

using UnityEngine.SceneManagement;

 

public class SwitchToMain : MonoBehaviour

{

    // Start is called before the first frame update

    void Start()

{

    //register a listener function.

        this.GetComponent<Button>().onClick.AddListener(OnClick);

    }

 

    // Update is called once per frame

    void OnClick()

    {

        SceneManager.LoadScene("MainScene");

    }

}

 

The script mainly registered a listener function named OnClick in start function. When there’s any click event occur and Onclick willl be transfered, the script also need to drag to the button transform.

该脚本主要在start函数中注册了一个名为OnClick的监听函数。 当有任何点击事件发生时Onclick函数将被调用,脚本也需要拖动到按钮组件。

3.如何实现缩放?

我们都知道3D声音与距离有关,这意味着如果我们接近音频源,我们将大声听到音频源的声音,如果我们远离它,我们甚至听不到它。 所以为了测试和验证,我尝试将游戏对象设置为手动缩放,我添加了一个脚本:Solar-System-With-3D-Sound / Assets / Scripts / ZoomInAndout.cs。

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

 

public class ZoomInAndout : MonoBehaviour

{

//In order to record two old touch’s distance.

 

    private Touch oldTouch1; 

    private Touch oldTouch2;

    void Start() {

       

    }

    void Update() {

       

        if (Input.touchCount <= 0)

        {

            return;

        }    

       

        //Get new touches from input.(from screen touch)

 

        Touch newTouch1 = Input.GetTouch(0);

        Touch newTouch2 = Input.GetTouch(1);

       

        //the second touch, just record ,not handle.

 

        if (newTouch2.phase == TouchPhase.Began)

        {

            oldTouch2 = newTouch2;

            oldTouch1 = newTouch1;

            return;

        }

       

        //caculate two distance.

 

        float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);

        float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);

       

        float offset = newDistance - oldDistance;

       

        //get scale factor.(becaue offset is too bigger than pixel.)

 

        float scaleFactor = offset / 100f;

 

         //zoom in out.

 

        Vector3 localScale = transform.localScale;

        Vector3 scale = new Vector3(localScale.x + scaleFactor,

            localScale.y + scaleFactor,

            localScale.z + scaleFactor);

      

        //only when scale greater or equal to 0.5, change it’s scale.

 

        if (scale.x >= 0.05f && scale.y >=0.05f && scale.z >= 0.05f)

        {

            transform.localScale = scale;

        }

      

        //update two old touch.

 

        oldTouch1 = newTouch1;

        oldTouch2 = newTouch2;

    }

}

这个脚本主要是实现游戏对象放大和缩小,你可以在注释中看到更多细节。

4.如何使用3D音频插件?

参考连接,包含了以下部分

A、安装3D音频插件

B、设置3D音频插件

a、Set Spatializer Plugin to QObjectsSpatializer and Ambisonic Decoder Plugin to QSoundfieldSpatializer.

b、Place a Q3D AudioListener on the same object that has your AudioListener.

c、Navigate to Window -> Audio Mixer to set up Q3D Audio Groups for sound objects and soundfields.

C、使用3D音频插件来空间化音频

D、将混响添加到空间化音频场景

5.如何将Mono audioclips转换为3D音频声音

Q3DAudioGlobalSettings provides several "SpatializeMonoAudioSources" check boxes that can enable settings in the 3D Audio Plugin to automatically detect Audio Sources that have mono AudioClips and make them Q3D Audio sound objects.

6.如何将混响卸载到DSP

set "1st Choice Reverb Processor" and "2nd Choice Reverb Processor" to "COMPUATE_DSP." Currently this is a global setting that can't be changed at runtime.

7.也遇到了一些其他的问题,我在博客上做了一个简单的记录,细节可以可以参考链接

1、从此链接处下载源码。

2、安装unity,unity版本低于5的不可用。(主要是3D音频插件要求unity版本高于5。)

3、双击.unity文件打开demo.

4、编译成apk并安装到安卓设备。

5、打开apk,欣赏它。

6.如果关注性能指标,可以使用骁龙Profiler查看。 

贡献者信息

姓名 公司

Zhen

sunzhen@thundersoft.com
Thundersoft

Rong

yangrong0925@thundersoft.com
Thundersoft

Jie

wangjie0508@thundersoft.com
Thundersoft

Kou

kouzw0723@thundersoft.com
Thundersoft

Eric

yansh0810@thundersoft.com
Thundersoft

>>浏览更多Qualcomm硬件案例:http://qualcomm.csdn.net/m/zone/qualcomm2016/project

Qualcomm 解决方案

 

高通 AI Hub

全新高通 AI Hub 包含预优化AI模型库,支持在搭载骁龙和高通平台的终端上进行无缝部署。
该模型库为开发者提供超过75个主流的AI和生成式AI模型,比如Whisper、ControlNet、Stable Diffusion和Baichuan-7B,可在不同执行环境(runtime)中打包,能够在不同形态终端中实现卓越的终端侧AI性能、降低内存占用并提升能效。所有模型均经过优化,以充分利用高通AI引擎内所有核心(NPU、CPU和GPU)的硬件加速能力,从而使推理速度提升4倍。

了解更多

SDK 下载

本版块下载 SDK,只需简单注册,就可轻松下载。