23

如何在 node.js 中渲染 three.js 代码?

我想从搅拌机导出,然后打开导出fs并用它渲染一个场景。

4

3 回答 3

25

这是一种臭名昭著的实现,但要记住的关键部分是创建几何图形的部分,其他一切都非常简单。稍后我主要将其放在这里供我自己参考,但它确实有效,并且在 nodejs 中进行 3d 渲染很酷。哦,是的,它也需要画布才能工作。

它依赖于three.jsnpm 模块 https://github.com/uniba/node-three.js

fs = require("fs")

THREE = require("three.js")
join = require("path").join

app.get '/test/top_:top_id/side_:side_id/x_:x/y_:y.jpg', (req, res, next) =>

  width = 660
  height = 500

  camera = new THREE.PerspectiveCamera(50, width / height, 1, 1000)
  scene = new THREE.Scene()
  renderer = new THREE.CanvasRenderer( )
  renderer.setSize width, height

  camera.position.z = 100

  camera_container = new THREE.Object3D
  scene.add camera_container
  camera_container.add camera

  camera.position.z = 75

  # We have one background plane
  plane_image = new Image
  plane_image.src = fs.readFileSync TOP_DIR + "public/images/vtx_logo.jpg"
  texture = new THREE.Texture plane_image, new THREE.UVMapping()
  texture.needsUpdate = true

  loader = new THREE.JSONLoader()

  geometry = new THREE.PlaneGeometry(200, 200)
  material = new THREE.MeshBasicMaterial
    color       : 0x698144
    #shading        : THREE.SmoothShading
    map     : texture
    overdraw: true
  plane = new THREE.Mesh geometry, material
  plane.position.z = -50
  plane.position.y = -4
  plane.position.x = 4.5

  # We also have an object in the foreground
  scene.add plane
  geometry = false
  loader.createModel JSON.parse(fs.readFileSync(TOP_DIR + 'public/blender_export.json')), (done) =>
    geometry = done

  # Imager.texture gives us a canvas based on some code that grabs specific info
  texture = new THREE.Texture (Imager.texture req.params.side_id, req.params.top_id), new THREE.UVMapping()
  texture.needsUpdate = true

  material = new THREE.MeshBasicMaterial
    color: 0xaaaaaa
    map: texture
    overdraw: true
  mesh = new THREE.Mesh geometry, material

  mesh.rotation.x = parseFloat req.params.x
  mesh.rotation.y = parseFloat req.params.y

  scene.add mesh
  mesh.dynamic = true
  renderer.render scene, camera

  renderer.domElement.toBuffer (err, buf) ->
    res.contentType 'image/jpg'
    res.send buf

如果您收到错误消息cannot find ./lib/Three

我提到的three.js模块可能在内部指向旧版本的三个。require('./lib/Three')我记得必须进入模块并编辑一个require('./lib/three'). 我猜他在他的 package.json 中包含了一个非特定的三个,所以它在没有更新他的 npm 模块的情况下得到了更新.. 现在可以修复

于 2013-10-03T14:58:44.930 回答
-1

我看不出你为什么不能使用像electron之类的东西,它允许你创建桌面应用程序并在其中实现 node.js。这不是问题的确切答案,但为了人们寻找这样的东西,我会发布这个答案

于 2017-08-04T20:43:11.213 回答
-15

你真的不应该在服务器上运行视觉代码,如果你在服务器上做计算昂贵的东西,你需要准备好在这些东西上投入大量资金。您需要将导出和渲染委托给客户端计算机。

于 2013-10-03T17:54:20.273 回答