bnbThree({
w: 540, h: 540,
numFrames: 100,
fps: 20,
record: true,
createScene: (THREE, sketch, globals) => {
const scene = new THREE.Scene()
scene.fog = new THREE.FogExp2(0x1E2630, 0.01)
const light1 = new THREE.DirectionalLight('red')
light1.position.set(5, 0, 5)
light1.castShadow = true
scene.add(light1)
const light2 = new THREE.DirectionalLight('blue')
light2.position.set(5, 0, 5)
light2.castShadow = true
scene.add(light2)
const ambient = new THREE.AmbientLight('#ccc')
scene.add(ambient)
const geometry = new THREE.TorusGeometry(40, 10, 400, 1000)
geometry.setAttribute('uv2', new THREE.BufferAttribute(geometry.attributes.uv.array, 2))
const material = new THREE.MeshStandardMaterial({ side: THREE.BackSide })
const mesh = new THREE.Mesh(geometry, material)
mesh.receiveShadow = true
scene.add(mesh)
mesh.rotation.x = Math.PI/2
const wantedTextures = [colorURL, aoURL, normalURL, roughnessURL, heightURL].length
let texturesLoaded = 0
// Set `globals.isReady` to false to prevent the loop to start
// until all textures are loaded
globals.isReady = false
const onLoad = () => {
texturesLoaded ++
if(texturesLoaded === wantedTextures) globals.isReady = true
}
const textureLoader = new THREE.TextureLoader()
const repetition = new THREE.Vector2(5, 2)
const colorTexture = textureLoader.load(colorURL, onLoad)
colorTexture.repeat = repetition
colorTexture.wrapS = colorTexture.wrapT = THREE.RepeatWrapping
material.map = colorTexture
const aoTexture = textureLoader.load(aoURL, onLoad)
mesh.geometry.setAttribute('uv2', new THREE.BufferAttribute(mesh.geometry.attributes.uv.array, 2))
aoTexture.repeat = repetition
aoTexture.wrapS = aoTexture.wrapT = THREE.RepeatWrapping
aoTexture.colorSpace = THREE.SRGBColorSpace
material.aoMap = aoTexture
material.aoMapIntensity = 2
const normalTexture = textureLoader.load(normalURL, onLoad)
normalTexture.repeat = repetition
normalTexture.wrapS = normalTexture.wrapT = THREE.RepeatWrapping
normalTexture.colorSpace = THREE.SRGBColorSpace
material.normalMap = normalTexture
const roughnessTexture = textureLoader.load(roughnessURL, onLoad)
roughnessTexture.repeat = repetition
roughnessTexture.wrapS = roughnessTexture.wrapT = THREE.RepeatWrapping
roughnessTexture.colorSpace = THREE.SRGBColorSpace
material.roughnessMap = roughnessTexture
const heightTexture = textureLoader.load(heightURL, onLoad)
heightTexture.repeat = repetition
heightTexture.wrapS = heightTexture.wrapT = THREE.RepeatWrapping
heightTexture.colorSpace = THREE.SRGBColorSpace
material.displacementMap = heightTexture
material.displacementScale = 5
const camera = new THREE.PerspectiveCamera(75, globals.w / globals.h, 0.001, 1000)
camera.position.x = 39
camera.position.z = 30
const renderer = new THREE.WebGLRenderer({ antialias: true })
renderer.setSize(globals.w, globals.h)
// createScene must return a function to update the scene
return (s, t, g) => {
const angle = t * s.TAU
// mesh.rotation.y = angle
mesh.rotation.z = angle
camera.position.y = Math.sin(angle)
light1.position.x = Math.cos(angle) * 5
light2.position.x = Math.cos(angle + Math.PI) * 5
renderer.render(scene, camera)
// the update function must return the renderer
return renderer
}
}
})