Exemple
three 0.185.1: ShaderMaterial defaults forceSinglePass to true with built-in GLSL shaders, deep-clones Three math uniforms while nulling render-target textures on clone, and ignores index0AttributeName when passed via constructor parameters.
Échantillon vérifié pour npm three 0.185.1: ShaderMaterial defaults forceSinglePass to true with built-in GLSL shaders, deep-clones Three math uniforms while…
sha256:4542282f712d34732a99bc34abe42cedb9b96c5bff68536333598d1a47b9df4f
Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré.
Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes.
MIT-0
Preuves d'exécution
L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.
- Base de preuve
- Contrat signé réussi
- Reçus de vérification
- 3
- Clés de signature qui l’ont compilé
- 3
Environnement déclaré
node linux x64 node node npm
Environnements des exécutions de vérification
| Environnement | Contrat | Étapes | Exécution |
|---|---|---|---|
| node 22 · linux alpine/x64 · docker ed25519:d91480838ac982c9 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · node-typescript@1 |
2026-08-17 |
| node 22 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · node-typescript@1 |
2026-08-18 |
| node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5… |
2026-09-07 |
Cas
HOW- Objectif
- ShaderMaterial defaults forceSinglePass to true with built-in GLSL shaders, deep-clones Three math uniforms while nulling render-target textures on clone, and ignores index0AttributeName when passed via constructor parameters.
- Paquets
- Symboles
-
- ShaderMaterial
- Environnement
- node
- Créé
- 2026-08-17T09:10:50Z
Contrat
- ShaderMaterial deep-clones Three math uniforms so mutations to cloned Vector and Color values do not mutate the original material, while resetting render-target textures to null.
- ShaderMaterial initializes forceSinglePass to true, glslVersion to null, and provides default projection vertex and solid red fragment GLSL shaders.
- ShaderMaterial ignores index0AttributeName when provided in the constructor options object due to setValues treating undefined initial properties as invalid, requiring direct assignment on the instance.
- ShaderMaterial.toJSON serializes math uniforms into typed entries ('c', 'v2', 'v3', 'v4', 'm3', 'm4') that fromJSON restores to their respective Three math class instances.
- ShaderMaterial.clone creates independent uniform and defines objects but retains shared array references within defaultAttributeValues.
Fichiers
- NOTES.md
- csx.json
- package-lock.json
- package.json
- test/contract.mjs
Code source
# three: ShaderMaterial Behavior and Cloning Contract
## Search and Prior Art
A network query for `three` returned three existing samples:
- `sha256:37f8aea1060a00ee39671b0525ce4ffb4acbd57858a862d0b15c3874e09b1eca` (`MeshSurfaceSampler.sample` behavior)
- `sha256:8454974a0cab4d93b0cf9ad4f2e64b2a4ada01e8766dc0f2ee4f94ae26108f9b` (`InstancedMesh` type and instance matrix initialization)
- `sha256:961598ac78d3b67c9b8b2e7ca83648daa11ea9e976e9ef72e5fd7dc4fdc5a4a4` (`CanvasTexture` versioning and update setter behavior)
No prior sample addresses `ShaderMaterial`.
## Pinned Release
- `three@0.185.1`
## Observed Behavior
### 1. Uniform Cloning and Render Target Textures
When calling `material.clone()`, `ShaderMaterial.copy()` invokes `cloneUniforms()`. This deeply clones Three math uniform values (`Color`, `Vector2`, `Vector3`, `Vector4`, `Matrix3`, `Matrix4`, `Quaternion`) so that mutating properties on the clone does not affect the original instance. However, if a uniform references a render-target texture (`isRenderTargetTexture: true`), `cloneUniforms()` logs a warning and assigns `null` to that uniform value on the cloned material.
### 2. Constructor Parameter Filtering for index0AttributeName
`ShaderMaterial` declares `this.index0AttributeName = undefined;` in its constructor before calling `setValues(parameters)`. Because `Material.prototype.setValues` tests `if (this[key] === undefined)` to reject unknown properties, passing `index0AttributeName` in the constructor configuration object causes `setValues` to log a warning and skip setting the property, leaving it `undefined`. The property must be set via direct assignment on the created material instance.
### 3. Material Defaults
Unlike base `Material` which initializes `forceSinglePass` to `false`, `ShaderMaterial` initializes `forceSinglePass` to `true`. It defaults `glslVersion` to `null` and assigns built-in GLSL shaders containing a standard MVP projection in the vertex shader and solid red output (`vec4(1.0, 0.0, 0.0, 1.0)`) in the fragment shader.
### 4. Default Attribute Values Sharing on Clone
`ShaderMaterial.copy()` performs a shallow `Object.assign({}, source.defaultAttributeValues)` on `defaultAttributeValues`. While the outer dictionary object is cloned, the nested array references (`color`, `uv`, `uv1`) remain shared across material clones.
{"case":{"believed":"cloning a ShaderMaterial shallow-copies uniforms and passing index0AttributeName to the constructor assigns it to the material instance","caseId":"case:sha256:c299e4ccc3de92338cbcbf0684d575dc1044d5d1e2a41e2b3ac414021911dcc2","contract":["ShaderMaterial deep-clones Three math uniforms so mutations to cloned Vector and Color values do not mutate the original material, while resetting render-target textures to null.","ShaderMaterial initializes forceSinglePass to true, glslVersion to null, and provides default projection vertex and solid red fragment GLSL shaders.","ShaderMaterial ignores index0AttributeName when provided in the constructor options object due to setValues treating undefined initial properties as invalid, requiring direct assignment on the instance.","ShaderMaterial.toJSON serializes math uniforms into typed entries ('c', 'v2', 'v3', 'v4', 'm3', 'm4') that fromJSON restores to their respective Three math class instances.","ShaderMaterial.clone creates independent uniform and defines objects but retains shared array references within defaultAttributeValues."],"goal":"ShaderMaterial defaults forceSinglePass to true with built-in GLSL shaders, deep-clones Three math uniforms while nulling render-target textures on clone, and ignores index0AttributeName when passed via constructor parameters.","kind":"HOW","packages":["pkg:npm/three@0.185.1"],"schemaVersion":1,"symbols":["ShaderMaterial"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","ecosystem":"npm","executionContext":"node","language":"node","os":"linux","packageManager":"npm","runtime":"node","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/three@0.185.1"],"schemaVersion":1,"symbols":["ShaderMaterial"],"verifierAdapter":"node-typescript@1"}
{
"name": "npm-three-wanted-0-185-1-shadermaterial",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "npm-three-wanted-0-185-1-shadermaterial",
"version": "1.0.0",
"dependencies": {
"three": "0.185.1"
}
},
"node_modules/three": {
"version": "0.185.1",
"resolved": "https://registry.npmjs.org/three/-/three-0.185.1.tgz",
"integrity": "sha512-5aojFCXKwnjBRZvUnt3WFfEcvUJgkN5LlijRFN95hMy8WVkG4I0QNcJE+OuWvuJ0bOdStrbfXn0pkd6/QyiAlg==",
"license": "MIT"
}
}
}
{
"name": "npm-three-wanted-0-185-1-shadermaterial",
"version": "1.0.0",
"type": "module",
"scripts": {
"test": "node test/contract.mjs"
},
"dependencies": {
"three": "0.185.1"
}
}
import assert from 'node:assert/strict';
import * as THREE from 'three';
// 1. ShaderMaterial deep-clones Three math uniforms while nulling render-target textures on clone.
{
const rt = new THREE.WebGLRenderTarget(64, 64);
const original = new THREE.ShaderMaterial({
uniforms: {
uColor: { value: new THREE.Color(1, 0, 0) },
uOffset: { value: new THREE.Vector2(10, 20) },
uMatrix: { value: new THREE.Matrix4().identity() },
uRenderTarget: { value: rt.texture },
uPrimitiveArray: { value: [1, 2, 3] },
uVectorArray: { value: [new THREE.Vector3(1, 2, 3), new THREE.Vector3(4, 5, 6)] }
}
});
const cloned = original.clone();
// Uniform dictionary itself is a new object
assert.notStrictEqual(cloned.uniforms, original.uniforms);
// Math objects are cloned into independent instances
assert.notStrictEqual(cloned.uniforms.uColor.value, original.uniforms.uColor.value);
assert.strictEqual(cloned.uniforms.uColor.value.getHex(), 0xff0000);
cloned.uniforms.uColor.value.setHex(0x00ff00);
assert.strictEqual(original.uniforms.uColor.value.getHex(), 0xff0000, 'Original uniform Color must not mutate when clone changes');
assert.notStrictEqual(cloned.uniforms.uOffset.value, original.uniforms.uOffset.value);
cloned.uniforms.uOffset.value.x = 999;
assert.strictEqual(original.uniforms.uOffset.value.x, 10, 'Original uniform Vector2 must not mutate when clone changes');
// Array of Three objects has cloned element instances
assert.notStrictEqual(cloned.uniforms.uVectorArray.value, original.uniforms.uVectorArray.value);
assert.notStrictEqual(cloned.uniforms.uVectorArray.value[0], original.uniforms.uVectorArray.value[0]);
assert.strictEqual(cloned.uniforms.uVectorArray.value[0].x, 1);
// Array of primitives is sliced
assert.notStrictEqual(cloned.uniforms.uPrimitiveArray.value, original.uniforms.uPrimitiveArray.value);
assert.deepStrictEqual(cloned.uniforms.uPrimitiveArray.value, [1, 2, 3]);
// Render target textures cannot be cloned and are reset to null
assert.strictEqual(original.uniforms.uRenderTarget.value, rt.texture);
assert.strictEqual(cloned.uniforms.uRenderTarget.value, null, 'Render-target texture uniform must be nulled on clone');
}
// 2. ShaderMaterial initializes forceSinglePass to true, glslVersion to null, and provides default projection vertex and solid red fragment GLSL shaders.
{
const mat = new THREE.ShaderMaterial();
assert.strictEqual(mat.type, 'ShaderMaterial');
assert.strictEqual(mat.isShaderMaterial, true);
assert.strictEqual(mat.isMaterial, true);
assert.strictEqual(mat.forceSinglePass, true, 'ShaderMaterial defaults forceSinglePass to true (unlike base Material which defaults to false)');
assert.strictEqual(mat.glslVersion, null, 'glslVersion defaults to null');
assert.strictEqual(mat.wireframe, false);
assert.strictEqual(mat.fog, false);
assert.strictEqual(mat.lights, false);
assert.strictEqual(mat.clipping, false);
assert.strictEqual(mat.uniformsNeedUpdate, false);
// Default shaders
assert.match(mat.vertexShader, /projectionMatrix \* modelViewMatrix \* vec4\( position, 1\.0 \)/);
assert.match(mat.fragmentShader, /gl_FragColor = vec4\( 1\.0, 0\.0, 0\.0, 1\.0 \)/);
}
// 3. ShaderMaterial ignores index0AttributeName when provided in the constructor options object due to setValues treating undefined initial properties as invalid, requiring direct assignment on the instance.
{
// Constructor options parameter
const matFromParam = new THREE.ShaderMaterial({ index0AttributeName: 'customAttrib' });
assert.strictEqual(matFromParam.index0AttributeName, undefined, 'index0AttributeName is dropped by constructor setValues');
// Direct assignment
matFromParam.index0AttributeName = 'customAttrib';
assert.strictEqual(matFromParam.index0AttributeName, 'customAttrib', 'index0AttributeName can be set via direct property assignment');
// Copying preserves index0AttributeName
const copied = new THREE.ShaderMaterial().copy(matFromParam);
assert.strictEqual(copied.index0AttributeName, 'customAttrib');
}
// 4. ShaderMaterial.toJSON serializes math uniforms into typed entries ('c', 'v2', 'v3', 'v4', 'm3', 'm4') that fromJSON restores to their respective Three math class instances.
{
const mat = new THREE.ShaderMaterial({
uniforms: {
color: { value: new THREE.Color(0x123456) },
v2: { value: new THREE.Vector2(1, 2) },
v3: { value: new THREE.Vector3(3, 4, 5) },
v4: { value: new THREE.Vector4(6, 7, 8, 9) },
m3: { value: new THREE.Matrix3().identity() },
m4: { value: new THREE.Matrix4().identity() },
scalar: { value: 3.14 }
},
defines: { FEATURE_A: 1 },
glslVersion: THREE.GLSL3,
lights: true,
clipping: true
});
const json = JSON.parse(JSON.stringify(mat.toJSON()));
// Uniform types in serialized JSON
assert.strictEqual(json.uniforms.color.type, 'c');
assert.strictEqual(json.uniforms.color.value, 0x123456);
assert.strictEqual(json.uniforms.v2.type, 'v2');
assert.deepStrictEqual(json.uniforms.v2.value, [1, 2]);
assert.strictEqual(json.uniforms.v3.type, 'v3');
assert.deepStrictEqual(json.uniforms.v3.value, [3, 4, 5]);
assert.strictEqual(json.uniforms.v4.type, 'v4');
assert.deepStrictEqual(json.uniforms.v4.value, [6, 7, 8, 9]);
assert.strictEqual(json.uniforms.scalar.value, 3.14);
assert.strictEqual(json.uniforms.scalar.type, undefined);
// Restore via fromJSON
const restored = new THREE.ShaderMaterial().fromJSON(json, {});
assert.strictEqual(restored.uniforms.color.value.isColor, true);
assert.strictEqual(restored.uniforms.color.value.getHex(), 0x123456);
assert.strictEqual(restored.uniforms.v2.value.isVector2, true);
assert.strictEqual(restored.uniforms.v2.value.x, 1);
assert.strictEqual(restored.uniforms.v2.value.y, 2);
assert.strictEqual(restored.uniforms.v3.value.isVector3, true);
assert.strictEqual(restored.uniforms.v4.value.isVector4, true);
assert.strictEqual(restored.uniforms.scalar.value, 3.14);
assert.strictEqual(restored.glslVersion, '300 es');
assert.strictEqual(restored.lights, true);
assert.strictEqual(restored.clipping, true);
assert.deepStrictEqual(restored.defines, { FEATURE_A: 1 });
}
// 5. ShaderMaterial.clone creates independent uniform and defines objects but retains shared array references within defaultAttributeValues.
{
const mat = new THREE.ShaderMaterial({
defines: { BASE: 10 }
});
const cloned = mat.clone();
// Defines object is cloned
assert.notStrictEqual(cloned.defines, mat.defines);
cloned.defines.BASE = 20;
assert.strictEqual(mat.defines.BASE, 10);
// defaultAttributeValues object is a new shallow wrapper, but its attribute value arrays are shared
assert.notStrictEqual(cloned.defaultAttributeValues, mat.defaultAttributeValues);
assert.strictEqual(cloned.defaultAttributeValues.color, mat.defaultAttributeValues.color);
assert.strictEqual(cloned.defaultAttributeValues.uv, mat.defaultAttributeValues.uv);
}