Am working with the three.js and am created a scene with the House , which have the lots of door/windows on the wall , and inside the rooms there is some living things like chairs, sofas and so on, all of them are seperate mesh and which is combined as single House object , i got neraly what am expecting , but am facing some issue on the materials am having for the walls
i have attached 2 image which illustare my problem, also here am explain what am getting
Pic 1:
my model looks nice from the long shot of camera, i can see the wall attached with the doors/windows inside the room i can see the sofas
Pic 2:
while look into my room from the attached doors/windows on the wall, the other side wall get disappeared it looks transparent , but i can see the doors/windows on that wall
am not sure what is wrong with my wall
and my code is follows:
function wall_fill(type,no_of_walls,wall_no,inner_filltype,inner_fill,outer_filltype,outer_fill,left_filltype,left_fill,right_filltype,right_fill,top_filltype,top_fill,bottom_filltype,bottom_fill,front_filltype,front_fill,back_filltype,back_fill)
{
var materials,inner_material,outer_material,left_material,right_material,top_material,bottom_material,front_material,back_material;
// inner wall
if(inner_filltype == 'texture')
{
var inner_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/'+inner_fill );
inner_texture.anisotropy = renderer.getMaxAnisotropy();
inner_texture.wrapS = inner_texture.wrapT = THREE.RepeatWrapping;
inner_material = new THREE.MeshPhongMaterial({map: inner_texture,transparent : true,transparency : 1.0,opacity: 1.5, overdraw: 0.5});
}
else if(inner_filltype == 'color')
{
inner_fill = parseInt(inner_fill);
inner_material = new THREE.MeshBasicMaterial({color: inner_fill,transparent: false,opacity: 1.5, overdraw: 0.5,side: THREE.DoubleSide })
}
else
{
var inner_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/trans.png' );
inner_texture.anisotropy = renderer.getMaxAnisotropy();
inner_texture.wrapS = inner_texture.wrapT = THREE.RepeatWrapping;
//inner_material = new THREE.MeshPhongMaterial({map: inner_texture,transparent: true,opacity: 1.5, overdraw: 0.5 });
inner_material = new THREE.MeshBasicMaterial({color: 0xd6d6d6,transparent: true,opacity: -1.5, overdraw: 0.5 })
}
// outer wall
if(outer_filltype == 'texture')
{
var outer_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/'+outer_fill );
outer_texture.anisotropy = renderer.getMaxAnisotropy();
outer_texture.wrapS = outer_texture.wrapT = THREE.RepeatWrapping;
outer_material = new THREE.MeshPhongMaterial({map: outer_texture,transparent: true, opacity: 1.5,overdraw: 0.5, });
}
else if(outer_filltype == 'color')
{
outer_fill = parseInt(outer_fill);
outer_material = new THREE.MeshBasicMaterial({color: outer_fill,transparent: true, opacity: 1.5, overdraw: 0.5,side: THREE.DoubleSide });
}
else
{
var outer_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/trans.png');
outer_texture.anisotropy = renderer.getMaxAnisotropy();
outer_texture.wrapS = outer_texture.wrapT = THREE.RepeatWrapping;
//outer_material = new THREE.MeshPhongMaterial({map: outer_texture,transparent: true, opacity: 1.5,overdraw: 0.5 });
outer_material = new THREE.MeshBasicMaterial({color: 0xd6d6d6,transparent: true, opacity: -1.5, overdraw: 0.5 });
}
// left wall
if(left_filltype == 'texture')
{
var left_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/'+left_fill );
left_texture.anisotropy = renderer.getMaxAnisotropy();
left_texture.wrapS = left_texture.wrapT = THREE.RepeatWrapping;
left_material = new THREE.MeshPhongMaterial({map: left_texture,transparent: true, opacity: 1.5,overdraw: 0.5 });
}
else if(left_filltype == 'color')
{
left_fill = parseInt(left_fill);
left_material = new THREE.MeshBasicMaterial({color: left_fill,transparent: true, opacity: 1.5, overdraw: 0.5,side: THREE.DoubleSide });
}
else
{
var left_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/trans.png');
left_texture.anisotropy = renderer.getMaxAnisotropy();
left_texture.wrapS = left_texture.wrapT = THREE.RepeatWrapping;
//left_material = new THREE.MeshPhongMaterial({map: left_texture,transparent: true, opacity: 1.5,overdraw: 0.5 });
left_material = new THREE.MeshBasicMaterial({color: 0xd6d6d6,transparent: true, opacity: -1.5, overdraw: 0.5 });
}
// right wall
if(right_filltype == 'texture')
{
var right_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/'+right_fill );
right_texture.anisotropy = renderer.getMaxAnisotropy();
right_texture.wrapS = right_texture.wrapT = THREE.RepeatWrapping;
right_material = new THREE.MeshPhongMaterial({map: right_texture,transparent: true, opacity: 1.5,overdraw: 0.5 });
}
else if(right_filltype == 'color')
{
right_fill = parseInt(right_fill);
right_material = new THREE.MeshBasicMaterial({color: right_fill,transparent: true, opacity: 1.5, overdraw: 0.5,side: THREE.DoubleSide });
}
else
{
var right_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/trans.png');
right_texture.anisotropy = renderer.getMaxAnisotropy();
right_texture.wrapS = right_texture.wrapT = THREE.RepeatWrapping;
//right_material = new THREE.MeshPhongMaterial({map: right_texture,transparent: true, opacity: 1.5,overdraw: 0.5 });
right_material = new THREE.MeshBasicMaterial({color: 0xd6d6d6,transparent: true, opacity: -1.5, overdraw: 0.5 });
}
// top wall
if(top_filltype == 'texture')
{
var top_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/'+top_fill );
top_texture.anisotropy = renderer.getMaxAnisotropy();
top_texture.wrapS = top_texture.wrapT = THREE.RepeatWrapping;
top_material = new THREE.MeshPhongMaterial({map: top_texture,transparent: true, opacity: 1.5,overdraw: 0.5 });
}
else if(top_filltype == 'color')
{
top_fill = parseInt(top_fill);
top_material = new THREE.MeshBasicMaterial({color: top_fill,transparent: true, opacity: 1.5, overdraw: 0.5,side: THREE.DoubleSide });
}
else
{
var top_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/trans.png');
top_texture.anisotropy = renderer.getMaxAnisotropy();
top_texture.wrapS = top_texture.wrapT = THREE.RepeatWrapping;
//top_material = new THREE.MeshPhongMaterial({map: top_texture,transparent: true, opacity: 1.5,overdraw: 0.5 });
top_material = new THREE.MeshBasicMaterial({color: 0xd6d6d6,transparent: true, opacity: -1.5, overdraw: 0.5 });
}
// bottom wall
if(bottom_filltype == 'texture')
{
var bottom_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/'+bottom_fill );
bottom_texture.anisotropy = renderer.getMaxAnisotropy();
bottom_texture.wrapS = bottom_texture.wrapT = THREE.RepeatWrapping;
bottom_material = new THREE.MeshPhongMaterial({map: bottom_texture,transparent: true, opacity: 1.5,overdraw: 0.5 });
}
else if(bottom_filltype == 'color')
{
bottom_fill = parseInt(bottom_fill);
bottom_material = new THREE.MeshBasicMaterial({color: bottom_fill,transparent: true, opacity: 1.5, overdraw: 0.5,side: THREE.DoubleSide });
}
else
{
var bottom_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/trans.png');
bottom_texture.anisotropy = renderer.getMaxAnisotropy();
bottom_texture.wrapS = bottom_texture.wrapT = THREE.RepeatWrapping;
//bottom_material = new THREE.MeshPhongMaterial({map: bottom_texture,transparent: true, opacity: 1.5,overdraw: 0.5 });
bottom_material = new THREE.MeshBasicMaterial({color: 0xd6d6d6,transparent: true, opacity: -1.5, overdraw: 0.5 });
}
// front wall
if(front_filltype == 'texture')
{
var front_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/'+front_fill );
front_texture.anisotropy = renderer.getMaxAnisotropy();
front_texture.wrapS = front_texture.wrapT = THREE.RepeatWrapping;
front_material = new THREE.MeshPhongMaterial({map: front_texture,transparent: true, opacity: 1.5,overdraw: 0.5 });
}
else if(front_filltype == 'color')
{
front_fill = parseInt(front_fill);
front_material = new THREE.MeshBasicMaterial({color: front_fill,transparent: true, opacity: 1.5, overdraw: 0.5 ,side: THREE.DoubleSide});
}
else
{
var front_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/trans.png');
front_texture.anisotropy = renderer.getMaxAnisotropy();
front_texture.wrapS = front_texture.wrapT = THREE.RepeatWrapping;
//front_material = new THREE.MeshPhongMaterial({map: front_texture,transparent: true, opacity: 1.5,overdraw: 0.5 });
front_material = new THREE.MeshBasicMaterial({color: 0xd6d6d6,transparent: true, opacity: -1.5, overdraw: 0.5 });
}
// back wall
if(back_filltype == 'texture')
{
var back_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/'+back_fill );
back_texture.anisotropy = renderer.getMaxAnisotropy();
back_texture.wrapS = back_texture.wrapT = THREE.RepeatWrapping;
back_material = new THREE.MeshPhongMaterial({map: back_texture,transparent: true, opacity: 1.5,overdraw: 0.5 });
}
else if(back_filltype == 'color')
{
back_fill = parseInt(back_fill);
back_material = new THREE.MeshBasicMaterial({color: back_fill,transparent: true, opacity: 1.5, overdraw: 0.5 ,side: THREE.DoubleSide});
}
else
{
var back_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/trans.png');
back_texture.anisotropy = renderer.getMaxAnisotropy();
back_texture.wrapS = back_texture.wrapT = THREE.RepeatWrapping;
//back_material = new THREE.MeshPhongMaterial({map: back_texture,transparent: true, opacity: 1.5,overdraw: 0.5 });
back_material = new THREE.MeshBasicMaterial({color: 0xd6d6d6,transparent: true, opacity: -1.5, overdraw: 0.5 });
}
var left,right,top,bottom,front,back;
if( wall_no == 1)
{
left = left_material;
right = right_material;
top = top_material;
bottom = bottom_material;
front = inner_material;
back = outer_material;
}
if( wall_no == 2)
{
left = outer_material;
right = inner_material;
top = top_material;
bottom = bottom_material;
front = front_material;
back = back_material;
}
if( wall_no == 3)
{
left = left_material;
right = right_material;
top = top_material;
bottom = bottom_material;
front = outer_material;
back = inner_material;
}
if( wall_no == 4)
{
left = inner_material;
right = outer_material;
top = top_material;
bottom = bottom_material;
front = front_material;
back = back_material;
}
materials = [
left, // Left side
right, // Right side
top, // Top side
bottom,// Bottom side
front, // Front side
back // Back side
];
var material = new THREE.MeshFaceMaterial(materials);
return material;
}
Updated function
function wall_fill(type,no_of_walls,wall_no,inner_filltype,inner_fill,outer_filltype,outer_fill,left_filltype,left_fill,right_filltype,right_fill,top_filltype,top_fill,bottom_filltype,bottom_fill,front_filltype,front_fill,back_filltype,back_fill)
{
var materials,inner_material,outer_material,left_material,right_material,top_material,bottom_material,front_material,back_material;
// inner wall
if(inner_filltype == 'texture')
{
var inner_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/'+inner_fill );
inner_texture.anisotropy = renderer.getMaxAnisotropy();
inner_texture.wrapS = inner_texture.wrapT = THREE.RepeatWrapping;
inner_material = new THREE.MeshBasicMaterial({map: inner_texture,transparent : true, opacity:0.5,side: THREE.DoubleSide });
}
else if(inner_filltype == 'color')
{
inner_fill = parseInt(inner_fill);
inner_material = new THREE.MeshBasicMaterial({color: inner_fill,transparent: true,opacity:0.5 })
}
else
{
var inner_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/trans.png' );
inner_texture.anisotropy = renderer.getMaxAnisotropy();
inner_texture.wrapS = inner_texture.wrapT = THREE.RepeatWrapping;
//inner_material = new THREE.MeshBasicMaterial({map: inner_texture,transparent: true });
inner_material = new THREE.MeshBasicMaterial({color: 0xd6d6d6,transparent: true,opacity:0.5 })
}
// outer wall
if(outer_filltype == 'texture')
{
var outer_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/'+outer_fill );
outer_texture.anisotropy = renderer.getMaxAnisotropy();
outer_texture.wrapS = outer_texture.wrapT = THREE.RepeatWrapping;
outer_material = new THREE.MeshBasicMaterial({map: outer_texture,transparent: true,opacity:0.5,side: THREE.DoubleSide});
}
else if(outer_filltype == 'color')
{
outer_fill = parseInt(outer_fill);
outer_material = new THREE.MeshBasicMaterial({color: outer_fill,transparent: true, opacity:0.5});
}
else
{
var outer_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/trans.png');
outer_texture.anisotropy = renderer.getMaxAnisotropy();
outer_texture.wrapS = outer_texture.wrapT = THREE.RepeatWrapping;
//outer_material = new THREE.MeshBasicMaterial({map: outer_texture,transparent: true });
outer_material = new THREE.MeshBasicMaterial({color: 0xd6d6d6,transparent: true ,opacity:0.5});
}
// left wall
if(left_filltype == 'texture')
{
var left_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/'+left_fill );
left_texture.anisotropy = renderer.getMaxAnisotropy();
left_texture.wrapS = left_texture.wrapT = THREE.RepeatWrapping;
left_material = new THREE.MeshBasicMaterial({map: left_texture,transparent: true ,opacity:0.5});
}
else if(left_filltype == 'color')
{
left_fill = parseInt(left_fill);
left_material = new THREE.MeshBasicMaterial({color: left_fill,transparent: true,opacity:0.5 });
}
else
{
var left_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/trans.png');
left_texture.anisotropy = renderer.getMaxAnisotropy();
left_texture.wrapS = left_texture.wrapT = THREE.RepeatWrapping;
//left_material = new THREE.MeshBasicMaterial({map: left_texture,transparent: true });
left_material = new THREE.MeshBasicMaterial({color: 0xd6d6d6,transparent: true,opacity:0.5 });
}
// right wall
if(right_filltype == 'texture')
{
var right_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/'+right_fill );
right_texture.anisotropy = renderer.getMaxAnisotropy();
right_texture.wrapS = right_texture.wrapT = THREE.RepeatWrapping;
right_material = new THREE.MeshBasicMaterial({map: right_texture,transparent: true ,opacity:0.5});
}
else if(right_filltype == 'color')
{
right_fill = parseInt(right_fill);
right_material = new THREE.MeshBasicMaterial({color: right_fill,transparent: true,opacity:0.5 });
}
else
{
var right_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/trans.png');
right_texture.anisotropy = renderer.getMaxAnisotropy();
right_texture.wrapS = right_texture.wrapT = THREE.RepeatWrapping;
//right_material = new THREE.MeshBasicMaterial({map: right_texture,transparent: true });
right_material = new THREE.MeshBasicMaterial({color: 0xd6d6d6,transparent: true,opacity:0.5});
}
// top wall
if(top_filltype == 'texture')
{
var top_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/'+top_fill );
top_texture.anisotropy = renderer.getMaxAnisotropy();
top_texture.wrapS = top_texture.wrapT = THREE.RepeatWrapping;
top_material = new THREE.MeshBasicMaterial({map: top_texture,transparent: true,opacity:0.5 });
}
else if(top_filltype == 'color')
{
top_fill = parseInt(top_fill);
top_material = new THREE.MeshBasicMaterial({color: top_fill,transparent: true,opacity:0.5});
}
else
{
var top_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/trans.png');
top_texture.anisotropy = renderer.getMaxAnisotropy();
top_texture.wrapS = top_texture.wrapT = THREE.RepeatWrapping;
//top_material = new THREE.MeshBasicMaterial({map: top_texture,transparent: true });
top_material = new THREE.MeshBasicMaterial({color: 0xd6d6d6,transparent: true,opacity:0.5 });
}
// bottom wall
if(bottom_filltype == 'texture')
{
var bottom_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/'+bottom_fill );
bottom_texture.anisotropy = renderer.getMaxAnisotropy();
bottom_texture.wrapS = bottom_texture.wrapT = THREE.RepeatWrapping;
bottom_material = new THREE.MeshBasicMaterial({map: bottom_texture,transparent: true,opacity:0.5 });
}
else if(bottom_filltype == 'color')
{
bottom_fill = parseInt(bottom_fill);
bottom_material = new THREE.MeshBasicMaterial({color: bottom_fill,transparent: true, opacity:0.5});
}
else
{
var bottom_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/trans.png');
bottom_texture.anisotropy = renderer.getMaxAnisotropy();
bottom_texture.wrapS = bottom_texture.wrapT = THREE.RepeatWrapping;
//bottom_material = new THREE.MeshBasicMaterial({map: bottom_texture,transparent: true });
bottom_material = new THREE.MeshBasicMaterial({color: 0xd6d6d6,transparent: true,opacity:0.5 });
}
// front wall
if(front_filltype == 'texture')
{
var front_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/'+front_fill );
front_texture.anisotropy = renderer.getMaxAnisotropy();
front_texture.wrapS = front_texture.wrapT = THREE.RepeatWrapping;
front_material = new THREE.MeshBasicMaterial({map: front_texture,transparent: true,opacity:0.5 });
}
else if(front_filltype == 'color')
{
front_fill = parseInt(front_fill);
front_material = new THREE.MeshBasicMaterial({color: front_fill,transparent: true ,opacity:0.5});
}
else
{
var front_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/trans.png');
front_texture.anisotropy = renderer.getMaxAnisotropy();
front_texture.wrapS = front_texture.wrapT = THREE.RepeatWrapping;
//front_material = new THREE.MeshBasicMaterial({map: front_texture,transparent: true });
front_material = new THREE.MeshBasicMaterial({color: 0xd6d6d6,transparent: true,opacity:0.5 });
}
// back wall
if(back_filltype == 'texture')
{
var back_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/'+back_fill );
back_texture.anisotropy = renderer.getMaxAnisotropy();
back_texture.wrapS = back_texture.wrapT = THREE.RepeatWrapping;
back_material = new THREE.MeshBasicMaterial({map: back_texture,transparent: true,opacity:0.5, });
}
else if(back_filltype == 'color')
{
back_fill = parseInt(back_fill);
back_material = new THREE.MeshBasicMaterial({color: back_fill,transparent: true ,opacity:0.5});
}
else
{
var back_texture = THREE.ImageUtils.loadTexture( baseurl+'uploads/textures/'+type+'/trans.png');
back_texture.anisotropy = renderer.getMaxAnisotropy();
back_texture.wrapS = back_texture.wrapT = THREE.RepeatWrapping;
//back_material = new THREE.MeshBasicMaterial({map: back_texture,transparent: true });
back_material = new THREE.MeshBasicMaterial({color: 0xd6d6d6,transparent: true ,opacity:0.5});
}
var left,right,top,bottom,front,back;
if( wall_no == 1)
{
left = left_material;
right = right_material;
top = top_material;
bottom = bottom_material;
front = inner_material;
back = outer_material;
}
if( wall_no == 2)
{
left = outer_material;
right = inner_material;
top = top_material;
bottom = bottom_material;
front = front_material;
back = back_material;
}
if( wall_no == 3)
{
left = left_material;
right = right_material;
top = top_material;
bottom = bottom_material;
front = outer_material;
back = inner_material;
}
if( wall_no == 4)
{
left = inner_material;
right = outer_material;
top = top_material;
bottom = bottom_material;
front = front_material;
back = back_material;
}
materials = [
left, // Left side
right, // Right side
top, // Top side
bottom,// Bottom side
front, // Front side
back // Back side
];
var material = new THREE.MeshFaceMaterial(materials);
return material;
}
still am getting the same set of result
What I have done in my THREE.js application is disable transparency completely on all your materials. Then I enable "alphaTest" parameter on my materials. What this will do is disable smooth alpha, and use a cut-off value for either transparent, or not transparent.
Here are my settings for materials:
specular: new THREE.Color( 0x101010 ),
shininess: 40,
alphaTest: 0.15,
color: new THREE.Color( 0xffffff ),
metal: true,
wrapAround: true,
side: THREE.DoubleSide
With this it is safe to set transparent: false.
If none of your objects are semi-transparent this will solve your issue. If they are semi-transparent, you have to implement much more complicated algorithms to draw transparency properly (see depth-peeling, or other methods such as drawing objects in specific orderings).
Related
I've changed the version of my three.js because i wanted to use projector but now all my texture went white. I can't find what is wrong with it. I'm not getting any error in the console, which makes me a bit lost.
I was using r122.
Any body know what is happening ?
most of the code here comes from a code pen :
https://codepen.io/yitliu/pen/bJoQLw
//----------VARIABLES----------
const dpi = window.devicePixelRatio;
const theCanvas = document.getElementById('canvas');
var h = window.innerHeight,
w = window.innerWidth;
aspectRatio = w / h,
fieldOfView = 25,
nearPlane = .1,
farPlane = 1000;
container = document.getElementById('container');
var dae, scene, camera, renderer;
var Colors = {
cyan: 0x248079,
brown: 0xA98F78,
brownDark: 0x9A6169,
green: 0x65BB61,
greenLight: 0xABD66A,
blue: 0x6BC6FF
};
function init() {
//----------SCENE----------
scene = new THREE.Scene();
//----------CAMERA----------
camera = new THREE.PerspectiveCamera(
fieldOfView,
aspectRatio,
nearPlane,
farPlane);
//----------RENDER----------
renderer = new THREE.WebGLRenderer({ canvas: canvas, alpha: true, antialias: true });
renderer.setSize(w * dpi, h * dpi);
theCanvas.style.width = `${w}px`;
theCanvas.style.height = `${h}px`;
renderer.shadowMapEnabled = true;
renderer.shadowMapType = THREE.PCFSoftShadowMap;
document.body.appendChild(renderer.domElement);
camera.position.set(-5, 6, 8);
camera.lookAt(new THREE.Vector3(0, 0, 0));
//---------LIGHT---------
var light = new THREE.AmbientLight(0xffffff, .5);
var shadowLight = new THREE.DirectionalLight(0xffffff, .5);
shadowLight.position.set(200, 200, 200);
shadowLight.castShadow = true;
var backLight = new THREE.DirectionalLight(0xffffff, .2);
backLight.position.set(-100, 200, 50);
backLight.castShadow = true;
scene.add(backLight);
scene.add(light);
scene.add(shadowLight);
//---------CONTROLS---------
const controls = new THREE.OrbitControls(camera, renderer.domElement);
//---------GEOMETRY---------
var geometry_left = new THREE.CubeGeometry(2, .2, 4);
var material_grass = new THREE.MeshLambertMaterial({ color: Colors.greenLight });
var ground_left = new THREE.Mesh(geometry_left, material_grass);
ground_left.position.set(-1, 0.1, 0);
ground_left.receiveShadow = true;
scene.add(ground_left);
var geometry_bot = new THREE.CubeGeometry(4, .2, 1);
var material_grass = new THREE.MeshLambertMaterial({ color: Colors.greenLight });
var ground_bot = new THREE.Mesh(geometry_bot, material_grass);
ground_bot.position.set(0, 0.1, 2);
ground_bot.receiveShadow = true;
scene.add(ground_bot);
var geometry_top = new THREE.CubeGeometry(4, .2, 1);
var material_grass = new THREE.MeshLambertMaterial({ color: Colors.greenLight });
var ground_top = new THREE.Mesh(geometry_top, material_grass);
ground_top.position.set(0, 0.1, -2);
ground_top.receiveShadow = true;
scene.add(ground_top);
var geometry_river = new THREE.CubeGeometry(1, .1, 4);
var material_river = new THREE.MeshLambertMaterial({ color: Colors.blue });
var river = new THREE.Mesh(geometry_river, material_river);
river.position.set(.5, .1, 0);
river.receiveShadow = true;
scene.add(river);
var geometry_bed = new THREE.CubeGeometry(1, .05, 4);
var bed = new THREE.Mesh(geometry_bed, material_grass);
bed.position.set(.5, .025, 0);
scene.add(bed);
var geometry_right = new THREE.CubeGeometry(1, .2, 4);
var ground_right = new THREE.Mesh(geometry_right, material_grass);
ground_right.position.set(1.5, 0.1, 0);
ground_right.receiveShadow = true;
scene.add(ground_right);
var tree = function (x, z) {
this.x = x;
this.z = z;
var material_trunk = new THREE.MeshLambertMaterial({ color: Colors.brownDark });
var geometry_trunk = new THREE.CubeGeometry(.15, .15, .15);
var trunk = new THREE.Mesh(geometry_trunk, material_trunk);
trunk.position.set(this.x, .275, this.z);
trunk.castShadow = true;
trunk.receiveShadow = true;
scene.add(trunk);
var geometry_leaves = new THREE.CubeGeometry(.25, .4, .25);
var material_leaves = new THREE.MeshLambertMaterial({ color: Colors.green });
var leaves = new THREE.Mesh(geometry_leaves, material_leaves);
leaves.position.set(this.x, .2 + .15 + .4 / 2, this.z);
leaves.castShadow = true;
scene.add(leaves);
}
tree(-1.5, -1.5);
//tree(-1.25,.75);
tree(-.25, -.85);
tree(0.4, 2);
//tree(1.25,-2);
tree(1.75, .35);
var material_wood = new THREE.MeshLambertMaterial({ color: Colors.brown });
var geometry_block = new THREE.CubeGeometry(1.3, .02, .6);
var block = new THREE.Mesh(geometry_block, material_wood);
block.position.set(.5, .21, .2);
block.castShadow = true;
block.receiveShadow = true;
scene.add(block);
//---------CAR---------
var loader = new THREE.ColladaLoader();
loader.load('offroadcar.dae', function (collada) {
dae = collada.scene;
dae.scale.x = dae.scale.y = dae.scale.z = 0.1;
dae.position.set(-1, .2, 0);
dae.updateMatrix();
//customizeShadow(dae, .25)
scene.add(dae);
dae.castShadow = true;
dae.receiveShadow = true;
})
}// end of init
//---------STARTING---------
init();
animate();
//---------LISTENERS---------
theCanvas.addEventListener('click', function (evt) {
clickInfo.userHasClicked = true;
clickInfo.x = evt.clientX - theCanvas.offsetLeft;
clickInfo.y = evt.clientY - theCanvas.offsetTop;
}, false);
//---------METHODS---------
function animate() {
renderer.render(scene, camera);
requestAnimationFrame(function () { animate(); });
}
I'm pretty new to this and I'm trying to create lightning and shadows on the objects I just made. As you can see on the picture of the result bellow none of them actually cast or recieve shadows.
I've already enable shadow mapping for the renderer, and also enabled shadow casting and receiving for all of my objects. What am I doing wrong?
<head>
<meta charset=utf-8>
<title>Three.js Object Tester</title>
<style>
body { margin: 0; overflow: hidden; }
canvas { width: 100%; height: 100% }
</style>
</head>
<body>
<script type="module">
import * as THREE from './js-r119/build/three.module.js';
import { TrackballControls } from './js-r119/examples/jsm/controls/TrackballControls.js';
var WIDTH, HEIGHT, aspectRatio;
var renderer;
var scene, camera;
var controls;
var mesh;
init();
animate();
function init() {
HEIGHT = window.innerHeight;
WIDTH = window.innerWidth;
aspectRatio = WIDTH / HEIGHT;
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( WIDTH, HEIGHT );
renderer.setClearColor( 0x000000 );
renderer.shadowMap.enabled = true;
document.body.appendChild( renderer.domElement );
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 75, aspectRatio, 0.1, 1000 );
camera.position.set( 0, 40, 80 );
camera.lookAt( scene.position );
var light = new THREE.AmbientLight( 0xF5F5F3 ); //ambiens fény
scene.add( light );
var floorgeometry = new THREE.PlaneGeometry( 150, 150, 1, 1 );
var floormaterial = new THREE.MeshLambertMaterial( { color: 0xFFFFFF, wireframe: false } );
var floor = new THREE.Mesh( floorgeometry, floormaterial );
floor.material.side = THREE.DoubleSide;
floor.rotation.x = 1.57;
floor.position.z = 0;
floor.position.x = 0;
floor.position.y = -42.5;
floor.receiveShadow = true;
floor.castShadow = true;
scene.add( floor );
var vertgeometry = new THREE.BoxGeometry( 20, 45, 20);
var vertmaterial = new THREE.MeshLambertMaterial( { color: 0xD5D8DC, wireframe: false } );
var vert = new THREE.Mesh( vertgeometry, vertmaterial );
vert.castShadow = true;
vert.receiveShadow = true;
vert.rotation.z = 0;
vert.rotation.x = 0;
vert.rotation.y = 0;
vert.position.z = 0;
vert.position.x = 0;
vert.position.y = -20;
var horgeometry = new THREE.BoxGeometry( 20, 40, 20);
var hormaterial = new THREE.MeshLambertMaterial( { color: 0xD5D8DC, wireframe: false } );
var hor = new THREE.Mesh( horgeometry, hormaterial );
hor.castShadow = true;
hor.position.z = 0;
hor.position.y = -32.5;
hor.position.x = 30;
hor.rotation.z = 1.57;
hor.rotation.x = 0;
hor.rotation.y = 0;
scene.add( hor );
var roofgeometry = new THREE.ConeGeometry( 14.142, 40, 4);
var roofmaterial = new THREE.MeshLambertMaterial( { color: 0xF98E76, wireframe: false } );
var roof = new THREE.Mesh( roofgeometry, roofmaterial );
roof.castShadow = true;
roof.position.z = 0;
roof.position.y = 22.5;
roof.position.x = 0;
roof.rotation.z = 0;
roof.rotation.x = 0;
roof.rotation.y = 0.775;
scene.add( roof );
scene.add( vert );
var lampgeometry = new THREE.BoxGeometry( 2, 25, 2);
var lampmaterial = new THREE.MeshLambertMaterial( { color: 0x566573, wireframe: false } );
var lamp = new THREE.Mesh( lampgeometry, lampmaterial );
lamp.castShadow = true;
lamp.rotation.z = 0;
lamp.rotation.y = 0;
lamp.position.z = 0;
lamp.position.x = -60;
lamp.position.y = -30;
var spotgeometry = new THREE.BoxGeometry( 3, 3, 3);
var spotmaterial = new THREE.MeshLambertMaterial( { color: 0xF6F50B, wireframe: false } );
var spot = new THREE.Mesh( spotgeometry, spotmaterial );
spot.position.z = 0;
spot.position.y = -17.5;
spot.position.x = -60;
scene.add( lamp );
scene.add( spot );
var geometry = new THREE.SphereGeometry( 200, 20, 20);
var appearence = new THREE.MeshLambertMaterial ({
color: 0xa2a7a9,
wireframe: false
});
var objgeometry = new THREE.BoxGeometry(8,12,8);
var objmaterial = new THREE.MeshLambertMaterial({color: 0x1C1C03, wireframe: false});
var obj = new THREE.Mesh(objgeometry, objmaterial);
obj.castShadow = true;
obj.receiveShadow = true;
obj.position.z = 0;
obj.position.x = -40;
obj.position.y = -36.5;
scene.add(obj);
var sLight = new THREE.SpotLight( 0xF6F50B, 3 ); // spotfény segédgeometriával
sLight.position.set( -60, -17.5, 0 );
sLight.castShadow = true;
sLight.distance = 100;
sLight.target = obj;
sLight.castShadow = true;
sLight.shadow.mapSize.width = 2048;
sLight.shadow.mapSize.height = 2048;
scene.add( sLight );
var spotLightHelper = new THREE.SpotLightHelper( sLight );
scene.add( spotLightHelper );
window.addEventListener( 'resize', handleWindowResize, false );
controls = new TrackballControls( camera, renderer.domElement );
controls.rotateSpeed = 5.0;
controls.panSpeed = 1.0;
}
function handleWindowResize() {
HEIGHT = window.innerHeight;
WIDTH = window.innerWidth;
console.log( 'WIDTH=' + WIDTH + '; HEIGHT=' + HEIGHT );
renderer.setSize( WIDTH, HEIGHT );
aspectRatio = WIDTH / HEIGHT;
camera.aspect = aspectRatio;
camera.updateProjectionMatrix();
render();
}
function animate() {
requestAnimationFrame( animate );
controls.update();
render();
}
function render() {
renderer.render( scene, camera );
}
</script>
</body>
</html>
If you place a spot light in your scene, you have to ensure that no shadow casting meshes block the emissions of the lights. This happens in your app since the light is place "inside" the lamp mesh. Update code:
var WIDTH, HEIGHT, aspectRatio;
var renderer;
var scene, camera;
var controls;
var mesh, spotLightHelper;
init();
animate();
function init() {
HEIGHT = window.innerHeight;
WIDTH = window.innerWidth;
aspectRatio = WIDTH / HEIGHT;
renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setSize(WIDTH, HEIGHT);
renderer.setClearColor(0x000000);
renderer.shadowMap.enabled = true;
document.body.appendChild(renderer.domElement);
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(75, aspectRatio, 0.1, 1000);
camera.position.set(0, 40, 80);
camera.lookAt(scene.position);
var light = new THREE.AmbientLight(0xF5F5F3, 0.4); //ambiens fény
scene.add(light);
var floorgeometry = new THREE.PlaneGeometry(150, 150, 1, 1);
var floormaterial = new THREE.MeshPhongMaterial({
color: 0xFFFFFF,
wireframe: false
});
var floor = new THREE.Mesh(floorgeometry, floormaterial);
//floor.material.side = THREE.DoubleSide;
floor.rotation.x = -Math.PI * 0.5;
floor.position.z = 0;
floor.position.x = 0;
floor.position.y = -42.5;
floor.receiveShadow = true;
scene.add(floor);
var vertgeometry = new THREE.BoxGeometry(20, 45, 20);
var vertmaterial = new THREE.MeshPhongMaterial({
color: 0xD5D8DC,
wireframe: false
});
var vert = new THREE.Mesh(vertgeometry, vertmaterial);
vert.castShadow = true;
vert.receiveShadow = true;
vert.rotation.z = 0;
vert.rotation.x = 0;
vert.rotation.y = 0;
vert.position.z = 0;
vert.position.x = 0;
vert.position.y = -20;
var horgeometry = new THREE.BoxGeometry(20, 40, 20);
var hormaterial = new THREE.MeshPhongMaterial({
color: 0xD5D8DC,
wireframe: false
});
var hor = new THREE.Mesh(horgeometry, hormaterial);
hor.castShadow = true;
hor.position.z = 0;
hor.position.y = -32.5;
hor.position.x = 30;
hor.rotation.z = 1.57;
hor.rotation.x = 0;
hor.rotation.y = 0;
scene.add(hor);
var roofgeometry = new THREE.ConeGeometry(14.142, 40, 4);
var roofmaterial = new THREE.MeshPhongMaterial({
color: 0xF98E76,
wireframe: false
});
var roof = new THREE.Mesh(roofgeometry, roofmaterial);
roof.castShadow = true;
roof.position.z = 0;
roof.position.y = 22.5;
roof.position.x = 0;
roof.rotation.z = 0;
roof.rotation.x = 0;
roof.rotation.y = 0.775;
scene.add(roof);
scene.add(vert);
var lampgeometry = new THREE.BoxGeometry(2, 25, 2);
var lampmaterial = new THREE.MeshPhongMaterial({
color: 0x566573,
wireframe: false
});
var lamp = new THREE.Mesh(lampgeometry, lampmaterial);
lamp.rotation.z = 0;
lamp.rotation.y = 0;
lamp.position.z = 0;
lamp.position.x = -60;
lamp.position.y = -30;
var spotgeometry = new THREE.BoxGeometry(3, 3, 3);
var spotmaterial = new THREE.MeshPhongMaterial({
color: 0xF6F50B,
wireframe: false
});
var spot = new THREE.Mesh(spotgeometry, spotmaterial);
spot.position.z = 0;
spot.position.y = -17.5;
spot.position.x = -60;
scene.add(lamp);
scene.add(spot);
var geometry = new THREE.SphereGeometry(200, 20, 20);
var appearence = new THREE.MeshPhongMaterial({
color: 0xa2a7a9,
wireframe: false
});
var objgeometry = new THREE.BoxGeometry(8, 12, 8);
var objmaterial = new THREE.MeshPhongMaterial({
color: 0x1C1C03,
wireframe: false
});
var obj = new THREE.Mesh(objgeometry, objmaterial);
obj.castShadow = true;
obj.receiveShadow = true;
obj.position.z = 0;
obj.position.x = -40;
obj.position.y = -36.5;
scene.add(obj);
var sLight = new THREE.SpotLight(0xF6F50B, 1); // spotfény segédgeometriával
sLight.position.set(-60, -17.5, 0);
sLight.castShadow = true;
sLight.distance = 100;
sLight.target = obj;
sLight.angle = Math.PI * 0.2;
sLight.shadow.camera.near = 0.1;
sLight.shadow.camera.far = 100;
sLight.shadow.mapSize.width = 2048;
sLight.shadow.mapSize.height = 2048;
scene.add(sLight);
spotLightHelper = new THREE.SpotLightHelper(sLight);
scene.add(spotLightHelper);
var cameraHelper = new THREE.CameraHelper(sLight.shadow.camera);
scene.add(cameraHelper)
window.addEventListener('resize', handleWindowResize, false);
}
function handleWindowResize() {
HEIGHT = window.innerHeight;
WIDTH = window.innerWidth;
renderer.setSize(WIDTH, HEIGHT);
aspectRatio = WIDTH / HEIGHT;
camera.aspect = aspectRatio;
camera.updateProjectionMatrix();
render();
}
function animate() {
requestAnimationFrame(animate);
spotLightHelper.update();
render();
}
function render() {
renderer.render(scene, camera);
}
body {
margin: 0;
}
canvas {
display: block;
}
<script src="https://cdn.jsdelivr.net/npm/three#0.121.1/build/three.js"></script>
BTW: When using SpotLightHelper, makes sure to update this helper in the animation loop. Besides, CameraHelper is useful for debugging the shadow frustum.
I am loading a building model to the scene using GLTFLoader and apply some texture, also Adding two PointLight source with shadow enabled. I am using self shadow for the building, that is the building itself should create shadow from light source.
But when I load the page, I am having some performance issue withe page, the frame rate is too slow on status and entire system behave hang.
But if disable the shadow everything work as expected. Is this expected or I can improve it on coding.
Here is the working fiddle
http://jsfiddle.net/n9eg3kox/
var camera, scene, renderer, stats;
var mesh;
var controls;
var BuildingObj;
var OutFloor;
var enableShadow = true; // if I make it false the code work fine
var pointLight2, pointLight3;
function init() {
var webglEl = document.getElementById('webgl');
if (!Detector.webgl) {
Detector.addGetWebGLMessage(webglEl);
alert("WebGL no Support");
return;
}
THREE.ImageUtils.crossOrigin = '';
renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true });
renderer.setPixelRatio(window.devicePixelRatio * 1.5);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.physicallyCorrectLights = true;
renderer.gammaInput = true;
renderer.gammaOutput = true;
renderer.shadowMap.enabled = enableShadow;
renderer.toneMapping = THREE.ReinhardToneMapping;
webglEl.appendChild(renderer.domElement);
renderer.setClearColor(0x555555, 1);
var width = window.innerWidth, height = window.innerHeight;
camera = new THREE.PerspectiveCamera(45, width / height, 0.01, 2000);
camera.position.x = 0;
camera.position.y = 4;
camera.position.z = 30;
scene = new THREE.Scene();
var axesHelper = new THREE.AxesHelper(100);
scene.add(axesHelper);
var intensity = 5;
var distance = 40;
var decay = 1.0;
pointLight2 = new THREE.PointLight(0xFFFFFF, intensity);
pointLight2.add(new THREE.Mesh(new THREE.SphereBufferGeometry(3, 16, 16), new THREE.MeshPhongMaterial({ color: 0xfffdfb, emissive: 0xfffdfb, emissiveIntensity: 100 })));
pointLight2.position.set(-60, 80, 40);
pointLight2.castShadow = enableShadow;
pointLight2.visible = true;
pointLight2.shadow.mapSize.width = 1024;
pointLight2.shadow.mapSize.height = 1024;
scene.add(pointLight2);
pointLight3 = new THREE.PointLight(0xFFFFFF, intensity);
pointLight3.add(new THREE.Mesh(new THREE.SphereBufferGeometry(3, 16, 16), new THREE.MeshPhongMaterial({ color: 0xfffdfb, emissive: 0xfffdfb, emissiveIntensity: 100 })));
pointLight3.position.set(60, 80, 40);
pointLight3.castShadow = enableShadow;
pointLight3.shadow.mapSize.width = 1024;
pointLight3.shadow.mapSize.height = 1024;
pointLight3.visible = true;
scene.add(pointLight3);
//var light = new THREE.HemisphereLight( 0xffffbb, 0x080820, 21 );
//scene.add( light );
controls = new THREE.OrbitControls(camera, webglEl);
stats = new Stats();
webglEl.appendChild(stats.dom);
loadObject();
animate();
}
function animate() {
controls.update();
stats.update();
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
function loadObject() {
var loader = new THREE.GLTFLoader();
loader.load(
//'./Model/GLTF/test.glb',
'./test/test.glb',
function (gltf) {
gltf.scene.traverse(function (node) {
if (node.isGroup) {
if (node.name === "Building") {
BuildingObj = node;
} else if (node.name === "Cube") {
OutFloor = node;
}
}
});
scene.add(OutFloor);
scene.add(BuildingObj);
applyMaterialToObject();
},
function (xhr) {
console.log((xhr.loaded / xhr.total * 100) + '% loaded');
},
function (error) {
console.log('An error happened---' + error);
}
);
}
function applyMaterialToObject(object) {
for (var i = 0; i < BuildingObj.children.length; i++) {
var obj = BuildingObj.children[i];
var material = new THREE.MeshStandardMaterial({ roughness: 0.8, metalness: 0.3, bumpScale: - 0.05, color: 0xffffff, });
loadTexturesToMaterial(BuildingObj.children[i], material, "./test/brick.jpg", "./test/brick_b.jpg", "./test/brick_r.jpg", 25, 70, Math.PI / 2);
}
for (var i = 0; i < OutFloor.children.length; i++) {
var obj = OutFloor.children[i];
var material = new THREE.MeshStandardMaterial({ roughness: 0.5, metalness: 0.8, bumpScale: - 0.05, color: 0xffffff });
loadTexturesToMaterial(obj, material, "./test/tile2.jpg", "./test/tile2_b.jpg", "./test/tile2_r.jpg", 35, 100, Math.PI / 2);
}
}
function loadTexturesToMaterial(obj, material, map_img, bumpMap_img, roughnessMap_img, v_count, h_count, rotation) {
obj.receiveShadow = enableShadow;
obj.castShadow = enableShadow;
obj.material = material;
obj.material.side = THREE.DoubleSide;
var textureLoader = new THREE.TextureLoader();
textureLoader.load(map_img, function (map) {
map.wrapS = THREE.RepeatWrapping;
map.wrapT = THREE.RepeatWrapping;
map.rotation = rotation;
map.repeat.set(v_count, h_count);
obj.material.map = map;
obj.material.needsUpdate = true;
});
textureLoader.load(bumpMap_img, function (map) {
map.wrapS = THREE.RepeatWrapping;
map.wrapT = THREE.RepeatWrapping;
map.rotation = rotation;
map.repeat.set(v_count, h_count);
obj.material.bumpMap = map;
obj.material.needsUpdate = true;
});
textureLoader.load(roughnessMap_img, function (map) {
map.wrapS = THREE.RepeatWrapping;
map.wrapT = THREE.RepeatWrapping;
map.rotation = rotation;
map.repeat.set(v_count, h_count);
obj.material.roughnessMap = map;
obj.material.needsUpdate = true;
});
}
Note: I have created the model using blender.
Edit: Texture and model link
https://github.com/SourceCodeZone/3D
I want to be able to add images in the script code "Bad TV Shader" (https://www.airtightinteractive.com/demos/js/badtvshader/). It was originally developed for video, but I want to implement an image that also is under these distortion and noise effects.
Who can help I would be very grateful! =)
<script>
//Bad TV Effect Demo
//Using Three.js r.66
//by Felix Turner / www.airtight.cc / #felixturner
var camera, scene, renderer;
var video, videoTexture,videoMaterial;
var composer;
var shaderTime = 0;
var badTVParams, badTVPass;
var staticParams, staticPass;
var rgbParams, rgbPass;
var filmParams, filmPass;
var renderPass, copyPass;
var gui;
var pnoise, globalParams;
init();
animate();
function init() {
camera = new THREE.PerspectiveCamera(55, 1080/ 720, 20, 3000);
camera.position.z = 1000;
scene = new THREE.Scene();
//Load Video
video = document.createElement( 'video' );
video.loop = true;
video.src = "res/fits.mp4";
video.play();
//Use webcam
// video = document.createElement('video');
// video.width = 320;
// video.height = 240;
// video.autoplay = true;
// video.loop = true;
// //Webcam video
// window.URL = window.URL || window.webkitURL;
// navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
// //get webcam
// navigator.getUserMedia({
// video: true
// }, function(stream) {
// //on webcam enabled
// video.src = window.URL.createObjectURL(stream);
// }, function(error) {
// prompt.innerHTML = 'Unable to capture WebCam. Please reload the page.';
// });
//init video texture
videoTexture = new THREE.Texture( video );
videoTexture.minFilter = THREE.LinearFilter;
videoTexture.magFilter = THREE.LinearFilter;
videoMaterial = new THREE.MeshBasicMaterial( {
map: videoTexture
} );
//Add video plane
var planeGeometry = new THREE.PlaneGeometry( 1080, 720,1,1 );
var plane = new THREE.Mesh( planeGeometry, videoMaterial );
scene.add( plane );
plane.z = 0;
plane.scale.x = plane.scale.y = 1.45;
//add stats
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
container.appendChild( stats.domElement );
//init renderer
renderer = new THREE.WebGLRenderer();
renderer.setSize( 800, 600 );
document.body.appendChild( renderer.domElement );
//POST PROCESSING
//Create Shader Passes
renderPass = new THREE.RenderPass( scene, camera );
badTVPass = new THREE.ShaderPass( THREE.BadTVShader );
rgbPass = new THREE.ShaderPass( THREE.RGBShiftShader );
filmPass = new THREE.ShaderPass( THREE.FilmShader );
staticPass = new THREE.ShaderPass( THREE.StaticShader );
copyPass = new THREE.ShaderPass( THREE.CopyShader );
//set shader uniforms
filmPass.uniforms[ "grayscale" ].value = 0;
//Init DAT GUI control panel
badTVParams = {
mute:true,
show: true,
distortion: 3.0,
distortion2: 1.0,
speed: 0.3,
rollSpeed: 0.1
}
staticParams = {
show: true,
amount:0.5,
size2:4.0
}
rgbParams = {
show: true,
amount: 0.005,
angle: 0.0,
}
filmParams = {
show: true,
count: 800,
sIntensity: 0.9,
nIntensity: 0.4
}
gui = new dat.GUI();
gui.add(badTVParams, 'mute').onChange(onToggleMute);
var f1 = gui.addFolder('Bad TV');
f1.add(badTVParams, 'show').onChange(onToggleShaders);
f1.add(badTVParams, 'distortion', 0.1, 20).step(0.1).listen().name("Thick Distort").onChange(onParamsChange);
f1.add(badTVParams, 'distortion2', 0.1, 20).step(0.1).listen().name("Fine Distort").onChange(onParamsChange);
f1.add(badTVParams, 'speed', 0.0,1.0).step(0.01).listen().name("Distort Speed").onChange(onParamsChange);
f1.add(badTVParams, 'rollSpeed', 0.0,1.0).step(0.01).listen().name("Roll Speed").onChange(onParamsChange);
f1.open();
var f2 = gui.addFolder('RGB Shift');
f2.add(rgbParams, 'show').onChange(onToggleShaders);
f2.add(rgbParams, 'amount', 0.0, 0.1).listen().onChange(onParamsChange);
f2.add(rgbParams, 'angle', 0.0, 2.0).listen().onChange(onParamsChange);
f2.open();
var f4 = gui.addFolder('Static');
f4.add(staticParams, 'show').onChange(onToggleShaders);
f4.add(staticParams, 'amount', 0.0,1.0).step(0.01).listen().onChange(onParamsChange);
f4.add(staticParams, 'size2', 1.0,100.0).step(1.0).onChange(onParamsChange);
f4.open();
var f3 = gui.addFolder('Scanlines');
f3.add(filmParams, 'show').onChange(onToggleShaders);
f3.add(filmParams, 'count', 50, 1000).onChange(onParamsChange);
f3.add(filmParams, 'sIntensity', 0.0, 2.0).step(0.1).onChange(onParamsChange);
f3.add(filmParams, 'nIntensity', 0.0, 2.0).step(0.1).onChange(onParamsChange);
f3.open();
gui.close();
onToggleShaders();
onToggleMute();
onParamsChange();
window.addEventListener('resize', onResize, false);
renderer.domElement.addEventListener('click', randomizeParams, false);
onResize();
randomizeParams();
}
function onParamsChange() {
//copy gui params into shader uniforms
badTVPass.uniforms[ "distortion" ].value = badTVParams.distortion;
badTVPass.uniforms[ "distortion2" ].value = badTVParams.distortion2;
badTVPass.uniforms[ "speed" ].value = badTVParams.speed;
badTVPass.uniforms[ "rollSpeed" ].value = badTVParams.rollSpeed;
staticPass.uniforms[ "amount" ].value = staticParams.amount;
staticPass.uniforms[ "size" ].value = staticParams.size2;
rgbPass.uniforms[ "angle" ].value = rgbParams.angle*Math.PI;
rgbPass.uniforms[ "amount" ].value = rgbParams.amount;
filmPass.uniforms[ "sCount" ].value = filmParams.count;
filmPass.uniforms[ "sIntensity" ].value = filmParams.sIntensity;
filmPass.uniforms[ "nIntensity" ].value = filmParams.nIntensity;
}
function randomizeParams() {
if (Math.random() <0.2){
//you fixed it!
badTVParams.distortion = 0.1;
badTVParams.distortion2 =0.1;
badTVParams.speed =0;
badTVParams.rollSpeed =0;
rgbParams.angle = 0;
rgbParams.amount = 0;
staticParams.amount = 0;
}else{
badTVParams.distortion = Math.random()*10+0.1;
badTVParams.distortion2 =Math.random()*10+0.1;
badTVParams.speed =Math.random()*.4;
badTVParams.rollSpeed =Math.random()*.2;
rgbParams.angle = Math.random()*2;
rgbParams.amount = Math.random()*0.03;
staticParams.amount = Math.random()*0.2;
}
onParamsChange();
}
function onToggleMute(){
video.volume = badTVParams.mute ? 0 : 1;
}
function onToggleShaders(){
//Add Shader Passes to Composer
//order is important
composer = new THREE.EffectComposer( renderer);
composer.addPass( renderPass );
if (filmParams.show){
composer.addPass( filmPass );
}
if (badTVParams.show){
composer.addPass( badTVPass );
}
if (rgbParams.show){
composer.addPass( rgbPass );
}
if (staticParams.show){
composer.addPass( staticPass );
}
composer.addPass( copyPass );
copyPass.renderToScreen = true;
}
function animate() {
shaderTime += 0.1;
badTVPass.uniforms[ 'time' ].value = shaderTime;
filmPass.uniforms[ 'time' ].value = shaderTime;
staticPass.uniforms[ 'time' ].value = shaderTime;
if ( video.readyState === video.HAVE_ENOUGH_DATA ) {
if ( videoTexture ) videoTexture.needsUpdate = true;
}
requestAnimationFrame( animate );
composer.render( 0.1);
stats.update();
}
function onResize() {
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
}
</script>
Using video or image is the same process, you can use Planegeometry and TextureLoader to place an image instead of a video like this :
YourGroup = new THREE.Group();
var YourImage = new THREE.TextureLoader();
YourImage.load( 'folder/to_image.png', function ( texture ) {
var geometry = new THREE.PlaneGeometry( width, height );
var material = new THREE.MeshBasicMaterial({
map: texture,
transparent: true,
});
var mesh = new THREE.Mesh( geometry, material );
YourGroup.add( mesh );
});
scene.add( YourGroup );
Note:
Here i created a group with everything to put inside (a planegeometry with BasicMaterial to apply my loaded image to Plane, and create a mesh from these two.
I put a Png instead of jpeg because of transparency for Mesh.
I add my group to scene.
I'm having trouble controlling shadows in THREE.js. First off, the shadow in my scene is way too dark. From what I've read, there was a shadowDarkness property, that is know longer available in the current version of three.js. Does anyone know a work around?
Also, in the attached image: the "backface" geometry is not occluding light on the shadow of the seat - however, you can see the backface of the stool in the reflection of the sphere(cubeCamera). Does anyone know how to fix that?
On a side note: chrome gives me an error "Uncaught TypeError: Cannot set property 'visible' of undefined," regarding the
frameMesh.visible = false;
cubeCameraFrame.position.copy(frameMesh.position);
cubeCameraFrame.updateCubeMap(renderer, scene);
frameMesh.visible = true;
part of my code. Could that be effecting the shadows in some way? I can comment that part of the code and it will have little effect on the stoolframes "reflective" appearance. However it then no longer is reflects in the sphere. Any help is much appreciated.
///webGL - Locking down the Basics
/////////////////////////////////////////////////////////////Environment Settings///////////////////////////////////////////////////////////////////////
///Renderer
var scene = new THREE.Scene();
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMapType = THREE.PCFSoftShadowMap;
renderer.shadowMapEnabled = true;
document.body.appendChild(renderer.domElement);
///Camera's
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
scene.add(camera);
camera.position.set(0, 16, 25);
camera.rotation.x += -0.32;
var cubeCameraSphere = new THREE.CubeCamera(1, 1000, 256); // parameters: near, far, resolution
cubeCameraSphere.renderTarget.texture.minFilter = THREE.LinearMipMapLinearFilter; // mipmap filter
scene.add(cubeCameraSphere);
var cubeCameraFrame = new THREE.CubeCamera(1, 1000, 256); // parameters: near, far, resolution
cubeCameraFrame.renderTarget.texture.minFilter = THREE.LinearMipMapLinearFilter; // mipmap filter
scene.add(cubeCameraFrame);
///Controls
///Lights
var lightSpot_Right = new THREE.SpotLight(0xffffff);
lightSpot_Right.position.set(50, 50, 0);
lightSpot_Right.intensity = 1.25;
lightSpot_Right.castShadow = true;
lightSpot_Right.shadowDarkness = 0.1;
lightSpot_Right.shadowMapWidth = 2048;
lightSpot_Right.shadowMapHeight = 2048;
lightSpot_Right.shadowCameraNear = 1;
lightSpot_Right.shadowCameraFar = 100;
lightSpot_Right.shadowCameraFov = 65;
scene.add(lightSpot_Right);
var lightDirect_Left = new THREE.DirectionalLight(0xffffff, 0.25);
lightDirect_Left.position.set(-1, 0, 0);
scene.add(lightDirect_Left);
///Loaders
var loadTexture = new THREE.TextureLoader();
var loader = new THREE.JSONLoader();
///skyBox
var imagePrefix = "textures/";
var directions = ["skyboxRight", "skyboxLeft", "skyboxTop", "skyboxBottom", "skyboxFront", "skyboxBack"];
var imageSuffix = ".jpg";
var skyMaterialArray = [];
for (var i = 0; i < 6; i++)
skyMaterialArray.push(new THREE.MeshBasicMaterial({
map: new THREE.TextureLoader().load(imagePrefix + directions[i] + imageSuffix),
side: THREE.BackSide
}));
var skyMaterial = new THREE.MeshFaceMaterial(skyMaterialArray);
var skyGeometry = new THREE.CubeGeometry(1000, 1000, 1000);
var skyBox = new THREE.Mesh(skyGeometry, skyMaterial);
scene.add(skyBox);
////////////////////////////////////////////////////////Object Settings//////////////////////////////////////////////////////////////////
//Textures
var seatTexture = loadTexture.load("textures/Maharam_Mister_Notice_Diffuse.jpg");
seatTexture.wrapS = THREE.RepeatWrapping;
seatTexture.wrapT = THREE.RepeatWrapping;
seatTexture.repeat.set(3, 3);
var conceteDiffuse = loadTexture.load("textures/Contrete_Diffuse.jpg");
conceteDiffuse.wrapS = THREE.RepeatWrapping;
conceteDiffuse.wrapT = THREE.RepeatWrapping;
conceteDiffuse.repeat.set(3, 3);
var conceteNormal = loadTexture.load("textures/Contrete_Normal.jpg");
conceteNormal.wrapS = THREE.RepeatWrapping;
conceteNormal.wrapT = THREE.RepeatWrapping;
conceteNormal.repeat.set(3, 3);
var conceteSpecular = loadTexture.load("textures/Contrete_Specular.jpg");
conceteSpecular.wrapS = THREE.RepeatWrapping;
conceteSpecular.wrapT = THREE.RepeatWrapping;
conceteSpecular.repeat.set(3, 3);
///Materials
var seatMaterial = new THREE.MeshLambertMaterial({
map: seatTexture,
side: THREE.DoubleSide
});
var frameMaterial = new THREE.MeshPhongMaterial({
envMap: cubeCameraFrame.renderTarget,
color: 0xcccccc,
emissive: 0x404040,
shininess: 10,
reflectivity: .8
});
var frameHardwareMat = new THREE.MeshPhongMaterial({
color: 0x000000
});
var feetMat = new THREE.MeshPhongMaterial({
color: 0x050505,
shininess: 99
});
var sphereMat = new THREE.MeshPhongMaterial({
envMap: cubeCameraSphere.renderTarget
});
var groundMat = new THREE.MeshPhongMaterial({
map: conceteDiffuse,
specularMap: conceteSpecular,
normalMap: conceteNormal,
normalScale: new THREE.Vector2( 0.0, 0.6 ),
shininess: 50
});
///Geometry and Meshes
var barStool = new THREE.Object3D();
scene.add(barStool);
var seatMesh;
loader.load("models/stoolSeat.js", function (geometry, material) {
seatMesh = new THREE.Mesh(geometry, seatMaterial);
seatMesh.scale.set(.5, .5, .5);
seatMesh.castShadow = true;
seatMesh.receiveShadow = true;
barStool.add(seatMesh);
});
var frameMesh;
loader.load("models/stoolFrame.js", function (geometry, material) {
frameMesh = new THREE.Mesh(geometry, frameMaterial);
frameMesh.scale.set(.5, .5, .5);
frameMesh.castShadow = true;
barStool.add(frameMesh);
});
var frameFeetMesh;
loader.load("models/stoolFeet.js", function (geometry, material) {
frameFeetMesh = new THREE.Mesh(geometry, feetMat);
frameFeetMesh.scale.set(.5, .5, .5);
frameFeetMesh.castShadow = true;
barStool.add(frameFeetMesh);
});
var frameHardwareMesh;
loader.load("models/stoolHardware.js", function (geomtry, material) {
frameHardwareMesh = new THREE.Mesh(geomtry, frameHardwareMat);
frameHardwareMesh.scale.set(.5, .5, .5);
barStool.add(frameHardwareMesh);
});
var sphereGeo = new THREE.SphereGeometry(2.5, 50, 50);
var sphereMesh = new THREE.Mesh(sphereGeo, sphereMat);
scene.add(sphereMesh);
sphereMesh.position.set(-10, 5, 0);
var groundGeo = new THREE.PlaneGeometry(100, 50, 1);
var groundMesh = new THREE.Mesh(groundGeo, groundMat);
scene.add(groundMesh);
groundMesh.rotation.x = -90 * Math.PI / 180;
groundMesh.receiveShadow = true;
///Render Scene
var render = function () {
requestAnimationFrame(render);
barStool.rotation.y += 0.01;
skyBox.rotation.y -= 0.0002;
sphereMesh.visible = false;
cubeCameraSphere.position.copy(sphereMesh.position);
cubeCameraSphere.updateCubeMap(renderer, scene);
sphereMesh.visible = true;
//frameMesh.visible = false;
//cubeCameraFrame.position.copy(frameMesh.position);
//cubeCameraFrame.updateCubeMap(renderer, scene);
//frameMesh.visible = true;
renderer.render(scene, camera);
};
render();
Shadow darkness has been removed. The best work-around is to add ambient light to your scene.
scene.add( new THREE.AmbientLight( 0xffffff, 0.3 );
You may want to concurrently reduce the intensity of your SpotLight.
The shadow is actually correct given only back faces are casting shadows. It appears that the stool is hollow under the seat -- in other words, the seat is not a closed volume. Add a bottom to the underside of your seat.
Alternatively, you can leave your model as-is and experiment with
renderer.shadowMap.cullFace = THREE.CullFaceNone;
Finally, you are getting the error because you are accessing frameMesh in the animation loop before it is defined in the loader callback. The callback is asynchronous.
if ( frameMesh !== undefined ) {
// your code
}
three.js r.75