-
Notifications
You must be signed in to change notification settings - Fork 0
/
lowpoly.js
175 lines (144 loc) Β· 5.3 KB
/
lowpoly.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
163
164
165
166
167
168
169
170
171
172
173
174
175
/* global AFRAME, THREE */
AFRAME.registerComponent('lowpoly', {
schema: {
color: { type: 'string', default: '#FFF'},
nodes: { type: 'boolean', default: false },
opacity: { type: 'number', default: 1.0 },
wireframe: { type: 'boolean', default: false }
},
init: function() {
// Get the ref of the object to which the component is attached
const el = this.el;
const obj = el.getObject3D('mesh');
const position = obj.el.getAttribute('position');
// Grab the reference to the main WebGL scene
const scene = document.querySelector('a-scene').object3D
// Modify the color of the material
obj.material = new THREE.MeshPhongMaterial({
color: this.data.color,
emissive: this.data.color,
emissiveIntensity: 0.5,
flatShading: THREE.FlatShading
})
// Define the geometry for the outer wireframe
const frameGeom = new THREE.OctahedronGeometry(2.5, 2)
// Define the material for it
const frameMat = new THREE.MeshPhongMaterial({
color: this.data.color,
opacity: this.data.opacity,
transparent: true,
wireframe: true
})
// The final mesh is a composition of the geometry and the material
this.icosFrame = new THREE.Mesh(frameGeom, frameMat)
// Set the position of the mesh to the position of the sphere
const { x, y, z } = position
this.icosFrame.position.set(x, y + 0.2, z)
// If the wireframe prop is set to true, then we attach the new object
if (this.data.wireframe) {
scene.add(this.icosFrame)
}
// If the nodes attribute is set to true
if (this.data.nodes) {
let spheres = new THREE.Group()
let vertices = this.icosFrame.geometry.vertices
// Traverse the vertices of the wireframe and attach small spheres
for (var i in vertices) {
// Create a basic sphere
let geometry = new THREE.SphereGeometry(0.045, 16, 16)
let material = new THREE.MeshBasicMaterial({
color: this.data.color,
opacity: this.data.opacity,
flatShading: THREE.FlatShading,
transparent: true
})
let sphere = new THREE.Mesh(geometry, material)
// Reposition them correctly
sphere.position.set(
vertices[i].x + x,
vertices[i].y + y + 0.2,
vertices[i].z + z
)
spheres.add(sphere)
}
scene.add(spheres)
}
},
update: function() {
// Get the ref of the object to which the component is attached
const obj = this.el.getObject3D('mesh')
obj.material.color = new THREE.Color(this.data.color)
},
tick: function() {
this.icosFrame.rotation.y += 0.005;
}
})
AFRAME.registerComponent('low-poly', {
schema: {
color: { type: 'string', default: '#FFF'},
nodes: { type: 'boolean', default: false },
opacity: { type: 'number', default: 1.0 },
wireframe: { type: 'boolean', default: false }
},
init: function() {
// Get the ref of the object to which the component is attached
const el = this.el;
const obj = el.getObject3D('mesh');
const position = obj.el.getAttribute('position');
// Grab the reference to the main WebGL scene
const scene = document.querySelector('a-scene').object3D
// Modify the color of the material
obj.material = new THREE.MeshPhongMaterial({
color: this.data.color,
flatShading: THREE.FlatShading
})
// Define the geometry for the outer wireframe
const frameGeom = new THREE.IcosahedronGeometry(2.5, 3)
// Define the material for it
const frameMat = new THREE.MeshPhongMaterial({
color: '#FFFFFF',
opacity: this.data.opacity,
transparent: true,
wireframe: true
})
// The final mesh is a composition of the geometry and the material
const icosFrame = new THREE.Mesh(frameGeom, frameMat)
// Set the position of the mesh to the position of the sphere
const { x, y, z } = position
icosFrame.position.set(x, y, z)
// If the wireframe prop is set to true, then we attach the new object
if (this.data.wireframe) {
scene.add(icosFrame)
}
// If the nodes attribute is set to true
if (this.data.nodes) {
let spheres = new THREE.Group()
let vertices = icosFrame.geometry.vertices
// Traverse the vertices of the wireframe and attach small spheres
for (var i in vertices) {
// Create a basic sphere
let geometry = new THREE.SphereGeometry(0.045, 16, 16)
let material = new THREE.MeshBasicMaterial({
color: '#FFFFFF',
opacity: this.data.opacity,
flatShading: THREE.FlatShading,
transparent: true
})
let sphere = new THREE.Mesh(geometry, material)
// Reposition them correctly
sphere.position.set(
vertices[i].x + x,
vertices[i].y + y,
vertices[i].z + z
)
spheres.add(sphere)
}
scene.add(spheres)
}
},
update: function() {
// Get the ref of the object to which the component is attached
const obj = this.el.getObject3D('mesh')
obj.material.color = new THREE.Color(this.data.color)
}
})