-
Notifications
You must be signed in to change notification settings - Fork 6
/
threex.transparency.js
45 lines (38 loc) · 1.25 KB
/
threex.transparency.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
var THREEx = THREEx || {}
/**
* namespace for the extension
* @type {Object}
*/
THREEx.Transparency = {};
/**
* init transparency of a object recursivly
* @param {THREE.Object3D[]} objects the object which are transparent
*/
THREEx.Transparency.init = function(objects){
objects.forEach(function(object){
object.material.transparent = true
object.material.depthWrite = false
});
}
/**
* update the object for transparency rendering
* @param {THREE.Object3D[]} objects the objects which are transparent
* @param {THREE.Camera} camera the camera used for rendering
*/
THREEx.Transparency.update = function(objects, camera){
// update camera matrices
camera.updateMatrixWorld()
camera.matrixWorldInverse.getInverse( camera.matrixWorld )
var screenMatrix= new THREE.Matrix4().multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse)
var position = new THREE.Vector3()
// traverse the object
objects.forEach(function(object){
// update the matrixWorld of the object and its children
object.updateMatrixWorld()
// compute its position in screen space
position.setFromMatrixPosition( object.matrixWorld );
position.applyProjection( screenMatrix );
// use the position.x as renderDepth
object.renderOrder = -position.z;
})
}