forked from max-mapper/minecraft-skin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.js
106 lines (88 loc) · 2.53 KB
/
demo.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
var cw = 250, ch = 500;
var camera = new THREE.PerspectiveCamera(55, cw / ch, 1, 1000);
var scene = new THREE.Scene();
scene.add(camera)
var renderer = new THREE.WebGLRenderer({
antialias: true
})
renderer.setSize(cw, ch)
renderer.setClearColorHex(0xFFFFFF, 1.0)
renderer.clear()
var threecanvas = renderer.domElement;
document.body.appendChild(threecanvas);
var ambientLight, directionalLight
ambientLight = new THREE.AmbientLight(0xaaaaaa)
scene.add(ambientLight)
var light = new THREE.DirectionalLight( 0xffffff )
light.position.set( Math.random(), Math.random(), Math.random() ).normalize()
scene.add( light )
camera.lookAt(new THREE.Vector3(0, 0, 0))
scene.add(camera)
var plane = new THREE.Mesh( new THREE.PlaneGeometry( 25, 25 ), new THREE.MeshBasicMaterial({
color: 0xf0f0f0,
shading: THREE.FlatShading,
vertexColors: THREE.VertexColors})
)
plane.rotation.x = - Math.PI / 2
scene.add( plane )
window.plane = plane
var mouseX = 0;
var mouseY = 0.1;
var originMouseX = 0;
var originMouseY = 0;
var rad = 0;
var isMouseOver = false;
var isMouseDown = false;
var counter = 0;
var firstRender = true;
var startTime = Date.now();
var pausedTime = 0;
var isRotating = true;
var isPaused = false;
var isYfreezed = false;
var isFunnyRunning = false;
var skin = require('./')
var pngURL = window.location.hash
if (pngURL === '' || pngURL === '#') pngURL = 'viking.png'
else pngURL = pngURL.substr(1, pngURL.length - 1)
window.viking = skin(THREE, pngURL)
scene.add(viking.mesh)
var walk = require('voxel-walk')
var render = function () {
window.webkitRequestAnimationFrame(render, renderer.domElement);
var oldRad = rad;
walk.render(viking, time)
var time = (Date.now() - startTime)/1000;
if(!isMouseDown) {
if(!isYfreezed) {
mouseY*=0.97;
}
if(isRotating) {
rad += 2;
}
}
else {
rad = mouseX;
}
if(mouseY > 500) {
mouseY = 500;
}
else if(mouseY < -500) {
mouseY = -500;
}
camera.position.x = -Math.cos(rad/(cw/2)+(Math.PI/0.9));
camera.position.z = -Math.sin(rad/(cw/2)+(Math.PI/0.9));
camera.position.y = (mouseY/(ch/2))*1.5+0.2;
camera.position.setLength(70);
camera.lookAt(new THREE.Vector3(0, 1.5, 0));
renderer.render(scene, camera);
};
function renderStatic() {
window.webkitRequestAnimationFrame(renderStatic, renderer.domElement);
camera.position.copy({x: 0, y: 5, z: -10})
camera.position.setLength(70);
camera.lookAt(new THREE.Vector3(0, 1.5, 0));
renderer.render(scene, camera);
}
renderStatic()
document.querySelector('canvas').addEventListener('click', render)