gpt4 book ai didi

javascript - 如何在 node.js 中渲染three.js?

转载 作者:IT老高 更新时间:2023-10-28 22:12:39 37 4
gpt4 key购买 nike

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

我想从 blender 导出,然后通过 fs 打开导出并用它渲染一个场景。

最佳答案

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

它依赖于 three.js npm 模块 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

如果遇到错误找不到./lib/Three:

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

关于javascript - 如何在 node.js 中渲染three.js?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19162510/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com