Browse Source

性能测试渲染

master
修宁 6 months ago
parent
commit
84b4c4461d
  1. 247
      src/components/ThreePerfView.vue

247
src/components/ThreePerfView.vue

@ -2,6 +2,8 @@
<div class="model3d-view"> <div class="model3d-view">
<el-space :gutter="10" class="toolbar"> <el-space :gutter="10" class="toolbar">
<el-button type="primary" @click="test1">测试1</el-button> <el-button type="primary" @click="test1">测试1</el-button>
<el-button type="primary" @click="test2">测试2</el-button>
<el-button type="primary" @click="test3">测试3</el-button>
<div class="demo-color-block"> <div class="demo-color-block">
<span class="demonstration">物体数:<el-text type="danger">{{ restate.objects }}</el-text></span> <span class="demonstration">物体数:<el-text type="danger">{{ restate.objects }}</el-text></span>
<span class="demonstration"> 顶点数:<el-text type="danger">{{ restate.vertices }}</el-text></span> <span class="demonstration"> 顶点数:<el-text type="danger">{{ restate.vertices }}</el-text></span>
@ -54,9 +56,10 @@ const pointMaterial = new THREE.SpriteMaterial({
const lineMaterial = new LineMaterial({ const lineMaterial = new LineMaterial({
color: 0xFF8C00, color: 0xFF8C00,
linewidth: 5 linewidth: 5
}); })
function test1() { function test1() {
cleanupThree()
const xcount = 100 const xcount = 100
const zcount = 100 const zcount = 100
const dist = 1.25 const dist = 1.25
@ -78,78 +81,160 @@ function test1() {
} }
} }
// // 线 function drawLine(p1: THREE.Vector3, p2: THREE.Vector3) {
// const lines = [] const geometry = new LineGeometry()
// geometry.setPositions([p1.x, p1.y, p1.z, p2.x, p2.y, p2.z])
// // 线
// for (let z = 0; z < zcount; z++) { // const material = new LineMaterial({
// for (let x = 0; x < xcount - 1; x++) { // color: 0xFF8C00,
// const i = z * xcount + x // linewidth: 5
// drawLine(points[i], points[i + 1]) // });
// }
// } const line = new Line2(geometry, lineMaterial)
// line.computeLineDistances()
// for (let z = 0; z < zcount - 1; z++) {
// for (let x = 0; x < xcount; x++) { scene.add(line)
// const i = z * xcount + x }
// const belowIndex = (z + 1) * xcount + x
// drawLine(points[i], points[belowIndex]) // 线
// } for (let z = 0; z < zcount; z++) {
// } for (let x = 0; x < xcount - 1; x++) {
const positions = []; const i = z * xcount + x
drawLine(points[i], points[i + 1])
}
}
for (let z = 0; z < zcount - 1; z++) {
for (let x = 0; x < xcount; x++) {
const i = z * xcount + x
const belowIndex = (z + 1) * xcount + x
drawLine(points[i], points[belowIndex])
}
}
refreshCount()
}
function test2() {
cleanupThree()
const xcount = 100
const zcount = 100
const dist = 1.25
const spacing = dist
const y = 0.1
const points = []
//
for (let z = 0; z < zcount; z++) {
for (let x = 0; x < xcount; x++) {
const px = x * dist
const pz = z * dist
const point = new THREE.Sprite(pointMaterial)
point.position.set(px, y, pz)
point.scale.set(0.25, 0.25, 1) //
scene.add(point)
points.push(point.position.clone())
}
}
const positions = []
// //
for (let z = 0; z < zcount; z++) { for (let z = 0; z < zcount; z++) {
for (let x = 0; x < xcount - 1; x++) { for (let x = 0; x < xcount - 1; x++) {
const x1 = x * spacing; const x1 = x * spacing
const z1 = z * spacing; const z1 = z * spacing
const x2 = (x + 1) * spacing; const x2 = (x + 1) * spacing
const z2 = z * spacing; const z2 = z * spacing
positions.push(x1, y, z1, x2, y, z2); positions.push(x1, y, z1, x2, y, z2)
} }
} }
// //
for (let z = 0; z < zcount - 1; z++) { for (let z = 0; z < zcount - 1; z++) {
for (let x = 0; x < xcount; x++) { for (let x = 0; x < xcount; x++) {
const x1 = x * spacing; const x1 = x * spacing
const z1 = z * spacing; const z1 = z * spacing
const x2 = x * spacing; const x2 = x * spacing
const z2 = (z + 1) * spacing; const z2 = (z + 1) * spacing
positions.push(x1, y, z1, x2, y, z2); positions.push(x1, y, z1, x2, y, z2)
} }
} }
const geometry = new LineSegmentsGeometry(); const geometry = new LineSegmentsGeometry()
geometry.setPositions(positions); // [x1,y1,z1, x2,y2,z2, ...] geometry.setPositions(positions)
const material = new LineMaterial({ const material = new LineMaterial({
color: 0xFF8C00, color: 0xFF8C00,
linewidth: 5, linewidth: 5,
vertexColors: false vertexColors: false
}); })
const lineSegments = new LineSegments2(geometry, material); const lineSegments = new LineSegments2(geometry, material)
lineSegments.computeLineDistances(); // lineSegments.computeLineDistances() //
lineSegments.name = 'grid-lines'; lineSegments.name = 'grid-lines'
scene.add(lineSegments); scene.add(lineSegments)
refreshCount()
} }
function drawLine(p1: THREE.Vector3, p2: THREE.Vector3) {
const geometry = new LineGeometry();
geometry.setPositions([p1.x, p1.y, p1.z, p2.x, p2.y, p2.z]);
// const material = new LineMaterial({ function test3() {
// color: 0xFF8C00, cleanupThree()
// linewidth: 5 const xcount = 100
// }); const zcount = 100
const dist = 1.25
const spacing = dist
const y = 0.1
const line = new Line2(geometry, lineMaterial); const points = []
line.computeLineDistances();
scene.add(line) //
for (let z = 0; z < zcount; z++) {
for (let x = 0; x < xcount; x++) {
const px = x * dist
const pz = z * dist
const point = new THREE.Sprite(pointMaterial)
point.position.set(px, y, pz)
point.scale.set(0.25, 0.25, 1) //
scene.add(point)
points.push(point.position.clone())
}
}
const positions = []
//
for (let z = 0; z < zcount; z++) {
for (let x = 0; x < xcount - 1; x++) {
const x1 = x * spacing
const z1 = z * spacing
const x2 = (x + 1) * spacing
const z2 = z * spacing
positions.push(x1, y, z1, x2, y, z2)
}
}
//
for (let z = 0; z < zcount - 1; z++) {
for (let x = 0; x < xcount; x++) {
const x1 = x * spacing
const z1 = z * spacing
const x2 = x * spacing
const z2 = (z + 1) * spacing
positions.push(x1, y, z1, x2, y, z2)
}
}
const positionNums = new Float32Array(positions)
const geometry = new THREE.BufferGeometry()
geometry.setAttribute('position', new THREE.BufferAttribute(positionNums, 3))
const material = new THREE.LineBasicMaterial({ color: 0xFF8C00 })
const lineSegments = new THREE.LineSegments(geometry, material)
lineSegments.name = 'grid-lines'
scene.add(lineSegments)
refreshCount()
} }
const restate = reactive({ const restate = reactive({
targetColor: '#ff0000', targetColor: '#ff0000',
loadScale: 1, loadScale: 1,
@ -381,13 +466,46 @@ function renderView() {
renderer?.render(scene, camera) renderer?.render(scene, camera)
} }
function refreshCount() {
//
let totalObjects = 0
let totalVertices = 0
let totalFaces = 0
scene.traverse(function(child) {
if (child.isMesh) {
totalObjects++
//
const geometry = child.geometry
// BufferGeometry
if (geometry.isBufferGeometry) {
//
if (geometry.attributes.position) {
totalVertices += geometry.attributes.position.count
}
//
if (geometry.index) {
totalFaces += geometry.index.count / 3
} else if (geometry.attributes.position) {
//
totalFaces += geometry.attributes.position.count / 3
}
}
// Geometry 使
else if (geometry.isGeometry) {
//
totalVertices += geometry.vertices.length
function cleaupModel() { //
if (modelGroup) { totalFaces += geometry.faces.length
scene.remove(modelGroup) }
} }
tcontrols.detach() })
transformEditCtl.value.detach() restate.objects = totalObjects
restate.vertices = totalVertices
restate.faces = totalFaces
} }
function cleanupThree() { function cleanupThree() {
@ -412,37 +530,12 @@ function cleanupThree() {
if (obj.texture) { if (obj.texture) {
obj.texture.dispose() obj.texture.dispose()
} }
//
if (obj.renderTarget) {
obj.renderTarget.dispose()
}
// OrbitControls
if (obj.dispose) {
obj.dispose()
}
}) })
if (modelGroup) {
scene.remove(modelGroup)
}
// //
scene.children = [] scene.children = []
modelGroup = null modelGroup = null
} }
if (statsControls) {
statsControls.dom.remove()
}
if (renderer) {
renderer.dispose()
renderer.forceContextLoss()
console.log('WebGL disposed, memory:', renderer.info.memory)
renderer.domElement = null
}
} }

Loading…
Cancel
Save