Cesium 1.02.0 及以上版本下自定义材质报错:[Cesium WebGL] Fragment shader compile log: ERROR: 0:8: ‘texture2D‘

这篇具有很好参考价值的文章主要介绍了Cesium 1.02.0 及以上版本下自定义材质报错:[Cesium WebGL] Fragment shader compile log: ERROR: 0:8: ‘texture2D‘。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

2023年4月19日更新—搞了一个 Cesium 镜像,欢迎使用:沙盒示例 和 API

在看到 Cesium 官方更新的日志,最新版(1.103.0)支持平滑缩放,于是升级尝试一下。

结果偶然发现,之前写的墙体动态效果报错,经过调试,找到原因,Cesium 新版对 WebGL2 支持有变化,这里记录一下。

本文包括,报错原因、解决方法、在线示例三部分。


报错原因

报错原因:Cesium 自 1.102.0 开始,为了更好支持跨平台,尤其是移动端,Cesium 默认使用 WebGL2 上下文,如果想使用 WebGL1 上下文,需要在地球初始化的时候设置相应的参数。

为了在 WebGL2 上下文中工作,任何自定义材质、自定义图元和自定义着色器都需要升级到使用 GLSL 300

详见解释:

openGL之API学习(三十五)texture和texture2D

The OpenGL® Shading Language

而笔者写的自定义材质,使用的语法应该是低于 GLSL 300,因此不支持,将语法更新即可。

涉及语法:

vec4 colorImage = texture2D(image, vec2(fract(st.t - time), st.t));

官方更新说明。


Cesium 1.02.0 及以上版本下自定义材质报错:[Cesium WebGL] Fragment shader compile log: ERROR: 0:8: ‘texture2D‘,Cesium 学习教程,材质,webgl,cesium材质报错,cesium着色器报错,GLSL 材质报错

完整错误:


[Cesium WebGL] Fragment shader compile log: ERROR: 0:8: 'texture2D' : no matching overloaded function found
ERROR: 0:8: '=' : dimension mismatch
ERROR: 0:8: '=' : cannot convert from 'const mediump float' to 'highp 4-component vector of float'



[Cesium WebGL]  Fragment shader source:
#version 300 es
#ifdef GL_FRAGMENT_PRECISION_HIGH
    precision highp float;
    precision highp int;
#else
    precision mediump float;
    precision mediump int;
    #define highp mediump
#endif

#define OES_texture_float_linear

#define OES_texture_float


#line 0
layout(location = 0) out vec4 out_FragColor;



CesiumWidget.js:703 An error occurred while rendering.  Rendering has stopped.
RuntimeError: Fragment shader failed to compile.  Compile log: ERROR: 0:8: 'texture2D' : no matching overloaded function found
ERROR: 0:8: '=' : dimension mismatch
ERROR: 0:8: '=' : cannot convert from 'const mediump float' to 'highp 4-component vector of float'

解决方法

以下是两种方法解决问题,推荐第二种

1. 初始化地球时(不推荐),传入参数,使用 WebGL1 上下文

const viewer = new Viewer("cesiumContainer", {
  // 指定上下文
  contextOptions: {
    requestWebgl1: true,
  },
});

2. 修正 GLSL 代码(推荐)

错误代码(texture2D):

vec4 colorImage = texture2D(image, vec2(fract(st.t - time), st.t));

修正为:

vec4 colorImage = texture(image, vec2(fract(st.t - time), st.t));

完整代码


<!DOCTYPE html>
<html lang="en">
<head>
    <!-- Use correct character set. -->
    <meta charset="utf-8"/>
    <!-- Tell IE to use the latest, best version. -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <!-- Make the application on mobile take up the full browser screen and disable user scaling. -->
    <meta
            name="viewport"
            content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"
    />
    <title>Cesium model adjust</title>
    <script src="./Cesium.js"></script>
    <script src="./cesium_init.js"></script>
    <script src="http://www.openlayers.vip/examples/resources/jquery-3.5.1.min.js"></script>
    <style>
        @import url(./Widgets/widgets.css);

        html,
        body,
        #cesiumContainer {
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
            overflow: hidden;
        }
    </style>
    <script>
        var _hmt = _hmt || [];
        (function () {
            var hm = document.createElement("script");
            hm.src = "https://hm.baidu.com/hm.js?f80a36f14f8a73bb0f82e0fdbcee3058";
            var s = document.getElementsByTagName("script")[0];
            s.parentNode.insertBefore(hm, s);
        })();
    </script>
    <script>
        var _hmt = _hmt || [];
        (function() {
            var hm = document.createElement("script");
            hm.src = "https://hm.baidu.com/hm.js?f80a36f14f8a73bb0f82e0fdbcee3058";
            var s = document.getElementsByTagName("script")[0];
            s.parentNode.insertBefore(hm, s);
        })();
    </script></head>
