Commit 96175ead authored by 吕祺's avatar 吕祺 ⛹🏽

3d one cylinder one time

parent 787ddeb5
var camera, scene, renderer, controls, mat, geo, tex var camera, scene, renderer, controls, mat, geo, tex
var slice = 128 var pcPicWidth = 17943
var mobilePicWidth = 8980
var slice = Math.round(pcPicWidth / 256)
var isMobile = checkMobile() var isMobile = checkMobile()
var cylinderGroup, cylinderMobileGroup var cylinderGroup, cylinderMobileGroup
var materialArray, materialMobileArray var materialArray, materialMobileArray
...@@ -8,6 +10,10 @@ var target ...@@ -8,6 +10,10 @@ var target
var lan var lan
var resizeTimer var resizeTimer
var radius = pcPicWidth / Math.PI / 12
var planeHeight = 1024 / 6
window.addThreeD = function(en) { window.addThreeD = function(en) {
target = document.querySelector('#threeD') target = document.querySelector('#threeD')
lan = en lan = en
...@@ -47,8 +53,8 @@ function genMaterialArray(isMobile,en, callback) { ...@@ -47,8 +53,8 @@ function genMaterialArray(isMobile,en, callback) {
timeline.onload = function() { timeline.onload = function() {
const canvas = document.createElement('canvas') const canvas = document.createElement('canvas')
const sprite = isMobile const sprite = isMobile
? { w: 8980 / slice, h: 1024 } ? { w: mobilePicWidth / slice, h: 1024 }
: { w: 17943 / slice, h: 1024 } : { w: pcPicWidth / slice, h: 1024 }
const context = canvas.getContext('2d') const context = canvas.getContext('2d')
canvas.height = sprite.h canvas.height = sprite.h
canvas.width = sprite.w canvas.width = sprite.w
...@@ -73,8 +79,8 @@ function genMaterialArray(isMobile,en, callback) { ...@@ -73,8 +79,8 @@ function genMaterialArray(isMobile,en, callback) {
} }
} }
function getWindowWidth() { function getWindowWidth() {
isMobile = checkMobile() // isMobile = checkMobile()
const w = isMobile ? window.innerWidth * 2 : window.innerWidth const w = checkMobile() ? window.innerWidth * 2 : window.innerWidth
return w // > 1440 ? 1440 : w return w // > 1440 ? 1440 : w
} }
...@@ -82,7 +88,7 @@ function getWindowHeight() { ...@@ -82,7 +88,7 @@ function getWindowHeight() {
if(isMobile) { if(isMobile) {
return window.innerHeight * 0.9 return window.innerHeight * 0.9
} else { } else {
return window.innerHeight * 0.66 return window.innerHeight * 0.8
} }
} }
...@@ -93,73 +99,27 @@ function checkMobile() { ...@@ -93,73 +99,27 @@ function checkMobile() {
return mobile return mobile
} }
function init(en) { function initMobileGeo(en) {
if (!target) return
if (typeof THREE === 'undefined') {
console.log('three undefined')
return setTimeout(function() {
init(en)
}, 500)
}
renderer = new THREE.WebGLRenderer({ antialias: true })
renderer.setSize(getWindowWidth(), getWindowHeight())
renderer.setPixelRatio(window.devicePixelRatio)
target.appendChild(renderer.domElement)
// set the camera with the frustum parameters
camera = new THREE.PerspectiveCamera(30, getWindowWidth() / getWindowHeight(), 1, 1500)
controls = new THREE.OrbitControls(camera, renderer.domElement)
controls.dampingFactor = 0.6
controls.minPolarAngle = Math.PI / 2 - 0.03
controls.maxPolarAngle = controls.minPolarAngle
controls.enableZoom = false
controls.mouseButtons = { ORBIT: THREE.MOUSE.LEFT }
controls.enableKeys = false
controls.rotateSpeed = 0.5
// scene is like a graph of everything that will be rendered
scene = new THREE.Scene()
scene.background = new THREE.Color(0xffffff)
renderer.render(scene, camera)
scene.fog = new THREE.Fog(0xe3e3e3, 200, isMobile ? 640 : 1024)
cylinderGroup = new THREE.Group()
cylinderGroup.position.y -= 30
cylinderMobileGroup = new THREE.Group() cylinderMobileGroup = new THREE.Group()
cylinderMobileGroup.position.y += 30 cylinderMobileGroup.position.y += 30
for (let sliceIndex = 0; sliceIndex < slice; sliceIndex++) { for (let sliceIndex = 0; sliceIndex < slice; sliceIndex++) {
const r = 16384 / Math.PI / 12 const w = radius * Math.sin(Math.PI / slice) * 2
const w = r * Math.sin(Math.PI / slice) * 2
const h = 1024 / 6
const rotation = ((2 * Math.PI) / slice) * sliceIndex const rotation = ((2 * Math.PI) / slice) * sliceIndex
const _debugMaterial = new THREE.MeshBasicMaterial({ const _debugMaterial = new THREE.MeshBasicMaterial({
color: 16777215 / (sliceIndex * 10 + 1), color: 16777215 / (sliceIndex * 10 + 1),
transparent: true, transparent: true,
opacity: 0.0002, opacity: 0.0002,
}) })
const geo2 = new THREE.PlaneGeometry(w / 2, planeHeight)
const geo = new THREE.PlaneGeometry(w, h) geo2.translate(0, -56, 0.5 * radius * Math.cos(Math.PI / slice))
geo.translate(0, 0, r * Math.cos(Math.PI / slice))
geo.rotateY(rotation)
const mesh = new THREE.Mesh(geo, _debugMaterial)
cylinderGroup.add(mesh)
const geo2 = new THREE.PlaneGeometry(w / 2, h)
geo2.translate(0, -56, 0.5 * r * Math.cos(Math.PI / slice))
geo2.rotateY(rotation) geo2.rotateY(rotation)
const mesh2 = new THREE.Mesh(geo2, _debugMaterial) const mesh2 = new THREE.Mesh(geo2, _debugMaterial)
cylinderMobileGroup.add(mesh2) cylinderMobileGroup.add(mesh2)
} }
scene.add(cylinderGroup)
scene.add(cylinderMobileGroup) scene.add(cylinderMobileGroup)
genMaterialArray(true,en, function(canvasArray) {
genMaterialArray(false, en, function(canvasArray) {
for (let i = 0; i < slice; i++) { for (let i = 0; i < slice; i++) {
let tex = new THREE.TextureLoader().load(canvasArray[i]) let tex = new THREE.TextureLoader().load(canvasArray[i])
tex.anisotropy = renderer.capabilities.getMaxAnisotropy() tex.anisotropy = renderer.capabilities.getMaxAnisotropy()
...@@ -169,11 +129,33 @@ function init(en) { ...@@ -169,11 +129,33 @@ function init(en) {
transparent: true, transparent: true,
side: THREE.DoubleSide, side: THREE.DoubleSide,
}) })
cylinderGroup.children[i].material.needsUpdate = true cylinderMobileGroup.children[i].material.needsUpdate = true
cylinderGroup.children[i].material = mat cylinderMobileGroup.children[i].material = mat
} }
}) })
genMaterialArray(true,en, function(canvasArray) { }
function initPcGeo(en) {
cylinderGroup = new THREE.Group()
cylinderGroup.position.y -= 30
for (let sliceIndex = 0; sliceIndex < slice; sliceIndex++) {
const w = radius * Math.sin(Math.PI / slice) * 2
const rotation = ((2 * Math.PI) / slice) * sliceIndex
const _debugMaterial = new THREE.MeshBasicMaterial({
color: 16777215 / (sliceIndex * 10 + 1),
transparent: true,
opacity: 0.0002,
})
const geo = new THREE.PlaneGeometry(w, planeHeight)
geo.translate(0, 0, radius * Math.cos(Math.PI / slice))
geo.rotateY(rotation)
const mesh = new THREE.Mesh(geo, _debugMaterial)
cylinderGroup.add(mesh)
}
scene.add(cylinderGroup)
genMaterialArray(false, en, function(canvasArray) {
for (let i = 0; i < slice; i++) { for (let i = 0; i < slice; i++) {
let tex = new THREE.TextureLoader().load(canvasArray[i]) let tex = new THREE.TextureLoader().load(canvasArray[i])
tex.anisotropy = renderer.capabilities.getMaxAnisotropy() tex.anisotropy = renderer.capabilities.getMaxAnisotropy()
...@@ -183,13 +165,52 @@ function init(en) { ...@@ -183,13 +165,52 @@ function init(en) {
transparent: true, transparent: true,
side: THREE.DoubleSide, side: THREE.DoubleSide,
}) })
cylinderMobileGroup.children[i].material.needsUpdate = true cylinderGroup.children[i].material.needsUpdate = true
cylinderMobileGroup.children[i].material = mat cylinderGroup.children[i].material = mat
} }
}) })
}
function init(en) {
if (!target) return
if (typeof THREE === 'undefined') {
console.log('three undefined')
return setTimeout(function() {
init(en)
}, 500)
}
renderer = new THREE.WebGLRenderer({ antialias: true })
renderer.setSize(getWindowWidth(), getWindowHeight())
renderer.setPixelRatio(window.devicePixelRatio)
target.appendChild(renderer.domElement)
// set the camera with the frustum parameters
camera = new THREE.PerspectiveCamera(30, getWindowWidth() / getWindowHeight(), 1, 1500)
controls = new THREE.OrbitControls(camera, renderer.domElement)
controls.dampingFactor = 0.6
controls.minPolarAngle = Math.PI / 2 - 0.03
controls.maxPolarAngle = controls.minPolarAngle
controls.enableZoom = false
controls.mouseButtons = { ORBIT: THREE.MOUSE.LEFT }
controls.enableKeys = false
controls.rotateSpeed = 0.5
// scene is like a graph of everything that will be rendered
scene = new THREE.Scene()
scene.background = new THREE.Color(0xffffff)
renderer.render(scene, camera)
scene.fog = new THREE.Fog(0xe3e3e3, 200, isMobile ? 640 : 1024)
if(isMobile) {
initMobileGeo(en)
} else {
initPcGeo(en)
}
controls.update() controls.update()
onWindowResize() onWindowResize()
changeMobile() changeMobile(true)
animate() animate()
window.addEventListener('resize', onWindowResize, false) window.addEventListener('resize', onWindowResize, false)
} }
...@@ -201,14 +222,28 @@ function handleWindowResize() { ...@@ -201,14 +222,28 @@ function handleWindowResize() {
window.addThreeD(lan) window.addThreeD(lan)
} }
function changeMobile() { function changeMobile(forceUpdate = false) {
isMobile = checkMobile() var tmpIsMobile = checkMobile()
cylinderGroup.visible = !isMobile if(forceUpdate || isMobile != tmpIsMobile) {
cylinderMobileGroup.visible = isMobile isMobile = tmpIsMobile
camera.position.set(0, 0, isMobile ? 700 : 1000) if(isMobile) {
scene.fog.near = isMobile ? 500 : 500 scene.remove(cylinderGroup)
scene.fog.far = isMobile ? 550 : 1500 if(!cylinderMobileGroup) {
controls.update() initMobileGeo(lan)
}
scene.add(cylinderMobileGroup)
} else {
scene.remove(cylinderMobileGroup)
if(!cylinderGroup) {
initPcGeo(lan)
}
scene.add(cylinderGroup)
}
camera.position.set(0, 0, isMobile ? 700 : 1000)
scene.fog.near = isMobile ? 500 : 500
scene.fog.far = isMobile ? 550 : 1500
controls.update()
}
} }
function onWindowResize() { function onWindowResize() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment