- 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/
从 Redis 获取消息时,onDone:(){print('done')} 从未起作用。 import 'package:dartis/dartis.dart' as redis show PubS
昨天我玩了一些vim脚本,并设法通过循环来对当前输入的内容进行状态栏预测(请参见屏幕截图(灰色+黄色栏))。 问题是,我不记得我是怎么得到的,也找不到我用于该vim魔术的代码片段(我记得它很简单):它
我尝试加载 bash_completion在我的 bash (3.2.25) 中,它不起作用。没有消息等。我在我的 .bashrc 中使用了以下内容 if [ -f ~/.bash_completio
我正在尝试构建一个 bash 完成例程,它将建议命令行标志和合适的标志值。例如在下面 fstcompose 命令我想比赛套路先建议 compose_filter= 标志,然后建议来自 [alt_seq
当我尝试在重定向符号后完成路径时,bash 完成的行为就好像它仍在尝试在重定向之前完成命令的参数一样。 例如: dpkg -l > /med标签 通过在 /med 之后点击 Tab我希望它完成通往 /
我的类中有几个 CAKeyframeAnimation 对象。 他们都以 self 为代表。 在我的animationDidStop函数中,我如何知道调用来自哪里? 是否有任何变量可以传递给 CAKe
我有一个带有 NSDateFormatter 的 NSTextField。格式化程序接受“mm/dd/yy”。 可以自动补全日期吗?因此,用户可以输入“mm”,格式化程序将完成当前月份和年份。 最佳答
有一个解决方案可以使用以下方法完成 NSTextField : - (NSArray *)control:(NSControl *)control textView:(NSTextView *)tex
我正在阅读 Passport 的文档,我注意到 serialize()和 deserialize() done()被调用而不被返回。 但是,当使用 passport.use() 设置新策略时在回调函数
在 ubuntu 11.10 上的 Firefox 8.0 中,尽管 img.complete 为 false,但仍会调用 onload 函数 draw。我设法用 setTimeout hack 解决
假设我有两个与两个并行执行的计算相对应的 future 。我如何等到第一个 future 准备好?理想情况下,我正在寻找类似于Python asyncio's wait且参数为return_when=
我正在寻找一种 Java 7 数据结构,其行为类似于 java.util.Queue,并且还具有“最终项目已被删除”的概念。 例如,应可以表达如下概念: while(!endingQueue.isFi
这是一个简单的问题。 if ($('.dataTablePageList')) { 我想做的是执行一个 if 语句,该语句表示如果具有 dataTablesPageList 类的对象也具有 menu
我用replaceWith批量替换了许多div中的html。替换后,我使用 jTruncate 来截断文本。然而它不起作用,因为在执行时,replaceWith 还没有完成。 我尝试了回调技巧 ( H
有没有办法调用 javascript 表单 submit() 函数或 JQuery $.submit() 函数并确保它完成提交过程?具体来说,在一个表单中,我试图在一个 IFrame 中提交一个表单。
我有以下方法: function animatePortfolio(fadeElement) { fadeElement.children('article').each(function(i
我刚刚开始使用 AndEngine, 我正在像这样移动 Sprite : if(pValueY < 0 && !jumping) { jumping =
我正在使用 asynctask 来执行冗长的操作,例如数据库读取。我想开始一个新 Activity 并在所有异步任务完成后呈现其内容。实现这一目标的最佳方法是什么? 我知道 onPostExecute
我有一个脚本需要命令名称和该命令的参数作为参数。 所以我想编写一个完成函数来完成命令的名称并完成该命令的参数。 所以我可以这样完成命令的名称 if [[ "$COMP_CWORD" == 1 ]];
我的应用程序有一个相当奇怪的行为。我在 BOOT_COMPLETE 之后启动我的应用程序,因此在我启动设备后它是可见的。 GUI 响应迅速,一切正常,直到我调用 finish(),按下按钮时,什么都没
我是一名优秀的程序员,十分优秀!