ThreeJS r160 ShaderMaterial 纹理呈现黑色

问题描述 投票:0回答:1

我正在尝试将纹理添加到该着色器中,但是当我这样做时,它会呈现黑色。我可能遗漏了一些小东西,但我找不到 ThreeJS 版本 r160 的任何信息。顺便说一句,着色器代码现在都是默认的,但我计划对其进行自定义。

如果我使用漫反射颜色,它就可以工作。该页面位于 mfzcreations.com/HRD

编辑:我在简单的盒子网格上测试了材质,它的显示方式与现场网格上的完全相同。

这是JS代码。

let dirtTexture = new THREE.TextureLoader().load("textures/fieldDirtTexture.jpg");
let standard = THREE.ShaderLib['standard'];
    let dirtMaterial = new THREE.ShaderMaterial({
    lights: true,
    uniforms: THREE.UniformsUtils.clone( standard.uniforms ),
    defines: {
        PHYSICAL: true
    },
    vertexShader: document.getElementById( 'dirtVS' ).textContent,
    fragmentShader: document.getElementById( 'dirtFS' ).textContent
});

dirtMaterial.map = dirtTexture;

// dirtMaterial.uniforms.diffuse.value = new THREE.Color( 0x91773d );
dirtMaterial.uniforms.roughness.value = 1.0;
dirtMaterial.uniforms.metalness.value = 0.0;
dirtMaterial.uniforms.ior.value = 0.0;

这是着色器代码。

<script id="dirtVS" type="GLSL">
        #define STANDARD

        varying vec3 vViewPosition;

        #ifdef USE_TRANSMISSION

            varying vec3 vWorldPosition;

        #endif

        #include <common>
        #include <batching_pars_vertex>
        #include <uv_pars_vertex>
        #include <displacementmap_pars_vertex>
        #include <color_pars_vertex>
        #include <fog_pars_vertex>
        #include <normal_pars_vertex>
        #include <morphtarget_pars_vertex>
        #include <skinning_pars_vertex>
        #include <shadowmap_pars_vertex>
        #include <logdepthbuf_pars_vertex>
        #include <clipping_planes_pars_vertex>

        void main() {

            #include <uv_vertex>
            #include <color_vertex>
            #include <morphcolor_vertex>
            #include <batching_vertex>

            #include <beginnormal_vertex>
            #include <morphnormal_vertex>
            #include <skinbase_vertex>
            #include <skinnormal_vertex>
            #include <defaultnormal_vertex>
            #include <normal_vertex>

            #include <begin_vertex>
            #include <morphtarget_vertex>
            #include <skinning_vertex>
            #include <displacementmap_vertex>
            #include <project_vertex>
            #include <logdepthbuf_vertex>
            #include <clipping_planes_vertex>

            vViewPosition = - mvPosition.xyz;

            #include <worldpos_vertex>
            #include <shadowmap_vertex>
            #include <fog_vertex>

            #ifdef USE_TRANSMISSION

                vWorldPosition = worldPosition.xyz;

            #endif
        }
    </script>

