react组件化3d模型查看器

react-3d-viewer

一个基于react.js的组件化3d模型查看工具. Demo请戳 dwqdaiwenqi.github.io/react-3d-vi…

特征

  • 组件化的
  • .setState()方法更新UI
  • 支持 gltf、obj、mtl、json、dae 模型格式 - 其他格式以后支持
  • 提供 <DirectionLight/><AmbientLight/> 组件 - 其他灯光组件以后提供

使用

从npm或cdn上获取react-3d-viewer

npm i react-3d-viewer
复制代码

Commonjs

import {OBJModel} from 'react-3d-viewer'

render(){
  return(
    <div>
      <OBJModel src="./a.obj"/>
    </div>
  )
}
复制代码
import {Tick,MTLModel} from 'react-3d-viewer'

render(){
  return(
    <div>
     <MTLModel 
        enableZoom = {false}
        position={{x:0,y:-100,z:0}}
        rotation={this.state.rotation}
        mtl="./src/lib/model/freedom.mtl"
        src="./src/lib/model/freedom.obj"
     />
    </div>
  )
}
componentWillMount(){
    this.tick.animate = false
}
componentDidMount(){
  this.tick = Tick(()=>{
    var {rotation} = this.state
    rotation.y += 0.005
    this.setState({rotation})

  })
}
复制代码

HTML

<script src="https://unpkg.com/react-3d-viewer@latest/dist/scripts/main.js"></script>
<script src="https://unpkg.com/react@latest/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@latest/umd/react-dom.production.min.js"></script>
<div id="example"></div>
<script >
  // 可以,但别这么做。。。
  ReactDOM.render(
  React.createElement('div',{ style: { width: 600, margin: '0px auto' } },
  React.createElement(React3DViewer.JSONModel, {src:'./src/lib/model/kapool.js'})
  )
  ,document.getElementById('example'));
</script>
复制代码

更多Demo,这儿

属性

属性名类型默认值描述
widthnumber500容器宽
heightnumber500容器高
onLoadfunctionundefined函数加载完成后调用
onProgressfunctionundefined函数加载过程中调用
enableKeysboolentrue启用或不启用键盘控制
enableRotateboolentrue启用或不启用相机的水平和垂直方向旋转
enableZoomboolentrue启用或不启用相机的缩放
enabledboolentrue启用或不启用整个控制
srcstringundefined文件的路径
mtlstringundefined.mtl文件的路径
anitialiasboolentrue是否启用抗锯齿
positionobject{x:0,y:0,z:0}对象的坐标
rotationobject{x:0,y:0,z:0}对象的旋转

如何工作的

<FormatModel>组件创建了相机、场景、灯光和WebGL渲染器。它往文档中添加了一个填满了视口的DOM节点(<canvas>元素)。 内部scene是实时渲染的,在componentDidUpdate中检测props,改变对象的属性。

License

MIT

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
React组件开发是一种将用户界面划分为独立可复用的部分的方法。在React中,组件是构建用户界面的基本单位,可以将其视为自包含、独立运作的模块。 在React中创建组件的一种常见方式是使用类组件。一个类组件是一个继承自React.Component的JavaScript类,它有自己的状态(state)和生命周期方法。以下是一个示例: ```jsx import React from 'react'; class MyComponent extends React.Component { constructor(props) { super(props); this.state = { // 初始状态 }; } componentDidMount() { // 组件挂载后执行的操作 } componentDidUpdate(prevProps, prevState) { // 组件更新后执行的操作 } componentWillUnmount() { // 组件卸载前执行的操作 } render() { return ( // 组件的渲染内容 <div> <h1>Hello, World!</h1> </div> ); } } export default MyComponent; ``` 以上示例中,MyComponent是一个React组件。它定义了构造函数(constructor),用于初始组件的状态。还包含了生命周期方法(componentDidMount, componentDidUpdate, componentWillUnmount),用于在不同的组件生命周期阶段执行特定的操作。最后,render方法用于返回组件的渲染内容。 使用该组件时,可以在其他地方导入并使用它: ```jsx import React from 'react'; import MyComponent from './MyComponent'; function App() { return ( <div> <h1>My App</h1> <MyComponent /> </div> ); } export default App; ``` 在上述示例中,App组件中使用了MyComponent组件。通过组件开发,我们可以更好地组织和管理代码,并提高代码的可维护性和重用性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值