How do I render three.js in node.js?(如何在 node.js 中渲染three.js?)
问题描述
How do I render three.js code in node.js?
I'd like to export from blender, then open the export through fs and render a scene with it.
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.
it relies on three.js npm module
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:
The three.js module 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
这篇关于如何在 node.js 中渲染three.js?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 node.js 中渲染three.js?
				
        
 
            
        - 如何向 ipc 渲染器发送添加回调 2022-01-01
 - 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
 - 如何显示带有换行符的文本标签? 2022-01-01
 - 为什么我的页面无法在 Github 上加载? 2022-01-01
 - 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
 - 在不使用循环的情况下查找数字数组中的一项 2022-01-01
 - 从原点悬停时触发 translateY() 2022-01-01
 - 如何调试 CSS/Javascript 悬停问题 2022-01-01
 - 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
 - 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
 
						
						
						
						
						
				
				
				
				