├── .eslintcache ├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt └── src ├── App.js ├── App.scss ├── index.js ├── index.scss ├── reportWebVitals.js └── setupTests.js /.eslintcache: -------------------------------------------------------------------------------- 1 | [{"C:\\DevMobile\\web-React\\3d-controller\\src\\index.js":"1","C:\\DevMobile\\web-React\\3d-controller\\src\\App.js":"2","C:\\DevMobile\\web-React\\3d-controller\\src\\reportWebVitals.js":"3"},{"size":501,"mtime":1608919101025,"results":"4","hashOfConfig":"5"},{"size":5587,"mtime":1609584955540,"results":"6","hashOfConfig":"5"},{"size":362,"mtime":499162500000,"results":"7","hashOfConfig":"5"},{"filePath":"8","messages":"9","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"10"},"19ozj91",{"filePath":"11","messages":"12","errorCount":0,"warningCount":2,"fixableErrorCount":0,"fixableWarningCount":0,"source":null},{"filePath":"13","messages":"14","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"10"},"C:\\DevMobile\\web-React\\3d-controller\\src\\index.js",[],["15","16"],"C:\\DevMobile\\web-React\\3d-controller\\src\\App.js",["17","18"],"C:\\DevMobile\\web-React\\3d-controller\\src\\reportWebVitals.js",[],{"ruleId":"19","replacedBy":"20"},{"ruleId":"21","replacedBy":"22"},{"ruleId":"23","severity":1,"message":"24","line":78,"column":5,"nodeType":"25","messageId":"26","endLine":98,"endColumn":6},{"ruleId":"23","severity":1,"message":"24","line":103,"column":5,"nodeType":"25","messageId":"26","endLine":123,"endColumn":6},"no-native-reassign",["27"],"no-negated-in-lhs",["28"],"default-case","Expected a default case.","SwitchStatement","missingDefaultCase","no-global-assign","no-unsafe-negation"] -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Getting Started with Create React App 2 | 3 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 4 | 5 | ## Available Scripts 6 | 7 | In the project directory, you can run: 8 | 9 | ### `npm start` 10 | 11 | Runs the app in the development mode.\ 12 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 13 | 14 | The page will reload if you make edits.\ 15 | You will also see any lint errors in the console. 16 | 17 | ### `npm test` 18 | 19 | Launches the test runner in the interactive watch mode.\ 20 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 21 | 22 | ### `npm run build` 23 | 24 | Builds the app for production to the `build` folder.\ 25 | It correctly bundles React in production mode and optimizes the build for the best performance. 26 | 27 | The build is minified and the filenames include the hashes.\ 28 | Your app is ready to be deployed! 29 | 30 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 31 | 32 | ### `npm run eject` 33 | 34 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 35 | 36 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 37 | 38 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 39 | 40 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 41 | 42 | ## Learn More 43 | 44 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 45 | 46 | To learn React, check out the [React documentation](https://reactjs.org/). 47 | 48 | ### Code Splitting 49 | 50 | This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting) 51 | 52 | ### Analyzing the Bundle Size 53 | 54 | This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size) 55 | 56 | ### Making a Progressive Web App 57 | 58 | This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app) 59 | 60 | ### Advanced Configuration 61 | 62 | This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration) 63 | 64 | ### Deployment 65 | 66 | This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment) 67 | 68 | ### `npm run build` fails to minify 69 | 70 | This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify) 71 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "3d-controller", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^5.11.6", 7 | "@testing-library/react": "^11.2.2", 8 | "@testing-library/user-event": "^12.6.0", 9 | "drei": "^2.2.12", 10 | "react": "^17.0.1", 11 | "react-dom": "^17.0.1", 12 | "react-scripts": "4.0.1", 13 | "react-spring": "^8.0.27", 14 | "react-three-fiber": "^5.3.10", 15 | "sass": "^1.30.0", 16 | "three": "^0.124.0", 17 | "web-vitals": "^0.2.4" 18 | }, 19 | "scripts": { 20 | "start": "react-scripts start", 21 | "build": "react-scripts build", 22 | "test": "react-scripts test", 23 | "eject": "react-scripts eject" 24 | }, 25 | "eslintConfig": { 26 | "extends": [ 27 | "react-app", 28 | "react-app/jest" 29 | ] 30 | }, 31 | "browserslist": { 32 | "production": [ 33 | ">0.2%", 34 | "not dead", 35 | "not op_mini all" 36 | ], 37 | "development": [ 38 | "last 1 chrome version", 39 | "last 1 firefox version", 40 | "last 1 safari version" 41 | ] 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kacemws/ThreeJs-Controllable/55fbb69535e223101bea264c7445d312b2669508/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kacemws/ThreeJs-Controllable/55fbb69535e223101bea264c7445d312b2669508/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kacemws/ThreeJs-Controllable/55fbb69535e223101bea264c7445d312b2669508/public/logo512.png -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import { useEffect, useRef, useState } from "react"; 2 | 3 | import { Canvas, useFrame } from "react-three-fiber"; 4 | 5 | import { softShadows } from "drei"; 6 | 7 | import "./App.scss"; 8 | 9 | import { useSpring, a } from "react-spring/three"; 10 | 11 | softShadows(); 12 | 13 | const Controllable = ({ 14 | moveForward, 15 | moveBackwards, 16 | moveLeft, 17 | moveRight, 18 | sizes, 19 | color, 20 | }) => { 21 | const mesh = useRef(null); // the reference for the mesh 22 | useFrame(({ clock, camera }) => { 23 | if (moveForward) { 24 | mesh.current.position.z += 0.05; 25 | console.log(Math.round(mesh.current.position.z)); 26 | camera.position.z += 0.049; 27 | } 28 | 29 | if (moveBackwards) { 30 | console.log(Math.round(mesh.current.position.z)); 31 | mesh.current.position.z -= 0.05; 32 | camera.position.z -= 0.049; 33 | } 34 | 35 | if (moveLeft) { 36 | camera.position.x -= 0.049; 37 | mesh.current.position.x -= 0.05; 38 | } 39 | 40 | if (moveRight) { 41 | mesh.current.position.x += 0.05; 42 | camera.position.x += 0.049; 43 | } 44 | }); // to rotate the shape defined in our mesh 45 | 46 | const [expand, setExpand] = useState(false); 47 | 48 | const props = useSpring({ 49 | scale: expand ? [2, 2, 2] : [1, 1, 1], // change the scale dynamically 50 | }); 51 | 52 | return ( 53 | { 55 | setExpand(!expand); // to expand the box on click 56 | }} 57 | // position={position} 58 | ref={mesh} 59 | castShadow 60 | {...props} 61 | > 62 | {/* position is an array in the x y z format */} 63 | 64 | {/* since our shape is a box, the sizes is going to be an array like so : width, height, depth */} 65 | 66 | 67 | ); 68 | }; 69 | 70 | function App() { 71 | const [moveForward, setForward] = useState(false); // to detect wheter the user is moving forward 72 | const [moveBackward, setBackward] = useState(false); // to detect wheter the user is moving backward 73 | const [moveLeft, setLeft] = useState(false); // to detect wheter the user is moving to the left 74 | const [moveRight, setRight] = useState(false); // to detect wheter the user is moving to the right 75 | 76 | const onKeyDown = (event) => { 77 | //event handler for when the user is pressing a button 78 | switch (event.keyCode) { 79 | case 38: /*up*/ 80 | case 87: 81 | /*W*/ setForward(true); 82 | break; 83 | 84 | case 40: /*down*/ 85 | case 83: 86 | /*S*/ setBackward(true); 87 | break; 88 | 89 | case 37: /*left*/ 90 | case 65: 91 | /*A*/ setLeft(true); 92 | break; 93 | 94 | case 39: /*right*/ 95 | case 68: 96 | /*D*/ setRight(true); 97 | break; 98 | } 99 | }; 100 | 101 | const onKeyUp = (event) => { 102 | //event handler for when the user is done pressing a button 103 | switch (event.keyCode) { 104 | case 38: /*up*/ 105 | case 87: 106 | /*W*/ setForward(false); 107 | break; 108 | 109 | case 40: /*down*/ 110 | case 83: 111 | /*S*/ setBackward(false); 112 | break; 113 | 114 | case 37: /*left*/ 115 | case 65: 116 | /*A*/ setLeft(false); 117 | break; 118 | 119 | case 39: /*right*/ 120 | case 68: 121 | /*D*/ setRight(false); 122 | break; 123 | } 124 | }; 125 | 126 | useEffect(() => { 127 | document.addEventListener("keydown", onKeyDown, false); // registreing the event that would determine where the user is moving 128 | document.addEventListener("keyup", onKeyUp, false); // registreing the event that would determine when the user is done moving 129 | return () => { 130 | document.removeEventListener("keydown", onKeyDown, false); // deleting the event that would determine where the user is moving 131 | document.removeEventListener("keyup", onKeyUp, false); // deleting the event that would determine when the user is done moving 132 | }; 133 | }, []); 134 | 135 | return ( 136 | <> 137 | 145 | {/* Lightning, PS : FOV means Field Of View, its just how much zoomed in we are in the view */} 146 | 147 | 148 | {/* Ambient light */} 149 | 150 | 162 | {/* Main Source of Light */} 163 | 164 | 165 | {/* left light */} 166 | 167 | 168 | {/* bottom light */} 169 | 170 | {/* Lightning */} 171 | 172 | {/* Plane */} 173 | 174 | 179 | {/* The floor 'or' scene for the app */} 180 | 181 | 182 | 183 | {/* Plane */} 184 | 185 | {/* shapes */} 186 | 194 | {/*you have to extract the mesh component and its ref and frame in order for it to work */} 195 | {/* shapes */} 196 | 197 | 198 | {/* Allows us to move the canvas around for different prespectives */} 199 | 200 | 201 | ); 202 | } 203 | 204 | export default App; 205 | -------------------------------------------------------------------------------- /src/App.scss: -------------------------------------------------------------------------------- 1 | * { 2 | box-sizing: border-box; 3 | } 4 | 5 | html, 6 | body, 7 | #root { 8 | width: 100%; 9 | height: 100%; 10 | margin: 0; 11 | padding: 0; 12 | } 13 | 14 | body { 15 | background-color: #f1f4f8; 16 | position: fixed; 17 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", 18 | "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", 19 | sans-serif; 20 | -webkit-font-smoothing: antialiased; 21 | -moz-osx-font-smoothing: grayscale; 22 | } 23 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import ReactDOM from "react-dom"; 3 | import "./index.scss"; 4 | import App from "./App"; 5 | import reportWebVitals from "./reportWebVitals"; 6 | 7 | ReactDOM.render( 8 | 9 | 10 | , 11 | document.getElementById("root") 12 | ); 13 | 14 | // If you want to start measuring performance in your app, pass a function 15 | // to log results (for example: reportWebVitals(console.log)) 16 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals 17 | reportWebVitals(); 18 | -------------------------------------------------------------------------------- /src/index.scss: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', 12 | monospace; 13 | } 14 | -------------------------------------------------------------------------------- /src/reportWebVitals.js: -------------------------------------------------------------------------------- 1 | const reportWebVitals = onPerfEntry => { 2 | if (onPerfEntry && onPerfEntry instanceof Function) { 3 | import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => { 4 | getCLS(onPerfEntry); 5 | getFID(onPerfEntry); 6 | getFCP(onPerfEntry); 7 | getLCP(onPerfEntry); 8 | getTTFB(onPerfEntry); 9 | }); 10 | } 11 | }; 12 | 13 | export default reportWebVitals; 14 | -------------------------------------------------------------------------------- /src/setupTests.js: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom'; 6 | --------------------------------------------------------------------------------