three.js box geometry opacity - three.js

Friends, I use three.js basic material and created 2 box, I don't want to see the small box inside the bigger box, but I can see all of them now. could you help me?
var cube1 = new THREE.Mesh(
new THREE.BoxGeometry(200, 200, 200),
new THREE.MeshBasicMaterial({
color: 0xffffff,
transparent: false,
opacity: 1,
overdraw: 0.5
}));
scene.add(cube1);
var cube2 = new THREE.Mesh(
new THREE.BoxGeometry(100, 100, 100),
new THREE.MeshBasicMaterial({
color: 0x000000,
transparent: false,
opacity: 1,
overdraw: 0.5
}));
scene.add(cube2);

Ideally this should not happen maybe you might have missed something, I here is the complete code in the snippet of what you are trying to achieve.
var camera, scene, renderer;
var mesh;
init();
animate();
function init() {
renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0xAAAAAA);
renderer.sortObjects = true;
document.body.appendChild(renderer.domElement);
//
camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.z = 400;
scene = new THREE.Scene();
cube1 = new THREE.Mesh(
new THREE.BoxGeometry(200, 200, 200),
new THREE.MeshBasicMaterial({
color: 0xffffff,
transparent: false,
opacity: 1,
overdraw: 0.5
}));
scene.add(cube1);
//
cube2 = new THREE.Mesh(
new THREE.BoxGeometry(100, 100, 100),
new THREE.MeshBasicMaterial({
color: 0x000000,
transparent: false,
opacity: 1,
overdraw: 0.5
}));
scene.add(cube2);
window.addEventListener('resize', onWindowResize, false);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
requestAnimationFrame(animate);
cube1.rotation.x += 0.005;
cube1.rotation.y += 0.01;
cube2.rotation.x += 0.010;
cube2.rotation.y += 0.01;
renderer.render(scene, camera);
}
html, body {
padding:0px;
margin:0px;
}
canvas {
width: 100%;
height: 100%
}
<script src="http://cdnjs.cloudflare.com/ajax/libs/three.js/r69/three.min.js"></script>
I have also created a jsfiddle link if you prefer.Hoping it solves your problem,else you can edit the snippet to reproduce your problem.

find the reason:
see through example
if I put the cube position.z < 0, I'll 'see through'.
but if you want to see through, this is a tricky.
[http://jsfiddle.net/agpcn1ej/1/][1]
seems find the answer.
that's because part of the house model position.z < 0, and camera's near < 0, maybe three.js z-buffer clear negative = 0, z-buffer determines the sheltery relation.

Related

How to set opacity in drawing lines use three.js, use LineBasicMaterial?

I want to change lines' width and opacity,and my code as this:
var geometry_link = new THREE.Geometry();
geometry_link.vertices.push(
new THREE.Vector3(item[0].x *1,item[0].y *1,item[0].z),
new THREE.Vector3(item[1].x *1,item[1].y *1,item[1].z)
);
geometry_link.colors.push(
new THREE.Color(0x000000),
new THREE.Color(0xffffff)
);
var line = new THREE.Line(geometry_link, new THREE.LineBasicMaterial({
vertexColors: true,
linewidth: 8,
transparent: true,
opacity: 0.1
})
but it dose not work, both linewidth and opacity. I have found the similar question,said linewidth does not work on windows, that's 5 years ago, how about now?
Thanks for your idea
LineBasicMaterial does support transparent lines as demonstrated by the following live example:
let camera, scene, renderer;
init();
animate();
function init() {
camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 10);
camera.position.z = 1;
scene = new THREE.Scene();
const geometry = new THREE.BufferGeometry().setFromPoints([new THREE.Vector3(), new THREE.Vector3(1, 0, 0)]);
const material = new THREE.LineBasicMaterial({
transparent: true,
opacity: 0.5
});
const mesh = new THREE.Line(geometry, material);
scene.add(mesh);
renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
}
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
body {
margin: 0;
}
canvas {
display: block;
}
<script src="https://cdn.jsdelivr.net/npm/three#0.119.1/build/three.js"></script>
Wide lines however are not supported. So setting linewidth to a different value like 1 has no effect in most browsers. This is a WebGL limitation in context of line primitives.
However, three.js provides a wide line implementation based on triangles (sometimes called mesh lines or ribbons). Check out the following example for more information:
https://threejs.org/examples/webgl_lines_fat

How to generate a shape with 3 faces in three.js

I am trying to generate a shape like this, 3d rectangle with a transparent face or just 3 panels in a 3d way.
I am new to three.js and I wanted to know if this is possible and if you could guide me.
Thanks a lot!
You can find an image of what I want to generate here:
https://i.stack.imgur.com/h0ja7.png
As an option, you can do it this way:
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.set(2, 2, 3);
camera.lookAt(scene.position);
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x404040);
document.body.appendChild(renderer.domElement);
var contorls = new THREE.OrbitControls(camera, renderer.domElement);
var boxGeom = new THREE.BoxGeometry(2, 1, 1);
var mat1 = new THREE.MeshBasicMaterial({
color: "red",
side: THREE.DoubleSide
});
var mat2 = new THREE.MeshBasicMaterial({
color: "aqua",
side: THREE.DoubleSide,
transparent: true,
opacity: 0.5
});
var boxMat = [mat1, mat1, null, null, mat2, null];
var box = new THREE.Mesh(boxGeom, boxMat);
scene.add(box);
render();
function render() {
requestAnimationFrame(render);
renderer.render(scene, camera);
}
body {
overflow: hidden;
margin: 0;
}
<script src="https://threejs.org/build/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>

