Update 5
Created another fiddle to show what is expected would look like. An invisible skydome and a cubecamera are added and environment map is used; in my case, none of these technique should be used for the reasons already mentioned.
var MatcapTransformer = function(uvs, face) {
for (var i = uvs.length; i-- > 0;) {
uvs[i].x = face.vertexNormals[i].x * 0.5 + 0.5;
uvs[i].y = face.vertexNormals[i].y * 0.5 + 0.5;
}
};
var TransformUv = function(geometry, xformer) {
// The first argument is also used as an array in the recursive calls
// as there's no method overloading in javascript; and so is the callback.
var a = arguments[0],
callback = arguments[1];
var faceIterator = function(uvFaces, index) {
xformer(uvFaces[index], geometry.faces[index]);
};
var layerIterator = function(uvLayers, index) {
TransformUv(uvLayers[index], faceIterator);
};
for (var i = a.length; i-- > 0;) {
callback(a, i);
}
if (!(i < 0)) {
TransformUv(geometry.faceVertexUvs, layerIterator);
}
};
var SetResizeHandler = function(renderer, camera) {
var callback = function() {
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
};
// bind the resize event
window.addEventListener('resize', callback, false);
// return .stop() the function to stop watching window resize
return {
stop: function() {
window.removeEventListener('resize', callback);
}
};
};
(function() {
var fov = 45;
var aspect = window.innerWidth / window.innerHeight;
var loader = new THREE.TextureLoader();
var texture = loader.load('https://i.postimg.cc/mTsN30vx/canyon-s.jpg');
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.center.set(1 / 2, 1 / 2);
var cubeCam = new THREE.CubeCamera(.1, 200, 4096);
cubeCam.renderTarget.texture.wrapS = THREE.RepeatWrapping;
cubeCam.renderTarget.texture.wrapT = THREE.RepeatWrapping;
cubeCam.renderTarget.texture.center.set(1 / 2, 1 / 2);
var geoSky = new THREE.SphereGeometry(2, 16, 16);
var matSky = new THREE.MeshBasicMaterial({
'map': texture,
'side': THREE.BackSide
});
var meshSky = new THREE.Mesh(geoSky, matSky);
meshSky.visible = false;
var geometry = new THREE.IcosahedronGeometry(1, 1);
var material = new THREE.MeshBasicMaterial({
'envMap': cubeCam.renderTarget.texture
});
var mesh = new THREE.Mesh(geometry, material);
var geoWireframe = new THREE.WireframeGeometry(geometry);
var matWireframe = new THREE.LineBasicMaterial({
'color': 'red',
'linewidth': 2
});
mesh.add(new THREE.LineSegments(geoWireframe, matWireframe));
var camera = new THREE.PerspectiveCamera(fov, aspect);
camera.position.setZ(20);
var scene = new THREE.Scene();
scene.add(mesh);
scene.add(meshSky);
{
var mirror = new THREE.CubeCamera(.1, 2000, 4096);
var geoPlane = new THREE.PlaneGeometry(16, 16);
var matPlane = new THREE.MeshBasicMaterial({
'envMap': mirror.renderTarget.texture
});
var plane = new THREE.Mesh(geoPlane, matPlane);
plane.add(mirror);
plane.position.setZ(-4);
plane.lookAt(mesh.position);
scene.add(plane);
}
var renderer = new THREE.WebGLRenderer();
var container = document.getElementById('container1');
container.appendChild(renderer.domElement);
SetResizeHandler(renderer, camera);
renderer.setSize(window.innerWidth, window.innerHeight);
var controls = new THREE.TrackballControls(camera, container);
var fixTextureWhenRotateAroundAllAxis = function() {
mesh.rotation.y += 0.01;
mesh.rotation.x += 0.01;
mesh.rotation.z += 0.01;
cubeCam.update(renderer, scene);
};
renderer.setAnimationLoop(function() {
// controls.update();
plane.visible = false;
{
meshSky.visible = true;
mesh.visible = false;
fixTextureWhenRotateAroundAllAxis();
mesh.visible = true;
meshSky.visible = false;
}
mirror.update(renderer, scene);
plane.visible = true;
renderer.render(scene, camera);
});
})();
body {
background-color: #000;
margin: 0px;
overflow: hidden;
}
<script src="https://threejs.org/build/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/TrackballControls.js"></script>
<div id='container1'></div>
Update 4
Important: Please note there is a reflective plane in back of the target mesh which is for observing if the texture binds to the mesh surface correctly, it has nothing to do with what I'm trying to solve.
Update 3
Created a new fiddle to show what is NOT the expected behaviour
- Code
var MatcapTransformer=function(uvs, face) {
for(var i=uvs.length; i-->0;) {
uvs[i].x=face.vertexNormals[i].x*0.5+0.5;
uvs[i].y=face.vertexNormals[i].y*0.5+0.5;
}
};
var TransformUv=function(geometry, xformer) {
// The first argument is also used as an array in the recursive calls
// as there's no method overloading in javascript; and so is the callback.
var a=arguments[0], callback=arguments[1];
var faceIterator=function(uvFaces, index) {
xformer(uvFaces[index], geometry.faces[index]);
};
var layerIterator=function(uvLayers, index) {
TransformUv(uvLayers[index], faceIterator);
};
for(var i=a.length; i-->0;) {
callback(a, i);
}
if(!(i<0)) {
TransformUv(geometry.faceVertexUvs, layerIterator);
}
};
var SetResizeHandler=function(renderer, camera) {
var callback=function() {
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect=window.innerWidth/window.innerHeight;
camera.updateProjectionMatrix();
};
// bind the resize event
window.addEventListener('resize', callback, false);
// return .stop() the function to stop watching window resize
return {
stop: function() {
window.removeEventListener('resize', callback);
}
};
};
var getVertexShader=function() {
return `
void main() {
gl_Position=projectionMatrix*modelViewMatrix*vec4(position, 1.0);
}
`;
};
var getFragmentShader=function(size) {
return `
uniform sampler2D texture1;
const vec2 size=vec2(`+size.x+`, `+size.y+`);
void main() {
gl_FragColor=texture2D(texture1, gl_FragCoord.xy/size.xy);
}
`;
};
(function() {
var fov=45;
var aspect=window.innerWidth/window.innerHeight;
var loader=new THREE.TextureLoader();
var texture=loader.load('https://i.postimg.cc/mTsN30vx/canyon-s.jpg');
texture.wrapS=THREE.RepeatWrapping;
texture.wrapT=THREE.RepeatWrapping;
texture.center.set(1/2, 1/2);
var geometry=new THREE.SphereGeometry(1, 16, 16);
// var geometry=new THREE.BoxGeometry(2, 2, 2);
// var material=new THREE.MeshBasicMaterial({ 'map': texture });
var material=new THREE.ShaderMaterial({
'uniforms': { 'texture1': { 'type': 't', 'value': texture } }
, 'vertexShader': getVertexShader()
, 'fragmentShader': getFragmentShader({ 'x': 512, 'y': 256 })
});
var mesh=new THREE.Mesh(geometry, material);
var geoWireframe=new THREE.WireframeGeometry(geometry);
var matWireframe=new THREE.LineBasicMaterial({ 'color': 'red', 'linewidth': 2 });
mesh.add(new THREE.LineSegments(geoWireframe, matWireframe));
var camera=new THREE.PerspectiveCamera(fov, aspect);
camera.position.setZ(20);
var scene=new THREE.Scene();
scene.add(mesh);
{
var mirror=new THREE.CubeCamera(.1, 2000, 4096);
var geoPlane=new THREE.PlaneGeometry(16, 16);
var matPlane=new THREE.MeshBasicMaterial({
'envMap': mirror.renderTarget.texture
});
var plane=new THREE.Mesh(geoPlane, matPlane);
plane.add(mirror);
plane.position.setZ(-4);
plane.lookAt(mesh.position);
scene.add(plane);
}
var renderer=new THREE.WebGLRenderer();
var container=document.getElementById('container1');
container.appendChild(renderer.domElement);
SetResizeHandler(renderer, camera);
renderer.setSize(window.innerWidth, window.innerHeight);
var fixTextureWhenRotateAroundYAxis=function() {
mesh.rotation.y+=0.01;
texture.offset.set(mesh.rotation.y/(2*Math.PI), 0);
};
var fixTextureWhenRotateAroundZAxis=function() {
mesh.rotation.z+=0.01;
texture.rotation=-mesh.rotation.z
TransformUv(geometry, MatcapTransformer);
};
var fixTextureWhenRotateAroundAllAxis=function() {
mesh.rotation.y+=0.01;
mesh.rotation.x+=0.01;
mesh.rotation.z+=0.01;
};
var controls=new THREE.TrackballControls(camera, container);
renderer.setAnimationLoop(function() {
fixTextureWhenRotateAroundAllAxis();
controls.update();
plane.visible=false;
mirror.update(renderer, scene);
plane.visible=true;
renderer.render(scene, camera);
});
})();
body {
background-color: #000;
margin: 0px;
overflow: hidden;
}
<script src="https://threejs.org/build/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/TrackballControls.js"></script>
<div id='container1'></div>
Maybe I should rephrase my question, but I lack the knowledge to describe accurately about what I'm trying to solve, please help .. (Panoramic-Transform-With-Texture-Looking-At-Direction-Locked-Onto-The-Camera maybe .. ?)
Update 2
(Has deprecated as code snippet is applied. )
Update
OK .. I've added 3 methods:
TransformUv
accepts a geometry, and a transformer method which handles uv-transform. The callback accepts an uvs array for each face and the correspondingFace3
ofgeometry.faces[]
as its parameters.MatcapTransformer
is the uv-transform handler callback to do the matcap transform.and
fixTextureWhenRotateAroundZAxis
works like what it named.
So far none of the fixTexture..
methods can work alltogether, also, fixTextureWhenRotateAroundXAxis
is not figured out. The problem remains unsolved, I wish what's just added could help you to help me out.
I'm trying to make the texture of a mesh always face an active perspective camera, no matter what are the relative positions.
For constructing a real case of my scene and the interaction would be quite complex, I built a minimal example to demonstrate my intention.
- Code
var MatcapTransformer=function(uvs, face) { for(var i=uvs.length; i-->0;) { uvs[i].x=face.vertexNormals[i].x*0.5+0.5; uvs[i].y=face.vertexNormals[i].y*0.5+0.5; } }; var TransformUv=function(geometry, xformer) { // The first argument is also used as an array in the recursive calls // as there's no method overloading in javascript; and so is the callback. var a=arguments[0], callback=arguments[1]; var faceIterator=function(uvFaces, index) { xformer(uvFaces[index], geometry.faces[index]); }; var layerIterator=function(uvLayers, index) { TransformUv(uvLayers[index], faceIterator); }; for(var i=a.length; i-->0;) { callback(a, i); } if(!(i<0)) { TransformUv(geometry.faceVertexUvs, layerIterator); } }; var SetResizeHandler=function(renderer, camera) { var callback=function() { renderer.setSize(window.innerWidth, window.innerHeight); camera.aspect=window.innerWidth/window.innerHeight; camera.updateProjectionMatrix(); }; // bind the resize event window.addEventListener('resize', callback, false); // return .stop() the function to stop watching window resize return { stop: function() { window.removeEventListener('resize', callback); } }; }; (function() { var fov=45; var aspect=window.innerWidth/window.innerHeight; var loader=new THREE.TextureLoader(); var texture=loader.load('https://i.postimg.cc/mTsN30vx/canyon-s.jpg'); texture.wrapS=THREE.RepeatWrapping; texture.wrapT=THREE.RepeatWrapping; texture.center.set(1/2, 1/2); var geometry=new THREE.SphereGeometry(1, 16, 16); var material=new THREE.MeshBasicMaterial({ 'map': texture }); var mesh=new THREE.Mesh(geometry, material); var geoWireframe=new THREE.WireframeGeometry(geometry); var matWireframe=new THREE.LineBasicMaterial({ 'color': 'red', 'linewidth': 2 }); mesh.add(new THREE.LineSegments(geoWireframe, matWireframe)); var camera=new THREE.PerspectiveCamera(fov, aspect); camera.position.setZ(20); var scene=new THREE.Scene(); scene.add(mesh); { var mirror=new THREE.CubeCamera(.1, 2000, 4096); var geoPlane=new THREE.PlaneGeometry(16, 16); var matPlane=new THREE.MeshBasicMaterial({ 'envMap': mirror.renderTarget.texture }); var plane=new THREE.Mesh(geoPlane, matPlane); plane.add(mirror); plane.position.setZ(-4); plane.lookAt(mesh.position); scene.add(plane); } var renderer=new THREE.WebGLRenderer(); var container=document.getElementById('container1'); container.appendChild(renderer.domElement); SetResizeHandler(renderer, camera); renderer.setSize(window.innerWidth, window.innerHeight); var fixTextureWhenRotateAroundYAxis=function() { mesh.rotation.y+=0.01; texture.offset.set(mesh.rotation.y/(2*Math.PI), 0); }; var fixTextureWhenRotateAroundZAxis=function() { mesh.rotation.z+=0.01; texture.rotation=-mesh.rotation.z TransformUv(geometry, MatcapTransformer); }; // This is wrong var fixTextureWhenRotateAroundAllAxis=function() { mesh.rotation.y+=0.01; mesh.rotation.x+=0.01; mesh.rotation.z+=0.01; // Dun know how to do it correctly .. texture.offset.set(mesh.rotation.y/(2*Math.PI), 0); }; var controls=new THREE.TrackballControls(camera, container); renderer.setAnimationLoop(function() { fixTextureWhenRotateAroundYAxis(); // Uncomment the following line and comment out `fixTextureWhenRotateAroundYAxis` to see the demo // fixTextureWhenRotateAroundZAxis(); // fixTextureWhenRotateAroundAllAxis(); // controls.update(); plane.visible=false; mirror.update(renderer, scene); plane.visible=true; renderer.render(scene, camera); }); })();
body { background-color: #000; margin: 0px; overflow: hidden; }
<script src="https://threejs.org/build/three.min.js"></script> <script src="https://threejs.org/examples/js/controls/TrackballControls.js"></script> <div id='container1'></div>
Please note that although the mesh itself rotates in this demonstration, my real intention is making the camera move like orbiting around the mesh.
I've added the wireframe to make the movement more clear. As you can see
I use fixTextureWhenRotateAroundYAxis
to do it correctly, but it's only for the y-axis. The mesh.rotation.y
in my real code is calculated something like
var ve=camera.position.clone();
ve.sub(mesh.position);
var rotY=Math.atan2(ve.x, ve.z);
var offsetX=rotY/(2*Math.PI);
However, I lack the knowledge of how to do fixTextureWhenRotateAroundAllAxis
correctly. There are some restrictions of solving this:
CubeCamera/CubeMap cannot be used as the client machines might have performance issues
Do not simply make the mesh
lookAt
the camera as they are eventually of any kind of geometry, not only the spheres; tricks likelookAt
and restore.quaternion
in a frame would be ok.
Please don't get me wrong that I'm asking an XY problem as I don't have the right to expose proprietary code or I wouldn't have to pay the effort to build a minimal example :)