Skip to content

Commit 67c6fde

Browse files
author
Rui Wang
committed
initial commit
1 parent 9e3e765 commit 67c6fde

28 files changed

+2414
-0
lines changed

osgExport/BundleAnimation.cs

+201
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using UnityEditor;
5+
6+
namespace nwTools
7+
{
8+
9+
public class BundleKeyframe
10+
{
11+
public Vector3 pos = new Vector3(0, 0, 0);
12+
public Vector3 scale = new Vector3(1, 1, 1);
13+
public Quaternion rot = Quaternion.identity;
14+
public float time = 0.0f;
15+
}
16+
17+
public class BundleAnimationClip
18+
{
19+
public string name;
20+
public Dictionary<string, List<BundleKeyframe>> keyframes = new Dictionary<string, List<BundleKeyframe>>();
21+
}
22+
23+
public class BundleAnimation : BundleComponent
24+
{
25+
override public void Preprocess()
26+
{
27+
// m_LocalScale.x - z
28+
// m_LocalPosition.x - z
29+
// m_LocalRotation.x - w
30+
31+
// for now copy the keys, we could evaluate and maybe should
32+
// as Unity keys have in/out tangents
33+
unityAnim = unityComponent as AnimationHelper;
34+
35+
// no way to get clips like this, going to need a custom component
36+
// for now, support one animation
37+
//for (int i = 0; i < unityAnim.GetClipCount(); i++)
38+
//{
39+
// var clip = unityAnim.clips[i];
40+
//}
41+
42+
for ( int i=0; i<unityAnim.animationClips.Length; i++ )
43+
{
44+
AddClip( unityAnim.animationClips[i] );
45+
}
46+
}
47+
48+
void AddClip( AnimationClip clip )
49+
{
50+
BundleAnimationClip aclip = new BundleAnimationClip();
51+
aclip.name = clip.name;
52+
clips.Add( aclip );
53+
54+
AnimationClipCurveData[] curveData = AnimationUtility.GetAllCurves(clip, true);
55+
Dictionary<string, List<AnimationClipCurveData>> animdata = new Dictionary<string, List<AnimationClipCurveData>>();
56+
for ( int i=0; i<curveData.Length; i++ )
57+
{
58+
AnimationClipCurveData cd = curveData[i];
59+
List<AnimationClipCurveData> nodedata;
60+
if ( !animdata.TryGetValue(cd.path, out nodedata) )
61+
nodedata = animdata[cd.path] = new List<AnimationClipCurveData>();
62+
nodedata.Add( cd );
63+
}
64+
65+
foreach ( KeyValuePair<string, List<AnimationClipCurveData>> entry in animdata )
66+
{
67+
var boneName = entry.Key;
68+
var keyframes = aclip.keyframes[boneName] = new List<BundleKeyframe>();
69+
70+
float maxTime = 0;
71+
foreach ( AnimationClipCurveData cd in entry.Value )
72+
{
73+
var curve = cd.curve;
74+
if ( curve.keys.Length==0 )
75+
continue;
76+
if ( curve.keys[curve.keys.Length-1].time>maxTime )
77+
maxTime = curve.keys[curve.keys.Length-1].time;
78+
}
79+
80+
Vector3 pos = new Vector3(0, 0, 0);
81+
Vector3 scale = new Vector3(1, 1, 1);
82+
Quaternion rot = Quaternion.identity;
83+
84+
Vector3 lastpos = new Vector3(0, 0, 0);
85+
Vector3 lastscale = new Vector3(1, 1, 1);
86+
Quaternion lastrot = Quaternion.identity;
87+
88+
for ( float time=0.0f; time<=maxTime; )
89+
{
90+
foreach ( AnimationClipCurveData cd in entry.Value )
91+
{
92+
var curve = cd.curve;
93+
float value = curve.Evaluate(time);
94+
if ( cd.propertyName=="m_LocalScale.x" )
95+
scale.x = value;
96+
else if ( cd.propertyName=="m_LocalScale.y" )
97+
scale.y = value;
98+
else if ( cd.propertyName=="m_LocalScale.z" )
99+
scale.z = value;
100+
else if ( cd.propertyName=="m_LocalPosition.x" )
101+
pos.x = value;
102+
else if ( cd.propertyName=="m_LocalPosition.y" )
103+
pos.y = value;
104+
else if ( cd.propertyName=="m_LocalPosition.z" )
105+
pos.z = value;
106+
else if ( cd.propertyName=="m_LocalRotation.x" )
107+
rot.x = value;
108+
else if ( cd.propertyName=="m_LocalRotation.y" )
109+
rot.y = value;
110+
else if ( cd.propertyName=="m_LocalRotation.z" )
111+
rot.z = value;
112+
else if ( cd.propertyName=="m_LocalRotation.w" )
113+
rot.w = value;
114+
else
115+
Debug.Log("Unknown Animtation: " + cd.propertyName);
116+
}
117+
118+
bool needFrame = false;
119+
float t = Math.Abs(lastpos.x - pos.x);
120+
if ( t<0.01f ) needFrame = true;
121+
t = Math.Abs(lastpos.y - pos.y);
122+
if ( t<0.01f ) needFrame = true;
123+
t = Math.Abs(lastpos.z - pos.z);
124+
if ( t<0.01f ) needFrame = true;
125+
t = Math.Abs(lastscale.x - scale.x);
126+
if ( t<0.01f ) needFrame = true;
127+
t = Math.Abs(lastscale.y - scale.y);
128+
if ( t<0.01f ) needFrame = true;
129+
t = Math.Abs(lastscale.z - scale.z);
130+
if ( t<0.01f ) needFrame = true;
131+
t = Math.Abs(lastrot.x - rot.x);
132+
if ( t<0.01f ) needFrame = true;
133+
t = Math.Abs(lastrot.y - rot.y);
134+
if ( t<0.01f ) needFrame = true;
135+
t = Math.Abs(lastrot.z - rot.z);
136+
if ( t<0.01f ) needFrame = true;
137+
t = Math.Abs(lastrot.w - rot.w);
138+
if ( t<0.01f ) needFrame = true;
139+
140+
if ( needFrame )
141+
{
142+
lastpos = pos;
143+
lastrot = rot;
144+
lastscale = scale;
145+
146+
BundleKeyframe keyframe = new BundleKeyframe();
147+
keyframe.pos = pos;
148+
keyframe.rot = rot;
149+
keyframe.scale = scale;
150+
keyframe.time = time;
151+
keyframes.Add( keyframe );
152+
}
153+
time += 0.025f; // FIXME?
154+
}
155+
}
156+
}
157+
158+
new public static void Reset()
159+
{
160+
}
161+
162+
public override SceneComponent GetObjectData()
163+
{
164+
var sceneData = new SceneAnimation();
165+
sceneData.type = "Animation";
166+
sceneData.clips = new SceneAnimationClip[clips.Count];
167+
for ( int i=0; i<clips.Count; i++ )
168+
{
169+
BundleAnimationClip clip = clips[i];
170+
var jclip = sceneData.clips[i] = new SceneAnimationClip();
171+
jclip.name = clip.name;
172+
173+
int count = 0;
174+
SceneAnimationNode[] jnodes = jclip.nodes = new SceneAnimationNode[clip.keyframes.Count];
175+
foreach ( KeyValuePair<string, List<BundleKeyframe>> entry in clip.keyframes )
176+
{
177+
SceneAnimationNode node = new SceneAnimationNode();
178+
node.name = entry.Key;
179+
node.keyframes = new SceneKeyframe[entry.Value.Count];
180+
181+
int kcount = 0;
182+
foreach ( var key in entry.Value )
183+
{
184+
var jkeyframe = new SceneKeyframe();
185+
jkeyframe.pos = key.pos;
186+
jkeyframe.scale = key.scale;
187+
jkeyframe.rot = key.rot;
188+
jkeyframe.time = key.time;
189+
node.keyframes[kcount++] = jkeyframe;
190+
}
191+
jnodes[count++] = node;
192+
}
193+
}
194+
return sceneData;
195+
}
196+
197+
AnimationHelper unityAnim;
198+
List<BundleAnimationClip> clips = new List<BundleAnimationClip>();
199+
}
200+
201+
}

