mirror of
https://github.com/pissang/claygl.git
synced 2026-01-18 16:22:29 +00:00
* wip: use camera.projection instead of different camera instances Avoid switch camera projection needs to recreate the instance * update examples * release 2.0.0-alpha.35
53 lines
1.1 KiB
TypeScript
53 lines
1.1 KiB
TypeScript
import {
|
|
Renderer,
|
|
Scene,
|
|
CubeGeometry,
|
|
createStandardShader,
|
|
Material,
|
|
Texture2D,
|
|
Mesh,
|
|
DirectionalLight,
|
|
Vector3,
|
|
Camera
|
|
} from 'claygl';
|
|
|
|
const renderer = new Renderer({
|
|
canvas: document.getElementById('main') as HTMLCanvasElement
|
|
});
|
|
const scene = new Scene();
|
|
const camera = new Camera('perspective', {
|
|
aspect: renderer.getViewportAspect(),
|
|
far: 500
|
|
});
|
|
const cube = new CubeGeometry({
|
|
widthSegments: 10,
|
|
heightSegments: 10
|
|
});
|
|
cube.generateUniqueVertex();
|
|
cube.generateTangents();
|
|
|
|
const material = new Material(createStandardShader());
|
|
material.set('normalMap', loadTexture('assets/textures/normal_map.jpg'));
|
|
material.set('diffuseMap', loadTexture('assets/textures/diffuse.jpg'));
|
|
|
|
camera.position.set(0, 0, 4);
|
|
|
|
const mesh = new Mesh(cube, material);
|
|
scene.add(mesh);
|
|
const light = new DirectionalLight({
|
|
position: new Vector3(10, 10, 10)
|
|
});
|
|
light.lookAt(mesh.position);
|
|
scene.add(light);
|
|
|
|
setInterval(function () {
|
|
renderer.render(scene, camera);
|
|
mesh.rotation.rotateY(Math.PI / 500);
|
|
}, 20);
|
|
|
|
function loadTexture(src: string) {
|
|
var texture = new Texture2D();
|
|
texture.load(src);
|
|
return texture;
|
|
}
|