向场景添加灯光没有效果

发布于 2024-12-08 21:47:05 字数 1166 浏览 1 评论 0原文

我刚开始玩 Three.JS,但一开始就卡住了。当我向场景添加灯光时,它没有任何效果。

renderer = new THREE.WebGLRenderer()
camera = new THREE.PerspectiveCamera 45, # View Angle
  800 / 640, # Aspect
  0.1, # Near
  10000 # Far

camera.position.z = 300

scene = new THREE.Scene()
renderer.setSize 800, 640

document.body.appendChild(renderer.domElement)

createSphere = (radius = 50, segments = 16, rings = 16) ->
  sphere = new THREE.SphereGeometry(radius, segments, rings)
  material = new THREE.MeshBasicMaterial {
    color: 0xCC000F,
    shading: THREE.SmoothShading,
    ambient: 0x555555,
    specular: 0xffffff
  }

  new THREE.Mesh sphere, material 

light = new THREE.PointLight(0x0040ff)
light.position.x = 10
light.position.y = 50
light.position.z = 300
light.intensity  = 0.1

object = createSphere()
scene.add new THREE.AmbientLight(0x0000F0)
scene.add light
scene.add object

draw = ->
  time = new Date().getTime() * 0.0005;
  light.position.x = Math.sin(time * 0.7) * 30
  object.rotation.x += 0.02
  renderer.render scene, camera
  requestAnimationFrame draw

draw()

我还使用解析后的 js 创建了一个 js fiddle

I was just starting to play around with Three.JS but I am stuck at the beginning. When I add a light to the scene, it has no effect.

renderer = new THREE.WebGLRenderer()
camera = new THREE.PerspectiveCamera 45, # View Angle
  800 / 640, # Aspect
  0.1, # Near
  10000 # Far

camera.position.z = 300

scene = new THREE.Scene()
renderer.setSize 800, 640

document.body.appendChild(renderer.domElement)

createSphere = (radius = 50, segments = 16, rings = 16) ->
  sphere = new THREE.SphereGeometry(radius, segments, rings)
  material = new THREE.MeshBasicMaterial {
    color: 0xCC000F,
    shading: THREE.SmoothShading,
    ambient: 0x555555,
    specular: 0xffffff
  }

  new THREE.Mesh sphere, material 

light = new THREE.PointLight(0x0040ff)
light.position.x = 10
light.position.y = 50
light.position.z = 300
light.intensity  = 0.1

object = createSphere()
scene.add new THREE.AmbientLight(0x0000F0)
scene.add light
scene.add object

draw = ->
  time = new Date().getTime() * 0.0005;
  light.position.x = Math.sin(time * 0.7) * 30
  object.rotation.x += 0.02
  renderer.render scene, camera
  requestAnimationFrame draw

draw()

I also created a js fiddle with the parsed js.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丶情人眼里出诗心の 2024-12-15 21:47:06

MeshBasicMaterial 不支持光照,您应该更改材质。支持光照的基本材质是 MeshLambertMaterial,我已经更新了你的 jsfiddle。

更详细的例子:
http://mrdoob.github.com/third.js/examples/canvas_lights_pointlights.html

MeshBasicMaterial doesn't support lighting, you should change your material. A basic material supporting lighting is MeshLambertMaterial, I have updated your jsfiddle.

More detailed example:
http://mrdoob.github.com/three.js/examples/canvas_lights_pointlights.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文