Socialify

Folder ..

Viewing geoPieces.js
278 lines (231 loc) • 8.6 KB

  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
// ECMAScript 5 strict mode
/* jshint globalstrict: true*/
/* global THREE,console,BLACK,WHITE,WIREFRAME */
"use strict";
var pieceMaterial = [];
pieceMaterial[BLACK] = new THREE.MeshPhongMaterial({color:0x111111,specular:0xaaaaaa,shininess:30.0,wireframe:WIREFRAME});
pieceMaterial[WHITE] = new THREE.MeshPhongMaterial({color:0xdddddd,wireframe:WIREFRAME});
function createPawn(size,color) {
	var pawn = new THREE.Object3D();
	var baseHeight = size*0.1;
	var baseRadius = size*0.8;
	var base = new THREE.Mesh(
		new THREE.CylinderGeometry(baseRadius,baseRadius,baseHeight,32,1),pieceMaterial[color]);
	base.position.y = baseHeight/2;

	var bodyRadius = size*0.6;
	var body = new THREE.Mesh(
		new THREE.SphereGeometry(bodyRadius,32,16),pieceMaterial[color]);
	body.position.y = baseHeight + bodyRadius;

	var neckHeight = size;
	var neckRadius = bodyRadius*0.7;
	var neck = new THREE.Mesh(
		new THREE.CylinderGeometry(0,neckRadius,neckHeight,32,1),pieceMaterial[color]);
	neck.position.y = baseHeight + bodyRadius*2+neckHeight/2;

	var headRadius = size*0.2;
	var head = new THREE.Mesh(
		new THREE.SphereGeometry(headRadius,32,16),pieceMaterial[color]);
	head.position.y = baseHeight + bodyRadius*2 +neckHeight+headRadius/2;

	pawn.add(head);
	pawn.add(neck);
	pawn.add(body);
	pawn.add(base);
	pawn.name = "Pawn";
	return pawn;
}

function createRook(size,color) {
	var rook = new THREE.Object3D();
	var baseHeight = size*0.1;
	var baseRadius = size*0.8;
	var base = new THREE.Mesh(
		new THREE.CylinderGeometry(baseRadius,baseRadius,baseHeight,32,1),pieceMaterial[color]);
	base.position.y = baseHeight/2;
	var bodyRadius = size*0.4;
	var bodyHeight = size*1.6;
	var body = new THREE.Mesh(
		new THREE.CylinderGeometry(bodyRadius*1.2,bodyRadius,bodyHeight,32,16),pieceMaterial[color]);
	body.position.y = baseHeight + bodyHeight/2;

	var wedgeHeight = size*0.5;
	var wedgeRadius = bodyRadius * 1.5;
	var wedge = new THREE.Mesh(
		new THREE.CylinderGeometry(wedgeRadius,bodyRadius*1.2,wedgeHeight,32,1),pieceMaterial[color]);

	wedge.position.y = baseHeight + bodyHeight+wedgeHeight/2;

	var teethThickness = size*0.2;
	var teethHeight = wedgeHeight;
	var teethTilt = size*0.1;

	var obr = wedgeRadius;
	var ibr = wedgeRadius-teethThickness;
	var otr = obr-teethTilt;
	var itr = ibr-teethTilt;
	var teethCount = 6;
	var subdivision = Math.round(32/(teethCount*2));

	var teethGeo = new THREE.TubeGeometry(otr,obr,itr,ibr,teethHeight,subdivision,1,0,2*Math.PI/(teethCount*2));

	for (var i = 0; i < teethCount; i++) {
		var teeth = new THREE.Mesh(teethGeo,pieceMaterial[color]);
		teeth.position.y = baseHeight+bodyHeight+wedgeHeight+teethHeight/2;
		teeth.rotation.y = i*2*Math.PI/teethCount;
		rook.add(teeth);
	}

	rook.add(wedge);
	rook.add(body);
	rook.add(base);
	rook.name = "Rook";
	return rook;
}

