1. 使用3D Scanner App + iPhone 12 Pro进行实体空间的扫描

示意图中是High Res模式,但是因为实景空间比较大,所以真实情况是用了Low Res模式扫描的。

扫码结果:


担心一次性扫描完整内容会导致文件过大,渲染出问题,所以后面有对休息区单独扫描了一次。

扫描过后,我们通过导出OBJ格式的文件,并通过Air Drop同步到Mac上

我们在Mac上接收到后,可以看到是一个文件夹,文件夹中包含了3个文件:

1. xxx.mtl
- xxx.jpg
- xxx.obj
2. 将OBJ文件导入Unity
2.1. 在Unity中创建一个3D项目

2.2. 将OBJ模型导入到Assets文件夹
我们先在Assets文件夹下创建 Models/room1 两个文件夹,再把刚刚导出的文件拖拽到这个文件夹中

为了后续不重复,我们统一把三个文件的名称都改为room1
2.3. 将模型导入场景
直接将room1.obj拖入场景中

2.4. 给模型贴上材质
选中room1这个对象,然后在 Inspector中点击“Select”按钮

之后再Inspector中切换到Material,再展开“On Demand Remap”箭头

将Naming下拉框从“By Base Texture Name” 改为 “Model Name + Model Material”

之后再点击 “Seach and Remap”, Unity便会在同目录下搜索相同名称的贴图文件,并进行贴图。

如果上述方法不行的话,也可以通过自己创建Material的方法:
- 在room1文件夹右键,Create->Material,命名为room1
- 将图片拖到 Albedo前面的方框中

- 选中room1对象的子对象 default,将room1的material拖到 Materials-> Element 0中

3. 导入任务模型,并增加控制脚本
3.1. 导入角色模型
我之前用RealityComposer做过一个Code47的模型,通过blender导出了fbx格式,这里接直接拖进来就可以用了

需要注意的是,要把模型自身的Light禁用掉
3.2. 增加移动控制脚本,通过WASD控制角色移动
选择角色模型,Add Component-> New Script-> Player

打开脚本文件,写入代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public CharacterController controller;
public float speed = 12f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// keyboard move
float x = Input.GetAxisRaw("Horizontal"); // A D
float z = Input.GetAxisRaw("Vertical"); // W S
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
}
}
之后再在 角色模型上添加一个Character Controller Component,并把Code47这个对象拖拽到 Player Script中的Controller一栏中

同时还需要将包裹的Collider调整到和模型一致,主要调整Center,Radius和Height这几个参数。

这样就可以通过 AWSD来控制模型的运动了。
3.3. 通过鼠标来控制角色视角
选择Code47模型下面的 Camer对象,并添加脚本MouseLook,代码如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseLook : MonoBehaviour
{
public float mouseSensitivity = 100f;
public Transform playerBody;
float xRotation = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
}
并把Code47模型拖到 MouseLook脚本中的Player Body一栏中,就OK了。

4. 给空间模型增加碰撞
在模型->Select->Model中 勾选Generate Colliders来增加空间的碰撞,这样角色就不会穿墙了

演示视频可以微信搜索GunnerTalk公众号
网友评论