admin管理员组文章数量:1430652
I've been trying to do the same things shadertoy does for passing audio frequency/waveform into the shader with three.js.
In this example it seems that IQ is putting frequency/waveform audio data into an image and then sampling that as a texture in the shader. How would I create that audio texture in Javascript?
To be clear I don't need help loading the texture uniform into the shader. I just don't know how to create the audio texture from an audio file.
var texture = new THREE.Texture();
shader.uniforms = {
iChannel0: { type: 't', value: texture }
};
I'm guessing I'll need to somehow put audio data into the texture I just don't know how to do that.
I've been trying to do the same things shadertoy does for passing audio frequency/waveform into the shader with three.js.
https://www.shadertoy./view/Xds3Rr
In this example it seems that IQ is putting frequency/waveform audio data into an image and then sampling that as a texture in the shader. How would I create that audio texture in Javascript?
To be clear I don't need help loading the texture uniform into the shader. I just don't know how to create the audio texture from an audio file.
var texture = new THREE.Texture();
shader.uniforms = {
iChannel0: { type: 't', value: texture }
};
I'm guessing I'll need to somehow put audio data into the texture I just don't know how to do that.
Share Improve this question edited Feb 25, 2016 at 17:14 Joseph asked Feb 25, 2016 at 15:41 JosephJoseph 1792 silver badges8 bronze badges1 Answer
Reset to default 8You can get audio data from the Web Audio API be creating an analyser node
const audioContext = new window.AudioContext();
const analyser = audioContext.createAnalyser();
Then create a buffer to receive teh data
const numSamples = analyser.frequencyBinCount;
const audioData = new Uint8Array(numSamples);
Then in your render loop get the data and put it in a texture
analyser.getByteFrequencyData(audioData);
...
gl.texImage2D(gl.TEXTURE_2D, 0, gl.LUMINANCE, numSamples, 1, 0,
gl.LUMINANCE, gl.UNSIGNED_BYTE, audioData);
or in three.js use a DataTexture
That's the short version. The longer version is audio needs to be on the same domain or you'll run into CORS issues. To get data for an audio stream like an <audio>
tag's you'd call
const source = audioContext.createMediaElementSource(audio);
That doesn't work in mobile Chrome nor mobile Safari at the moment and there are bugs in Safari.
Here's a working sample
本文标签: javascriptHow does ShaderToy load sounds into a textureStack Overflow
版权声明:本文标题:javascript - How does ShaderToy load sounds into a texture - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745544307a2662636.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论