首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >THREE.WebGLShader: Shader无法编译;“s_noise_1_2”:没有找到匹配的重载函数

THREE.WebGLShader: Shader无法编译;“s_noise_1_2”:没有找到匹配的重载函数
EN

Stack Overflow用户
提问于 2019-09-22 17:49:33
回答 1查看 2.5K关注 0票数 3

我一直在玩一些THREE.js示例,并试图编译一些包含在示例中的着色器,特别是:https://github.com/brunoimbrizi/interactive-particles

如果您导航到src/着色器/您将找到我正在编译的着色器。我目前正在运行一个React应用程序,并一直在更新这段代码以满足我的个人项目需求,但在此过程中我遇到了以下问题:

没有找到匹配的重载函数

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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;
}`;

界定材料:

代码语言:javascript
运行
复制
const material = new THREE.RawShaderMaterial({
            uniforms,
            vertexShader: shaders.particle.vert,
            fragmentShader: shaders.particle.frag,
            depthTest: false,
            transparent: true,
            // blending: THREE.AdditiveBlending
        });

所有的一切都来自于这个渲染调用:

代码语言:javascript
运行
复制
draw() {
        this.renderer.render(this.scene, this.camera);
    }

我显然需要对着色器进行编译,我对这类事情缺乏经验,这使得在一个反应项目中难以实现。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-24 20:13:17

通过执行以下操作,我设法解决了这个问题:

  • 为我的项目增加了对Babel/Webpack的支持。
  • 在CSS类定义的“容器”中呈现粒子化图像。
  • webpack对格利格的支持得到了证实。

为了在一个反应项目中实现这个操作,我们做了很多的改变,但是随着巴贝尔和Webpack的转移,似乎就能做到这一点,因为对着色文件有webpack的支持。

如果你有任何问题,更确切地说,我得到了这个操作,可以随时向我发送信息!

webpack.config.js

代码语言:javascript
运行
复制
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'
        })
    ]
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58051887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档