ivec2 getOffset(int frame) {
int i = frame % 4;
if (i == 0) return ivec2(0, 0);
else if (i == 1) return ivec2(1, 1);
else if (i == 2) return ivec2(0, 1);
return ivec2(1, 0);
}
vec4 createParticle(float id) {
float temperature = ambientTemperature;
if (id == SMOKE) temperature = smokeTemperature;
else if (id == LAVA) temperature = lavaTemperature;
else if (id == ICE) temperature = iceTemperature;
return vec4(temperature, 0.0, 0.0, id);
}
void main() {
// Initial frame setup
if (frame == 0) {
float r = hash12(gl_FragCoord.xy);
float id = AIR;
if (r < 0.15) id = SAND;
else if (r < 0.25) id = SMOKE;
fragColor = createParticle(id);
return;
}
// Handle mouse input
if (mouse.x > 0.0) {
float d = length(gl_FragCoord.xy - mouse.xy);
if (d < brushRadius) {
fragColor = createParticle(float(materialType));
return;
}
}
// Main simulation logic
ivec2 offset = getOffset(frame);
ivec2 fc = ivec2(gl_FragCoord.xy) + offset;
ivec2 p = (fc / 2) * 2 - offset;
ivec2 xy = fc % 2;
int i = xy.x + xy.y * 2;
vec4 t00 = getData(p);
vec4 t10 = getData(p + ivec2(1, 0));
vec4 t01 = getData(p + ivec2(0, 1));
vec4 t11 = getData(p + ivec2(1, 1));
// Temperature calculations
vec4 neighbors[4] = vec4[4](t00, t10, t01, t11);
for (int idx = 0; idx < 4; idx++) {
float tempDiff = neighbors[idx].r - t00.r;
neighbors[idx].r -= tempDiff * temperatureTransferRate;
t00.r += tempDiff * temperatureTransferRate;
}
// Material-specific temperature changes
if (t00.a == SMOKE) t00.r -= smokeCoolingRate;
else if (t00.a == LAVA) t00.r -= lavaCoolingRate;
// Phase transitions
if (t00.a == STONE && t00.r > stoneMeltingPoint) t00 = createParticle(LAVA);
else if (t00.a == SAND && t00.r > sandMeltingPoint) t00 = createParticle(STONE);
else if (t00.a == WATER && t00.r > waterBoilingPoint) t00 = createParticle(SMOKE);
else if (t00.a == WATER && t00.r <= waterFreezingPoint) t00 = createParticle(ICE);
else if (t00.a == LAVA && t00.r <= ambientTemperature + 50.0) t00 = createParticle(STONE);
else if (t00.a == ICE && t00.r > waterFreezingPoint) t00 = createParticle(WATER);
else if (t00.a == SMOKE && t00.r <= ambientTemperature + 10.0) t00 = createParticle(AIR);
// Temperature limits
t00.r = clamp(t00.r, -100.0, 1500.0);
fragColor = i == 0 ? t00 :
i == 1 ? t10 :
i == 2 ? t01 : t11;
The errors you're encountering in your shader code are due to two main issues:
The time variable is undeclared in your renderShaderText shader.
The palette function is not included in the renderShaderText shader.
Let's address each issue step by step.
1. time Variable Undeclared
In your renderShaderText shader, you're using the time variable in the getColor function:
else if (materialId == GLITTER) {
color = palette(fract(time * 0.1));
}
However, time is not declared within this shader. To fix this, you need to declare time as a uniform variable:
uniform float time;
This tells the shader that time will be provided externally (typically from your JavaScript or application code).
2. palette Function Not Included
You're using the palette function in your getColor function, but the function is not defined within your renderShaderText shader. Even though you've defined palette in your commonShader, it's not automatically included in renderShaderText.
To fix this, you need to include commonShader in your renderShaderText shader code.
Putting It All Together
Here's how you can modify your renderShaderText shader to fix both issues:
const renderShaderText = `#version 300 es
precision highp float;
uniform vec2 texResolution;
uniform float texScale;
uniform vec2 resolution;
uniform sampler2D tex;
uniform sampler2D shadowTexR;
uniform sampler2D shadowTexG;
uniform sampler2D shadowTexB;
uniform float scale;
uniform float time; // Added this line
` +
constantsShader +
commonShader + // Added this line
`
out vec4 fragColor;
vec2 getCoordsAA(vec2 uv) {
float w = 1.0;
vec2 fl = floor(uv + 0.5);
vec2 fr = fract(uv + 0.5);
vec2 aa = fwidth(uv) * w * 0.5;
fr = smoothstep(0.5 - aa, 0.5 + aa, fr);
return fl + fr - 0.5;
}
vec3 linearTosRGB(vec3 col)
{
return mix(1.055 * pow(col, vec3(1.0 / 2.4)) - 0.055, col * 12.92, lessThan(col, vec3(0.0031308)));
}
vec3 getColor(float materialId, float temperature) {
vec3 color = vec3(0.0);
if (materialId == AIR) {
color = bgColor;
}
else if (materialId == SMOKE) {
color = vec3(0.2, 0.2, 0.2);
}
else if (materialId == WATER) {
color = vec3(0.0, 0.3, 0.8);
}
else if (materialId == LAVA) {
float t = clamp((temperature - 500.0) / 500.0, 0.0, 1.0);
color = mix(vec3(1.0, 0.5, 0.0), vec3(1.0, 1.0, 0.0), t);
}
else if (materialId == SAND) {
color = vec3(0.86, 0.62, 0.27);
}
else if (materialId == GLITTER) {
color = palette(fract(time * 0.1)); // This now works because `palette` and `time` are defined
}
else if (materialId == STONE) {
color = vec3(0.5, 0.5, 0.5);
}
else if (materialId == WALL) {
color = bgColor * 0.5;
}
else if (materialId == ICE) {
color = vec3(0.8, 0.9, 1.0);
}
// Apply temperature effect
if (temperature > ambientTemperature) {
float t = clamp((temperature - ambientTemperature) / 1000.0, 0.0, 1.0);
color = mix(color, vec3(1.0, 0.5, 0.0), t);
}
else if (temperature < ambientTemperature) {
float t = clamp((ambientTemperature - temperature) / 100.0, 0.0, 1.0);
color = mix(color, vec3(0.5, 0.6, 1.0), t);
}
return color;
}
void main() {
// ... rest of your shader code ...
}
`;
Explanation:
Added uniform float time;
This declares time as a uniform variable, making it available within the shader.
Included commonShader
By adding commonShader to the concatenation, you include all the common functions, including palette, into your renderShaderText shader.
Additional Notes:
Ensure All Necessary Uniforms Are Declared:
When using variables like time in shaders, they must be declared as either uniform, varying, or defined within the shader.
Uniforms are used for variables that are constant across a draw call but might change between draw calls.
Include All Required Functions:
Functions used within a shader must be defined within that shader or included via concatenation or preprocessor directives.
Since GLSL doesn't have an include mechanism, concatenating shader strings is a common method to include shared code.
Variable Naming Conflicts:
Be careful with variable names that might conflict with built-in GLSL variables or reserved words.
In your case, time is acceptable, but always check for conflicts.
Conclusion
By declaring time as a uniform and including the palette function via commonShader, your shader should now compile without errors. Make sure that in your application code (e.g., JavaScript), you set the value of the time uniform every frame to animate the effect properly.