function createKnight(size,color) {
	var knight = new THREE.Object3D();
	var baseHeight = size*0.1;
	var baseRadius = size*0.8;
	var base = new THREE.Mesh(
		new THREE.CylinderGeometry(baseRadius,baseRadius,baseHeight,32,1),pieceMaterial[color]);
	base.position.y = baseHeight/2;

	var bodyRadius = size*0.6;
	var body = new THREE.Mesh(
		new THREE.SphereGeometry(bodyRadius,32,16),pieceMaterial[color]);

	var neckHeight = size*1.2;
	var neckBottomRadius = bodyRadius*0.6;
	var neckTopRadius = bodyRadius*0.0;
	var _neck = new THREE.Mesh(
		new THREE.CylinderGeometry(neckTopRadius,neckBottomRadius,neckHeight,32,1),pieceMaterial[color]);
	_neck.position.y =  bodyRadius+neckHeight/2;
	var neck = new THREE.Object3D();
	neck.add(_neck);
	neck.rotation.z = -Math.PI/32;


	var head = new THREE.Object3D();

	var skullRadius = size*0.6;
	var skull = new THREE.Mesh(
		new THREE.SphereGeometry(skullRadius,32,16),pieceMaterial[color]);

	var faceHeight = size*1.0;
	var faceBottomRadius = skullRadius*0.8;
	var faceTopRadius = skullRadius*0.3;
	var face = new THREE.Mesh(
		new THREE.CylinderGeometry(faceTopRadius,faceBottomRadius,faceHeight,32,1),pieceMaterial[color]);
	face.rotation.z = Math.PI/16;
	face.position.y = skullRadius;

	var noseRadius = size*0.2;
	var nose = new THREE.Mesh(
		new THREE.SphereGeometry(noseRadius,32,16),pieceMaterial[color]);
	nose.position.y = skullRadius +faceHeight+noseRadius/2;

	head.add(skull);
	head.add(face);
	//head.add(nose);
	head.scale.z = 0.5;
	head.rotation.z = 5*Math.PI/8;
	head.position.y = bodyRadius+neckHeight;


	var horse = new THREE.Object3D();
	horse.add(body);
	horse.add(neck);
	horse.add(head);
	horse.rotation.z = -Math.PI/32;
	horse.rotation.y = (color == whiteMat) ? -Math.PI/2 : Math.PI/2;
	horse.position.y = baseHeight+bodyRadius;

	knight.add(horse);
	knight.add(base);
	knight.name = "Knight";
	return knight;
}

function createBishop(size,color) {
	var bishop = new THREE.Object3D();
	var baseHeight = size*0.1;
	var baseRadius = size*0.8;
	var base = new THREE.Mesh(
		new THREE.CylinderGeometry(baseRadius,baseRadius,baseHeight,32,1),pieceMaterial[color]);
	base.position.y = baseHeight/2;

	var bodyRadius = size*0.6;
	var body = new THREE.Mesh(
		new THREE.SphereGeometry(bodyRadius,32,16),pieceMaterial[color]);
	body.position.y = baseHeight + bodyRadius;

	var neckHeight = size*1.7;
	var neckBottomRadius = bodyRadius*0.3;
	var neckTopRadius = bodyRadius*0.05;
	var neck = new THREE.Mesh(
		new THREE.CylinderGeometry(neckTopRadius,neckBottomRadius,neckHeight,32,1),pieceMaterial[color]);
	neck.position.y = baseHeight + bodyRadius*2+neckHeight/2;

	var headRadius = size*0.2;
	var head = new THREE.Mesh(
		new THREE.SphereGeometry(headRadius,32,16),pieceMaterial[color]);
	head.scale.y = 1.5;
	head.position.y = baseHeight + bodyRadius*2 +neckHeight+headRadius/2;

	bishop.add(head);
	bishop.add(neck);
	bishop.add(body);
	bishop.add(base);
	bishop.name = "Bishop";
	return bishop;
}



