File: DecalGeometry.js

package info (click to toggle)
three.js 111%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 15,184 kB
  • sloc: javascript: 133,174; makefile: 24; sh: 1
file content (360 lines) | stat: -rw-r--r-- 8,611 bytes parent folder | download | duplicates (2)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/**
 * @author Mugen87 / https://github.com/Mugen87
 * @author spite / https://github.com/spite
 *
 * You can use this geometry to create a decal mesh, that serves different kinds of purposes.
 * e.g. adding unique details to models, performing dynamic visual environmental changes or covering seams.
 *
 * Constructor parameter:
 *
 * mesh — Any mesh object
 * position — Position of the decal projector
 * orientation — Orientation of the decal projector
 * size — Size of the decal projector
 *
 * reference: http://blog.wolfire.com/2009/06/how-to-project-decals/
 *
 */

import {
	BufferGeometry,
	Float32BufferAttribute,
	Matrix4,
	Vector3
} from "../../../build/three.module.js";

var DecalGeometry = function ( mesh, position, orientation, size ) {

	BufferGeometry.call( this );

	// buffers

	var vertices = [];
	var normals = [];
	var uvs = [];

	// helpers

	var plane = new Vector3();

	// this matrix represents the transformation of the decal projector

	var projectorMatrix = new Matrix4();
	projectorMatrix.makeRotationFromEuler( orientation );
	projectorMatrix.setPosition( position );

	var projectorMatrixInverse = new Matrix4().getInverse( projectorMatrix );

	// generate buffers

	generate();

	// build geometry

	this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
	this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
	this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );

	function generate() {

		var i;
		var geometry = new BufferGeometry();
		var decalVertices = [];

		var vertex = new Vector3();
		var normal = new Vector3();

		// handle different geometry types

		if ( mesh.geometry.isGeometry ) {

			geometry.fromGeometry( mesh.geometry );

		} else {

			geometry.copy( mesh.geometry );

		}

		var positionAttribute = geometry.attributes.position;
		var normalAttribute = geometry.attributes.normal;

		// first, create an array of 'DecalVertex' objects
		// three consecutive 'DecalVertex' objects represent a single face
		//
		// this data structure will be later used to perform the clipping

		if ( geometry.index !== null ) {

			// indexed BufferGeometry

			var index = geometry.index;

			for ( i = 0; i < index.count; i ++ ) {

				vertex.fromBufferAttribute( positionAttribute, index.getX( i ) );
				normal.fromBufferAttribute( normalAttribute, index.getX( i ) );

				pushDecalVertex( decalVertices, vertex, normal );

			}

		} else {

			// non-indexed BufferGeometry

			for ( i = 0; i < positionAttribute.count; i ++ ) {

				vertex.fromBufferAttribute( positionAttribute, i );
				normal.fromBufferAttribute( normalAttribute, i );

				pushDecalVertex( decalVertices, vertex, normal );

			}

		}

		// second, clip the geometry so that it doesn't extend out from the projector

		decalVertices = clipGeometry( decalVertices, plane.set( 1, 0, 0 ) );
		decalVertices = clipGeometry( decalVertices, plane.set( - 1, 0, 0 ) );
		decalVertices = clipGeometry( decalVertices, plane.set( 0, 1, 0 ) );
		decalVertices = clipGeometry( decalVertices, plane.set( 0, - 1, 0 ) );
		decalVertices = clipGeometry( decalVertices, plane.set( 0, 0, 1 ) );
		decalVertices = clipGeometry( decalVertices, plane.set( 0, 0, - 1 ) );

		// third, generate final vertices, normals and uvs

		for ( i = 0; i < decalVertices.length; i ++ ) {

			var decalVertex = decalVertices[ i ];

			// create texture coordinates (we are still in projector space)

			uvs.push(
				0.5 + ( decalVertex.position.x / size.x ),
				0.5 + ( decalVertex.position.y / size.y )
			);

			// transform the vertex back to world space

			decalVertex.position.applyMatrix4( projectorMatrix );

			// now create vertex and normal buffer data

			vertices.push( decalVertex.position.x, decalVertex.position.y, decalVertex.position.z );
			normals.push( decalVertex.normal.x, decalVertex.normal.y, decalVertex.normal.z );

		}

	}

	function pushDecalVertex( decalVertices, vertex, normal ) {

		// transform the vertex to world space, then to projector space

		vertex.applyMatrix4( mesh.matrixWorld );
		vertex.applyMatrix4( projectorMatrixInverse );

		normal.transformDirection( mesh.matrixWorld );

		decalVertices.push( new DecalVertex( vertex.clone(), normal.clone() ) );

	}

	function clipGeometry( inVertices, plane ) {

		var outVertices = [];

		var s = 0.5 * Math.abs( size.dot( plane ) );

		// a single iteration clips one face,
		// which consists of three consecutive 'DecalVertex' objects

		for ( var i = 0; i < inVertices.length; i += 3 ) {

			var v1Out, v2Out, v3Out, total = 0;
			var nV1, nV2, nV3, nV4;

			var d1 = inVertices[ i + 0 ].position.dot( plane ) - s;
			var d2 = inVertices[ i + 1 ].position.dot( plane ) - s;
			var d3 = inVertices[ i + 2 ].position.dot( plane ) - s;

			v1Out = d1 > 0;
			v2Out = d2 > 0;
			v3Out = d3 > 0;

			// calculate, how many vertices of the face lie outside of the clipping plane

			total = ( v1Out ? 1 : 0 ) + ( v2Out ? 1 : 0 ) + ( v3Out ? 1 : 0 );

			switch ( total ) {

				case 0: {

					// the entire face lies inside of the plane, no clipping needed

					outVertices.push( inVertices[ i ] );
					outVertices.push( inVertices[ i + 1 ] );
					outVertices.push( inVertices[ i + 2 ] );
					break;

				}

				case 1: {

					// one vertex lies outside of the plane, perform clipping

					if ( v1Out ) {

						nV1 = inVertices[ i + 1 ];
						nV2 = inVertices[ i + 2 ];
						nV3 = clip( inVertices[ i ], nV1, plane, s );
						nV4 = clip( inVertices[ i ], nV2, plane, s );

					}

					if ( v2Out ) {

						nV1 = inVertices[ i ];
						nV2 = inVertices[ i + 2 ];
						nV3 = clip( inVertices[ i + 1 ], nV1, plane, s );
						nV4 = clip( inVertices[ i + 1 ], nV2, plane, s );

						outVertices.push( nV3 );
						outVertices.push( nV2.clone() );
						outVertices.push( nV1.clone() );

						outVertices.push( nV2.clone() );
						outVertices.push( nV3.clone() );
						outVertices.push( nV4 );
						break;

					}

					if ( v3Out ) {

						nV1 = inVertices[ i ];
						nV2 = inVertices[ i + 1 ];
						nV3 = clip( inVertices[ i + 2 ], nV1, plane, s );
						nV4 = clip( inVertices[ i + 2 ], nV2, plane, s );

					}

					outVertices.push( nV1.clone() );
					outVertices.push( nV2.clone() );
					outVertices.push( nV3 );

					outVertices.push( nV4 );
					outVertices.push( nV3.clone() );
					outVertices.push( nV2.clone() );

					break;

				}

				case 2: {

					// two vertices lies outside of the plane, perform clipping

					if ( ! v1Out ) {

						nV1 = inVertices[ i ].clone();
						nV2 = clip( nV1, inVertices[ i + 1 ], plane, s );
						nV3 = clip( nV1, inVertices[ i + 2 ], plane, s );
						outVertices.push( nV1 );
						outVertices.push( nV2 );
						outVertices.push( nV3 );

					}

					if ( ! v2Out ) {

						nV1 = inVertices[ i + 1 ].clone();
						nV2 = clip( nV1, inVertices[ i + 2 ], plane, s );
						nV3 = clip( nV1, inVertices[ i ], plane, s );
						outVertices.push( nV1 );
						outVertices.push( nV2 );
						outVertices.push( nV3 );

					}

					if ( ! v3Out ) {

						nV1 = inVertices[ i + 2 ].clone();
						nV2 = clip( nV1, inVertices[ i ], plane, s );
						nV3 = clip( nV1, inVertices[ i + 1 ], plane, s );
						outVertices.push( nV1 );
						outVertices.push( nV2 );
						outVertices.push( nV3 );

					}

					break;

				}

				case 3: {

					// the entire face lies outside of the plane, so let's discard the corresponding vertices

					break;

				}

			}

		}

		return outVertices;

	}

	function clip( v0, v1, p, s ) {

		var d0 = v0.position.dot( p ) - s;
		var d1 = v1.position.dot( p ) - s;

		var s0 = d0 / ( d0 - d1 );

		var v = new DecalVertex(
			new Vector3(
				v0.position.x + s0 * ( v1.position.x - v0.position.x ),
				v0.position.y + s0 * ( v1.position.y - v0.position.y ),
				v0.position.z + s0 * ( v1.position.z - v0.position.z )
			),
			new Vector3(
				v0.normal.x + s0 * ( v1.normal.x - v0.normal.x ),
				v0.normal.y + s0 * ( v1.normal.y - v0.normal.y ),
				v0.normal.z + s0 * ( v1.normal.z - v0.normal.z )
			)
		);

		// need to clip more values (texture coordinates)? do it this way:
		// intersectpoint.value = a.value + s * ( b.value - a.value );

		return v;

	}

};

DecalGeometry.prototype = Object.create( BufferGeometry.prototype );
DecalGeometry.prototype.constructor = DecalGeometry;

// helper

var DecalVertex = function ( position, normal ) {

	this.position = position;
	this.normal = normal;

};

DecalVertex.prototype.clone = function () {

	return new this.constructor( this.position.clone(), this.normal.clone() );

};

export { DecalGeometry, DecalVertex };