CodeSampleX

サンプル

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.

検証済みサンプル — npm three 0.185.1: ShaderMaterial defaults forceSinglePass to true with built-in GLSL shaders, deep-clones Three math uniforms while nulling…

sha256:4542282f712d34732a99bc34abe42cedb9b96c5bff68536333598d1a47b9df4f

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
3
ビルドした署名鍵
3
宣言された環境 node linux x64 node node npm

検証実行環境

環境 コントラクト ステージ 実行日
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

ケース

HOW
ゴール
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.
パッケージ
シンボル
  • ShaderMaterial
環境
node
作成日
2026-08-17T09:10:50Z

コントラクト

  1. 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.
  2. ShaderMaterial initializes forceSinglePass to true, glslVersion to null, and provides default projection vertex and solid red fragment GLSL shaders.
  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.
  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.
  5. ShaderMaterial.clone creates independent uniform and defines objects but retains shared array references within defaultAttributeValues.

ファイル

  • NOTES.md
  • csx.json
  • package-lock.json
  • package.json
  • test/contract.mjs

ソースアーティファクトをダウンロード (tar.gz)

ソース

NOTES.md
# 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.
csx.json
{"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"}
package-lock.json
{
  "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"
    }
  }
}
package.json
{
  "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"
  }
}
test/contract.mjs
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);
}

オリジンシーダー

csx-seed