透明阴影在three.js

时间:2016-11-24 09:57:19

标签: javascript three.js shadow

我想在页面上渲染带阴影的对象。但阴影是深黑色的渲染,但我希望阴影背后的页面是可见的,我怎样才能在阴影中添加alpha以查看背​​后的蓝色平面?

var deg_to_rad_factor = Math.PI / 180.;
$(window).load(function() {
  console.log("3d");

  var cont = $("#container");
  var camera = new THREE.PerspectiveCamera( 70, cont.width() / cont.height(), 1, 1000 );
  camera.position.z = 50;
  var scene = new THREE.Scene();

  // ground
  geometry = new THREE.BoxBufferGeometry( 60, 60, 0.01 );
  material = new THREE.ShadowMaterial( { opacity : 0.5 } );
  ground = new THREE.Mesh( geometry, material );
  ground.position.y = 0.0;
  ground.receiveShadow = true;
  scene.add( ground );

  // box
  geometry = new THREE.BoxBufferGeometry( 10, 10, 10 );
  material = new THREE.MeshLambertMaterial( { color: 'rgb(255,0,0)' } );
  mesh = new THREE.Mesh( geometry, material );
  mesh.position.x = 20;
  mesh.position.z = mesh.scale.z/2;
  mesh.rotation.z = deg_to_rad_factor * 45;
  mesh.castShadow = true;
  scene.add( mesh );

  // sun
  var sunLight = new THREE.DirectionalLight( 'rgb(255,255,255)', 1 );
  sunLight.position.set( 0, 2 , 1 );
  sunLight.castShadow = true;

  var lit = 200;
  sunLight.shadow.camera.right = lit;
  sunLight.shadow.camera.left = -lit;
  sunLight.shadow.camera.top = lit;
  sunLight.shadow.camera.bottom = -lit;
  sunLight.shadow.mapSize.width = 1024*4;
  sunLight.shadow.mapSize.height = 1024*4;

  scene.add( sunLight );

  var light = new THREE.AmbientLight( 0x0f0f0f ); // soft white light
  scene.add( light );

  renderer = new THREE.WebGLRenderer({ alpha: true });
  renderer.setPixelRatio( window.devicePixelRatio );
  renderer.setSize(cont.width(),cont.height());
  renderer.shadowMap.enabled = true;


  cont.append(renderer.domElement);

  renderer.render( scene, camera );
});
div {
  width: 300px;
  height: 300px;
  background-color: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r79/three.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="container">
</div>

修改

如果我改变THREE.js的来源

,我发现我可以控制不透明度
function ShadowMaterial() {

ShaderMaterial.call( this, {
    uniforms: UniformsUtils.merge( [
        UniformsLib[ "lights" ],
        {
            opacity: { value: 0.5 }
        }
    ] ),
    vertexShader: ShaderChunk[ 'shadow_vert' ],
    fragmentShader: ShaderChunk[ 'shadow_frag' ]
} );

我如何从外面控制这个值?

1 个答案:

答案 0 :(得分:2)

我明白了

我已经设置了正确的参数material = new THREE.ShadowMaterial( { opacity : 0.1 } );

但似乎构造函数丢弃它

        material = new THREE.ShadowMaterial( { opacity : 0.1 } );
        material.opacity = 0.5;

似乎有用

&#13;
&#13;
var deg_to_rad_factor = Math.PI / 180.;
$(window).load(function() {
  console.log("3d");

  var cont = $("#container");
  var camera = new THREE.PerspectiveCamera( 70, cont.width() / cont.height(), 1, 1000 );
  camera.position.z = 50;
  var scene = new THREE.Scene();

  // ground
  geometry = new THREE.BoxBufferGeometry( 60, 60, 0.01 );
  material = new THREE.ShadowMaterial( { opacity : 0.5 } );
  material.opacity = 0.5;
  ground = new THREE.Mesh( geometry, material );
  ground.position.y = 0.0;
  ground.receiveShadow = true;
  scene.add( ground );

  // box
  geometry = new THREE.BoxBufferGeometry( 10, 10, 10 );
  material = new THREE.MeshLambertMaterial( { color: 'rgb(255,0,0)' } );
  mesh = new THREE.Mesh( geometry, material );
  mesh.position.x = 20;
  mesh.position.z = mesh.scale.z/2;
  mesh.rotation.z = deg_to_rad_factor * 45;
  mesh.castShadow = true;
  scene.add( mesh );

  // sun
  var sunLight = new THREE.DirectionalLight( 'rgb(255,255,255)', 1 );
  sunLight.position.set( 0, 2 , 1 );
  sunLight.castShadow = true;

  var lit = 200;
  sunLight.shadow.camera.right = lit;
  sunLight.shadow.camera.left = -lit;
  sunLight.shadow.camera.top = lit;
  sunLight.shadow.camera.bottom = -lit;
  sunLight.shadow.mapSize.width = 1024*4;
  sunLight.shadow.mapSize.height = 1024*4;

  scene.add( sunLight );

  var light = new THREE.AmbientLight( 0x0f0f0f ); // soft white light
  scene.add( light );

  renderer = new THREE.WebGLRenderer({ alpha: true });
  renderer.setPixelRatio( window.devicePixelRatio );
  renderer.setSize(cont.width(),cont.height());
  renderer.shadowMap.enabled = true;


  cont.append(renderer.domElement);

  renderer.render( scene, camera );
});
&#13;
div {
  width: 300px;
  height: 300px;
  background-color: blue;
}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r79/three.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="container">
</div>
&#13;
&#13;
&#13;