- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 udacity 的视频类(class)中学习three.js,当我尝试完成有关“three.js中的阴影”(exercise's link)的练习时遇到问题。我按照视频的说明添加了一些代码在练习中,但我没有得到预期的效果。没有创建任何阴影 code and result of finished exercise on jsfiddle 。
////////////////////////////////////////////////////////////////////////////////
// Adding shadows to a spotlight
////////////////////////////////////////////////////////////////////////////////
/* global THREE, Coordinates, document, window, dat */
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
import $ from "jquery";
// import themes from './themes'
// import { Coordinates as CoordinateCreater } from './Coordinates';
// const Coordinates = new CoordinateCreater({}, themes.dark);
import * as dat from 'dat.gui';
var camera, scene, renderer;
var cameraControls;
var effectController;
var clock = new THREE.Clock();
var cylinder, sphere, cube;
var bevelRadius = 1.9; // TODO: 2.0 causes some geometry bug.
var headlight;
var spotlight;
// this exercise is based on old version of three.js, and current version of three.js do not turn on shadows by this way.
function init() {
var canvasWidth = 846;
var canvasHeight = 494;
// For grading the window is fixed in size; here's general code:
//var canvasWidth = window.innerWidth;
//var canvasHeight = window.innerHeight;
// RENDERER
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.gammaInput = true;
renderer.gammaOutput = true;
renderer.setSize(canvasWidth, canvasHeight);
renderer.setClearColor( 0x0, 1.0 );
renderer.shadowMapEnabled = true; // this is the old version api and it seems do not work in current version
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap; // default THREE.PCFShado
// CAMERA
camera = new THREE.PerspectiveCamera( 35, canvasWidth/ canvasHeight, 1, 4000 );
camera.position.set( -1160, 350, -600 );
// CONTROLS
cameraControls = new OrbitControls(camera, renderer.domElement);
cameraControls.target.set(0,310,0);
}
function fillScene() {
scene = new THREE.Scene();
scene.fog = new THREE.Fog( 0x0, 2000, 4000 );
// LIGHTS
scene.add( new THREE.AmbientLight( 0x222222 ) );
headlight = new THREE.PointLight( 0x606060, 1.0 );
scene.add( headlight );
spotlight = new THREE.SpotLight( 0xFFFFFF, 1.0 );
spotlight.position.set( -400, 1200, 300 );
spotlight.angle = 20 * Math.PI / 180;
spotlight.exponent = 1;
spotlight.target.position.set( 0, 200, 0 );
spotlight.castShadow = true;
scene.add( spotlight );
var lightSphere = new THREE.Mesh(
new THREE.SphereGeometry( 10, 12, 6 ),
new THREE.MeshBasicMaterial() );
lightSphere.position.copy( spotlight.position );
scene.add( lightSphere );
// GROUND
// put grid lines every 10000/100 = 100 units
var solidGround = new THREE.Mesh(
new THREE.PlaneGeometry( 10000, 10000 ),
new THREE.MeshPhongMaterial({ color: 0xFFFFFF,
// polygonOffset moves the plane back from the eye a bit, so that the lines on top of
// the grid do not have z-fighting with the grid:
// Factor == 1 moves it back relative to the slope (more on-edge means move back farther)
// Units == 4 is a fixed amount to move back, and 4 is usually a good value
polygonOffset: true, polygonOffsetFactor: 1.0, polygonOffsetUnits: 4.0
}));
solidGround.rotation.x = -Math.PI / 2;
solidGround.receiveShadow = true;
scene.add( solidGround );
//////////////////////////////
// Bird
var bird = new THREE.Object3D();
createDrinkingBird( bird );
scene.add( bird );
}
// Supporting frame for the bird - base + legs + feet
function createSupport( bsupport ) {
var legMaterial = new THREE.MeshPhongMaterial( { shininess: 4 } );
legMaterial.color.setHex( 0xAdA79b );
legMaterial.specular.setRGB( 0.5, 0.5, 0.5 );
legMaterial.color.copy( legMaterial.color );
var footMaterial = new THREE.MeshPhongMaterial( { color: 0x960f0b, shininess: 30 } );
footMaterial.specular.setRGB( 0.5, 0.5, 0.5 );
footMaterial.color.copy( footMaterial.color );
// base
cube = new THREE.Mesh(
new THREE.CubeGeometry( 20+64+110, 4, 2*77+12, bevelRadius ), footMaterial );
cube.position.x = -45; // (20+32) - half of width (20+64+110)/2
cube.position.y = 4/2; // half of height
cube.position.z = 0; // centered at origin
bsupport.add( cube );
// feet
cube = new THREE.Mesh(
new THREE.CubeGeometry( 20+64+110, 52, 6, bevelRadius ), footMaterial );
cube.position.x = -45; // (20+32) - half of width (20+64+110)/2
cube.position.y = 52/2; // half of height
cube.position.z = 77 + 6/2; // offset 77 + half of depth 6/2
bsupport.add( cube );
cube = new THREE.Mesh(
new THREE.CubeGeometry( 20+64+110, 52, 6, bevelRadius ), footMaterial );
cube.position.x = -45; // (20+32) - half of width (20+64+110)/2
cube.position.y = 52/2; // half of height
cube.position.z = -(77 + 6/2); // negative offset 77 + half of depth 6/2
bsupport.add( cube );
cube = new THREE.Mesh(
new THREE.CubeGeometry( 64, 104, 6, bevelRadius ), footMaterial );
cube.position.x = 0; // centered on origin along X
cube.position.y = 104/2;
cube.position.z = 77 + 6/2; // negative offset 77 + half of depth 6/2
bsupport.add( cube );
cube = new THREE.Mesh(
new THREE.CubeGeometry( 64, 104, 6, bevelRadius ), footMaterial );
cube.position.x = 0; // centered on origin along X
cube.position.y = 104/2;
cube.position.z = -(77 + 6/2); // negative offset 77 + half of depth 6/2
bsupport.add( cube );
// legs
cube = new THREE.Mesh(
new THREE.CubeGeometry( 60, 282+4, 4, bevelRadius ), legMaterial );
cube.position.x = 0; // centered on origin along X
cube.position.y = 104 + 282/2 - 2;
cube.position.z = 77 + 6/2; // negative offset 77 + half of depth 6/2
bsupport.add( cube );
cube = new THREE.Mesh(
new THREE.CubeGeometry( 60, 282+4, 4, bevelRadius ), legMaterial );
cube.position.x = 0; // centered on origin along X
cube.position.y = 104 + 282/2 - 2;
cube.position.z = -(77 + 6/2); // negative offset 77 + half of depth 6/2
bsupport.add( cube );
}
// Body of the bird - body and the connector of body and head
function createBody(bbody) {
var bodyMaterial = new THREE.MeshPhongMaterial( { shininess: 100 } );
bodyMaterial.color.setRGB( 31/255, 86/255, 169/255 );
bodyMaterial.specular.setRGB( 0.5, 0.5, 0.5 );
bodyMaterial.color.copy( bodyMaterial.color );
var glassMaterial = new THREE.MeshPhongMaterial( { color: 0x0, specular: 0xFFFFFF, shininess: 100, opacity: 0.3, transparent: true } );
glassMaterial.color.copy( glassMaterial.color );
var crossbarMaterial = new THREE.MeshPhongMaterial( { color: 0x808080, specular: 0xFFFFFF, shininess: 400 } );
crossbarMaterial.color.copy( crossbarMaterial.color );
// body
sphere = new THREE.Mesh(
new THREE.SphereGeometry( 104/2, 32, 16, 0, Math.PI * 2, Math.PI/2, Math.PI ), bodyMaterial );
sphere.position.x = 0;
sphere.position.y = 160;
sphere.position.z = 0;
bbody.add( sphere );
// cap for top of hemisphere
cylinder = new THREE.Mesh(
new THREE.CylinderGeometry( 104/2, 104/2, 0, 32 ), bodyMaterial );
cylinder.position.x = 0;
cylinder.position.y = 160;
cylinder.position.z = 0;
bbody.add( cylinder );
cylinder = new THREE.Mesh(
new THREE.CylinderGeometry( 12/2, 12/2, 390 - 100, 32 ), bodyMaterial );
cylinder.position.x = 0;
cylinder.position.y = 160 + 390/2 - 100;
cylinder.position.z = 0;
bbody.add( cylinder );
// glass stem
sphere = new THREE.Mesh(
new THREE.SphereGeometry( 116/2, 32, 16 ), glassMaterial );
sphere.position.x = 0;
sphere.position.y = 160;
sphere.position.z = 0;
bbody.add( sphere );
cylinder = new THREE.Mesh(
new THREE.CylinderGeometry( 24/2, 24/2, 390, 32 ), glassMaterial );
cylinder.position.x = 0;
cylinder.position.y = 160 + 390/2;
cylinder.position.z = 0;
bbody.add( cylinder );
// crossbar
cylinder = new THREE.Mesh(
new THREE.CylinderGeometry( 5, 5, 200, 32 ), crossbarMaterial );
cylinder.position.set( 0, 360, 0 );
cylinder.rotation.x = 90 * Math.PI / 180.0;
bbody.add( cylinder );
}
// Head of the bird - head + hat
function createHead(bhead) {
var headMaterial = new THREE.MeshLambertMaterial( );
headMaterial.color.r = 104/255;
headMaterial.color.g = 1/255;
headMaterial.color.b = 5/255;
headMaterial.color.copy( headMaterial.color );
var hatMaterial = new THREE.MeshPhongMaterial( { shininess: 100 } );
hatMaterial.color.r = 24/255;
hatMaterial.color.g = 38/255;
hatMaterial.color.b = 77/255;
hatMaterial.specular.setRGB( 0.5, 0.5, 0.5 );
hatMaterial.color.copy( hatMaterial.color );
var eyeMaterial = new THREE.MeshPhongMaterial( { color: 0x000000, specular: 0x303030, shininess: 4 } );
eyeMaterial.color.copy( eyeMaterial.color );
// head
sphere = new THREE.Mesh(
new THREE.SphereGeometry( 104/2, 32, 16 ), headMaterial );
sphere.position.x = 0;
sphere.position.y = 160 + 390;
sphere.position.z = 0;
bhead.add( sphere );
// hat
cylinder = new THREE.Mesh(
new THREE.CylinderGeometry( 142/2, 142/2, 10, 32 ), hatMaterial );
cylinder.position.x = 0;
cylinder.position.y = 160 + 390 + 40 + 10/2;
cylinder.position.z = 0;
bhead.add( cylinder );
cylinder = new THREE.Mesh(
new THREE.CylinderGeometry( 80/2, 80/2, 70, 32 ), hatMaterial );
cylinder.position.x = 0;
cylinder.position.y = 160 + 390 + 40 + 10 + 70/2;
cylinder.position.z = 0;
bhead.add( cylinder );
// nose
cylinder = new THREE.Mesh(
new THREE.CylinderGeometry( 6, 14, 70, 32 ), headMaterial );
cylinder.position.set( -70, 530, 0 );
cylinder.rotation.z = 90 * Math.PI / 180.0;
bhead.add( cylinder );
// eyes
var sphGeom = new THREE.SphereGeometry( 10, 32, 16 );
// left eye
sphere = new THREE.Mesh( sphGeom, eyeMaterial );
sphere.position.set( -48, 560, 0 );
var eye = new THREE.Object3D();
eye.add( sphere );
eye.rotation.y = 20 * Math.PI / 180.0;
bhead.add( eye );
// right eye
sphere = new THREE.Mesh( sphGeom, eyeMaterial );
sphere.position.set( -48, 560, 0 );
eye = new THREE.Object3D();
eye.add( sphere );
eye.rotation.y = -20 * Math.PI / 180.0;
bhead.add( eye );
}
function createDrinkingBird(bbird) {
var support = new THREE.Object3D();
var body = new THREE.Object3D();
var head = new THREE.Object3D();
// MODELS
// base + legs + feet
createSupport(support);
// body + body/head connector
createBody(body);
// head + hat
createHead(head);
bbird.add(support);
bbird.add(body);
bbird.add(head);
// go through all objects and set the meshes (only)
// so that they cast shadows
bbird.traverse( function ( object ) {
if ( object instanceof THREE.Mesh ) {
object.castShadow = true;
object.receiveShadow = true;
}
} );
}
function setupGui() {
effectController = {
shadowBias: 0.00001 // hack to make dat.GUI show decimal places
};
var gui = new dat.GUI();
gui.add( effectController, "shadowBias", -0.01, 0.01 ).name("shadow bias");
gui.close();
}
function drawHelpers() {
// Coordinates.drawGrid({size:10000,scale:0.01});
}
function addToDOM() {
var container = document.getElementById('container');
var canvas = container.getElementsByTagName('canvas');
if (canvas.length>0) {
container.removeChild(canvas[0]);
}
container.appendChild( renderer.domElement );
}
function animate() {
window.requestAnimationFrame(animate);
render();
}
function render() {
var delta = clock.getDelta();
cameraControls.update(delta);
headlight.position.copy( camera.position );
// VM2636 three.module.js:49433 THREE.Light: .shadowBias is now .shadow.bias.
spotlight.shadow.bias = effectController.shadowBias;
renderer.render(scene, camera);
}
try {
init();
fillScene();
setupGui();
drawHelpers();
addToDOM();
animate();
} catch(e) {
var errorReport = "Your program encountered an unrecoverable error, can not draw on canvas. Error was:<br/><br/>";
$('#container').append(errorReport+e);
}
最佳答案
问题是阴影相机的聚光灯视锥体太小了。我建议您使用以下代码行来调试相机的视锥体:
scene.add( new THREE.CameraHelper( light.shadow.camera ) );
spotlight.shadow.camera.far
来修复您的代码。到
2500
的值.
关于three.js - 当我在 udacity 中完成类(class)练习并想在three.js中添加阴影时遇到一些问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61290570/
我只想知道它们之间的区别: .class .class{ font-size:14px; } 对比: .class > .class{ font-size:14px; } 是一样的东西吗? 最佳答案
PrimeFaces 文档的以下摘录使标题中描述的两个选择器之间似乎存在差异: .ui-widget, .ui-widget .ui-widget { font-size: 90% !imp
我正在尝试选择特定值。但我遇到了一个问题。 我有两个元素,一个有 X Y,另一个有 X Y Z。 当选择 X Y Z 时,我也收到 X Y 的值...有没有办法让它寻找 X AND Y AND Z 而
.class.class 和 .class .class 有什么区别? 最佳答案 .class .class 匹配类 .class 的任何元素,这些元素是类 .class< 的另一个元素的后代/. .
我正在研究 Classname.class 和 Classname.class.toString() 并发现了一些不寻常的东西。 .class 在同一个类上运行时似乎等同于 .class。尽管 .cl
我试图在Dart中扩展列表并在此列表中使用另一个类。 这是我的示例,其中注释出了问题: import "Radio.dart"; // extends ListBase { List ra
我有一个很大的“经理”类,我认为它做得太多了,但我不确定如何将它划分为更多逻辑单元。 一般来说类主要由以下方法组成: class FooBarManager{ GetFooEntities();
我在一个文件中定义了一个抽象父类(super class),在另一个文件中定义了一个子类。我需要父类(super class)文件和堆栈跟踪报告来找到一个包含它。 但是,当它到达“extends”行时
我在 A. Alexenderscu 的现代 C++ 设计中找到了一些模板示例 作者使用以下行的地方 template class CheckingPolicy // class SmartPt
看一下这段代码: public static class A { public void doA() { } } public static class B extends A {
我有两个具有 .body 类的 div,但是,一个位于另一个具有 .box 类的 div 中 - 如下所示: 我只想为 .box 内部的 .body 设置样式...但我在下面所
我一定是遗漏了 C++ 规范中的某些内容,因为我无法解释为什么以下代码可以成功编译: class MyClass { static void fun(); }; int main() { MyClas
我正在尝试在 python 中“模拟”命名空间。我使用内部和外部类层次结构来创建我的命名空间。例如,您希望将文件(如资源)的路径保存在一个位置。我试过这样的事情: src = #path to sou
在试验 online crystal compiler 时(这太棒了),我遇到了一个我似乎无法找到解释的错误: class Person class Current < self end
在查看我的一段代码时,我陷入了如下的一条语句。 TMyObjectClass = TMyObject 类; 我有点困惑,不知道这句话是什么意思。由于 TMyObjectClass 在该语句上方没有声明
我正在编写一个简单的应用程序,以学习一些基本的Dart编程,但无法弄清楚其结构和包含的内容-我得到了一个重复的类Point 首先,我有一个叫做MouseTrack的主类。它将初始化列表并产生循环。 #
在 org.springframework.core.SerializableTypeWrapper (版本 5.2.3),第 112 行有以下代码: if (GraalDetector.in
我希望将鼠标悬停在子导航中的列表项上,以激活页面上该类别中所有项的类(不仅仅是父元素或同级元素)。有任何想法吗?这是我的意思的一个例子: img.BLUE {border:1px solid #FF
我正在通过 ClassLoader 加载类: Class clazz = urlClassLoader.loadClass(name.substring(0, name.length() - 6).r
以下简化的类在从 get() 返回值时执行不同的操作,具体取决于该类是被赋予 double 值还是数组作为模板参数: #include "array" #include "type_traits" t
我是一名优秀的程序员,十分优秀!