osgExport/BundleBoxCollider.cs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Collections.Generic;
2+
using UnityEngine;
3+
using UnityEditor;
4+
5+
namespace nwTools
6+
{
7+
8+
public class BundleBoxCollider : BundleComponent
9+
{
10+
override public void Preprocess()
11+
{
12+
unityBoxCollider = unityComponent as BoxCollider;
13+
}
14+
15+
override public void QueryResources()
16+
{
17+
}
18+
19+
new public static void Reset()
20+
{
21+
}
22+
23+
public override SceneComponent GetObjectData()
24+
{
25+
var sceneData = new SceneBoxCollider();
26+
sceneData.type = "BoxCollider";
27+
sceneData.size = unityBoxCollider.size;
28+
sceneData.center = unityBoxCollider.center;
29+
return sceneData;
30+
}
31+
32+
BoxCollider unityBoxCollider;
33+
}
34+
35+
}

osgExport/BundleCamera.cs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using UnityEditor;
5+
6+
namespace nwTools
7+
{
8+
9+
public class BundleCamera : BundleComponent
10+
{
11+
override public void Preprocess()
12+
{
13+
unityCamera = unityComponent as Camera;
14+
}
15+
16+
new public static void Reset()
17+
{
18+
}
19+
20+
public override SceneComponent GetObjectData()
21+
{
22+
var sceneData = new SceneCamera();
23+
sceneData.type = "Camera";
24+
return sceneData;
25+
}
26+
27+
Camera unityCamera;
28+
}
29+
30+
}

