Code Monkey home page Code Monkey logo

react-3d-viewer's Introduction

English| 简体中文

react-3d-viewer

A 3D model viewer component based on react.js. Demo located at https://dwqdaiwenqi.github.io/react-3d-viewer/site/

Features

  • Component-Based
  • Update UI using .setState() method
  • Support gltf,obj,mtl,json,dae model formats - other formats will be added in the future.
  • Provied <DirectionLight/> and <AmbientLight/> light components - other components will be provided in the future.

Usage

Get react-3d-viewer through npm or cdn:

npm i react-3d-viewer

Work with module bundler

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

render(){
  return(
    <div>
      <OBJModel src="./a.obj" texPath=""/>
    </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}
        texPath="./src/lib/model/"
        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})

  })
}
import {DAEModel,DirectionLight } from 'react-3d-viewer'

render(){
  return(
    <div>
     <DAEModel 
        src={'./src/lib/model/Ruins_dae.dae'}
        onLoad={()=>{
          // ...
        }}
      >
        <DirectionLight color={0xff00ff}/>
      </DAEModel>
    </div>
  )
}
 

HTML

<script src="https://unpkg.com/react-3d-viewer@latest/dist/scripts/react-3d-viewer.js"></script>
<script src="https://unpkg.com/[email protected]/umd/react.production.min.js"></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.production.min.js"></script>
<div id="example"></div>
<script >
  // It's not recommended to use this way.
  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>

To view a demo,click here.

Properties

Property Type Default Description
width number 500 The width of container
height number 500 The height of container
texPath string '' Set the base path for the img file
onLoad function undefined A function to be called after the loading is successfully completed
onProgress function undefined A function to be called while the loading is in progress
enableKeys boolen true Enable or disable the use of keyboard controls
enableRotate boolen true Enable or disable horizontal and vertical rotation of the camera
enableZoom boolen true Enable or disable zooming of the camera
enabled boolen true Whether or not the controls are enabled
src string undefined The path of the file
mtl string undefined The path of the .mtl file
anitialias boolen true Whether to perform antialiasing
position object {x:0,y:0,z:0} Object's position
rotation object {x:0,y:0,z:0} Object's rotation

License

MIT

react-3d-viewer's People

Contributors

dependabot[bot] avatar dwqdaiwenqi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

react-3d-viewer's Issues

20 MB OBJ file not loading.

Hi, first of all, we would like to thank you for this amazing library. It works fine and it is very efficient. We can load small files without any issue in ReactJS when when we try to Load a file having a size of 20 MB this library doesn't work and show a blank screen. Is there any size limitations for using this library?
We would be happy to know more about it and contribute to fixing this issue.

Kind Regards.

Cross Origin with textures

Hello @dwqdaiwenqi ,
I really appreciate this repo, actually we are developing an electron / react app, and we need a simple 3d view inside of it.
I tried your repo and this is exactly what we need.
We have some troubles loading the texture file though, because we are taking it from a web server and THREE is giving us a cross-origin error

"THREE.WebGLState: DOMException: Failed to execute 'texImage2D' on 'WebGLRenderingContext': The image element contains cross-origin data, and may not be loaded"

On the server we have all the response headers for CORS

image

How to use Gltf viewer

I was following the example below to render a GLTF asset:
https://dwqdaiwenqi.github.io/react-3d-viewer/site/#/GLTF

