質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Three.js

Three.jsはWebGLをサポートしているJavaScriptの3D描画用ライブラリです。

スライダー

GUIのグラフィカルウィジェットのひとつです。インジケーターを動かすことで値を調節可能とします。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

WebGL

WebGL(ウェブジーエル)は、ウェブブラウザで 3次元コンピュータグラフィックスを表示させるための標準仕様です。

Q&A

解決済

1回答

1376閲覧

three.jsのトランジション処理について

s0320318

総合スコア4

Three.js

Three.jsはWebGLをサポートしているJavaScriptの3D描画用ライブラリです。

スライダー

GUIのグラフィカルウィジェットのひとつです。インジケーターを動かすことで値を調節可能とします。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

WebGL

WebGL(ウェブジーエル)は、ウェブブラウザで 3次元コンピュータグラフィックスを表示させるための標準仕様です。

0グッド

0クリップ

投稿2020/03/12 03:55

前提・実現したいこと

https://tympanus.net/Development/webGLImageTransitions/index6.html
上記ページの動きを参考に、スライドショーを実装したいと思っています。
「前へ」「次へ」ボタンを設置して、「次へ」を押したときは上記ページの動きのままで良いのですが、「前へ」を押したときは逆に(左から右へ)アニメーションさせたいと思っています。

発生している問題・エラーメッセージ

上記デモページをダウンロードして、ローカルで検証しているのですが、Three.jsやWebGLの知識がなく、実現できていない状態です。

該当のソースコード

sketch.js

JavaScript