osgExport/BundleComponent.cs

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using UnityEditor;
5+
6+
namespace nwTools
7+
{
8+
9+
public class BundleComponent : BundleObject
10+
{
11+
virtual public void QueryResources()
12+
{
13+
}
14+
15+
virtual public void Preprocess()
16+
{
17+
}
18+
19+
virtual public void Process()
20+
{
21+
}
22+
23+
virtual public void PostProcess()
24+
{
25+
}
26+
27+
public virtual new SceneComponent GetObjectData()
28+
{
29+
throw new NotImplementedException("Attempting to call GetObjectData");
30+
}
31+
32+
public static void Reset()
33+
{
34+
BundleTransform.Reset();
35+
BundleMeshRenderer.Reset();
36+
conversions = new Dictionary<Type, Type>();
37+
}
38+
39+
public static void QueryComponents( BundleGameObject jgo )
40+
{
41+
// for every registered conversion get that component
42+
foreach( KeyValuePair<Type, Type> pair in conversions )
43+
{
44+
Component[] components = jgo.unityGameObject.GetComponents(pair.Key);
45+
foreach ( Component component in components )
46+
{
47+
MeshRenderer meshRenderer = component as MeshRenderer;
48+
if ( meshRenderer!=null && !meshRenderer.enabled )
49+
continue;
50+
51+
var jcomponent = Activator.CreateInstance(pair.Value) as BundleComponent;
52+
if ( jcomponent==null )
53+
ExportError.FatalError("Export component creation failed");
54+
55+
jcomponent.unityComponent = component;
56+
jcomponent.jeGameObject = jgo;
57+
jgo.AddComponent( jcomponent );
58+
}
59+
}
60+
}
61+
62+
public static void RegisterConversion( Type componentType, Type exportType )
63+
{
64+
conversions[componentType] = exportType;
65+
}
66+
67+
public static void RegisterStandardComponents()
68+
{
69+
RegisterConversion(typeof(Transform), typeof(BundleTransform));
70+
RegisterConversion(typeof(Camera), typeof(BundleCamera));
71+
RegisterConversion(typeof(MeshRenderer), typeof(BundleMeshRenderer));
72+
RegisterConversion(typeof(SkinnedMeshRenderer), typeof(BundleSkinnedMeshRenderer));
73+
RegisterConversion(typeof(Terrain), typeof(BundleTerrain));
74+
RegisterConversion(typeof(AnimationHelper), typeof(BundleAnimation));
75+
RegisterConversion(typeof(BoxCollider), typeof(BundleBoxCollider));
76+
RegisterConversion(typeof(MeshCollider), typeof(BundleMeshCollider));
77+
RegisterConversion(typeof(Rigidbody), typeof(BundleRigidBody));
78+
RegisterConversion(typeof(Light), typeof(BundleLight));
79+
RegisterConversion(typeof(TimeOfDay), typeof(BundleTimeOfDay));
80+
}
81+
82+
static Dictionary<Type, Type> conversions;
83+
public Component unityComponent;
84+
public BundleGameObject jeGameObject;
85+
}
86+
87+
}

0 commit comments

Comments
 (0)