-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModelBase.java
279 lines (246 loc) · 9.05 KB
/
ModelBase.java
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* Copyright 2014 MovingBlocks
*
* 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.
*/
package net.minecraft.client.model;
import com.google.common.collect.Lists;
import net.minecraft.entity.Entity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.asset.AssetManager;
import org.terasology.entitySystem.entity.EntityRef;
import org.terasology.logic.characters.CharacterMoveInputEvent;
import org.terasology.logic.location.LocationComponent;
import org.terasology.math.geom.Quat4f;
import org.terasology.math.geom.Vector2f;
import org.terasology.math.geom.Vector3f;
import org.terasology.registry.In;
import org.terasology.rendering.assets.mesh.MeshBuilder;
import org.terasology.rendering.assets.skeletalmesh.Bone;
import org.terasology.rendering.assets.skeletalmesh.SkeletalMeshData;
import org.terasology.rendering.assets.skeletalmesh.SkeletalMeshDataBuilder;
import org.terasology.rendering.logic.SkeletalMeshComponent;
import org.terasology.rendering.nui.properties.OneOf;
import org.terasology.rendering.nui.properties.Range;
import java.util.List;
public class ModelBase {
private final Logger logger = LoggerFactory.getLogger(ModelBase.class);
public int textureWidth;
public int textureHeight;
private List<Bone> bones = Lists.newArrayList();
private List<ModelRenderer> renderers = Lists.newArrayList();
@In
private AssetManager assetManager;
private SkeletalMeshDataBuilder builder;
private Bone root;
private EntityRef entity;
@Range(min = 0, max = 40, precision = 1)
private float rdata;
@Range(min = 0, max = 40, precision = 1)
private float rtime;
@Range(min = 0, max = 1, precision = 3)
private float rwalkspeed;
@Range(min = 0, max = 360, precision = 1)
private float ryaw;
@Range(min = 0, max = 360, precision = 1)
private float rpitch;
@Range(min = 0, max = 360, precision = 1)
private float myaw;
@Range(min = 0, max = 360, precision = 1)
private float mpitch;
@Range
private float scale;
public enum Target {
OFF, DATA, TIME, WALKSPEED, YAW, PITCH, SCALE
}
public enum Form {
SAW, WAVE
}
@OneOf.Enum
private Target lfo1Target = Target.OFF;
@OneOf.Enum
private Form lfo1Form;
@Range(min = -3, max = 3, precision = 3)
private float lfo1Speed;
@Range(min = 0, max = 40, precision = 1)
private float lfo1Max;
private float lfo1Time;
@OneOf.Enum
private Target lfo2Target = Target.OFF;
@OneOf.Enum
private Form lfo2Form;
@Range(min = -3, max = 3, precision = 3)
private float lfo2Speed;
@Range(min = 0, max = 40, precision = 1)
private float lfo2Max;
private float lfo2Time;
public ModelBase() {
builder = new SkeletalMeshDataBuilder();
builder.setTextureMapper(new MeshBuilder.TextureMapper() {
float textureCoord[];
@Override
public void initialize(Vector3f offset, Vector3f size) {
float width = size.x;
float height = size.y;
float depth = size.z;
textureCoord = new float[]{
// Back
2 * depth + width, depth,
2 * depth + 2 * width, depth,
2 * depth + 2 * width, depth + height,
2 * depth + width, depth + height,
// front
depth + width, depth,
depth + width, depth + height,
depth, depth + height,
depth, depth,
// Bottom
depth + 2 * width, 0,
depth + 2 * width, depth,
depth + width, depth,
depth + width, 0,
// Top
depth, depth,
depth + width, depth,
depth + width, 0,
depth, 0,
// Right
depth + width, depth,
depth + width, depth + height,
2 * depth + width, depth + height,
2 * depth + width, depth,
// Left
depth, depth,
0, depth,
0, depth + height,
depth, depth + height,
};
}
@Override
public Vector2f map(int vertexIndex, float u, float v) {
return new Vector2f((textureCoord[vertexIndex * 2] + u) / textureWidth, (textureCoord[vertexIndex * 2 + 1] + v) / textureHeight);
}
});
root = new Bone(0, "root", new Vector3f(), new Quat4f(0, 0, 0, 1));
builder.addBone(root);
bones.add(root);
}
public void setEntity(EntityRef entity) {
this.entity = entity;
SkeletalMeshComponent skeletalMesh = entity.getComponent(SkeletalMeshComponent.class);
for (ModelRenderer renderer : renderers) {
EntityRef bone = skeletalMesh.boneEntities.get(renderer.name);
renderer.setBoneEntity(bone);
}
}
public void render(Entity entity, float walkspeed, float time, float data, float yaw, float pitch, float scale) {
}
public void setRotationAngles(float par1, float par2, float par3, float par4, float par5, float par6, Entity par7Entity) {
}
public Bone createBone(ModelRenderer renderer, Vector3f position, Quat4f rotation) {
int id = bones.size();
Bone bone = new Bone(id, "Box" + id, position, rotation);
renderer.name = bone.getName();
bones.add(bone);
builder.addBone(bone);
root.addChild(bone);
renderers.add(renderer);
return bone;
}
public void addBox(Bone bone, Vector3f offset, Vector3f size, float u, float v) {
builder.addBox(bone, offset, size, u, v);
}
public SkeletalMeshData getMeshData() {
return builder.build();
}
public void updateModel() {
for (ModelRenderer renderer : renderers) {
renderer.updateModel();
}
}
private float getLfo(Form form, float speed, float max, float time) {
switch (form) {
case SAW:
return (speed * time) % max;
case WAVE:
return (float) Math.cos(speed * time) * max / 2 + max / 2;
}
return 0;
}
private void applyLfo(Target target, Form form, float speed, float max, float time) {
if (target != Target.OFF && form != null) {
float value = getLfo(form, speed, max, time);
switch (target) {
case DATA:
rdata = value;
break;
case TIME:
rtime = value;
break;
case WALKSPEED:
rwalkspeed = value;
break;
case YAW:
ryaw = value;
break;
case PITCH:
rpitch = value;
break;
case SCALE:
scale = value;
break;
}
}
}
public void update(float delta) {
applyLfo(lfo1Target, lfo1Form, lfo1Speed, lfo1Max, lfo1Time);
applyLfo(lfo2Target, lfo2Form, lfo2Speed, lfo2Max, lfo2Time);
lfo1Time += delta;
lfo2Time += delta;
render(new Entity() {
@Override
public int getActivity() {
return 0;
}
@Override
public int getAttacking() {
return 0;
}
@Override
public boolean isSitting() {
return false;
}
@Override
public float getHead1Ext() {
return 0;
}
@Override
public float getHead2Ext() {
return 0;
}
@Override
public float getHead3Ext() {
return 0;
}
}, rdata, rwalkspeed, rtime, ryaw, rpitch, scale);
Quat4f worldRot = new Quat4f(0, 0, 0, 1);
LocationComponent location = entity.getComponent(LocationComponent.class);
location.getWorldRotation(worldRot);
worldRot.inverse(worldRot);
for (ModelRenderer renderer : renderers) {
renderer.updateEntity(worldRot, delta);
}
entity.send(new CharacterMoveInputEvent(0, mpitch, myaw, new Vector3f(), false, false));
}
}