It works well if the GLTF only contains a .gltf file, but when I try to render a GLTF folder contains .gltf file .png for material, and .bin file, and I use the `src="../abc.gltf", "An error happened", how to properly reference the GLTF asset with multiple files. Can you provide an example?

Thank you!

OBJ Model Usage issues

Hello,

Im trying to use the OBJModel from the lib but i keep getting following error:

./node_modules/react-3d-viewer/dist/scripts/react-3d-viewer.js
Cannot find file: 'index.js' does not match the corresponding name on disk: './node_modules/React/react'.

Setup:

import React, { Component } from 'react'
import { OBJModel } from 'react-3d-viewer'
import TEST_OBJ_MODEL from 'static/__TEST_IMAGES__/test_3d_model.obj'

class 3DViewer extends Component {

    render() {
        // const { t } = this.props

        return (
            <div>
                    <OBJModel 
                        width="400" height="400"  
                        position={{x:0,y:-100,z:0}}
                        src={TEST_OBJ_MODEL} 
                        texPath=""
                    />
            </div>
        );
    }
}

Not sure what im doing wrong here, im just following the example on the website :)

But i will give you some ❤️ for making such lib :D

gray MTL Model problem

Hi, thanks for your 3d viewer.

I tried MTLModel with several 3d obj and mtl files.
One of them is this dog model, https://free3d.com/3d-model/dog-v1--722788.html
and another one is this kitchen table model. https://free3d.com/3d-model/kitchen-set-15847.html

Anyway, when I import {MTLModel} and put obj and mtl file
mtl file doesn't work at all. All I get is gray(almost black) 3d objects like this:

the dog model
https://user-images.githubusercontent.com/39916556/48070323-f5765480-e21a-11e8-9479-d31589f28c5c.png

the kithcen table model
https://user-images.githubusercontent.com/39916556/48070178-9a446200-e21a-11e8-921c-321a1307b07a.png

Also, this is my code for MTLModel:
<MTLModel
width="500" height="500"
position={{x:0,y:0,z:0}}
mtl={dogMaterial}
src={dogModel}

/>

Is there any recommendation for me?

r.setValue is not a function error in production

This component renders fine when I'm developing and viewing with npm start, but after running npm run build and uploading the build to my production server, I just get this error in console:

react-3d-viewer.js:6821 Uncaught TypeError: r.setValue is not a function
    at $n.setValue (react-3d-viewer.js:6821)
    at Be (react-3d-viewer.js:9198)
    at Ar.renderBufferDirect (react-3d-viewer.js:8970)
    at Ue (react-3d-viewer.js:9113)
    at De (react-3d-viewer.js:9100)
    at Ar.render (react-3d-viewer.js:9312)
    at Object.fuc (react-3d-viewer.js:17663)
    at react-3d-viewer.js:21729
    at Array.forEach (<anonymous>)
    at e (react-3d-viewer.js:21726)

repeatedly, and the object is never rendered.

Any idea what could be causing this?

Unable to Update Model Source With State Update

Our use case requires us to query a database for the location of a file. We update state with the returned URL and we are trying to set the src of the model with the state. For example:

< OBJModel src={this.state.filePath} texPath="" />

This does not work. Is there something I am missing here? Or is this something that would be difficult to add? The only way we can get this to work is to hardcode the url into the src path.

Thanks in advance for any help you can offer!

Setting zoom level

Is it possible to set the zoom level when loading a model? Should I perhaps be passing something as an onLoad function?

How to access model meshes etc

Loading a model using <GLTFModel....../> is OK,
(a) how does one access the model data, such as vertices?
(b) How does one add user annotations into the model ?
(c) Is there a full list of parameters for <GLTFModel.... other than src= & width=...?)

模型的透明没生效,MTLModel

我使用的是MTLModel组件,我的模型本身是有透明的
image
但是我用组件的运行结果却是这样子的
image
这个有办法解决吗

Setting width by percentage

is there any i can set the width by percantage(like 100%). I know right now i can do it by width={400}

<GLTFModel
                                                            src={this.state.apiUrl + '/api/v1/visitImageRequirementInfo/getImageByImagePathId?imagePath=' + this.props.filePathing} >
                                                            <AmbientLight color={0xffffff}/>
                                                            <DirectionLight color={0xffffff} position={{x:100,y:200,z:100}}/>
                                                            <DirectionLight color={0xff00ff} position={{x:-100,y:200,z:-100}}/>
                                                        </GLTFModel>

unable to show the file

I am trying to show a .obj file.

       <OBJModel
                    width="400" height="400"
                    position={{x: 0, y: -100, z: 0}}
                    src={mayas}
                    onLoad={() => {
                        //...
                    }}
                    onProgress={xhr => {
                        //...
                    }}
                />

I am gettting this error

./static/images/Maya.obj 1:0
Module parse failed: Unexpected character '#' (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> # Alias OBJ Model File
| # Exported from SketchUp, (c) 2000-2012 Trimble Navigation Limited
| # File units = inches

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.