How to draw a cylinder along x-axis using three js?

The default axis of the cylinder is y-axis, forms a verticle cylinder. I need to draw a horizontal cylinder.
You can use .rotateZ() method of your cylinder geometry:
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.set(0, 5, 10);
camera.lookAt(scene.position);
var renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
var controls = new THREE.OrbitControls(camera, renderer.domElement);
scene.add(new THREE.GridHelper(10, 10));
var geom = new THREE.CylinderGeometry(1, 1, 7, 16, 1);
geom.rotateZ(-Math.PI * 0.5); // rotate 90 degrees clockwise around z-axis
var cylinder = new THREE.Mesh(geom, new THREE.MeshBasicMaterial({
color: "aqua",
wireframe: true
}));
scene.add(cylinder);
render();
function render() {
requestAnimationFrame(render);
renderer.render(scene, camera);
}
body {
overflow: hidden;
margin: 0;
}
<script src="https://threejs.org/build/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>

DstAlphaFactor doesn't work in THREE.js

I want create a circle on the plane.
The blending is (circleColor * planeAlpha). But the result is not what I expect.
First, the plane is render with background. So the Alpha in result should be Zero. Because "blendSrcAlpha blendDstAlpha" both are ZeroFactor.
Second, The Previous results is blending with circle. I set the blendSrc: THREE.DstAlphaFactor in cirlceMaterial.So the result should be
circleColor * planeAlpha = 0xff0000 * 0 = 0*000000
But it's show a red circle in reality.
How did alpha blending work here?
import * as THREE from "three";
import dat from 'dat.gui';
//创建场景.
let scene = new THREE.Scene();
//相机
let camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.x = 10;
camera.position.y = 10;
camera.position.z = 50;
camera.lookAt(scene.position);
//渲染器
let renderer = new THREE.WebGLRenderer();
renderer.setClearColor(0xEEEEEE);
renderer.setClearAlpha(0.1);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
document.body.appendChild(renderer.domElement);
//第二步,创建几何体.
let planeMeometry = new THREE.PlaneGeometry(20, 20, 10);
let circleGeometry = new THREE.SphereGeometry(5, 32, 32);
let planeMaterial = new THREE.MeshBasicMaterial({
color: 0x00ff00,
opacity: 0.1,
depthTest: false,
transparent: true,
blending: THREE.CustomBlending,
blendEquation: THREE.AddEquation,
blendSrc: THREE.ZeroFactor,
blendDst: THREE.ZeroFactor,
blendEquationAlpha: THREE.AddEquation,
blendSrcAlpha: THREE.ZeroFactor,
blendDstAlpha: THREE.ZeroFactor
});
let circleMaterial = new THREE.MeshBasicMaterial({
color: 0xff0000,
opacity: 0.1,
depthTest: false,
transparent: true,
blending: THREE.CustomBlending,
blendEquation: THREE.AddEquation,
blendSrc: THREE.DstAlphaFactor, //It doesn't work . Expert is Zero, but is's One in fact.
blendDst: THREE.ZeroFactor
});
let plane = new THREE.Mesh(planeMeometry, planeMaterial);
let circle = new THREE.Mesh(circleGeometry, circleMaterial);
plane.renderOrder = 200;
circle.renderOrder = 300;
//加入到场景
scene.add(camera);
scene.add(plane);
scene.add(circle);
let axes = new THREE.AxesHelper(20);
scene.add(axes);
//渲染循环
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
animate();
Three.js use glBlendFuncSeparate for CustomBlending.
https://github.com/mrdoob/three.js/blob/635179212570586f8130ce9a287b4bbc4fb4338c/src/renderers/webgl/WebGLState.js#L631
You have to also set material's blendEquationAlpha, blendSrcAlpha and blendDstAlpha

Rotating a cube about an axis

In my program, I have done as:
var object = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial( 0xff0000 ) );
object.rotation.x = Math.PI / 2;
var box = new THREE.BoxHelper( object, 0xffff00 );
scene.add(box);
Here,I first try to create a wireframe box and then rotate it by 90 degrees about x axis(line 2). But I don't see any change in box's orientation.
What is correct way of doing it?
Just set rotation of the cube at 90 degrees (here it's 45) and you'll have the same cube:
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.set(2, 5, 10);
var renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
var controls = new THREE.OrbitControls(camera, renderer.domElement);
scene.add(new THREE.GridHelper(10, 10));
var cube = new THREE.Mesh(new THREE.BoxGeometry(2, 2, 2), new THREE.MeshBasicMaterial({
color: "aqua",
wireframe: true
}));
cube.position.set(0, 1, 0);
cube.rotation.x = Math.PI * 0.25;
scene.add(cube);
var box = new THREE.BoxHelper(cube, 0xff9900);
scene.add(box);
render();
function render() {
requestAnimationFrame(render);
renderer.render(scene, camera);
}
body {
overflow: hidden;
margin: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/92/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>

Resources