1说明: 1。1APythonThreeJSbridgeutilizingtheJupyterwidgetinfrastructure。 1。2Pythreejs是基于Jupyter,连接python和threejs的一个3d可视化图形库。 1。3使用JupyterInteractiveWidgets中的three。js,为Jupyter笔记本提供交互式3D图形。 编辑2准备: 2。1官网:https:pythreejs。readthedocs。ioenstableindex。htmlhttps:github。comjupyterwidgetspythreejs 2。2安装:pipinstallpythreejs本机安装sudopip3。8installihttps:mirrors。aliyun。compypisimplepythreejs 2。3环境: 华为笔记本电脑、深度deepinlinux操作系统、谷歌浏览器、python3。8和微软vscode编辑器。 例子 注意以下均是用微软编辑器vscode调用jupyter,格式: 3长方体: 3。1代码:frompythreejsimportgBoxGeometry(width5,height10,depth15,widthSegments5,heightSegments10,depthSegments15,)g 3。2操作和效果图:后面就放效果图。 4三条线坐标轴: 4。1代码:frompythreejsimportimportnumpyasnpsize4linesgeomGeometry(vertices〔〔0,0,0〕,〔size,0,0〕,〔0,0,0〕,〔0,size,0〕,〔0,0,0〕,〔0,0,size〕〕,colors〔red,red,green,green,white,orange〕)linesLine(geometrylinesgeom,materialLineBasicMaterial(linewidth5,vertexColorsVertexColors),typeLinePieces,)sceneScene(children〔lines,DirectionalLight(colorccaabb,position〔0,10,0〕),AmbientLight(colorcccccc),〕)cPerspectiveCamera(position〔10,10,10〕)rendererRenderer(camerac,backgroundblack,backgroundopacity1,scenescene,controls〔OrbitControls(controllingc)〕,width400,height400)renderer 4。2效果图: 5彩色正方体: 5。1colorcube的代码:frompythreejsimportimportnumpyasnpvertices顶点verticesnp。asarray(〔〔0,0,0〕,〔0,0,1〕,〔0,1,0〕,〔0,1,1〕,〔1,0,0〕,〔1,0,1〕,〔1,1,0〕,〔1,1,1〕〕,dtypefloat32)facesnp。asarray(〔〔0,1,3〕,〔0,3,2〕,〔0,2,4〕,〔2,6,4〕,〔0,4,1〕,〔1,4,5〕,〔2,3,6〕,〔3,7,6〕,〔1,5,3〕,〔3,5,7〕,〔4,6,5〕,〔5,6,7〕〕,dtypeuint16)。ravel()Weneedtoflattenindexarrayvertexcolorsnp。asarray(〔(0,0,0),(0,0,1),(0,1,0),(1,0,0),(0,1,1),(1,0,1),(1,1,0),(1,1,1)〕,dtypefloat32)Geometry几何位置cubeGeometryBufferGeometry(attributesdict(BufferAttribute缓冲属性positionBufferAttribute(vertices,normalizedFalse),indexBufferAttribute(faces,normalizedFalse),colorBufferAttribute(vertexcolors),))myobjectCubeMesh(geometrycubeGeometry,materialMeshLambertMaterial(vertexColorsVertexColors),position〔0。5,0。5,0。5〕Centerthecube)cCubePerspectiveCamera(position〔3,3,3〕,fov20,children〔DirectionalLight(colorffffff,position〔3,5,1〕,intensity0。5)〕)sceneCubeScene(children〔myobjectCube,cCube,AmbientLight(colordddddd)〕)rendererCubeRenderer(cameracCube,backgroundblack,backgroundopacity1,scenesceneCube,controls〔OrbitControls(controllingcCube)〕)rendererCube 5。2效果图: 6origugv: 6。1代码:frompythreejsimportimportnumpyasnpffunctionf(origu,origv,out){scaleuandvtotherangesIwant:〔0,2pi〕varu2Math。PIvarv2Math。PIvarxMath。sin(u);varyMath。cos(v);varzMath。cos(uv);out。set(x,y,z)}surfgParametricGeometry(funcf,slices16,stacks16)surfMesh(geometrysurfg,materialMeshLambertMaterial(colorgreen,sideFrontSide))surf2Mesh(geometrysurfg,materialMeshLambertMaterial(coloryellow,sideBackSide))cPerspectiveCamera(position〔5,5,3〕,up〔0,0,1〕,children〔DirectionalLight(colorwhite,position〔3,5,1〕,intensity0。6)〕)sceneScene(children〔surf,surf2,c,AmbientLight(intensity0。5)〕)rendererRenderer(camerac,scenescene,controls〔OrbitControls(controllingc)〕,width400,height400)renderer 6。2效果图: 7ball: 7。1代码:frompythreejsimportimportnumpyasnpballMesh(geometrySphereGeometry(radius1,widthSegments32,heightSegments24),materialMeshLambertMaterial(colorred),position〔2,1,0〕)cPerspectiveCamera(position〔0,5,5〕,up〔0,1,0〕,children〔DirectionalLight(colorwhite,position〔3,5,1〕,intensity0。5)〕)sceneScene(children〔ball,c,AmbientLight(color777777)〕)rendererRenderer(camerac,scenescene,controls〔OrbitControls(controllingc)〕)renderer 7。2效果图: 8附注几张gif图:代码省略。 自己整理并分享出来 喜欢的人,请点赞、关注、评论、转发和收藏。