gpt4 book ai didi

javascript - babylonjs:多元素的多阴影不起作用

转载 作者:行者123 更新时间:2023-11-29 19:44:48 25 4
gpt4 key购买 nike

我正在尝试使用 Babylonjs 获得正确的阴影。没有任何快乐:p

这是我找到的关于阴影的资源

但我在“元素上的元素”阴影上找不到任何内容。 :(

这是我的尝试:我的资源大致基于 Babylonjs wiki:17-Shadows

我有 2 个灯和 3 个对象我在球体后面得到了一个阴影,但随后在球体的正面也得到了一个人工制品。

► 实时代码:jsfiddle.net/codemeasandwich/z64Ba

感谢您的帮助,因为我已经为此苦苦挣扎了一段时间。

function createSceneTuto(engine) {
var scene = new BABYLON.Scene(engine);

//freeCamera is a FPS like camera where you control the camera with the cursors keys and the mouse
//touchCamera is a camera controlled with touch events (it requireshand.jsto work)
//arcRotateCamera is a camera that rotates around a given pivot. It can be controlled with the mouse or touch events (and it also requires hand.js to work)

// ArcRotateCamera >> Camera turning around a 3D point (here Vector zero)
// Parameters : name, alpha, beta, radius, target, scene
var camera = new BABYLON.ArcRotateCamera("Camera", 0, 0, 90, BABYLON.Vector3.Zero(), scene);
camera.setPosition(new BABYLON.Vector3(30, 30, 30));

// pointLight (like the sun for instance) which emits lights in every direction from a specific position
// directionalLight which emits lights from the infinite towards a specific direction
var light = new BABYLON.DirectionalLight("Dir0", new BABYLON.Vector3( -1,0, 0), scene);


var light0 = new BABYLON.PointLight("Omni0", new BABYLON.Vector3(1, 10, 100), scene);
light0.diffuse = new BABYLON.Color3( 0,1, 0);
light0.specular = new BABYLON.Color3(1, 1, 1);

var box = BABYLON.Mesh.CreateBox("Box", 3, scene);
var torus = BABYLON.Mesh.CreateTorus("torus", 5, 1, 20, scene);
// var plan = BABYLON.Mesh.CreatePlane("Plane", 50.0, scene);
// plan.position.z = -40
var sphere = BABYLON.Mesh.CreateSphere("Sphere", 15, 20, scene);

// Shadows
var shadowGenerator = new BABYLON.ShadowGenerator(1024, light);
var shadowGenerator0 = new BABYLON.ShadowGenerator(1024, light0);

shadowGenerator.getShadowMap().renderList.push(box);
shadowGenerator.getShadowMap().renderList.push(torus);
shadowGenerator.getShadowMap().renderList.push(sphere);

shadowGenerator0.getShadowMap().renderList.push(box);
shadowGenerator0.getShadowMap().renderList.push(torus);
shadowGenerator0.getShadowMap().renderList.push(sphere);

box.receiveShadows = true;
torus.receiveShadows = true;
sphere.receiveShadows = true;

var alphaTorus = 0, alphaBox =0;
scene.registerBeforeRender(function () {
torus.rotation.x += 0.02;
torus.position = new BABYLON.Vector3(Math.cos(alphaTorus) * 15, 0, Math.sin(alphaTorus) * 15);
alphaTorus += 0.003;

box.position = new BABYLON.Vector3(Math.cos(alphaBox) * 15, 0, Math.sin(alphaBox) * 15);
alphaBox += 0.01;
});

return scene;
}

以上灯为定向灯

var light = new BABYLON.DirectionalLight("Dir0", new BABYLON.Vector3( -1,0, 0), scene);
light.position = new BABYLON.Vector3(0, 0, 20);
light.intensity = 0.5;

var light0 = new BABYLON.DirectionalLight("Omni0", new BABYLON.Vector3(0,0,-1), scene);
light0.position = new BABYLON.Vector3(25, 0, 0);
light.intensity = 0.5;

最佳答案

只有定向光可以转换阴影,它们还需要一个位置来定义阴影的来源

我更新了 wiki 以添加此重要 信息:)

Only directional ligths can cast shadows:var light = new BABYLON.DirectionalLight("dir01", new BABYLON.Vector3(-1, -2, -1), scene);

You must also define a position for your light (because Babylon.js must define a point of view to > create the shadow map):light.position = new BABYLON.Vector3(20, 40, 20);

Please note that you should have to move the position to define the area where the shadows are seen.

关于javascript - babylonjs:多元素的多阴影不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20497793/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com