Give Both Stages a Portable Header
Start with a custom Surface3D shader. To opt a stage in, put // lenga:portable-glsl on its first line in place of #version. Write the marker exactly as shown, with no leading spaces or blank lines. Opt in each stage separately.
Put this in the file assigned to Vertex Source. Lenga provides the mesh's vertexPosition and the mvp matrix used to position it on screen.
// lenga:portable-glsl
in vec3 vertexPosition;
uniform mat4 mvp;
void main() {
gl_Position = mvp * vec4(vertexPosition, 1.0);
}
Put this in the file assigned to Fragment Source. Use a regular main() and one out vec4 so Lenga can apply the material's alpha and color handling.
// lenga:portable-glsl
out vec4 finalColor;
void main() {
finalColor = vec4(1.0, 0.5, 0.25, 1.0);
}
Assign the shader to a material on a cube. The cube should render in a solid orange color. On desktop Lenga supplies #version 330. In a Web build it supplies #version 300 es and default precision declarations for floats and integers. Use in and out for the stage interfaces, and give any varyings you add the same names and types in both files.
Keep an Explicit Version When Needed
A stage without the marker keeps its authored #version. Use that form when a stage needs GLSL features or extensions specific to one target. Both stages must still compile and link on the target that runs them. The portable header does not translate shader code or make desktop-only features available in WebGL 2.
If the files must also load in an older player, keep the explicit version for that player on line one and place the marker immediately below it. Lenga 0.10.1 and later replace that header with the version for the current target. Older players ignore the comment and compile with the explicit version. For example, a fragment stage that needs to load in an older Web player can begin with this header.
#version 300 es
// lenga:portable-glsl
#ifdef GL_ES
precision highp float;
precision highp int;
#endif
Choose the explicit version for the older player you need to support and make sure the shader compiles for that target.
Check Both Builds
- Select the Shader asset and check the Inspector for asset and compile errors. Play the scene in the editor to confirm the material appears on the mesh.
- Export the scene with a 0.10.1 or later Web player. Preflight checks that the shader files are available to export. Their source stays unchanged in the ZIP.
- Play the exported scene in a desktop browser with WebGL 2 and check the developer console for shader errors. A version error can mean the marker is misplaced, or that an unmarked stage requests a version the browser does not accept. A link error can mean the outputs of the vertex stage and inputs of the fragment stage do not match.
See Web export limitations and Fix Web export problems if the scene behaves differently in the browser.