function createQueen(size,color) {
	var queen = new THREE.Object3D();
	var baseHeight = size*0.1;
	var baseRadius = size*0.8;
	var base = new THREE.Mesh(
		new THREE.CylinderGeometry(baseRadius,baseRadius,baseHeight,32,1),pieceMaterial[color]);
	base.position.y = baseHeight/2;

	var bodyRadius = size*0.6;
	var body = new THREE.Mesh(
		new THREE.SphereGeometry(bodyRadius,32,16),pieceMaterial[color]);
	body.position.y = baseHeight + bodyRadius;

	var neckHeight = size*2.3;
	var neckBottomRadius = bodyRadius*0.3;
	var neckTopRadius = bodyRadius*0.05;
	var neck = new THREE.Mesh(
		new THREE.CylinderGeometry(neckTopRadius,neckBottomRadius,neckHeight,32,1),pieceMaterial[color]);
	neck.position.y = baseHeight + bodyRadius*2+neckHeight/2;

	var collarHeight = size* 0.1;
	var collarRadius = size* 0.4;
	var collar = new THREE.Mesh(
		new THREE.CylinderGeometry(collarRadius,collarRadius,collarHeight,32,1),pieceMaterial[color]);
	collar.position.y = baseHeight+bodyRadius*2+neckHeight - size*0.1;

	var headRadius = size*0.2;
	var head = new THREE.Mesh(
		new THREE.SphereGeometry(headRadius,32,16),pieceMaterial[color]);
	head.position.y = baseHeight + bodyRadius*2 +neckHeight+headRadius/2;

	queen.add(head);
	queen.add(collar);
	queen.add(neck);
	queen.add(body);
	queen.add(base);
	queen.name = "Queen";
	return queen;
}


function createKing(size,color) {
	var king = new THREE.Object3D();
	var baseHeight = size*0.1;
	var baseRadius = size*0.8;
	var base = new THREE.Mesh(
		new THREE.CylinderGeometry(baseRadius,baseRadius,baseHeight,32,1),pieceMaterial[color]);
	base.position.y = baseHeight/2;

	var bodyRadius = size*0.6;
	var body = new THREE.Mesh(
		new THREE.SphereGeometry(bodyRadius,32,16),pieceMaterial[color]);
	body.position.y = baseHeight + bodyRadius;

	var neckHeight = size*2.3;
	var neckBottomRadius = bodyRadius*0.15;
	var neckTopRadius = bodyRadius*0.2;
	var neck = new THREE.Mesh(
		new THREE.CylinderGeometry(neckTopRadius,neckBottomRadius,neckHeight,32,1),pieceMaterial[color]);
	neck.position.y = baseHeight + bodyRadius*2+neckHeight/2;

	var collarHeight = size* 0.4;
	var collarBottomRadius = size* 0.25;
	var collarTopRadius = size* 0.45;
	var collar = new THREE.Mesh(
		new THREE.CylinderGeometry(collarTopRadius,collarBottomRadius,collarHeight,32,1),pieceMaterial[color]);
	collar.position.y = baseHeight+bodyRadius*2+neckHeight - size*0.1;

	var cross = new THREE.Object3D();
	var crossThickness = size*0.2;
	var crossHeight = size*0.8;
	var crossGeo = new THREE.CubeGeometry(crossThickness,crossHeight,crossThickness);

	var hCross = new THREE.Mesh(crossGeo,pieceMaterial[color]);
	hCross.position.y = crossHeight/2;
	var vCross = new THREE.Mesh(crossGeo,pieceMaterial[color]);
	vCross.rotation.z = Math.PI/2;
	vCross.position.y = crossHeight/2;

	cross.add(hCross);
	cross.add(vCross);
	cross.position.y = baseHeight+bodyRadius*2 + neckHeight- size*0.1+collarHeight/2;



	king.add(cross);
	king.add(collar);
	king.add(neck);
	king.add(body);
	king.add(base);
	king.name = "King";
	return king;
}