<body>
<div id="cesiumContainer"></div>
<script>


    Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJhMzVlZmUzZi0xMWRmLTQxZGEtYWQyMS1iMDhmYWM3NDkyZjMiLCJpZCI6MTIzMTAsInNjb3BlcyI6WyJhc3IiLCJnYyJdLCJpYXQiOjE1NjA4MzkzNTZ9.ORdbpPmws56kqB_GOcdYo-v38ezBkYUVqjPJiZ73JvA';

    // 创建三维球
    const viewer = init();

    viewer.scene.debugShowFramesPerSecond = true;



    /**
     * @description:立体墙效果
     * @date:2022-02-12
     * @param:viewer
     * @positions: 墙体底部位置坐标
     */
    function drawWall(positionsTemp) {



        // 绘制墙体
        viewer.entities.add({
            name: "立体墙效果",
            wall: {
                positions: Cesium.Cartesian3.fromDegreesArrayHeights([
                    118.286419,31.864436,20000.0,
                    119.386419,31.864436,20000.0,
                    119.386419,32.864436,20000.0,
                    118.286419,32.864436,20000.0,
                    118.286419,31.864436,20000.0,
                ]),
                // 设置高度
                maximumHeights: new Array(5).fill(10000),
                minimumHeights: new Array(5).fill(1000),
                material: new DynamicWallMaterialProperty({
                    color: new Cesium.Color(1.0, 0.0, 0.0, 0.5),
                    duration: 1000
                }),
            }
        });
    }






    /**
     * @description:动态立体墙材质
     * @date: 2022-02-11
     */


//动态墙材质
    function DynamicWallMaterialProperty(options) {
        // 默认参数设置
        this._definitionChanged = new Cesium.Event();
        this._color = undefined;
        this._colorSubscription = undefined;
        this.color = options.color;
        this.duration = options.duration;
        this.trailImage = options.trailImage;
        this._time = (new Date()).getTime();
    }
    Object.defineProperties(DynamicWallMaterialProperty.prototype, {
        isConstant: {
            get: function() {
                return false;
            }
        },
        definitionChanged: {
            get: function() {
                return this._definitionChanged;
            }
        },
        color: Cesium.createPropertyDescriptor('color')
    });
    DynamicWallMaterialProperty.prototype.getType = function(time) {
        return 'DynamicWall';
    };
    DynamicWallMaterialProperty.prototype.getValue = function(time, result) {
        if (!Cesium.defined(result)) {
            result = {};
        }
        result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, Cesium.Color.WHITE, result.color);
        if (this.trailImage) {
            result.image = this.trailImage;
        } else {
            result.image = Cesium.Material.DynamicWallImage;
        }

        if (this.duration) {
            result.time = (((new Date()).getTime() - this._time) % this.duration) / this.duration;
        }
        viewer.scene.requestRender();
        return result;
    };
    DynamicWallMaterialProperty.prototype.equals = function(other) {
        return this === other ||
            (other instanceof DynamicWallMaterialProperty &&
                Cesium.Property.equals(this._color, other._color));
    };
    Cesium.DynamicWallMaterialProperty = DynamicWallMaterialProperty;
    Cesium.Material.DynamicWallType = 'DynamicWall';
    Cesium.Material.DynamicWallImage = "http://openlayers.vip/resources/polylineTrailLinkMaterial.png";
    Cesium.Material.DynamicWallSource = "czm_material czm_getMaterial(czm_materialInput materialInput)\n\
                                            {\n\
                                            czm_material material = czm_getDefaultMaterial(materialInput);\n\
                                            vec2 st = materialInput.st;\n\
                                            vec4 colorImage = texture(image, vec2(fract(st.t - time), st.t));\n\
                                            vec4 fragColor;\n\
                                            fragColor.rgb = color.rgb / 1.0;\n\
                                            fragColor = czm_gammaCorrect(fragColor);\n\
                                            material.alpha = colorImage.a * color.a;\n\
                                            material.diffuse = color.rgb;\n\
                                            material.emission = fragColor.rgb;\n\
                                            return material;\n\
                                            }";
    Cesium.Material._materialCache.addMaterial(Cesium.Material.DynamicWallType, {
        fabric: {
            type: Cesium.Material.DynamicWallType,
            uniforms: {
                color: new Cesium.Color(1.0, 1.0, 1.0, 1),
                image: Cesium.Material.DynamicWallImage,
                time: 0
            },
            source: Cesium.Material.DynamicWallSource
        },
        translucent: function(material) {
            return true;
        }
    });


    drawWall();

    viewer.zoomTo(viewer.entities);
</script>
</body>
</html>


在线示例

示例中展示了, 三维地图动态墙效果

Cesium 沙盒测试

三维地图动态墙效果

Cesium 1.02.0 及以上版本下自定义材质报错:[Cesium WebGL] Fragment shader compile log: ERROR: 0:8: ‘texture2D‘,Cesium 学习教程,材质,webgl,cesium材质报错,cesium着色器报错,GLSL 材质报错文章来源地址https://www.toymoban.com/news/detail-673289.html


