123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475 |
- import * as THREE from 'three';
- import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer.js';
- import { RenderPass } from 'three/examples/jsm/postprocessing/RenderPass.js';
- import { OutlinePass } from 'three/examples/jsm/postprocessing/OutlinePass.js';
- import { FXAAShader } from 'three/examples/jsm/shaders/FXAAShader.js';
- import { ShaderPass } from 'three/examples/jsm/postprocessing/ShaderPass.js';
- import { TWEEN } from 'three/examples/jsm/libs/tween.module.min.js';
- import { RGBELoader } from 'three/examples/jsm/loaders/RGBELoader.js';
- import gsap from 'gsap';
- import { useAppStore } from '/@/store/modules/app';
- import UseThree from './useThree';
- export const setModalCenter = (group, modal?) => {
- const box3 = new THREE.Box3();
-
-
- box3.expandByObject(group);
-
- const center = new THREE.Vector3();
- box3.getCenter(center);
-
-
- group.position.x = group.position.x - center.x;
- group.position.y = group.position.y - center.y;
- group.position.z = group.position.z - center.z;
- };
- export const getTextCanvas = (w, h, textArr, imgUrl) => {
-
- const width = w;
- const height = h;
-
- const canvas = document.createElement('canvas');
- canvas.style.letterSpacing = 10 + 'px';
- const ctx = canvas.getContext('2d') as CanvasRenderingContext2D;
- canvas.width = width;
- canvas.height = height;
-
- ctx.textAlign = 'start';
- ctx.fillStyle = 'rgba(0, 0, 0, 0)';
-
-
-
-
-
-
-
- ctx.shadowColor = 'rgba(0, 10,0,0.8)';
- ctx.shadowBlur = 4;
- ctx.shadowOffsetX = 1;
- ctx.shadowOffsetY = 1;
- ctx.fillRect(0, 0, width, height);
-
- return new Promise((resolve, reject) => {
- if (imgUrl) {
- const img = new Image();
- img.src = new URL('../../assets/images/vent/model_image/' + imgUrl, import.meta.url).href;
- img.onload = () => {
-
- ctx.clearRect(0, 0, width, height);
-
- ctx.drawImage(img, 0, 0, width, height);
- ctx.textBaseline = 'middle';
-
-
- textArr.forEach((item) => {
- ctx.font = item.font;
- ctx.fillStyle = item.color;
- ctx.fillText(item.text, item.x, item.y, 1024);
- });
- resolve(canvas);
- };
-
- img.onerror = (e) => {
- reject(e);
- };
- } else {
-
- ctx.clearRect(0, 0, width, height);
- ctx.textBaseline = 'middle';
- textArr.forEach((item) => {
- ctx.lineWidth = 2;
- ctx.font = item.font;
- ctx.fillStyle = item.color;
-
-
- ctx.fillText(item.text, item.x, item.y, 1024);
- });
- resolve(canvas);
- }
- });
- };
- export const setLineGeo = (scene) => {
- const box = new THREE.BoxGeometry(30, 30, 30);
-
- const edges = new THREE.EdgesGeometry(box);
-
- new THREE.TextureLoader().setPath('/model/hdr/').load('8.png', (texture) => {
- const edgesMaterial = new THREE.MeshBasicMaterial({
-
- map: texture,
- transparent: true,
- depthWrite: false,
- });
- const line = new THREE.LineSegments(edges, edgesMaterial);
-
- scene.add(line);
- });
- box.attributes.position.array;
- const lightMaterial = new THREE.ShaderMaterial({
- vertexShader: `varying vec3 vPosition;
- varying vec2 vUv;
- uniform float uTime;
-
- void main(){
- // vec3 scalePosition = vec3(position.x+uTime,position.y,position.z+uTime);
- vec4 viewPosition = viewMatrix * modelMatrix * vec4(position,1);
- gl_Position = projectionMatrix * viewPosition;
- vPosition = position;
- vUv = uv;
-
- }`,
- fragmentShader: `varying vec3 vPosition;
- varying vec2 vUv;
- uniform vec3 uColor;
- uniform float uHeight;
- vec4 permute(vec4 x)
- {
- return mod(((x*34.0)+1.0)*x, 289.0);
- }
- vec2 fade(vec2 t)
- {
- return t*t*t*(t*(t*6.0-15.0)+10.0);
- }
- float cnoise(vec2 P)
- {
- vec4 Pi = floor(P.xyxy) + vec4(0.0, 0.0, 1.0, 1.0);
- vec4 Pf = fract(P.xyxy) - vec4(0.0, 0.0, 1.0, 1.0);
- Pi = mod(Pi, 289.0); // To avoid truncation effects in permutation
- vec4 ix = Pi.xzxz;
- vec4 iy = Pi.yyww;
- vec4 fx = Pf.xzxz;
- vec4 fy = Pf.yyww;
- vec4 i = permute(permute(ix) + iy);
- vec4 gx = 2.0 * fract(i * 0.0243902439) - 1.0; // 1/41 = 0.024...
- vec4 gy = abs(gx) - 0.5;
- vec4 tx = floor(gx + 0.5);
- gx = gx - tx;
- vec2 g00 = vec2(gx.x,gy.x);
- vec2 g10 = vec2(gx.y,gy.y);
- vec2 g01 = vec2(gx.z,gy.z);
- vec2 g11 = vec2(gx.w,gy.w);
- vec4 norm = 1.79284291400159 - 0.85373472095314 * vec4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11));
- g00 *= norm.x;
- g01 *= norm.y;
- g10 *= norm.z;
- g11 *= norm.w;
- float n00 = dot(g00, vec2(fx.x, fy.x));
- float n10 = dot(g10, vec2(fx.y, fy.y));
- float n01 = dot(g01, vec2(fx.z, fy.z));
- float n11 = dot(g11, vec2(fx.w, fy.w));
- vec2 fade_xy = fade(Pf.xy);
- vec2 n_x = mix(vec2(n00, n01), vec2(n10, n11), fade_xy.x);
- float n_xy = mix(n_x.x, n_x.y, fade_xy.y);
- return 2.3 * n_xy;
- }
-
- void main(){
-
- float strength = (vPosition.y+uHeight/2.0)/uHeight;
- gl_FragColor = vec4(uColor,1.0 - strength);
- // float strength =1.0 - abs(cnoise(vUv * 10.0)) ;
- // gl_FragColor =vec4(strength,strength,strength,1);
- }`,
- transparent: true,
- side: THREE.DoubleSide,
- });
- const lightMesh = new THREE.Mesh(box, lightMaterial);
- lightMesh.geometry.computeBoundingBox();
- const { min, max } = lightMesh.geometry.boundingBox;
- const uHeight = max.y - min.y;
- lightMaterial.uniforms.uHeight = {
- value: uHeight,
- };
- lightMaterial.uniforms.uColor = {
- value: new THREE.Color(0x00ff00),
- };
- lightMaterial.uniforms.uTime = {
- value: 0,
- };
-
-
-
-
-
-
-
-
-
- scene.add(lightMesh);
- };
- export const setOutline = (model, group) => {
- const { scene, renderer, camera } = model;
- const params = {
- edgeStrength: 10.0,
- edgeGlow: 1,
- edgeThickness: 1.0,
- pulsePeriod: 5,
- rotate: false,
- usePatternTexture: false,
- };
- const composer = new EffectComposer(renderer);
- const renderPass = new RenderPass(scene, camera);
- composer.addPass(renderPass);
- const outlinePass = new OutlinePass(new THREE.Vector2(window.innerWidth, window.innerHeight), scene, camera);
- composer.addPass(outlinePass);
- outlinePass.visibleEdgeColor.set(parseInt(0xffffff));
- outlinePass.hiddenEdgeColor.set('#190a05');
- outlinePass.edgeStrength = params.edgeStrength;
- outlinePass.edgeThickness = params.edgeThickness;
- outlinePass.pulsePeriod = params.pulsePeriod;
- outlinePass.usePatternTexture = params.usePatternTexture;
-
-
-
-
-
-
- const effectFXAA = new ShaderPass(FXAAShader);
- effectFXAA.uniforms['resolution'].value.set(1 / window.innerWidth, 1 / window.innerHeight);
- composer.addPass(effectFXAA);
- const scale = 1;
- group.traverse(function (child) {
- if (child instanceof THREE.Mesh) {
-
- child.geometry.computeBoundingSphere();
- }
- });
-
- group.scale.divideScalar(scale);
- return { outlinePass, composer };
- };
- export const renderVideo = (group, player, playerMeshName) => {
-
- const texture = new THREE.VideoTexture(player);
- if (texture && group?.getObjectByName(playerMeshName)) {
- const player = group.getObjectByName(playerMeshName);
- player.material.map = texture;
- } else {
-
- const planeGeometry = new THREE.PlaneGeometry(30, 20);
- const material = new THREE.MeshBasicMaterial({
- map: texture,
- side: THREE.DoubleSide,
- });
-
- texture.magFilter = THREE.LinearFilter;
- texture.minFilter = THREE.LinearFilter;
- texture.wrapS = texture.wrapT = THREE.ClampToEdgeWrapping;
- texture.format = THREE.RGBAFormat;
- texture.anisotropy = 0.5;
-
- const mesh = new THREE.Mesh(planeGeometry, material);
- mesh.name = playerMeshName;
-
- return mesh;
- }
- };
- export const animateCamera = (oldP, oldT, newP, newT, model, duration = 0.5, callBack?) => {
- return new Promise((resolve) => {
- const camera = model.camera;
- const controls = model.orbitControls;
- controls.enabled = false;
- controls.target.set(0, 0, 0);
- const animateObj = {
- x1: oldP.x,
- y1: oldP.y,
- z1: oldP.z,
- x2: oldT.x,
- y2: oldT.y,
- z2: oldT.z,
- };
- gsap.fromTo(
- animateObj,
- {
- x1: oldP.x,
- y1: oldP.y,
- z1: oldP.z,
- x2: oldT.x,
- y2: oldT.y,
- z2: oldT.z,
- },
- {
- x1: newP.x,
- y1: newP.y,
- z1: newP.z,
- x2: newT.x,
- y2: newT.y,
- z2: newT.z,
- duration: duration,
- ease: 'easeOutBounce',
- onUpdate: function (object) {
-
- camera.position.set(object.x1, object.y1, object.z1);
- controls.target.set(object.x2, object.y2, object.z2);
- controls.update();
- if (callBack) callBack();
- },
- onUpdateParams: [animateObj],
- onComplete: function () {
-
- controls.enabled = true;
- resolve(null);
- },
- }
- );
- });
- };
- export const transScreenCoord = (vector, camera) => {
-
-
-
-
-
- const stdVector = vector.project(camera);
- const a = window.innerWidth / 2;
- const b = window.innerHeight / 2;
- const x = Math.round(stdVector.x * a + a);
- const y = Math.round(-stdVector.y * b + b);
- return { x, y };
- };
- export const drawHot = (scale: number) => {
-
-
- const hotMap = new THREE.TextureLoader().load('/model/img/hot-point.png');
- const material = new THREE.SpriteMaterial({
- map: hotMap,
- });
- const hotPoint = new THREE.Sprite(material);
- const spriteTween = new TWEEN.Tween({
- scale: 1 * scale,
- })
- .to(
- {
- scale: 0.65 * scale,
- },
- 1000
- )
- .easing(TWEEN.Easing.Quadratic.Out);
- spriteTween.onUpdate(function (that) {
- hotPoint.scale.set(that.scale, that.scale, that.scale);
- });
- spriteTween.yoyo(true);
- spriteTween.repeat(Infinity);
- spriteTween.start();
- return hotPoint;
- };
- export const deviceDetailCard = () => {
-
- };
- export const updateAxisCenter = (modal: UseThree, group: THREE.Object3D, event, callBack?) => {
- if (!modal) return;
- const appStore = useAppStore();
- event.stopPropagation();
- const widthScale = appStore.getWidthScale;
- const heightScale = appStore.getHeightScale;
-
- modal.mouse.x =
- ((-modal.canvasContainer.getBoundingClientRect().left * widthScale + event.clientX) / (modal.canvasContainer.clientWidth * widthScale)) * 2 - 1;
- modal.mouse.y =
- -((-modal.canvasContainer.getBoundingClientRect().top + event.clientY) / (modal.canvasContainer.clientHeight * heightScale)) * 2 + 1;
- (modal.rayCaster as THREE.Raycaster).setFromCamera(modal.mouse, modal.camera as THREE.Camera);
- if (group) {
- const intersects = modal.rayCaster?.intersectObjects(group.children, true) as THREE.Intersection[];
- if (intersects.length > 0) {
- const point = intersects[0].point;
- const target0 = modal.orbitControls.target.clone();
- gsap.fromTo(
- modal.orbitControls.target,
- { x: target0.x, y: target0.y, z: target0.z },
- {
- x: point.x,
- y: point.y,
- z: point.z,
- duration: 0.4,
- ease: 'easeInCirc',
- onUpdate: function (object) {
- if (object) modal.camera?.lookAt(new THREE.Vector3(object.x, object.y, object.z));
- },
- }
- );
- callBack(intersects);
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- };
- export const addEnvMap = (hdr, modal) => {
- return new Promise((resolve) => {
- new RGBELoader().setPath('/model/hdr/').load(hdr + '.hdr', (texture) => {
- texture.mapping = THREE.EquirectangularReflectionMapping;
- const defaultEnvironment = texture;
- modal.scene.environment = defaultEnvironment;
- resolve(texture);
- });
- });
- };
|