Published
Edited
Oct 11, 2022
Fork of 3D CRPZ
1 fork
2 stars
Insert cell
Insert cell
renderer.domElement
Insert cell
height = 600
Insert cell
Insert cell
Insert cell
Insert cell
heightMap =
{
const pic = await FileAttachment("download@2.png").url()
const texture = new THREE.TextureLoader().load(pic);
texture.encoding = THREE.sRGBEncoding;
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.anisotropy = 16;
return texture;
}
Insert cell
textureMap =
{
const pic = await FileAttachment("download.png").url();
const texture = new THREE.TextureLoader().load(pic);
texture.encoding = THREE.sRGBEncoding;
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.anisotropy = 16;
return texture;
}
Insert cell
plane = {
const geometry = new THREE.PlaneGeometry(options.width, options.height, options.widthSegments, options.heightSegments);
// 使用自定义着色器
const material = new THREE.ShaderMaterial({
uniforms: {
// Feed the heightmap
bumpTexture: { value: heightMap },
// Feed the scaling constant for the heightmap
bumpScale: { value: 0.05 },
// Feed the texture map
terrainTexture: { value: textureMap }
},
transparent: false,
side: THREE.DoubleSide,
vertexShader: earthShader.vertexShader, // 顶点着色器
fragmentShader: earthShader.fragmentShader, // 片元着色器
});
const plane = new THREE.Mesh(geometry, material);
plane.rotation.set(-Math.PI / 2, 0, 0)
console.log('plane', plane);
return plane;
}

Insert cell
helper = {
const scale = 0.004
const geometryHelper = new THREE.ConeGeometry( 2 * scale, 10 * scale, 3 );
geometryHelper.translate( 0, 5 * scale, 0 );
// geometryHelper.rotateX( Math.PI / 2 );
// geometryHelper.translate( 5 * scale, 5 * scale, 0 );
const helper = new THREE.Mesh( geometryHelper, new THREE.MeshNormalMaterial() );
return helper;
}
Insert cell
scene = {
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x001b42);
scene.add(plane);
return scene;
}
Insert cell
camera = {
const fov = 45;
const aspect = width / height;
const near = 0.1;
const far = 1000;
const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
camera.position.set(0, 2, 2)
camera.lookAt(new THREE.Vector3(0, 0, 0));
return camera;
}
Insert cell
renderer = {
const renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(width, height);
renderer.setPixelRatio(devicePixelRatio);
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.addEventListener("change", () => renderer.render(scene, camera));
invalidation.then(() => (controls.dispose(), renderer.dispose()));

const axesHelper = new THREE.AxesHelper( 5 );
scene.add( axesHelper );

// helper
scene.add( helper );

return renderer;
}
Insert cell
raycaster = new THREE.Raycaster();
Insert cell
pointer = new THREE.Vector2();
Insert cell
function onPointerMove( event ) {
console.log('event', event);
pointer.x = ( event.offsetX / renderer.domElement.clientWidth ) * 2 - 1;
pointer.y = - ( event.offsetY / renderer.domElement.clientHeight ) * 2 + 1;
raycaster.setFromCamera( pointer, camera );

// See if the ray from the camera into the world hits one of our meshes
const intersects = raycaster.intersectObject( plane );

// Toggle rotation bool for meshes that we clicked
if ( intersects.length > 0 ) {
console.log('onPointerMove', intersects, intersects[ 0 ].face.normal, intersects[ 0 ].point);
helper.position.set( 0, 0, 0 );
helper.lookAt( intersects[ 0 ].face.normal );

helper.position.copy( intersects[ 0 ].point );

} else {
}
}
Insert cell
// renderer.domElement.addEventListener( 'pointermove', onPointerMove );
renderer.domElement.addEventListener( 'click', onPointerMove );
Insert cell
THREE = {
const THREE = window.THREE = await require("three@0.130.0/build/three.min.js");
await require("three@0.130.0/examples/js/controls/OrbitControls.js").catch(() => {});
return THREE;
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more