-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
162 lines (138 loc) · 4.26 KB
/
index.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
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
import {
Scene,
Color,
Mesh,
MeshNormalMaterial,
BoxBufferGeometry,
PerspectiveCamera,
WebGLRenderer
} from "three";
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
import Stats from "stats.js";
function N(i, k, t, u) {
if (k === 1) {
return u[i] <= t && t <= u[i + 1] ? 1 : 0;
}
const termLeft =
u[i + k - 1] - u[i]
? ((t - u[i]) * N(i, k - 1, t, u)) / (u[i + k - 1] - u[i])
: 0;
const termRight =
u[i + k] - u[i + 1]
? ((u[i + k] - t) * N(i + 1, k - 1, t, u)) / (u[i + k] - u[i + 1])
: 0;
//console.log(termLeft + termRight)
return termLeft + termRight;
}
class Example {
constructor() {
this.init = this.init.bind(this);
this.animate = this.animate.bind(this);
this.onWindowResize = this.onWindowResize.bind(this);
this.init();
}
init() {
this.aspect = window.innerWidth / window.innerHeight;
this.camera = new PerspectiveCamera(50, this.aspect, 1, 1000);
this.camera.position.z = 700;
this.controls = new OrbitControls(this.camera);
this.geometry = new THREE.Geometry();
this.nurbsGeometry = new THREE.Geometry();
this.material = new THREE.PointsMaterial({ color: 0x00ff00 });
this.nurbsMaterial = new THREE.PointsMaterial({ color: 0x00ffff });
const k = 4;
const l = 3;
const knotX = [0, 0, 0, 0, 1, 1, 1, 1];
const knotY = [0, 0, 0, 1, 2, 2, 2];
this.vertices = [
[-100, 0, 0],
[-50, 100, 0],
[0, 0, 0],
[50, 100, 0],
[100, 0, 0],
[150, 100, 0]
];
this.vertices2 = [[-100, 0, 0], [-50, 100, 0], [0, 0, 0], [50, 100, 0]];
this.vertices3 = [
[-15, 0, 15],
[-5, 5, 15],
[5, 5, 15],
[15, 0, 15],
[-15, 5, 5],
[-5, 10, 5],
[5, 10, 5],
[15, 5, 5],
[-15, 5, -5],
[-5, 10, -5],
[5, 10, -5],
[15, 5, -5],
[-15, 0, -15],
[-5, 5, -15],
[5, 5, -15],
[15, 0, -15]
];
//knot vector
this.knot = [0, 0, 0, 1, 1, 2, 2, 3, 3, 3];
this.knot1 = [0, 0, 0, 1, 2, 2, 2];
this.vertices3.forEach(v => {
this.geometry.vertices.push(new THREE.Vector3().fromArray(v));
});
this.points = this.vertices3.map(v => new THREE.Vector3().fromArray(v));
const step = 0.001*4;
for (let u =0; u <= 1; u+= step){
for (let w = 0; w<= 2; w+= step){
const vertex = new THREE.Vector3(0, 0, 0);
for (let i =0 ; i < 4; i++){
const vertex2 = new THREE.Vector3(0, 0, 0);
for (let j = 0; j < 4; j++){
const basis = N(j,l, w, knotY);
const {x, y, z} = this.points[4*j + i];
vertex2.x += x*basis;
vertex2.y += y*basis;
vertex2.z += z*basis;
}
const basis = N(i, k, u, knotX);
const {x, y, z} = vertex2;
vertex.x += x*basis;
vertex.y += y*basis;
vertex.z += z*basis;
}
this.nurbsGeometry.vertices.push(vertex);
}
}
this.mesh = new THREE.Points(this.geometry, this.material);
this.nurbsMesh = new THREE.Points(this.nurbsGeometry, this.nurbsMaterial);
console.log(this.vertices);
this.scene = new Scene();
this.scene.background = new Color("#191919");
//this.scene.add(this.mesh);
this.scene.add(this.nurbsMesh);
this.renderer = new WebGLRenderer({
powerPreference: "high-performance",
antialias: true
});
document.body.appendChild(this.renderer.domElement);
window.addEventListener("resize", this.onWindowResize);
this.stats = new Stats();
document.body.appendChild(this.stats.dom);
this.renderer.setPixelRatio(window.devicePixelRatio);
this.renderer.setSize(window.innerWidth, window.innerHeight);
this.renderer.render(this.scene, this.camera);
this.renderer.setAnimationLoop(this.animate);
}
animate() {
this.stats.begin();
this.nurbsMesh.rotation.x += 0.005;
this.nurbsMesh.rotation.y += 0.001;
this.controls.update();
this.renderer.render(this.scene, this.camera);
this.stats.end();
}
onWindowResize() {
this.camera.aspect = window.innerWidth / window.innerHeight;
this.camera.updateProjectionMatrix();
this.renderer.setSize(window.innerWidth, window.innerHeight);
}
}
new Example();