如何在 node.js 中渲染三个.js?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19162510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:51:58  来源:igfitidea点击:

How do I render three.js in node.js?

javascriptnode.jscoffeescriptthree.js

提问by Funkodebat

How do I render three.js code in node.js?

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

I'd like to export from blender, then open the export through fsand render a scene with it.

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

回答by Funkodebat

This is kind of a smelly implementation, but the key parts to remember are the part where the geometry is created, everything else is pretty straightforward. I'm mostly putting this here for my own reference later, but it does work and it's cool to have 3d rendering in nodejs. oh yea it requires canvas to work too.

这是一个很糟糕的实现,但要记住的关键部分是创建几何体的部分,其他一切都非常简单。我主要是把它放在这里供我以后参考,但它确实有效,而且在 nodejs 中进行 3d 渲染很酷。哦,是的,它也需要画布才能工作。

it relies on three.jsnpm module https://github.com/uniba/node-three.js

它依赖于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

If you get an error cannot find ./lib/Three:

如果出现错误cannot find ./lib/Three

The three.jsmodule I mentioned might point to an old version of three internally. I remember having to go into the module and edit a file that require('./lib/Three')to require('./lib/three'). I guess he included a non specific three in his package.json, so it got updated without his npm module being updated.. could be fixed by now

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

回答by Programah

I don't see any reason why you couldn't use something like electron, which allows you to create desktop apps and implement node.js in them. This isn't an exact answer to the question, but for the sake of people looking for something like this, I will post this answer

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

回答by Dmitry Matveev

You should not run code for visuals on the server really, if you are doing computationally expensive stuff on the server you will need to be prepared to put a considerable buk into those. What you need is to delegate exporting and rendering to the client machines.

你真的不应该在服务器上运行视觉代码,如果你在服务器上做计算量很大的事情,你需要准备好投入大量的精力。您需要的是将导出和渲染委托给客户端计算机。