-
Notifications
You must be signed in to change notification settings - Fork 14
/
LoadPC2.cs
64 lines (60 loc) · 1.96 KB
/
LoadPC2.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
// Example how to import and visualise tree wind animation. May be useful for custom tree rendering.
// Open Speedtree Modeler, import lowpoly tree model, Export Mesh (as FBX), select Wind, set Cache Options Format to PC2.
// Then set filepath (line 22). Play.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System;
public class LoadPC2 : MonoBehaviour
{
GameObject[] _Cubes;
int _Frame = 0;
int _NumPoints;
int _NumSamples;
Vector3[,] _Positions;
int _FramesPerSecond = 30;
void Start()
{
BinaryReader reader = new BinaryReader(File.OpenRead("E:\\FBX\\WhiteBirch_Low.pc2"));
byte[] signature = reader.ReadBytes(11);
if (System.Text.Encoding.UTF8.GetString(signature, 0, signature.Length) != "POINTCACHE2")
{
Debug.Log("Input PC2 file is incorrect!");
reader.Close();
return;
}
reader.BaseStream.Seek(12, SeekOrigin.Begin);
int version = reader.ReadInt32();
_NumPoints = reader.ReadInt32();
float startSample = reader.ReadSingle();
float sampleRate = reader.ReadSingle();
_NumSamples = reader.ReadInt32();
_Cubes = new GameObject[_NumPoints];
for (int i = 0; i < _NumPoints; i++)
{
_Cubes[i] = GameObject.CreatePrimitive(PrimitiveType.Cube);
_Cubes[i].transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
_Cubes[i].hideFlags = HideFlags.HideInHierarchy;
}
_Positions = new Vector3[_NumSamples, _NumPoints];
for (int j = 0; j < _NumSamples; j++)
{
for (int i = 0; i < _NumPoints; i++)
{
float x = reader.ReadSingle();
float y = reader.ReadSingle();
float z = reader.ReadSingle();
_Positions[j, i] = new Vector3(x, y, z);
}
}
reader.Close();
InvokeRepeating("WindAnimation", 0.0f, 1.0f / (float)_FramesPerSecond);
}
void WindAnimation()
{
if (_Frame >= _NumSamples) _Frame = 0;
for (int i = 0; i < _NumPoints; i++) _Cubes[i].transform.position = _Positions[_Frame, i];
_Frame++;
}
}