<script id="dirtFS" type="GLSL">
        #define STANDARD

    
        #ifdef PHYSICAL
            #define IOR
            #define USE_SPECULAR
        #endif
        

        uniform vec3 diffuse;
        uniform vec3 emissive;
        uniform float roughness;
        uniform float metalness;
        uniform float opacity;

        #ifdef IOR
            uniform float ior;
        #endif
    
        #ifdef USE_SPECULAR
            uniform float specularIntensity;
            uniform vec3 specularColor;

            #ifdef USE_SPECULAR_COLORMAP
                uniform sampler2D specularColorMap;
            #endif

            #ifdef USE_SPECULAR_INTENSITYMAP
                uniform sampler2D specularIntensityMap;
            #endif
        #endif

        
        #ifdef USE_CLEARCOAT
            uniform float clearcoat;
            uniform float clearcoatRoughness;
        #endif

        #ifdef USE_IRIDESCENCE
            uniform float iridescence;
            uniform float iridescenceIOR;
            uniform float iridescenceThicknessMinimum;
            uniform float iridescenceThicknessMaximum;
        #endif

        #ifdef USE_SHEEN
            uniform vec3 sheenColor;
            uniform float sheenRoughness;

            #ifdef USE_SHEEN_COLORMAP
                uniform sampler2D sheenColorMap;
            #endif

            #ifdef USE_SHEEN_ROUGHNESSMAP
                uniform sampler2D sheenRoughnessMap;
            #endif
        #endif


        #ifdef USE_ANISOTROPY
            uniform vec2 anisotropyVector;

            #ifdef USE_ANISOTROPYMAP
                uniform sampler2D anisotropyMap;
            #endif
        #endif

        varying vec3 vViewPosition;

        #include <common>
        #include <packing>
        #include <dithering_pars_fragment>
        #include <color_pars_fragment>
        #include <uv_pars_fragment>
        #include <map_pars_fragment>
        #include <alphamap_pars_fragment>
        #include <alphatest_pars_fragment>
        #include <alphahash_pars_fragment>
        #include <aomap_pars_fragment>
        #include <lightmap_pars_fragment>
        #include <emissivemap_pars_fragment>
        #include <iridescence_fragment>
        #include <cube_uv_reflection_fragment>
        #include <envmap_common_pars_fragment>
        #include <envmap_physical_pars_fragment>
        #include <fog_pars_fragment>
        #include <lights_pars_begin>
        #include <normal_pars_fragment>
        #include <lights_physical_pars_fragment>
        #include <transmission_pars_fragment>
        #include <shadowmap_pars_fragment>
        #include <bumpmap_pars_fragment>
        #include <normalmap_pars_fragment>
        #include <clearcoat_pars_fragment>
        #include <iridescence_pars_fragment>
        #include <roughnessmap_pars_fragment>
        #include <metalnessmap_pars_fragment>
        #include <logdepthbuf_pars_fragment>
        #include <clipping_planes_pars_fragment>

        void main() {

            #include <clipping_planes_fragment>

            vec4 diffuseColor = vec4( diffuse, opacity );
            ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );
            vec3 totalEmissiveRadiance = emissive;

            #include <logdepthbuf_fragment>
            #include <map_fragment>
            #include <color_fragment>
            #include <alphamap_fragment>
            #include <alphatest_fragment>
            #include <alphahash_fragment>
            #include <roughnessmap_fragment>
            #include <metalnessmap_fragment>
            #include <normal_fragment_begin>
            #include <normal_fragment_maps>
            #include <clearcoat_normal_fragment_begin>
            #include <clearcoat_normal_fragment_maps>
            #include <emissivemap_fragment>

            // accumulation
            #include <lights_physical_fragment>
            #include <lights_fragment_begin>
            #include <lights_fragment_maps>
            #include <lights_fragment_end>

            // modulation
            #include <aomap_fragment>

            vec3 totalDiffuse = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse;
            vec3 totalSpecular = reflectedLight.directSpecular + reflectedLight.indirectSpecular;

            #include <transmission_fragment>

            vec3 outgoingLight = totalDiffuse + totalSpecular + totalEmissiveRadiance;

            #ifdef USE_SHEEN

                // Sheen energy compensation approximation calculation can be found at the end of
                // https://drive.google.com/file/d/1T0D1VSyR4AllqIJTQAraEIzjlb5h4FKH/view?usp=sharing
                float sheenEnergyComp = 1.0 - 0.157 * max3( material.sheenColor );

                outgoingLight = outgoingLight * sheenEnergyComp + sheenSpecularDirect + sheenSpecularIndirect;

            #endif

            #ifdef USE_CLEARCOAT

                float dotNVcc = saturate( dot( geometryClearcoatNormal, geometryViewDir ) );

                vec3 Fcc = F_Schlick( material.clearcoatF0, material.clearcoatF90, dotNVcc );

                outgoingLight = outgoingLight * ( 1.0 - material.clearcoat * Fcc ) + ( clearcoatSpecularDirect + clearcoatSpecularIndirect ) * material.clearcoat;

            #endif

            #include <opaque_fragment>
            #include <tonemapping_fragment>
            #include <colorspace_fragment>
            #include <fog_fragment>
            #include <premultiplied_alpha_fragment>
            #include <dithering_fragment>

        }
    </script>
javascript three.js shader
1个回答
0
投票

好吧,这可行,但看起来很多余。我认为将来会有人发现这些信息有用,所以这就是答案。您必须通过 3 种不同的方式设置地图值。

let dirtTexture = new THREE.TextureLoader().load("textures/fieldDirtTexture.jpg");
let standard = THREE.ShaderLib['standard'];
let dirtUnis = THREE.UniformsUtils.clone( standard.uniforms );

// dirt shader
let dirtMaterial = new THREE.ShaderMaterial({
    lights: true,
    uniforms: dirtUnis,
    defines: {
        PHYSICAL: true
    },
    map: dirtTexture,
    vertexShader: document.getElementById( 'dirtVS' ).textContent,
    fragmentShader: document.getElementById( 'dirtFS' ).textContent
});

dirtMaterial.uniforms.map.value = dirtTexture;
dirtMaterial.map = dirtTexture;
// dirtMaterial.uniforms.diffuse.value = new THREE.Color( 0xFFFFFF );
dirtMaterial.uniforms.roughness.value = 1.0;
dirtMaterial.uniforms.metalness.value = 0.0;
dirtMaterial.uniforms.ior.value = 1.0;
© www.soinside.com 2019 - 2024. All rights reserved.