提交 60053bee 编写于 作者: S skyun

Wed Oct 18 22:53:00 CST 2023 inscode

上级 f414f2e2
import { ref, onMounted } from 'vue'
import * as THREE from 'three'
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
export class ThreejsDefault {
constructor(container) {
// init scene camera
this.scene = new THREE.Scene()
this.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000)
this.camera.position.z = 5
// init renderer
this.renderer = new THREE.WebGLRenderer()
this.renderer.setSize(window.innerWidth, window.innerHeight)
// init controls
this.controls = new OrbitControls(this.camera, this.renderer.domElement);
this.animateFuncs = []
const animate = () => {
requestAnimationFrame(animate)
this.animateFuncs.forEach(animFunc => animFunc())
this.renderer.render(this.scene, this.camera)
}
onMounted(() => {
container.value.appendChild(this.renderer.domElement)
animate()
})
}
onAnimate(animFunc) {
this.animateFuncs.push(animFunc)
}
}
...@@ -4,41 +4,35 @@ ...@@ -4,41 +4,35 @@
<script setup> <script setup>
import { ref, onMounted } from 'vue' import { ref, onMounted } from 'vue'
import * as TT from '../threejs-tools'
import * as THREE from 'three' import * as THREE from 'three'
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
// init scene camera
const scene = new THREE.Scene()
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000)
camera.position.z = 5
// init renderer
const container = ref(null) const container = ref(null)
const renderer = new THREE.WebGLRenderer() const td = new TT.ThreejsDefault(container)
renderer.setSize(window.innerWidth, window.innerHeight)
const animate = () => { // add mesh
requestAnimationFrame(animate) const geometry = new THREE.BoxGeometry(1, 1, 1)
const material = new THREE.MeshPhongMaterial({ color: 0x00ff00 })
const cube = new THREE.Mesh(geometry, material)
td.scene.add(cube)
td.onAnimate(() => {
cube.rotation.x += 0.01 cube.rotation.x += 0.01
cube.rotation.y += 0.01 cube.rotation.y += 0.01
renderer.render(scene, camera)
}
onMounted(() => {
container.value.appendChild(renderer.domElement)
animate()
}) })
// init controls const geometry2 = new THREE.BoxGeometry(1, 1, 1)
const controls = new OrbitControls(camera, renderer.domElement); const material2 = new THREE.MeshPhongMaterial({ color: 0x00ff00 })
const cube2 = new THREE.Mesh(geometry2, material2)
cube2.position.y += 2
td.scene.add(cube2)
td.onAnimate(() => {
cube2.rotation.x += 0.01
cube2.rotation.y += 0.01
})
// init lights // add light
const light = new THREE.AmbientLight(0xffffff); // soft white light td.scene.add(new THREE.AmbientLight(0xffffff));
scene.add(light);
// user code
const geometry = new THREE.BoxGeometry(1, 1, 1)
const material = new THREE.MeshPhongMaterial({ color: 0x00ff00 })
const cube = new THREE.Mesh(geometry, material)
scene.add(cube)
</script> </script>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册