-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfonctions.js
More file actions
225 lines (165 loc) · 6.26 KB
/
fonctions.js
File metadata and controls
225 lines (165 loc) · 6.26 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//Permet d'isoler les arêtes pour leur appliquer le LineMaterial
function geo2line( geo ) {
var geometry = new THREE.Geometry();
var vertices = geometry.vertices;
var vvv = geo.getAttribute('position');
for ( i = 0; i < vvv.length-2; i=i+3 ) {
vertices.push( new THREE.Vector3( vvv.array[i], vvv.array[i+1], vvv.array[i+2] ) );
}
geometry.computeLineDistances();
return geometry;
}
//pour appliquer les textures génériques comme les tuiles
function assignUVs(geometry) {
geometry.faceVertexUvs[0] = [];
geometry.faces.forEach(function(face) {
var components = ['x', 'y', 'z'].sort(function(a, b) {
return Math.abs(face.normal[a]) > Math.abs(face.normal[b]);
});
geometry.computeBoundingBox();
var min = geometry.boundingBox.min;
var v1 = geometry.vertices[face.a];
var v2 = geometry.vertices[face.b];
var v3 = geometry.vertices[face.c];
geometry.faceVertexUvs[0].push([
new THREE.Vector2((v1[components[0]] - min[components[0]]) , (v1[components[1]] - min[components[1]])),
new THREE.Vector2((v2[components[0]] - min[components[0]]) , (v2[components[1]] - min[components[1]])),
new THREE.Vector2((v3[components[0]] - min[components[0]]) , (v3[components[1]] - min[components[1]]))
//new THREE.Vector2(v1[components[0]], v1[components[1]])
//new THREE.Vector2(v2[components[0]], v2[components[1]])
//new THREE.Vector2(v3[components[0]], v3[components[1]])
]);
});
geometry.uvsNeedUpdate = true;
}
//déplace le mesh
function moveMesh (mesh, x, y, z) {
mesh.position.x = x;
mesh.position.y = y;
mesh.position.z = z;
return mesh;
}
/*function removeEntity(name) {
var selectedObject = scene.getObjectByName(name);
scene.remove( selectedObject );
animate();
}*/
//capture d'écran
function capture(e){
//Listen to 'P' key
if(e.which !== 80) return;
try {
window.open(renderer.domElement.toDataURL("image/png"));
}
catch(e) {
console.log("Browser does not support taking screenshot of 3d context");
return;
}
}
//retire de la scène tous les éléments du contexte
function clearContexte() {
var to_remove = [];
scene.traverse ( function( child ) {
if ( (child instanceof THREE.Mesh || child instanceof THREE.Line) && !child.userData.focus === true && !child.userData.fond === true && !child.userData.arbre === true ) {
to_remove.push( child );
}
} );
for ( var i = 0; i < to_remove.length; i++ ) {
scene.remove( to_remove[i] );
}
}
//crée le material texturé associé aux arêtes sketchy
function createMaterial(width, value){
//var texture = THREE.ImageUtils.loadTexture( "strokes/pencil1-tiled-136-135.png" );
var texture = THREE.ImageUtils.loadTexture( "strokes/small.png" );
//var texture2 = THREE.ImageUtils.loadTexture( "strokes/paint-brush.png" );
var texture2 = THREE.ImageUtils.loadTexture( "strokes/"+value+".png" );
var texture3 = THREE.ImageUtils.loadTexture( "strokes/"+value+".png" );
//var texture3 = THREE.ImageUtils.loadTexture( "strokes/chalk1-155-142.png" );
var paper = THREE.ImageUtils.loadTexture("paper2.png");
/*texture.wrapS = THREE.RepeatWrapping;
texture.repeat.set( 0.1, 0.1 );
texture.anisotropy = 10;
texture.anisotropy = 10;*/
var color = new THREE.Color().setHex(params.color_aretes_focus.replace("#", "0x"));
//Materiel appliqué à toutes les géométries de la couche
var material = new THREE.ShaderMaterial( {
uniforms: {
time: { value: 1.0 },
thickness : { value: width },
resolution: { value: new THREE.Vector2(window.innerWidth,window.innerHeight) }, // todo: vraie resolution
texture1: { type: "t", value: texture },
texture2: { type: "t", value: texture2 },
texture3: { type: "t", value: texture3 },
paper: { type: "t", value: paper },
color : {type: 'v3', value: [color.r,color.g,color.b]}
},
vertexShader: document.getElementById( 'vertexShader' ).textContent,
fragmentShader: document.getElementById( 'fragmentShader' ).textContent
} );
material.transparent = true;
material.polygonOffset = true;
material.polygonOffsetUnits = -150.0
return material;
}
//crée les six points pour deux triangles (un quad) à partir d'une arête
function createQuad(pt1,pt2){
//Définition propre a chaque géométrie
var geometry = new THREE.BufferGeometry();
//les 6 points
var vertices = new Float32Array( [
pt1.x, pt1.y, pt1.z, // -1
pt2.x, pt2.y, pt2.z, // -1
pt2.x, pt2.y, pt2.z, // 1
pt2.x, pt2.y, pt2.z, // 1
pt1.x, pt1.y, pt1.z, // 1
pt1.x, pt1.y, pt1.z // -1
] );
//pour chacun des six points, le point opposé correspondant
var vertices2 = new Float32Array( [
pt2.x, pt2.y, pt2.z,
pt1.x, pt1.y, pt1.z,
pt1.x, pt1.y, pt1.z,
pt1.x, pt1.y, pt1.z,
pt2.x, pt2.y, pt2.z,
pt2.x, pt2.y, pt2.z
] );
geometry.addAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) );
geometry.addAttribute( 'position2', new THREE.BufferAttribute( vertices2, 3 ) );
var uv = new Float32Array( [
-1, -1,
1, -1,
1, 1,
1, 1,
-1, 1,
-1,-1
] );
geometry.addAttribute( 'uv', new THREE.BufferAttribute( uv, 2 ) );
return geometry;
}
function changeMat(array, mat){
for (i=0; i<array.length; i++){
array[i].material = mat;
array.materialNeedsUpdate = true;
}
}
function mergeMeshes (meshArr) {
var geometry = new THREE.Geometry(),
materials = [],
m,
materialPointer = 0,
reindex = 0;
for (var i = 0; i < meshArr.length; i++) {
m = meshArr[i];
if (m.material.materials) {
for (var j = 0; j < m.material.materials.length; j++) {
materials[materialPointer++] = m.material.materials[j];
}
} else if (m.material) {
materials[materialPointer++] = m.material;
}
geometry.merge(m.geometry, m.matrix, reindex);
reindex = materialPointer;
}
return new THREE.Mesh(geometry, new THREE.MeshFaceMaterial(materials));
}