-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcube1.html
71 lines (66 loc) · 2.42 KB
/
cube1.html
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
<!DOCTYPE html>
<html>
<head>
<title>webgl demo</title>
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<script type="text/javascript" src="./js/three.min.js"></script>
<style>
*{margin:0;padding: 0;}
html,body{
height: 100%;
overflow: hidden;
}
</style>
</head>
<body>
<script type="text/javascript">
var scene = new THREE.Scene();
var mouseX = 0, mouseY = 0;
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100000);
var webGLRenderer = new THREE.WebGLRenderer();
webGLRenderer.setClearColor(0x333333);
webGLRenderer.setSize(window.innerWidth, window.innerHeight);
var texture = new THREE.TextureLoader().load("./img/1.png");
var geometry = new THREE.CubeGeometry(3,3,3);
var material = new THREE.MeshBasicMaterial({
transparent: true,
map: texture
});
var mesh = new THREE.Mesh(geometry,material);
mesh.position.set(0,0,0);
scene.add(mesh);
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
document.addEventListener( 'touchmove', onDocumentTouchMove, false );
document.addEventListener( 'touchend', onDocumentTouchEnd, false );
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
function onDocumentMouseMove(event) {
mouseX = ( event.clientX - windowHalfX )*0.1;
mouseY = ( event.clientY - windowHalfY )*0.1;
}
function onDocumentTouchMove(event) {
event.preventDefault();
mouseX = ( event.targetTouches[0].pageX - windowHalfX )*0.5;
mouseY = ( event.targetTouches[0].pageY - windowHalfY )*0.5;
}
function onDocumentTouchEnd(event){
mouseX = 0;
mouseY = 0;
}
camera.position.set(10,10,10);
camera.lookAt(scene.position);
function render() {
requestAnimationFrame(render);
mesh.rotation.x+=0.02;
mesh.rotation.z+=0.02;
camera.position.x += ( mouseX - camera.position.x ) * .08;
camera.position.y += ( - mouseY - camera.position.y ) * .08;
camera.lookAt( scene.position );
webGLRenderer.clear();
webGLRenderer.render(scene,camera);
}
render();
document.body.appendChild(webGLRenderer.domElement);
</script>
</body>
</html>