-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstereocube.js
109 lines (83 loc) · 3.16 KB
/
stereocube.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
// Add stereo components
// based on https://github.com/oscarmarinmiro/aframe-stereo-component/blob/master/index.js
AFRAME.registerComponent('stereocam', {
schema: {
eye: { type: 'string', default: "left"}
},
init: function(){
this.layer_changed = false;
},
tick: function(time){
var originalData = this.data;
if(!this.layer_changed){
var childrenTypes = [];
this.el.object3D.children.forEach( function(item, index, array) {
childrenTypes[index] = item.type;
});
var rootIndex = childrenTypes.indexOf("PerspectiveCamera");
var rootCam = this.el.object3D.children[rootIndex];
rootCam.layers.enable(originalData.eye === 'left' ? 1:2);
}
}
});
// Add sky (stereo cubemap support)
AFRAME.registerComponent('skycube', {
schema: {
folder: { type: "string" },
eye: { type: 'string', default: "left"}
},
// update: function() {
// var loader = new THREE.CubeTextureLoader();
// var urls = [ "L-2.jpg","L-0.jpg","L-5.jpg","L-4.jpg","L-1.jpg","L-3.jpg"];
// var materialArray = [];
// for (var i = 0; i < 6; i++) {
// materialArray.push( new THREE.MeshBasicMaterial({
// map: (new THREE.TextureLoader).load( this.data.folder+"/" + urls[i] ),
// depthWrite: false
// }));
// }
// var material = new THREE.MeshFaceMaterial(materialArray);
// var skyboxGeometry = new THREE.BoxGeometry(1000,1000,1000);
// //skyboxGeometry.applyMatrix( new THREE.Matrix4().makeScale( 1, 1, -1 ) );
// var mesh = new THREE.Mesh(skyboxGeometry,material);
// mesh.layers.set( this.data.eye === 'left' ? 1:2 );
// this.el.setObject3D('mesh', mesh);
// this.materials = material;
// }
update: function() {
var loader = new THREE.CubeTextureLoader();
var skyboxGeometry = new THREE.BoxGeometry(1000,1000,1000);
var textures = this.getTexturesFromAtlasFile( "Cubemaps/Construct_ImminentCollision_cubemap.jpg", 12 );
var materialArray = [];
for ( var i = 0; i < 12; i ++ ) {
materialArray.push( new THREE.MeshBasicMaterial( { map: textures[ i ] } ) );
}
var mesh = new THREE.Mesh(skyboxGeometry,materialArray);
mesh.layers.set( this.data.eye === 'left' ? 1:2 );
this.el.setObject3D('mesh', mesh);
this.materials = materialArray;
},
getTexturesFromAtlasFile: function( atlasImgUrl, tilesNum ) {
var textures = [];
for ( var i = 0; i < tilesNum; i ++ ) {
textures[ i ] = new THREE.Texture();
textures[ i ].minFilter = THREE.LinearFilter;
}
var imageObj = new Image();
imageObj.onload = function() {
var canvas, context;
var tileWidth = imageObj.height;
for ( var i = 0; i < textures.length; i ++ ) {
canvas = document.createElement( 'canvas' );
context = canvas.getContext( '2d' );
canvas.height = tileWidth;
canvas.width = tileWidth;
context.drawImage( imageObj, tileWidth * i, 0, tileWidth, tileWidth, 0, 0, tileWidth, tileWidth );
textures[ i ].image = canvas
textures[ i ].needsUpdate = true;
}
};
imageObj.src = atlasImgUrl;
return textures;
}
});