我一直在玩一些THREE.js示例,并试图编译一些包含在示例中的着色器,特别是:https://github.com/brunoimbrizi/interactive-particles。
如果您导航到src/着色器/您将找到我正在编译的着色器。我目前正在运行一个React应用程序,并一直在更新这段代码以满足我的个人项目需求,但在此过程中我遇到了以下问题:
THREE.WebGLRenderer 98
THREE.WebGLShader: Shader couldn't compile.
THREE.WebGLShader: gl.getShaderInfoLog() vertex WARNING: 0:1: 'glslify' : unrecognized pragma
ERROR: 0:40: 'snoise_1_2' : no matching overloaded function found
ERROR: 0:49: 'snoise_1_2' : no matching overloaded function found
我尝试过用多种不同的方式重构顶点/片段着色器代码,但是不管我尝试的解决方案是什么,结果都是一样的。似乎在React中对这样的东西的支持是非常小的,虽然gl-react看起来几乎是有帮助的,但是我不需要组件形式的着色器。
particleFrag.js
const glsl = require('glslify');
export const fragShader = glsl`precision highp float;
uniform sampler2D uTexture;
varying vec2 vPUv;
varying vec2 vUv;
void main() {
vec4 color = vec4(0.0);
vec2 uv = vUv;
vec2 puv = vPUv;
vec4 colA = texture2D(uTexture, puv);
float grey = colA.r * 0.21 + colA.g * 0.71 + colA.b * 0.07;
vec4 colB = vec4(grey, grey, grey, 1.0);
float border = 0.3;
float radius = 0.5;
float dist = radius - distance(uv, vec2(0.5));
float t = smoothstep(0.0, border, dist);
color = colB;
color.a = t;
gl_FragColor = color;
}`;
particleVert.js
const glsl = require('glslify');
export const vertShader = glsl`#pragma glslify: snoise2 = require(glsl-noise/simplex/2d)
precision highp float;
attribute float pindex;
attribute vec3 position;
attribute vec3 offset;
attribute vec2 uv;
attribute float angle;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
uniform float uTime;
uniform float uRandom;
uniform float uDepth;
uniform float uSize;
uniform vec2 uTextureSize;
uniform sampler2D uTexture;
uniform sampler2D uTouch;
varying vec2 vPUv;
varying vec2 vUv;
float random(float n) {
return fract(sin(n) * 43758.5453123);
}
void main() {
vUv = uv;
vec2 puv = offset.xy / uTextureSize;
vPUv = puv;
vec4 colA = texture2D(uTexture, puv);
float grey = colA.r * 0.21 + colA.g * 0.71 + colA.b * 0.07;
vec3 displaced = offset;
displaced.xy += vec2(random(pindex) - 0.5, random(offset.x + pindex) - 0.5) * uRandom;
float rndz = (random(pindex) + snoise_1_2(vec2(pindex * 0.1, uTime * 0.1)));
displaced.z += rndz * (random(pindex) * 2.0 * uDepth);
displaced.xy -= uTextureSize * 0.5;
float t = texture2D(uTouch, puv).r;
displaced.z += t * 20.0 * rndz;
displaced.x += cos(angle) * t * 20.0 * rndz;
displaced.y += sin(angle) * t * 20.0 * rndz;
float psize = (snoise_1_2(vec2(uTime, pindex) * 0.5) + 2.0);
psize *= max(grey, 0.2);
psize *= uSize;
vec4 mvPosition = modelViewMatrix * vec4(displaced, 1.0);
mvPosition.xyz += position * psize;
vec4 finalPosition = projectionMatrix * mvPosition;
gl_Position = finalPosition;
}`;
界定材料:
const material = new THREE.RawShaderMaterial({
uniforms,
vertexShader: shaders.particle.vert,
fragmentShader: shaders.particle.frag,
depthTest: false,
transparent: true,
// blending: THREE.AdditiveBlending
});
所有的一切都来自于这个渲染调用:
draw() {
this.renderer.render(this.scene, this.camera);
}
我显然需要对着色器进行编译,我对这类事情缺乏经验,这使得在一个反应项目中难以实现。
发布于 2019-09-24 20:13:17
通过执行以下操作,我设法解决了这个问题:
为了在一个反应项目中实现这个操作,我们做了很多的改变,但是随着巴贝尔和Webpack的转移,似乎就能做到这一点,因为对着色文件有webpack的支持。
如果你有任何问题,更确切地说,我得到了这个操作,可以随时向我发送信息!
webpack.config.js
const HtmlWebPackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const isDevelopment = process.env.NODE_ENV === 'development';
module.exports = {
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.html$/,
use: [
{
loader: "html-loader"
}
]
},
{
test: /\.(glsl|frag|vert)$/,
use: [
require.resolve('raw-loader'),
require.resolve('glslify-loader'),
]
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
{
test: /\.module\.s([ac])ss$/,
loader: [
isDevelopment ? 'style-loader' : MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
modules: true,
sourceMap: isDevelopment
}
},
{
loader: 'sass-loader',
options: {
sourceMap: isDevelopment
}
}
]
},
{
test: /\.s([ac])ss$/,
exclude: /\.module.(s(a|c)ss)$/,
loader: [
isDevelopment ? 'style-loader' : MiniCssExtractPlugin.loader,
'css-loader',
{
loader: 'sass-loader',
options: {
sourceMap: isDevelopment
}
}
]
},
{
test: /\.(jpe?g|gif|png|svg)$/i,
use: [
{
loader: 'url-loader',
options: {
limit: 10000
}
}
]
}
]
},
resolve: {
extensions: ['.js', '.jsx', '.scss', '.css', '.html', '.json', '.png']
},
plugins: [
new HtmlWebPackPlugin({
template: "./src/index.html",
filename: "./index.html"
}),
new MiniCssExtractPlugin({
filename: isDevelopment ? '[name].css' : '[name].[hash].css',
chunkFilename: isDevelopment ? '[id].css' : '[id].[hash].css'
})
]
};
https://stackoverflow.com/questions/58051887
复制相似问题