到了这里,关于Cesium 1.02.0 及以上版本下自定义材质报错:[Cesium WebGL] Fragment shader compile log: ERROR: 0:8: ‘texture2D‘的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • WebGL,Cesium以及GeoJSON数据的简单介绍

    目录 一、WebGL 二、Cesium (1)Cesium:一个用于显示三维地球和地图的开源js库。 (2)Cesium的基本功能: (3)cesium的依赖性与技术标准 (4)Cesium与周边产品的关系 (5)Cesium viewer界面部件介绍 三、GeoJSON数据 【GeoJSON数据】 【GeoJSON 对象 】 1 几何图形 2 Feature 3 特征集合 【可

    2024年02月12日
    浏览(43)
  • SuperMap iClient3D for WebGL/Cesium端性能优化

        目录 一、请求优化 1.1 多子域 1.1.1 scene.open()打开场景 1.1.2 加载地形 1.1.3 加载影像 1.1.4 加载S3M 1.1.5 加载MVT 1.2 批量请求 1.2.1 地形 1.2.2 影像 二、内存优化 2.1 根节点驻留内存 2.2 自动释放缓存 2.3 内存管理 三、图层优化 3.1 LOD 3.2 空间索引 3.3 控制图层显示范围 3.4 控制图层

    2024年01月25日
    浏览(35)
  • DEJA_VU3D - Cesium功能集 之 078-对象材质:图片轨迹线

    编写这个专栏主要目的是对工作之中基于Cesium实现过的功能进行整合,有自己琢磨实现的,也有参考其他大神后整理实现的,初步算了算现在有实现120个左右的功能,后续也会不断的追加,所以暂时打算一周2-3更的样子来更新本专栏(每篇博文都会奉上完整demo的源代码,尽可

    2024年02月02日
    浏览(46)
  • DEJA_VU3D - Cesium功能集 之 079-对象材质:动态扩散圆

    编写这个专栏主要目的是对工作之中基于Cesium实现过的功能进行整合,有自己琢磨实现的,也有参考其他大神后整理实现的,初步算了算现在有实现120个左右的功能,后续也会不断的追加,所以暂时打算一周2-3更的样子来更新本专栏(每篇博文都会奉上完整demo的源代码,尽可

    2024年02月02日
    浏览(26)
  • iClient3D for Cesium&WebGL入门之使用vscode以服务方式运行调试

    作者:超图研究院技术支持中心-于丁 iClient3D for CesiumWebGL入门之使用vscode以服务方式运行调试 相信大家第一次使用SuperMap iClient3D for Cesium或SuperMap iClient3D for WebGL的时候,都遇到过和我一样的事情: 在文件夹中直接打开html的示例代码文件于浏览器中; 或者使用vscode安装open

    2024年02月04日
    浏览(37)
  • UE4 材质学习 (02-利用UV来调整纹理)

    步骤: 1.在内容浏览器中新建一个材质 命名为uvTest    2.双击打开uvTest 随便将一个纹理(一种.tga格式的图片)拖入材质面板中:  3.创建如下节点并连接  4.选中 TextureCoordinate 节点,在细节面板中可以通过U平铺和V平铺两个参数来调整纹理的疏密。 比如,我将 U平铺和V平铺

    2024年02月12日
    浏览(31)
  • Android10以上版本调用相机拍照

    1、拍照功能 界面 逻辑处理 存放路径 2、从相册中选择图片的功能 在上面基本代码的基础之上添加

    2024年04月10日
    浏览(33)
  • Android Studio 4以上版本设置成中文

    我们安装好Android Studio后,默认是英文界面的,这可能对开发者来说并不是很友好,所以我们给它设置成中文。 本以为直接在设置里面有切换成中文的功能,结果找了一轮没有,最后网上查看相关方法,发现要到JET BRAINS的插件官网里面下载相关插件来使用!!!插件官网地址

    2023年04月08日
    浏览(28)
  • Openlayers使用WebGL加载多种自定义图标矢量点

    使用OpenLayers的 WebGLPointsLayer 加载海量矢量点(100W),并使用 多种自定义图标样式 。 你需要满足以下环境要求: 支持WebGL 的浏览器(Chrome、Firefox、Safari和Edge等)。 计算机具有一定的图形处理能力( 显卡 )。 一定的计算机 内存和网络带宽 。 一点耐心 1.创建一个 包含所有

    2024年04月11日
    浏览(30)
  • Unity WebGL发布页面报错

    1、报错内容 Unable to parse Build/test.framework.js.gz! This can happen if build compression was enabled but web server hosting the content was misconfigured to not serve the file with HTTP Response Header \\\"Content-Encoding: gzip\\\" present. Check browser Console and Devtools Network tab to debug. 2、报错页面 解决方案,在Unity的WebGL Player S

    2024年02月16日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包