-
Notifications
You must be signed in to change notification settings - Fork 10
/
example27.html
131 lines (105 loc) · 4.17 KB
/
example27.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
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
<!DOCTYPE html>
<html>
<head>
<title>Example 27 - Parametric geometries</title>
<style>
body{
margin: 0;
overflow: hidden;
}
#stats { /* Align stats top-left */
position: absolute;
left: 0px;
top: 0px;
}
</style>
</head>
<body>
<!-- JavaScript libraries -->
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/three.js/r67/three.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/stats.js/r11/Stats.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.5/dat.gui.min.js"></script>
<script src="assets/libs/TrackballControls.js"></script>
<!-- Javascript code that runs our Three.js examples -->
<script>
// once everything is loaded, we run our Three.js stuff.
$(function () {
var stats = initStats();
// create a scene, that will hold all our elements such as objects, cameras and lights.
var scene = new THREE.Scene();
// create a camera, which defines where we're looking at.
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
// create a render and set the size
var webGLRenderer = new THREE.WebGLRenderer();
webGLRenderer.setClearColor(new THREE.Color(0xEEEEEE, 1.0));
webGLRenderer.setSize(window.innerWidth, window.innerHeight);
webGLRenderer.shadowMapEnabled = true;
// position and point the camera to the center of the scene
camera.position.set(-30,50,50);
camera.lookAt(new THREE.Vector3(10, -20, 0));
var spotLight = new THREE.SpotLight(0xffffff);
spotLight.position = new THREE.Vector3(-20, 250, -50);
spotLight.target.position.x = 30;
spotLight.target.position.y = -40;
spotLight.target.position.z = -20;
spotLight.intensity = 0.7;
scene.add(spotLight);
// add the output of the renderer to the html element
$('body').append(webGLRenderer.domElement);
// call the render function
var step = 0;
var klein = function (u, v) {
u *= Math.PI;
v *= 2 * Math.PI;
u = u * 2;
var x, y, z;
if (u < Math.PI) {
x = 3 * Math.cos(u) * (1 + Math.sin(u)) + (2 * (1 - Math.cos(u) / 2)) * Math.cos(u) * Math.cos(v);
z = -8 * Math.sin(u) - 2 * (1 - Math.cos(u) / 2) * Math.sin(u) * Math.cos(v);
} else {
x = 3 * Math.cos(u) * (1 + Math.sin(u)) + (2 * (1 - Math.cos(u) / 2)) * Math.cos(v + Math.PI);
z = -8 * Math.sin(u);
}
y = -2 * (1 - Math.cos(u) / 2) * Math.sin(v);
return new THREE.Vector3(x, y, z);
};
var radialWave = function (u, v) {
var r = 50;
var x = Math.sin(u) * r;
var z = Math.sin(v / 2) * 2 * r;
var y = (Math.sin(u * 4 * Math.PI) + Math.cos(v * 2 * Math.PI)) * 2.8;
return new THREE.Vector3(x, y, z);
}
// var mesh = createMesh(new THREE.ParametricGeometry(klein, 20, 20));
var mesh = createMesh(new THREE.ParametricGeometry(radialWave, 20, 20));
scene.add(mesh);
render();
function createMesh(geom) {
geom.applyMatrix(new THREE.Matrix4().makeTranslation(-25, 0, -25));
// assign two materials
// var meshMaterial = new THREE.MeshLambertMaterial({color: 0xff5555, shading: THREE.FlatShading});
// var meshMaterial = new THREE.MeshNormalMaterial();
var meshMaterial = new THREE.MeshPhongMaterial({specular: 0xffffff, color: 0x3399ff, shininess: 100, metal: true});
meshMaterial.side = THREE.DoubleSide;
var plane = new THREE.Mesh(geom, meshMaterial);
return plane;
}
function render() {
stats.update();
mesh.rotation.y = step += 0.01;
mesh.rotation.x = step;
// render using requestAnimationFrame
requestAnimationFrame(render);
webGLRenderer.render(scene, camera);
}
function initStats() {
var stats = new Stats();
stats.setMode(0); // 0: fps, 1: ms
$('body').append(stats.domElement);
return stats;
}
});
</script>
</body>
</html>