Appearance
场景初始化
一、初始化3D场景
threejs-3d-container容器
1.1 src/components/Main/index.tsx
tsx
import React from "react";
import { init3D } from "./init-3d";
function Main() {
React.useEffect(() => {
const dom = document.getElementById('threejs-3d-container')
const { scene } = init3D(dom!);
return () => {
if (dom) {
dom.innerHTML = ''
}
}
}, [])
return (
<div className="Main">
<div id="threejs-3d-container"></div>
</div>
);
}
export default Main;1.2 初始化3D场景元素
- 创建
Scene、Camera、Renderer、OrbitControls renderer.setClearColor设置背景颜色浅黄色。- 宽度是
window.innerWidth高度是window.innerHeight - 60
1.3 src/components/Main/init-3d.ts
ts
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
export function init3D(dom: HTMLElement) {
const scene = new THREE.Scene();
const asesHelper = new THREE.AxesHelper(500);
scene.add(asesHelper);
const directionLight = new THREE.DirectionalLight(0xffffff);
directionLight.position.set(500, 400, 300);
scene.add(directionLight);
const ambientLight = new THREE.AmbientLight(0xffffff);
scene.add(ambientLight);
const width = window.innerWidth
const height = window.innerHeight - 60;
const camera = new THREE.PerspectiveCamera(60, width/height, 1, 10000);
camera.position.set(500, 500, 500);
camera.lookAt(0, 0, 0);
const renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(width, height);
renderer.setClearColor('lightyellow');
function render() {
renderer.render(scene, camera);
requestAnimationFrame(render);
}
render();
dom.append(renderer.domElement);
window.onresize = () => {
const width = window.innerWidth
const height = window.innerHeight - 60;
renderer.setSize(width, height);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
const controls = new OrbitControls(camera, renderer.domElement);
return {
scene,
controls
}
}二、初始化2D场景
threejs-2d-container容器
2.1 src/components/Main/index.tsx
tsx
import React from "react";
import { init3D } from "./init-3d";
import { init2D } from "./init-2d";
function Main() {
React.useEffect(() => {
const dom = document.getElementById('threejs-3d-container')
const { scene } = init3D(dom!);
return () => {
if (dom) {
dom.innerHTML = ''
}
}
}, [])
React.useEffect(() => {
const dom = document.getElementById('threejs-2d-container')
const { scene } = init2D(dom!);
return () => {
if (dom) {
dom.innerHTML = ''
}
}
}, [])
return (
<div className="Main">
<div id="threejs-3d-container"></div>
<div id="threejs-2d-container"></div>
</div>
);
}
export default Main;2.2 初始化2D场景元素
- 创建
Scene、Camera、Renderer、OrbitControls renderer.setClearColor设置背景颜色浅蓝色。- 宽度是
window.innerWidth高度是window.innerHeight - 60
2.3 src/components/Main/init-2d.ts
ts
import * as THREE from "three";
import { OrbitControls } from "three/addons/controls/OrbitControls.js";
export function init2D(dom: HTMLElement) {
const scene = new THREE.Scene();
const axesHelper = new THREE.AxesHelper(500);
scene.add(axesHelper);
const directionalLight = new THREE.DirectionalLight(0xffffff);
directionalLight.position.set(500, 400, 300);
scene.add(directionalLight);
const ambientLight = new THREE.AmbientLight(0xffffff);
scene.add(ambientLight);
const width = window.innerWidth;
const height = window.innerHeight - 60;
const camera = new THREE.PerspectiveCamera(60, width / height, 1, 10000);
camera.position.set(500, 500, 500);
camera.lookAt(0, 0, 0);
const renderer = new THREE.WebGLRenderer({
antialias: true,
});
renderer.setSize(width, height);
renderer.setClearColor("lightblue");
function render() {
renderer.render(scene, camera);
requestAnimationFrame(render);
}
render();
dom.append(renderer.domElement);
window.onresize = function () {
const width = window.innerWidth;
const height = window.innerHeight - 60;
renderer.setSize(width, height);
camera.aspect = width / height;
camera.updateProjectionMatrix();
};
const controls = new OrbitControls(camera, renderer.domElement);
return {
scene,
controls,
};
}三、查看效果
3.1 效果预览
- 让3D场景和2D场景重合,2D场景置于3D场景之上
- 调整菜单面板在场景之上
3.2 src/App.scss
diff
body {
margin: 0;
}
.wrap {
height: 100vh;
display: flex;
flex-direction: column;
.Header {
height: 60px;
border-bottom: 1px solid #000;
}
.editor {
flex: 1;
position: relative;
+ .Main {
+ position: relative;
+ #threejs-3d-container {
+ position: absolute;
+ z-index: 1;
+ }
+ #threejs-2d-container {
+ position: absolute;
+ z-index: 2;
+ }
+ }
.drawer-bar {
width: 10px;
height: 80px;
background: black;
position: absolute;
right: -10px;
top: 50%;
transform: translateY(-50%);
cursor:pointer;
}
.Menu {
+ z-index: 10;
position: absolute;
top: 0;
width: 200px;
height: calc(100vh - 60px);
background: pink;
left: 0;
border-right: 1px solid #000;
transition: left 0.5s ease-in-out;
}
}
}四、场景切换
4.1 用组件库的按钮实现场景切换
src/components/Main/index.tsx
diff
import React from "react";
import { init3D } from "./init-3d";
import { init2D } from "./init-2d";
import { Button } from "antd";
function Main() {
React.useEffect(() => {
const dom = document.getElementById('threejs-3d-container')
const { scene } = init3D(dom!);
return () => {
if (dom) {
dom.innerHTML = ''
}
}
}, [])
React.useEffect(() => {
const dom = document.getElementById('threejs-2d-container')
const { scene } = init2D(dom!);
return () => {
if (dom) {
dom.innerHTML = ''
}
}
}, [])
+ const [curMode, setCurMode] = React.useState<'2d' | '3d'>('2d')
return (
<div className="Main">
+ <div id="threejs-3d-container" style={{ display: curMode === '3d' ? 'block' : 'none' }}></div>
+ <div id="threejs-2d-container" style={{ display: curMode === '2d' ? 'block' : 'none' }}></div>
+ <div className="mode-change-btns">
+ <Button
+ type={curMode === '2d' ? 'primary' : 'default'}
+ onClick={() => setCurMode('2d') }
+ >2D</Button>
+ <Button
+ type={curMode === '3d' ? 'primary' : 'default'}
+ onClick={() => setCurMode('3d') }
+ >3D</Button>
+ </div>
</div>
);
}
export default Main;4.2 按钮位置放置
src/App.scss
diff
body {
margin: 0;
}
.wrap {
height: 100vh;
display: flex;
flex-direction: column;
.Header {
height: 60px;
border-bottom: 1px solid #000;
}
.editor {
flex: 1;
position: relative;
.Main {
position: relative;
+ height: calc(100vh - 60px);
#threejs-3d-container {
position: absolute;
z-index: 1;
}
#threejs-2d-container {
position: absolute;
z-index: 2;
}
+ .mode-change-btns {
+ position: absolute;
+ z-index: 3;
+ bottom: 20px;
+ left: 20px;
+ }
}
.drawer-bar {
width: 10px;
height: 80px;
background: black;
position: absolute;
right: -10px;
top: 50%;
transform: translateY(-50%);
cursor:pointer;
}
.Menu {
z-index: 10;
position: absolute;
top: 0;
width: 200px;
height: calc(100vh - 60px);
background: pink;
left: 0;
border-right: 1px solid #000;
transition: left 0.5s ease-in-out;
}
}
}