1class Sketch { 2 constructor(opts) { 3 this.scene = new THREE.Scene(); 4 this.vertex = `varying vec2 vUv;void main() {vUv = uv;gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );}`; 5 this.fragment = opts.fragment; 6 this.uniforms = opts.uniforms; 7 this.renderer = new THREE.WebGLRenderer(); 8 this.width = window.innerWidth; 9 this.height = window.innerHeight; 10 this.renderer.setPixelRatio(window.devicePixelRatio); 11 this.renderer.setSize(this.width, this.height); 12 this.renderer.setClearColor(0xeeeeee, 1); 13 this.duration = opts.duration || 1; 14 this.debug = opts.debug || false 15 this.easing = opts.easing || 'easeInOut' 16 17 this.clicker = document.getElementById("content"); 18 19 20 this.container = document.getElementById("slider"); 21 this.images = JSON.parse(this.container.getAttribute('data-images')); 22 this.width = this.container.offsetWidth; 23 this.height = this.container.offsetHeight; 24 this.container.appendChild(this.renderer.domElement); 25 26 this.camera = new THREE.PerspectiveCamera( 27 70, 28 window.innerWidth / window.innerHeight, 29 0.001, 30 1000 31 ); 32 33 this.camera.position.set(0, 0, 2); 34 this.time = 0; 35 this.current = 0; 36 this.textures = []; 37 38 this.paused = true; 39 this.initiate(()=>{ 40 console.log(this.textures); 41 this.setupResize(); 42 this.settings(); 43 this.addObjects(); 44 this.resize(); 45 this.clickEvent(); 46 this.play(); 47 }) 48 49 50 51 } 52 53 initiate(cb){ 54 const promises = []; 55 let that = this; 56 this.images.forEach((url,i)=>{ 57 let promise = new Promise(resolve => { 58 that.textures[i] = new THREE.TextureLoader().load( url, resolve ); 59 }); 60 promises.push(promise); 61 }) 62 63 Promise.all(promises).then(() => { 64 cb(); 65 }); 66 } 67 68 clickEvent(){ 69 this.clicker.addEventListener('click',()=>{ 70 this.next(); 71 }) 72 } 73 settings() { 74 let that = this; 75 if(this.debug) this.gui = new dat.GUI(); 76 this.settings = {progress:0.5}; 77 // if(this.debug) this.gui.add(this.settings, "progress", 0, 1, 0.01); 78 79 Object.keys(this.uniforms).forEach((item)=> { 80 this.settings[item] = this.uniforms[item].value; 81 if(this.debug) this.gui.add(this.settings, item, this.uniforms[item].min, this.uniforms[item].max, 0.01); 82 }) 83 } 84 85 setupResize() { 86 window.addEventListener("resize", this.resize.bind(this)); 87 } 88 89 resize() { 90 this.width = this.container.offsetWidth; 91 this.height = this.container.offsetHeight; 92 this.renderer.setSize(this.width, this.height); 93 this.camera.aspect = this.width / this.height; 94 95 96 // image cover 97 this.imageAspect = this.textures[0].image.height/this.textures[0].image.width; 98 let a1; let a2; 99 if(this.height/this.width>this.imageAspect) { 100 a1 = (this.width/this.height) * this.imageAspect ; 101 a2 = 1; 102 } else{ 103 a1 = 1; 104 a2 = (this.height/this.width) / this.imageAspect; 105 } 106 107 this.material.uniforms.resolution.value.x = this.width; 108 this.material.uniforms.resolution.value.y = this.height; 109 this.material.uniforms.resolution.value.z = a1; 110 this.material.uniforms.resolution.value.w = a2; 111 112 const dist = this.camera.position.z; 113 const height = 1; 114 this.camera.fov = 2*(180/Math.PI)*Math.atan(height/(2*dist)); 115 116 this.plane.scale.x = this.camera.aspect; 117 this.plane.scale.y = 1; 118 119 this.camera.updateProjectionMatrix(); 120 121 122 } 123 124 addObjects() { 125 let that = this; 126 this.material = new THREE.ShaderMaterial({ 127 extensions: { 128 derivatives: "#extension GL_OES_standard_derivatives : enable" 129 }, 130 side: THREE.DoubleSide, 131 uniforms: { 132 time: { type: "f", value: 0 }, 133 progress: { type: "f", value: 0 }, 134 border: { type: "f", value: 0 }, 135 intensity: { type: "f", value: 0 }, 136 scaleX: { type: "f", value: 40 }, 137 scaleY: { type: "f", value: 40 }, 138 transition: { type: "f", value: 40 }, 139 swipe: { type: "f", value: 0 }, 140 width: { type: "f", value: 0 }, 141 radius: { type: "f", value: 0 }, 142 texture1: { type: "f", value: this.textures[0] }, 143 texture2: { type: "f", value: this.textures[1] }, 144 displacement: { type: "f", value: new THREE.TextureLoader().load('img/disp1.jpg') }, 145 resolution: { type: "v4", value: new THREE.Vector4() }, 146 }, 147 // wireframe: true, 148 vertexShader: this.vertex, 149 fragmentShader: this.fragment 150 }); 151 152 this.geometry = new THREE.PlaneGeometry(1, 1, 2, 2); 153 154 this.plane = new THREE.Mesh(this.geometry, this.material); 155 this.scene.add(this.plane); 156 } 157 158 stop() { 159 this.paused = true; 160 } 161 162 play() { 163 this.paused = false; 164 this.render(); 165 } 166 167 next(){ 168 if(this.isRunning) return; 169 this.isRunning = true; 170 let len = this.textures.length; 171 let nextTexture =this.textures[(this.current +1)%len]; 172 this.material.uniforms.texture2.value = nextTexture; 173 let tl = new TimelineMax(); 174 tl.to(this.material.uniforms.progress,this.duration,{ 175 value:1, 176 ease: Power2[this.easing], 177 onComplete:()=>{ 178 console.log('FINISH'); 179 this.current = (this.current +1)%len; 180 this.material.uniforms.texture1.value = nextTexture; 181 this.material.uniforms.progress.value = 0; 182 this.isRunning = false; 183 }}) 184 } 185 render() { 186 if (this.paused) return; 187 this.time += 0.05; 188 this.material.uniforms.time.value = this.time; 189 // this.material.uniforms.progress.value = this.settings.progress; 190 191 Object.keys(this.uniforms).forEach((item)=> { 192 this.material.uniforms[item].value = this.settings[item]; 193 }); 194 195 // this.camera.position.z = 3; 196 // this.plane.rotation.y = 0.4*Math.sin(this.time) 197 // this.plane.rotation.x = 0.5*Math.sin(0.4*this.time) 198 199 requestAnimationFrame(this.render.bind(this)); 200 this.renderer.render(this.scene, this.camera); 201 } 202}

demo6.js

JavaScript

1// planetary 2let sketch = new Sketch({ 3 debug: true, 4 uniforms: { 5 intensity: {value: 1, type:'f', min:0., max:3} 6 }, 7 fragment: ` 8 uniform float time; 9 uniform float progress; 10 uniform float intensity; 11 uniform float width; 12 uniform float scaleX; 13 uniform float scaleY; 14 uniform float transition; 15 uniform float radius; 16 uniform float swipe; 17 uniform sampler2D texture1; 18 uniform sampler2D texture2; 19 uniform sampler2D displacement; 20 uniform vec4 resolution; 21 varying vec2 vUv; 22 mat2 getRotM(float angle) { 23 float s = sin(angle); 24 float c = cos(angle); 25 return mat2(c, -s, s, c); 26 } 27 const float PI = 3.1415; 28 const float angle1 = PI *0.25; 29 const float angle2 = -PI *0.75; 30 31 32 void main() { 33 vec2 newUV = (vUv - vec2(0.5))*resolution.zw + vec2(0.5); 34 35 vec4 disp = texture2D(displacement, newUV); 36 vec2 dispVec = vec2(disp.r, disp.g); 37 38 vec2 distortedPosition1 = newUV + getRotM(angle1) * dispVec * intensity * progress; 39 vec4 t1 = texture2D(texture1, distortedPosition1); 40 41 vec2 distortedPosition2 = newUV + getRotM(angle2) * dispVec * intensity * (1.0 - progress); 42 vec4 t2 = texture2D(texture2, distortedPosition2); 43 44 gl_FragColor = mix(t1, t2, progress); 45 46 } 47 48 ` 49});

試したこと

demo6.js の2箇所を以下の通り変更することで、逆にアニメーションすることはわかりました。

vec2 distortedPosition1 = newUV + getRotM(angle1) * dispVec * intensity * progress; ↓↓↓ vec2 distortedPosition1 = newUV + getRotM(angle1) * -1 * dispVec * intensity * progress;
vec2 distortedPosition2 = newUV + getRotM(angle2) * dispVec * intensity * (1.0 - progress); ↓↓↓ vec2 distortedPosition2 = newUV + getRotM(angle2) * -1 * dispVec * intensity * (1.0 - progress);

ただ、やりたいことは押したボタンによってアニメーションの向きを変えることなので、この2つのJS間でどのように変数などのやり取りをして実現すれば良いのか、わかっていない状態です。
お手数ですがよろしくお願いします!

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

自己解決できました!

投稿2020/03/13 00:05

編集2020/03/13 00:11
s0320318

総合スコア4

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問