简体   繁体   English

JS-我如何将其纳入ES6模块?

[英]JS- how would I make this into an ES6 module?

Ok, I'm breaking apart the ShaderGlow example from https://github.com/stemkoski/stemkoski.github.com/blob/master/Three.js/Shader-Glow.html trying to bring it into my node app. 好的,我从https://github.com/stemkoski/stemkoski.github.com/blob/master/Three.js/Shader-Glow.html分解了ShaderGlow示例,试图将其引入我的节点应用程序。

Author does everything in tags in the html doc, but my whole app is in app.js and calls in ES6 modules from helper files. 作者在html文档中的标记中执行所有操作,但是我的整个应用程序在app.js并从帮助程序文件中调用ES6模块。

The shader, before my tweaking, works like this: 在我进行调整之前,着色器的工作方式如下:

var customMaterial = new THREE.ShaderMaterial(
{
    uniforms:
    {
        "c":   { type: "f", value: 1.0 },
        "p":   { type: "f", value: 1.4 },
        glowColor: { type: "c", value: new THREE.Color(0xffff00) },
        viewVector: { type: "v3", value: camera.position }
    },
    vertexShader:   document.getElementById( 'vertexShader'   ).textContent,
    fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
    side: THREE.FrontSide,
    blending: THREE.AdditiveBlending,
    transparent: true
}   );

Where fragment and vertex shader are in their own 's: 片段着色器和顶点着色器位于各自的位置:

<!-- ---------------- Custom Shader Code ------------------------ -->
<script id="vertexShader" type="x-shader/x-vertex">
uniform vec3 viewVector;
uniform float c;
uniform float p;
varying float intensity;
void main() 
{
    vec3 vNormal = normalize( normalMatrix * normal );
    vec3 vNormel = normalize( normalMatrix * viewVector );
    intensity = pow( c - dot(vNormal, vNormel), p );

    gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}
</script>

<!-- fragment shader a.k.a. pixel shader -->
<script id="fragmentShader" type="x-shader/x-vertex"> 
uniform vec3 glowColor;
varying float intensity;
void main() 
{
    vec3 glow = glowColor * intensity;
    gl_FragColor = vec4( glow, 1.0 );
}
</script>

So far I've tried doing: 到目前为止,我已经尝试过:

import fragmentShader from './elements/GlowShaders.js';
import vertexShader from './elements/GlowShaders.js';

    var customMaterial = new THREE.ShaderMaterial(
    {
            uniforms:
        {
            "c":   { type: "f", value: 1.0 },
            "p":   { type: "f", value: 1.4 },
            glowColor: { type: "c", value: new THREE.Color(0xffff00) },
            viewVector: { type: "v3", value: this.camera.position }
        },
        vertexShader:  vertexShader,
        fragmentShader: fragmentShader,
        side: THREE.FrontSide,
        blending: THREE.AdditiveBlending,
        transparent: true
    }   );

With GlowShader.js written like: 使用GlowShader.js编写如下:

export default class vertexShader {
  uniform vec3 viewVector;
  uniform float c;
  uniform float p;
  varying float intensity;
  void main()
  {
      vec3 vNormal = normalize( normalMatrix * normal );
    vec3 vNormel = normalize( normalMatrix * viewVector );
    intensity = pow( c - dot(vNormal, vNormel), p );

      gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  }
}

export default class fragmentShader {
  uniform vec3 glowColor;
  varying float intensity;
  constructor() {
    vec3 glow = glowColor * intensity;
      gl_FragColor = vec4( glow, 1.0 );
  }
}

Honestly very new to modules and this does not work. 老实说对模块来说还很新,这是行不通的。 How can I break these special shader blocks into modules, or incorporate them in my app.js script? 如何将这些特殊的着色器块分解为模块,或将其合并到我的app.js脚本中?

Can you provide an answer where you put the code block into JSON.stringify or another format able to be read by the JS? 您能否提供将代码块放入JSON.stringify或JS可以读取的其他格式的答案? Pasting straight into the parameter doesn't work, no. 直接粘贴到参数中不起作用,不可以。

const vertexShader = `
uniform vec3 viewVector;
uniform float c;
uniform float p;
varying float intensity;
void main() 
{
    vec3 vNormal = normalize( normalMatrix * normal );
    vec3 vNormel = normalize( normalMatrix * viewVector );
    intensity = pow( c - dot(vNormal, vNormel), p );

    gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}`;

const fragmentShader = `
uniform vec3 glowColor;
varying float intensity;
void main() 
{
    vec3 glow = glowColor * intensity;
    gl_FragColor = vec4( glow, 1.0 );
}`;

var customMaterial = new THREE.ShaderMaterial({
    uniforms:
    {
        "c":   { type: "f", value: 1.0 },
        "p":   { type: "f", value: 1.4 },
        glowColor: { type: "c", value: new THREE.Color(0xffff00) },
        viewVector: { type: "v3", value: camera.position }
    },
    vertexShader,
    fragmentShader,
    side: THREE.FrontSide,
    blending: THREE.AdditiveBlending,
    transparent: true
});

or 要么

var customMaterial = new THREE.ShaderMaterial({
    uniforms:
    {
        "c":   { type: "f", value: 1.0 },
        "p":   { type: "f", value: 1.4 },
        glowColor: { type: "c", value: new THREE.Color(0xffff00) },
        viewVector: { type: "v3", value: camera.position }
    },
    vertexShader: "uniform vec3 viewVector;\nuniform float c;\nuniform float p;\nvarying float intensity;\nvoid main() \n{\n    vec3 vNormal = normalize( normalMatrix * normal );\n    vec3 vNormel = normalize( normalMatrix * viewVector );\n    intensity = pow( c - dot(vNormal, vNormel), p );\n\n    gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n}",
    fragmentShader: "uniform vec3 glowColor;\nvarying float intensity;\nvoid main() \n{\n    vec3 glow = glowColor * intensity;\n    gl_FragColor = vec4( glow, 1.0 );\n}",
    side: THREE.FrontSide,
    blending: THREE.AdditiveBlending,
    transparent: true
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM