-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
140 lines (114 loc) · 4 KB
/
index.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
132
133
134
135
136
137
138
139
140
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title>My first Three.js app</title>
<style>
body { margin: 0; }
canvas { width: 100%; height: 100% }
</style>
</head>
<body>
<script src="js/three.min.js"></script>
<script src="three.js/examples/js/controls/OrbitControls.js"></script>
<script>
// Our Javascript will go here.
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
var renderer = new THREE.WebGLRenderer({ antialias : true});
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
var orbit = new THREE.OrbitControls( camera, renderer.domElement );
orbit.enableZoom = true;
var material = new THREE.MeshPhongMaterial({
color: 0xffffff,
emissive: 0x6666ff,
side: THREE.DoubleSide,
shading: THREE.FlatShading
});
var lineMaterial = new THREE.LineBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.5
});
var flake = new THREE.Object3D();
//var tan120 = Math.tan(Math.PI/3);
var tan60 = Math.tan(Math.PI/3);
var tan30 = Math.tan(Math.PI/6);
var cos30 = Math.tan(Math.PI/6);
function createBranch(width, height, wideTip, depth, subBranches) {
var branch = new THREE.Object3D();
var halfWidth = width / 2;
var geometry = new THREE.Geometry();
var offset = tan60 * halfWidth;
var subOffset = tan30 * halfWidth;
var tipOffset = wideTip ? subOffset : offset;
geometry.vertices.push(
subBranches ? new THREE.Vector3( -halfWidth, subOffset, 0) : new THREE.Vector3( -halfWidth, offset, 0),
new THREE.Vector3( -halfWidth, height - tipOffset, 0),
new THREE.Vector3( 0, 0, 0),
new THREE.Vector3( 0, height, 0),
subBranches ? new THREE.Vector3( halfWidth, -subOffset, 0) : new THREE.Vector3( halfWidth, offset, 0),
new THREE.Vector3( halfWidth, height - tipOffset, 0)
);
geometry.faces.push(
new THREE.Face3(0,1,2),
new THREE.Face3(1,2,3),
new THREE.Face3(2,3,4),
new THREE.Face3(3,4,5)
);
branch.add(new THREE.Mesh( geometry, material ));
//branch.add(new THREE.LineSegments( geometry, lineMaterial ))
if(depth && subBranches) {
for(var i = 0; i < subBranches; i++) {
var sbHeight = height/4;
var sbWidth = width < sbHeight ? width : sbHeight;
var widthOffset = (sbWidth / cos30) / 2;
var subBranch = createBranch(sbWidth, sbHeight, wideTip, depth - 1, 1);
subBranch.translateY(((height - tipOffset - widthOffset) / subBranches) * (i + 1));
var subBranch2 = subBranch.clone();
subBranch.translateX(-halfWidth);
subBranch.rotateZ(2 * Math.PI / 6);
subBranch.rotateY(Math.PI);
subBranch2.translateX(halfWidth);
subBranch2.rotateZ(-2 * Math.PI / 6);
branch.add(subBranch);
branch.add(subBranch2);
}
}
return branch;
};
var width = 2;
var height = 10;
var branch = createBranch(width, height, true, 2, 2);
flake.add( branch );
for(var i = 1; i < 6; i++) {
branch = branch.clone();
branch.rotateZ(2 * Math.PI / 6);
flake.add( branch );
// geometry.computeBoundingSphere();
}
scene.add(flake);
var ambientLight = new THREE.AmbientLight( 0x000000 );
scene.add( ambientLight );
var lights = [];
lights[0] = new THREE.PointLight( 0xffffff, 1, 0 );
lights[1] = new THREE.PointLight( 0xffffff, 1, 0 );
lights[2] = new THREE.PointLight( 0xffffff, 1, 0 );
lights[0].position.set( 0, 200, 0 );
lights[1].position.set( 100, 200, 100 );
lights[2].position.set( -100, -200, -100 );
scene.add( lights[0] );
scene.add( lights[1] );
scene.add( lights[2] );
camera.position.z = 30;
function render() {
requestAnimationFrame( render );
//flake.rotation.x += 0.005;
//flake.rotation.y += 0.005;
renderer.render( scene, camera );
}
render();
</script>
</body>
</html>