-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTH.js
171 lines (167 loc) · 7.34 KB
/
TH.js
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
var TH = {
threediv : null,
width : null,
height : null,
scene : null,
camera : null,
renderer : null,
texloader : null,
floorY : -30,
clock : null,
animators : [],
init : function () {
TH.threediv = document.getElementById('game'),
TH.width = TH.threediv.clientWidth;
TH.height = TH.threediv.clientHeight;
TH.scene = new THREE.Scene();
//TH.scene.background = new THREE.Color(0x0c1013);
TH.camera = new THREE.PerspectiveCamera(45, TH.width / TH.height, 0.1, 4000);
TH.scene.add(TH.camera);
TH.camera.rotateY(-3.14 / 2);
TH.renderer = new THREE.WebGLRenderer();
TH.renderer.setSize(TH.width, TH.height);
TH.threediv.appendChild(TH.renderer.domElement);
TH.texloader = new THREE.TextureLoader();
TH.clock = new THREE.Clock();
},
_loadTextureMaterial : function(name, repeatX, repeatY, transp) {
var texture = TH._loadTexture(name, repeatX, repeatY);
return new THREE.MeshBasicMaterial({color: 0xffffff, flatShading: true, overdraw: 0.5, map: texture, side: THREE.DoubleSide, transparent: transp || false});
},
_loadSpriteMaterial : function(name, repeatX, repeatY) {
var tex = TH._loadTexture(name, repeatX, repeatY);
return new THREE.SpriteMaterial({map: tex, color: 0xffffff});
},
_loadTexture : function(name, repeatX, repeatY) {
var tex = TH.texloader.load('art/' + name);
tex.wrapS = THREE.RepeatWrapping;
tex.wrapT = THREE.RepeatWrapping;
tex.repeat.set(repeatX, repeatY);
tex.magFilter = THREE.NearestFilter;
tex.minFilter = THREE.NearestFilter;
return tex;
},
run : function(update) {
requestAnimationFrame( function(){TH.run(update)} );
update();
TH.renderer.render( TH.scene, TH.camera );
},
addSprite : function(x, y, z, width, height, scale, textureName, tilesX, tilesY, tilesTotal, duration) {
var tex = TH._loadTexture(textureName, 1, 1);
if (tilesTotal > 0 && duration > 0) {
var animator = new TextureAnimator(tex, tilesX, tilesY, tilesTotal, duration);
TH.animators.push(animator);
}
var mat = new THREE.SpriteMaterial({map: tex, color: 0xffffff});
var sprite = new THREE.Sprite(mat);
sprite.position.x = x;
sprite.position.z = z;
sprite.position.y = y;
sprite.scale.x = width * 25 * scale;
sprite.scale.y = height * 25 * scale;
TH.scene.add(sprite);
return sprite;
},
addAnimatedPlane : function(p1, p2, y, height, textureName, tilesX, tilesY, tilesTotal, duration) {
var tex = TH._loadTexture(textureName, 1, 1);
if (tilesTotal > 1) {
var animator = new TextureAnimator(tex, tilesX, tilesY, tilesTotal, duration);
TH.animators.push(animator);
}
var mat = new THREE.MeshBasicMaterial({color: 0xffffff, flatShading: true, overdraw: 0.5, map: tex, side: THREE.DoubleSide, transparent: true});
var length = distance(p1, p2);
var geometry = new THREE.PlaneGeometry(length, height);
var plane = new THREE.Mesh(geometry, mat);
var midpoint = {x: p1.x + (p2.x - p1.x) / 2, y: p1.y + (p2.y - p1.y) / 2};
plane.position.x = midpoint.x;
plane.position.z = midpoint.y;
var angle = -Math.atan((p2.y - p1.y) / (p2.x - p1.x));
plane.rotateY(angle);
plane.position.y = y;
TH.scene.add(plane);
},
addWallPlane : function(p1, p2, width, height, textureName, y, transparent) {
var length = distance(p1, p2);
var geometry = new THREE.PlaneGeometry(length, height);
var repeat = Math.floor(length / width);
var mat = TH._loadTextureMaterial(textureName, repeat || 1, 1, transparent);
var plane = new THREE.Mesh(geometry, mat);
var midpoint = {x: p1.x + (p2.x - p1.x) / 2, y: p1.y + (p2.y - p1.y) / 2};
plane.position.x = midpoint.x;
plane.position.z = midpoint.y;
var angle = -Math.atan((p2.y - p1.y) / (p2.x - p1.x));
plane.rotateY(angle);
plane.position.y = y;
TH.scene.add(plane);
},
addAnimatedFloor : function (x, y, z, width, height, textureName, tilesX, tilesY, tilesTotal, duration) {
var tex = TH._loadTexture(textureName, 1, 1);
if (tilesTotal > 1) {
var animator = new TextureAnimator(tex, tilesX, tilesY, tilesTotal, duration);
TH.animators.push(animator);
}
var mat = new THREE.MeshBasicMaterial({color: 0xffffff, flatShading: true, overdraw: 0.5, map: tex, side: THREE.DoubleSide, transparent: true});
var geometry = new THREE.PlaneGeometry(width, height);
var plane = new THREE.Mesh(geometry, mat);
var floor = new THREE.Mesh( geometry, mat);
floor.position.set(x, y, z);
floor.rotation.x = -Math.PI / 2;
TH.scene.add(floor);
},
addFloor : function (x, y, z, width, depth, image, transparent) {
var repeatX = Math.floor(width / 100);
var repeatY = Math.floor(depth / 100);
var mat = TH._loadTextureMaterial(image, repeatX || 1, repeatY || 1, transparent);
var geometry = new THREE.PlaneGeometry(width, depth);
var floor = new THREE.Mesh( geometry, mat);
floor.position.set(x, y, z);
floor.rotation.x = -Math.PI / 2;
TH.scene.add(floor);
},
// points must be a single closed figure
addFloorFromPoints : function(x, y, z, points, textureName, transparent) {
var thPoints = [];
var startKey = Object.keys(points)[0];
var current = points[startKey];
thPoints.push(new THREE.Vector2(current.x + x, current.y + z));
while (true) {
if (current.next >= 0 && current.next != startKey) {
current = points[current.next];
thPoints.push(new THREE.Vector2(current.x + x, current.y + z));
}
else {
break;
}
}
var shape = new THREE.Shape(thPoints);
var geometry = new THREE.ShapeGeometry(shape);
var mat = TH._loadTextureMaterial(textureName, 0.008, 0.008, transparent);
var floor = new THREE.Mesh(geometry, mat);
floor.position.y = y;
floor.rotation.x = Math.PI / 2;
TH.scene.add(floor);
},
// A one off function for adding the space texture at the end
addCylinder : function(x, y, z, radius, height, textureName) {
var geom = new THREE.CylinderGeometry(radius, radius, height, 64, 1, true, 0, Math.PI);
var texture = TH._loadTexture(textureName, -1, 1); // -1 reverses the x direction of the texture
var mat = new THREE.MeshBasicMaterial({color: 0xffffff, flatShading: true, overdraw: 0.5, map: texture, side: THREE.BackSide, transparent: false});
var cylinder = new THREE.Mesh(geom, mat);
cylinder.rotation.y = -Math.PI / 2;
cylinder.position.x = x;
cylinder.position.y = y;
cylinder.position.z = z;
TH.scene.add(cylinder);
},
update : function() {
var delta = TH.clock.getDelta();
for (var i in TH.animators) {
TH.animators[i].update(delta * 1000);
}
},
clearScene : function() {
// TODO: cleanup materials?
while (TH.scene.children.length)
TH.scene.remove(TH.scene.children[0]);
}
}