代码拉取完成,页面将自动刷新
import * as THREE from 'three';
const {
document,
window,
HTMLCanvasElement,
requestAnimationFrame,
cancelAnimationFrame,
core,
Event,
Event0
} = THREE.DHTML
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
import Stats from 'three/examples/jsm/libs/stats.module.js';
var requestId
Page({
onUnload() {
cancelAnimationFrame(requestId, this.canvas)
this.worker && this.worker.terminate()
if(this.canvas) this.canvas = null
setTimeout(() => {
if (this.renderer instanceof THREE.WebGLRenderer) {
this.renderer.dispose()
this.renderer.forceContextLoss()
this.renderer.context = null
this.renderer.domElement = null
this.renderer = null
}
}, 10)
},
webgl_touch(e){
const web_e = (window.platform=="devtools"?Event:Event0).fix(e)
this.canvas.dispatchEvent(web_e)
},
onLoad() {
document.createElementAsync("canvas", "webgl2",this).then(canvas => {
this.canvas = canvas
this.body_load(canvas).then()
})
},
async body_load(canvas3d) {
let camera, scene, renderer, stats, mesh, mixer, dummy;
const offset = 5000;
const timeOffsets = new Float32Array( 1024 );
for ( let i = 0; i < 1024; i ++ ) {
timeOffsets[ i ] = Math.random() * 3;
}
const clock = new THREE.Clock( true );
init();
animate();
function init() {
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 100, 10000 );
scene = new THREE.Scene();
scene.background = new THREE.Color( 0x99DDFF );
scene.fog = new THREE.Fog( 0x99DDFF, 5000, 10000 );
const light = new THREE.DirectionalLight( 0xffffff, 1 );
light.position.set( 200, 1000, 50 );
light.castShadow = true;
light.shadow.camera.left = - 5000;
light.shadow.camera.right = 5000;
light.shadow.camera.top = 5000;
light.shadow.camera.bottom = - 5000;
light.shadow.camera.far = 2000;
light.shadow.bias = - 0.01;
light.shadow.camera.updateProjectionMatrix();
scene.add( light );
const hemi = new THREE.HemisphereLight( 0x99DDFF, 0x669933, 1 / 3 );
scene.add( hemi );
const ground = new THREE.Mesh(
new THREE.PlaneGeometry( 1000000, 1000000 ),
new THREE.MeshStandardMaterial( { color: 0x669933, depthWrite: true } )
);
ground.rotation.x = - Math.PI / 2;
ground.receiveShadow = true;
scene.add( ground );
const loader = new GLTFLoader();
loader.load( 'models/gltf/Horse.glb', function ( glb ) {
dummy = glb.scene.children[ 0 ];
mesh = new THREE.InstancedMesh( dummy.geometry, dummy.material, 1024 );
mesh.castShadow = true;
for ( let x = 0, i = 0; x < 32; x ++ ) {
for ( let y = 0; y < 32; y ++ ) {
dummy.position.set( offset - 300 * x + 200 * Math.random(), 0, offset - 300 * y );
dummy.updateMatrix();
mesh.setMatrixAt( i, dummy.matrix );
mesh.setColorAt( i, new THREE.Color( `hsl(${Math.random() * 360}, 50%, 66%)` ) );
i ++;
}
}
scene.add( mesh );
mixer = new THREE.AnimationMixer( glb.scene );
const action = mixer.clipAction( glb.animations[ 0 ] );
action.play();
} );
//
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.VSMShadowMap;
//
stats = new Stats();
document.body.appendChild( stats.dom );
//
window.addEventListener( 'resize', onWindowResize );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
//
function animate() {
requestAnimationFrame( animate );
render();
stats.update();
}
function render() {
const time = clock.getElapsedTime();
const r = 3000;
camera.position.set( Math.sin( time / 10 ) * r, 1500 + 1000 * Math.cos( time / 5 ), Math.cos( time / 10 ) * r );
camera.lookAt( 0, 0, 0 );
if ( mesh ) {
for ( let i = 0; i < 1024; i ++ ) {
mixer.setTime( time + timeOffsets[ i ] );
mesh.setMorphAt( i, dummy );
}
mesh.morphTexture.needsUpdate = true;
}
renderer.render( scene, camera );
}
}
})
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。