【发布时间】:2021-03-21 10:07:06
【问题描述】:
我是 three.js 的新手,但我在 Unity 有过 3d 对象和渲染方面的经验, 我在使用 SpotLight 时遇到问题,我试图从一个进口的 obj 中发光,让它看起来像一盏工作灯。 我无法将灯光的角度设置为锐角,因为当它超过某个值时它就不会发光。
这是相关的代码:
const objLight = new THREE.SpotLight('yellow', 5);
objLight.angle = Math.PI/2;
objLight.penumbra = 0.1;
objLight.decay = 1;
objLight.distance = 40;
objLight.position.set(-0.48, 0.7, 0);
objLight.target.position.set(-0.9, 0, 0);
objLight.castShadow = true;
scene.add(objLight);
scene.add(objLight.target);
这是设置角度时的样子
Math.PI/2: https://prnt.sc/vzddfy 。
Math.PI/3:https://prnt.sc/vzdeel。
Math.PI/4:https://prnt.sc/vzdeks.
现在我将相机移得更近了,我看到光线照亮了物体,但就是这样:https://prnt.sc/vzder0。
我尝试了一个小时的值,但找不到解决方案,我什至尝试删除 obj 和框,但它没有做任何事情。
但我确实发现,如果我将光从 x=-0.48 移到 x=-4.48,我可以让它变得尖锐,直到 Math.PI/6:https://prnt.sc/vzdgls
如果我想让它更尖锐,我需要把它移得更远。
我的理论是它与目标的距离或其他东西有关,我曾试图将目标移开但它没有做任何事情。
有人知道可能是什么问题吗?
其余代码:
let renderer, camera, scene;
// const THREE = require('three');
function init() {
renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
document.body.appendChild(renderer.domElement);
scene = new THREE.Scene();
// scene.background = new THREE.Color('red');
camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
camera.position.z = 5;
new THREE.OrbitControls(camera, renderer.domElement);
const light = new THREE.AmbientLight('white', 0.2);
scene.add(light);
const BoxGeo = new THREE.BoxGeometry(1, 1, 1);
const BoxMat = new THREE.MeshLambertMaterial({color: 'blue'});
const Box = new THREE.Mesh(BoxGeo, BoxMat);
Box.castShadow = true;
scene.add(Box);
const TopPlaneGeo = new THREE.PlaneGeometry(10, 10, 1);
const TopPlaneMat = new THREE.MeshLambertMaterial({color: 'white', side: THREE.DoubleSide});
const TopPlane = new THREE.Mesh(TopPlaneGeo, TopPlaneMat);
TopPlane.position.y = 5;
TopPlane.rotation.x = Math.PI/2;
TopPlane.receiveShadow = true;
scene.add(TopPlane);
const BotPlaneGeo = new THREE.PlaneGeometry(10, 10, 1);
const BotPlaneMat = new THREE.MeshLambertMaterial({color: 'white', side: THREE.DoubleSide});
const BotPlane = new THREE.Mesh(BotPlaneGeo, BotPlaneMat);
BotPlane.position.y = -1;
BotPlane.rotation.x = Math.PI/2;
BotPlane.receiveShadow = true;
scene.add(BotPlane);
const loader = new THREE.OBJLoader();
loader.load
(
'objects/Lampe_FerBlanc.obj',
function(obj) {
obj.position.set(-0.40, 0.5, 0);
obj.scale.set(0.01, 0.01, 0.01);
scene.add(obj);
}
)
const objLight = new THREE.SpotLight('yellow', 5);
objLight.angle = Math.PI/6;
objLight.penumbra = 0.1;
objLight.decay = 1;
objLight.distance = 40;
objLight.position.set(-4.48, 0.7, 0);
objLight.target.position.set(-0.9, 0, 0);
objLight.castShadow = true;
scene.add(objLight);
scene.add(objLight.target);
const helper = new THREE.SpotLightHelper(objLight);
helper.name = 'helper';
scene.add(helper);
window.addEventListener('resize', () => {
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth/window.innerHeight;
camera.updateProjectionMatrix();
})
}
function animate() {
requestAnimationFrame(animate);
scene.getObjectByName('helper').update();
renderer.render(scene, camera);
}
init();
animate();
【问题讨论】:
标签: three.js angle lighting spotlight