-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
DracoDemoMeshData.cs
93 lines (76 loc) · 3.22 KB
/
DracoDemoMeshData.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2017 The Draco Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#if UNITY_STANDALONE || UNITY_WEBGL || UNITY_IOS || UNITY_TVOS || UNITY_VISIONOS || UNITY_ANDROID || UNITY_WSA || PLATFORM_LUMIN
#define DRACO_PLATFORM_SUPPORTED
#endif
using System.IO;
using UnityEngine;
#if DRACO_PLATFORM_SUPPORTED
using Draco;
#endif
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class DracoDemoMeshData : MonoBehaviour
{
public string filePath;
public bool requireNormals;
public bool requireTangents;
#if DRACO_PLATFORM_SUPPORTED
async void Start() {
var data = await DracoDemo.LoadData(filePath);
if (data == null) return;
// Allocate single mesh data (you can/should bulk allocate multiple at once, if you're loading multiple draco meshes)
var meshDataArray = Mesh.AllocateWritableMeshData(1);
// Set to true if you require normals. If Draco data does not contain them, they are allocated and we have to calculate them below
var decodeFlags = requireNormals ? DecodeSettings.RequireNormals : 0;
// Retrieve tangents is not supported, but this will ensure they are allocated and can be calculated later (see below)
decodeFlags |= requireTangents ? DecodeSettings.RequireTangents : 0;
// Async decoding has to start on the main thread and spawns multiple C# jobs.
var result = await DracoDecoder.DecodeMesh(
meshDataArray[0],
data,
decodeFlags
);
if (result.success) {
// Apply onto new Mesh
var mesh = new Mesh();
Mesh.ApplyAndDisposeWritableMeshData(meshDataArray,mesh);
// mesh.bounds = result.bounds;
// If Draco mesh has bone weights, apply them now.
// To get these, you have to supply the correct attribute IDs
// to `ConvertDracoMeshToUnity` above (optional parameters).
if (result.boneWeightData != null) {
result.boneWeightData.ApplyOnMesh(mesh);
result.boneWeightData.Dispose();
}
if (result.calculateNormals) {
// If draco didn't contain normals, calculate them.
mesh.RecalculateNormals();
}
if (requireTangents) {
// If required (e.g. for consistent specular shading), calculate tangents
mesh.RecalculateTangents();
}
// Use the resulting mesh
GetComponent<MeshFilter>().mesh = mesh;
}
}
#else // DRACO_PLATFORM_SUPPORTED
void Start()
{
Debug.LogError("Draco is not supported on this platform.");
}
#endif // DRACO_PLATFORM_SUPPORTED
}