You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

428 lines
22 KiB

<!DOCTYPE html><html lang="zh"><head>
<meta charset="utf-8">
<title>优化对象的同时保持动画效果</title>
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@threejs">
<meta name="twitter:title" content="Three.js – 优化对象的同时保持动画效果">
<meta property="og:image" content="https://threejs.org/files/share.png">
<link rel="shortcut icon" href="/files/favicon_white.ico" media="(prefers-color-scheme: dark)">
<link rel="shortcut icon" href="/files/favicon.ico" media="(prefers-color-scheme: light)">
<link rel="stylesheet" href="/manual/resources/lesson.css">
<link rel="stylesheet" href="/manual/resources/lang.css">
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "../../build/three.module.js"
}
}
</script>
<link rel="stylesheet" href="/manual/zh/lang.css">
</head>
<body>
<div class="container">
<div class="lesson-title">
<h1>优化对象的同时保持动画效果</h1>
</div>
<div class="lesson">
<div class="lesson-main">
<p>本文是关于 three.js 系列文章的一部分. 第一篇文章是 <a href="optimize-lots-of-objects.html">three.js 基础</a>. 如果你还没看过而且对three.js 还不熟悉,那应该从那里开始.</p>
<p>在上一章中, 我们合并了19000个对象到一个几何体中. 这带来的好处是优化掉19000次绘制操作但是缺点是没有办法再单独操作某一个了. </p>
<p>根据我们想达成的目标的不同, 有不同的解决方案可选. 本例中我们绘制大量的数据, 然后还能在这些数据集间设置动画</p>
<p>第一件事是获取数据集. 理想中我们可能需要预处理这些数据, 但是我们现在只需要载入两个数据集然后产生更多的. </p>
<p>这是我们之前的载入代码</p>
<pre class="prettyprint showlinemods notranslate lang-js" translate="no">loadFile('resources/data/gpw/gpw_v4_basic_demographic_characteristics_rev10_a000_014mt_2010_cntm_1_deg.asc')
.then(parseData)
.then(addBoxes)
.then(render);
</pre>
<p>稍微改成这样</p>
<pre class="prettyprint showlinemods notranslate lang-js" translate="no">async function loadData(info) {
const text = await loadFile(info.url);
info.file = parseData(text);
}
async function loadAll() {
const fileInfos = [
{name: 'men', hueRange: [0.7, 0.3], url: 'resources/data/gpw/gpw_v4_basic_demographic_characteristics_rev10_a000_014mt_2010_cntm_1_deg.asc' },
{name: 'women', hueRange: [0.9, 1.1], url: 'resources/data/gpw/gpw_v4_basic_demographic_characteristics_rev10_a000_014ft_2010_cntm_1_deg.asc' },
];
await Promise.all(fileInfos.map(loadData));
...
}
loadAll();
</pre>
<p>上面的代码将会加载<code class="notranslate" translate="no">fileInfos</code>中的所有文件, 加载完成后每一个<code class="notranslate" translate="no">fileInfos</code>中的对象都会有一个带着载入文件的<code class="notranslate" translate="no">file</code>属性. 我们稍后使用<code class="notranslate" translate="no">name</code><code class="notranslate" translate="no">hueRange</code>. <code class="notranslate" translate="no">name</code>是显示在界面上的字段, <code class="notranslate" translate="no">hueRange</code>是色调. </p>
<p>上面的两个文件显然是每个地区2010年男人和女人的数量. 注意了, 我不知道这些数据对不对, 但是不影响好吧. 重要的是如何去展示这些不同的数据. </p>
<p>让我们再产生两组数据. 一组是男人数量比女人多的, 另一组反过来. </p>
<p>首先,让我们编写一个函数,在给定一个二维数组的情况下,像以前一样映射生成一个新的二维数组</p>
<pre class="prettyprint showlinemods notranslate lang-js" translate="no">function mapValues(data, fn) {
return data.map((row, rowNdx) =&gt; {
return row.map((value, colNdx) =&gt; {
return fn(value, rowNdx, colNdx);
});
});
}
</pre>
<p>就像普通的<code class="notranslate" translate="no">Array.map</code>函数, <code class="notranslate" translate="no">mapValues</code>函数对数组的数组每一个值调用了<code class="notranslate" translate="no">fn</code>. 这将会将每个值和它的索引传进去. </p>
<p>现在让我们编写一些代码来生成一个新文件,它是两个文件之间的比较</p>
<pre class="prettyprint showlinemods notranslate lang-js" translate="no">function makeDiffFile(baseFile, otherFile, compareFn) {
let min;
let max;
const baseData = baseFile.data;
const otherData = otherFile.data;
const data = mapValues(baseData, (base, rowNdx, colNdx) =&gt; {
const other = otherData[rowNdx][colNdx];
if (base === undefined || other === undefined) {
return undefined;
}
const value = compareFn(base, other);
min = Math.min(min === undefined ? value : min, value);
max = Math.max(max === undefined ? value : max, value);
return value;
});
// 生成baseFile的一个副本, 然后用新文件的min max 和 data替代原来的
return {...baseFile, min, max, data};
}
</pre>
<p>上面的代码基于传入的<code class="notranslate" translate="no">compareFn</code><code class="notranslate" translate="no">mapValues</code>生成一个新的数据集. 这同样追踪<code class="notranslate" translate="no">min</code><code class="notranslate" translate="no">max</code>的比较结果. 最后这将会生成一个新文件, 除了<code class="notranslate" translate="no">min</code>, <code class="notranslate" translate="no">max</code><code class="notranslate" translate="no">data</code>所有的属性都和<code class="notranslate" translate="no">baseFile</code>一样. </p>
<p>然后我们用上面的代码生成两个新数据集</p>
<pre class="prettyprint showlinemods notranslate lang-js" translate="no">{
const menInfo = fileInfos[0];
const womenInfo = fileInfos[1];
const menFile = menInfo.file;
const womenFile = womenInfo.file;
function amountGreaterThan(a, b) {
return Math.max(a - b, 0);
}
fileInfos.push({
name: '&gt;50%men',
hueRange: [0.6, 1.1],
file: makeDiffFile(menFile, womenFile, (men, women) =&gt; {
return amountGreaterThan(men, women);
}),
});
fileInfos.push({
name: '&gt;50% women',
hueRange: [0.0, 0.4],
file: makeDiffFile(womenFile, menFile, (women, men) =&gt; {
return amountGreaterThan(women, men);
}),
});
}
</pre>
<p>现在我们写一个UI来选择数据集. 首先是html部分</p>
<pre class="prettyprint showlinemods notranslate lang-html" translate="no">&lt;body&gt;
&lt;canvas id="c"&gt;&lt;/canvas&gt;
+ &lt;div id="ui"&gt;&lt;/div&gt;
&lt;/body&gt;
</pre>
<p>CSS部分, 让其显示在左侧</p>
<pre class="prettyprint showlinemods notranslate lang-css" translate="no">#ui {
position: absolute;
left: 1em;
top: 1em;
}
#ui&gt;div {
font-size: 20pt;
padding: 1em;
display: inline-block;
}
#ui&gt;div.selected {
color: red;
}
</pre>
<p>我们遍历整个文件, 对于每一个数据集都生成了合并了的box, </p>
<p>然后我们可以遍历每个文件, 并为每组数据生成合并了的box和一个元素, 当鼠标悬停在上面时, 该元素将显示该集合并隐藏所有其他元素</p>
<pre class="prettyprint showlinemods notranslate lang-js" translate="no">// 展示选中的元素, 隐藏其他的
function showFileInfo(fileInfos, fileInfo) {
fileInfos.forEach((info) =&gt; {
const visible = fileInfo === info;
info.root.visible = visible;
info.elem.className = visible ? 'selected' : '';
});
requestRenderIfNotRequested();
}
const uiElem = document.querySelector('#ui');
fileInfos.forEach((info) =&gt; {
const boxes = addBoxes(info.file, info.hueRange);
info.root = boxes;
const div = document.createElement('div');
info.elem = div;
div.textContent = info.name;
uiElem.appendChild(div);
div.addEventListener('mouseover', () =&gt; {
showFileInfo(fileInfos, info);
});
});
// 起始展示第一组数据
showFileInfo(fileInfos, fileInfos[0]);
</pre>
<p>和之前例子有所不同的是, 我们还需要让<code class="notranslate" translate="no">addBoxes</code>获取<code class="notranslate" translate="no">hueRange</code></p>
<pre class="prettyprint showlinemods notranslate lang-js" translate="no">-function addBoxes(file) {
+function addBoxes(file, hueRange) {
...
// compute a color
- const hue = THREE.MathUtils.lerp(0.7, 0.3, amount);
+ const hue = THREE.MathUtils.lerp(...hueRange, amount);
...
</pre>
<p>把鼠标放到标签上可以在四组不同的数据集之间切换. </p>
<p></p><div translate="no" class="threejs_example_container notranslate">
<div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/lots-of-objects-multiple-data-sets.html"></iframe></div>
<a class="threejs_center" href="/manual/examples/lots-of-objects-multiple-data-sets.html" target="_blank">点击此处在新标签页中打开</a>
</div>
<p></p>
<p>咋回事, 怎么还有一些点非常突出??!! 而且切换得很生硬也没有动画啊</p>
<p>有这么一些想法</p>
<ul>
<li><p>通过使用<a href="/docs/#api/zh/materials/Material.opacity"><code class="notranslate" translate="no">Material.opacity</code></a>做消失过渡</p>
<p>这个解决方案的问题是立方体完全重叠了, 意思是在Z轴方向冲突. 我们可以通过改变depth函数和使用blending来修复. 我们应该试一试</p>
</li>
<li><p>放大我们想看到的集合,缩小其他集合</p>
<p>因为所有盒子的原点都在地球的中心, 如果我们把它们缩小到1.0以下, 它们就会沉入地球. 这听起来是个好主意, 但问题是所有的较低的盒子几乎会立即消失,直到新的数据集扩展到1.0才被替换. 这使得过渡非常不漂亮. 我们可以用一个神奇的自定义着色器来解决这个问题. </p>
</li>
<li><p>使用Morphtargets</p>
<p>所谓<em>变形目标morphtargets</em>是一种给每个顶点提供多个值, 以及使他们进行变形或者说lerp(线性插值)的方法. morphtargets通常用于3D角色的面部动画, 但这并不是唯一的用途. </p>
</li>
</ul>
<p>我们试试morphtargets</p>
<p>我们还是给每一个数据集做一个几何体, 但这次我们提取<code class="notranslate" translate="no">position</code>属性, 把他们作为morphtargets.</p>
<p>首先我们改动一下<code class="notranslate" translate="no">addBoxes</code>来生成并返回一个合并的几何体. </p>
<pre class="prettyprint showlinemods notranslate lang-js" translate="no">-function addBoxes(file, hueRange) {
+function makeBoxes(file, hueRange) {
const {min, max, data} = file;
const range = max - min;
...
- const mergedGeometry = BufferGeometryUtils.mergeBufferGeometries(
- geometries, false);
- const material = new THREE.MeshBasicMaterial({
- vertexColors: true,
- });
- const mesh = new THREE.Mesh(mergedGeometry, material);
- scene.add(mesh);
- return mesh;
+ return BufferGeometryUtils.mergeBufferGeometries(
+ geometries, false);
}
</pre>
<p>不过, 我们还有一件事要做. 变形目标的顶点数必须完全相同. 一个目标中的顶点#123需要在所有其他目标中有一个对应的顶点#123. 但是, 由于现在不同的数据集可能有一些没有数据的数据点, 因此不会为该点生成几何体, 这意味着另一个数据集没有相应的顶点. 所以, 我们需要检查所有的数据集,如果任何一个数据集中有数据, 就总是生成一些东西; 或者如果任何一个数据集中缺少数据, 就什么也不生成. 让我们以后者为准. </p>
<pre class="prettyprint showlinemods notranslate lang-js" translate="no">+function dataMissingInAnySet(fileInfos, latNdx, lonNdx) {
+ for (const fileInfo of fileInfos) {
+ if (fileInfo.file.data[latNdx][lonNdx] === undefined) {
+ return true;
+ }
+ }
+ return false;
+}
-function makeBoxes(file, hueRange) {
+function makeBoxes(file, hueRange, fileInfos) {
const {min, max, data} = file;
const range = max - min;
...
const geometries = [];
data.forEach((row, latNdx) =&gt; {
row.forEach((value, lonNdx) =&gt; {
+ if (dataMissingInAnySet(fileInfos, latNdx, lonNdx)) {
+ return;
+ }
const amount = (value - min) / range;
...
</pre>
<p>现在我们改动一下代码, 把调用<code class="notranslate" translate="no">addBoxes</code>的改成使用<code class="notranslate" translate="no">makeBoxes</code>生成变形目标.</p>
<pre class="prettyprint showlinemods notranslate lang-js" translate="no">+// 对每一个数据集生成几何体
+const geometries = fileInfos.map((info) =&gt; {
+ return makeBoxes(info.file, info.hueRange, fileInfos);
+});
+
+// 以第一个几何体作为基准, 将其他的作为变形目标
+const baseGeometry = geometries[0];
+baseGeometry.morphAttributes.position = geometries.map((geometry, ndx) =&gt; {
+ const attribute = geometry.getAttribute('position');
+ const name = `target${ndx}`;
+ attribute.name = name;
+ return attribute;
+});
+baseGeometry.morphAttributes.color = geometries.map((geometry, ndx) =&gt; {
+ const attribute = geometry.getAttribute('color');
+ const name = `target${ndx}`;
+ attribute.name = name;
+ return attribute;
+});
+const material = new THREE.MeshBasicMaterial({
+ vertexColors: true,
+});
+const mesh = new THREE.Mesh(baseGeometry, material);
+scene.add(mesh);
const uiElem = document.querySelector('#ui');
fileInfos.forEach((info) =&gt; {
- const boxes = addBoxes(info.file, info.hueRange);
- info.root = boxes;
const div = document.createElement('div');
info.elem = div;
div.textContent = info.name;
uiElem.appendChild(div);
function show() {
showFileInfo(fileInfos, info);
}
div.addEventListener('mouseover', show);
div.addEventListener('touchstart', show);
});
// 展示第一组数据集
showFileInfo(fileInfos, fileInfos[0]);
</pre>
<p>以上我们为每一组数据集创建了几何体, 以第一个作为基准, 获取了<code class="notranslate" translate="no">position</code>属性, 将其他的几何体作为其变形目标</p>
<p>现在我们需要改变显示和隐藏各种数据集的方式. 我们需要改动变形目标的influence, 而不是简单地显示和隐藏mesh. 对于我们我们想看到的数据集, influence应该是1, 不想看到的是0. 但是我们又不能直接将他们设置成1和0, 这将会显示开与闭的两种情况, 和现在这种没有区别. 我们也可以写一段自定义的动画效果, 听起来不难. 但是我们模仿的WebGL globe用了一个<a href="https://github.com/tweenjs/tween.js/">动画库</a>, 我们也用这一个. </p>
<p>我们这里首先引入它</p>
<pre class="prettyprint showlinemods notranslate lang-js" translate="no">import * as THREE from 'three';
import {BufferGeometryUtils} from 'three/addons/utils/BufferGeometryUtils.js';
import {OrbitControls} from 'three/addons/controls/OrbitControls.js';
+import {TWEEN} from 'three/addons/libs/tween.min.js';
</pre>
<p>然后创建一个<code class="notranslate" translate="no">Tween</code>来使influence变化</p>
<pre class="prettyprint showlinemods notranslate lang-js" translate="no">// show the selected data, hide the rest
function showFileInfo(fileInfos, fileInfo) {
fileInfos.forEach((info) =&gt; {
const visible = fileInfo === info;
- info.root.visible = visible;
info.elem.className = visible ? 'selected' : '';
+ const targets = {};
+ fileInfos.forEach((info, i) =&gt; {
+ targets[i] = info === fileInfo ? 1 : 0;
+ });
+ const durationInMs = 1000;
+ new TWEEN.Tween(mesh.morphTargetInfluences)
+ .to(targets, durationInMs)
+ .start();
});
requestRenderIfNotRequested();
}
</pre>
<p>我们也可以在每一帧的render函数中调用<code class="notranslate" translate="no">TWEEN.update</code>, 但这会带来一个问题. "tween.js"是为了连续渲染而设计的, 但是我们采用的是<a href="rendering-on-demand.html">按需渲染</a>. 我们可以再切换回连续渲染的方式, 但是为了省电和省资源起见, 还是按需渲染比较好. 所以我们看看是否能让它在按需渲染下工作. </p>
<p>我们需要<code class="notranslate" translate="no">TweenManaget</code>来完成这件事. 我们将用它来创建<code class="notranslate" translate="no">Tween</code>并追踪他们. 这里会有一个<code class="notranslate" translate="no">update</code>方法, 如果我们二次调用它的时候返回<code class="notranslate" translate="no">true</code>, 如果所有动画结束后则会返回<code class="notranslate" translate="no">false</code>.</p>
<pre class="prettyprint showlinemods notranslate lang-js" translate="no">class TweenManger {
constructor() {
this.numTweensRunning = 0;
}
_handleComplete() {
--this.numTweensRunning;
console.assert(this.numTweensRunning &gt;= 0);
}
createTween(targetObject) {
const self = this;
++this.numTweensRunning;
let userCompleteFn = () =&gt; {};
// 创建一个新的Tween, 并应用我们自己的回调函数
const tween = new TWEEN.Tween(targetObject).onComplete(function(...args) {
self._handleComplete();
userCompleteFn.call(this, ...args);
});
// 用我们自己的onComplete代替它的,
// 因此, 如果用户提供回调, 我们可以调用用户的回调
tween.onComplete = (fn) =&gt; {
userCompleteFn = fn;
return tween;
};
return tween;
}
update() {
TWEEN.update();
return this.numTweensRunning &gt; 0;
}
}
</pre>
<p>我们需要以下代码来使用</p>
<pre class="prettyprint showlinemods notranslate lang-js" translate="no">function main() {
const canvas = document.querySelector('#c');
const renderer = new THREE.WebGLRenderer({canvas});
+ const tweenManager = new TweenManger();
...
</pre>
<p>这是如何创建Tween</p>
<pre class="prettyprint showlinemods notranslate lang-js" translate="no">// show the selected data, hide the rest
function showFileInfo(fileInfos, fileInfo) {
fileInfos.forEach((info) =&gt; {
const visible = fileInfo === info;
info.elem.className = visible ? 'selected' : '';
const targets = {};
fileInfos.forEach((info, i) =&gt; {
targets[i] = info === fileInfo ? 1 : 0;
});
const durationInMs = 1000;
- new TWEEN.Tween(mesh.morphTargetInfluences)
+ tweenManager.createTween(mesh.morphTargetInfluences)
.to(targets, durationInMs)
.start();
});
requestRenderIfNotRequested();
}
</pre>
<p>我们需要改动render函数来更新tween, 让动画还在跑的时候保持渲染</p>
<pre class="prettyprint showlinemods notranslate lang-js" translate="no">function render() {
renderRequested = false;
if (resizeRendererToDisplaySize(renderer)) {
const canvas = renderer.domElement;
camera.aspect = canvas.clientWidth / canvas.clientHeight;
camera.updateProjectionMatrix();
}
+ if (tweenManager.update()) {
+ requestRenderIfNotRequested();
+ }
controls.update();
renderer.render(scene, camera);
}
render();
</pre>
<p>这样我们就可以在数据集中以动画的方式过渡了</p>
<p></p><div translate="no" class="threejs_example_container notranslate">
<div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/lots-of-objects-morphtargets.html"></iframe></div>
<a class="threejs_center" href="/manual/examples/lots-of-objects-morphtargets.html" target="_blank">点击此处在新标签页中打开</a>
</div>
<p></p>
<p>我希望上面讲的这些能有用. 通过threejs提供的方法或者自己写着色器来使用变形对象是一种常见的移动大量对象的手段. 作为一个例子, 我们可以给每一个立方体一个随机目标, 然后从这个位置变换到另一个位置. 这可能是一种超酷的介绍地球的方法. </p>
<p>接下来你可能感兴趣的是给地球上的一个位置添加标签, 这将在<a href="align-html-elements-to-3d.html">3D中排布HTML元素</a>中涉及. </p>
<p>Note: We could try to just graph percent of men or percent of women or the raw
difference but based on how we are displaying the info, cubes that grow from the
surface of the earth, we'd prefer most cubes to be low. If we used one of these
other comparisons most cubes would be about 1/2 their maximum height which would
not make a good visualization. Feel free to change the <code class="notranslate" translate="no">amountGreaterThan</code> from
<a href="/docs/#api/zh/math/Math.max(a - b, 0)"><code class="notranslate" translate="no">Math.max(a - b, 0)</code></a> to something like <code class="notranslate" translate="no">(a - b)</code> "raw difference" or <code class="notranslate" translate="no">a / (a +
b)</code> "percent" and you'll see what I mean.</p>
<p>注: 我们可以试着用图表表示男性的百分比或女性的百分比或原始差异. 但根据我们显示信息的方式, 也就是从地球表面生长出来的立方体的显示方式, 我们希望大多数立方体都是矮的. 如果我们使用其中一个做基准, 大多数立方体的高度大约是它们最大高度的1/2. 效果会很差. 自己动手改一下<code class="notranslate" translate="no">amountGreaterThan</code>中的<a href="/docs/#api/zh/math/Math.max(a - b, 0)"><code class="notranslate" translate="no">Math.max(a - b, 0)</code></a><code class="notranslate" translate="no">(a - b)</code> "原始差异"或者 <code class="notranslate" translate="no">a / (a + b)</code>"百分比", 你就会明白我什么意思了. </p>
</div>
</div>
</div>
<script src="/manual/resources/prettify.js"></script>
<script src="/manual/resources/lesson.js"></script>
</body></html>