OSDN Git Service

three.jsをThirdPartyに追加
[webglgame/webgl_framework.git] / webglFramework / Thirdparty / three.js-master / src / renderers / webgl / WebGLBackground.js
1 /**
2  * @author mrdoob / http://mrdoob.com/
3  */
4
5 import { BackSide } from '../../constants';
6 import { OrthographicCamera } from '../../cameras/OrthographicCamera';
7 import { PerspectiveCamera } from '../../cameras/PerspectiveCamera';
8 import { BoxBufferGeometry } from '../../geometries/BoxGeometry';
9 import { PlaneBufferGeometry } from '../../geometries/PlaneGeometry';
10 import { MeshBasicMaterial } from '../../materials/MeshBasicMaterial';
11 import { ShaderMaterial } from '../../materials/ShaderMaterial';
12 import { Color } from '../../math/Color';
13 import { Mesh } from '../../objects/Mesh';
14 import { ShaderLib } from '../shaders/ShaderLib';
15
16 function WebGLBackground( renderer, state, geometries, premultipliedAlpha ) {
17
18         var clearColor = new Color( 0x000000 );
19         var clearAlpha = 0;
20
21         var planeCamera, planeMesh;
22         var boxMesh;
23
24         function render( renderList, scene, camera, forceClear ) {
25
26                 var background = scene.background;
27
28                 if ( background === null ) {
29
30                         setClear( clearColor, clearAlpha );
31
32                 } else if ( background && background.isColor ) {
33
34                         setClear( background, 1 );
35                         forceClear = true;
36
37                 }
38
39                 if ( renderer.autoClear || forceClear ) {
40
41                         renderer.clear( renderer.autoClearColor, renderer.autoClearDepth, renderer.autoClearStencil );
42
43                 }
44
45                 if ( background && background.isCubeTexture ) {
46
47                         if ( boxMesh === undefined ) {
48
49                                 boxMesh = new Mesh(
50                                         new BoxBufferGeometry( 1, 1, 1 ),
51                                         new ShaderMaterial( {
52                                                 uniforms: ShaderLib.cube.uniforms,
53                                                 vertexShader: ShaderLib.cube.vertexShader,
54                                                 fragmentShader: ShaderLib.cube.fragmentShader,
55                                                 side: BackSide,
56                                                 depthTest: true,
57                                                 depthWrite: false,
58                                                 polygonOffset: true,
59                                                 fog: false
60                                         } )
61                                 );
62
63                                 boxMesh.geometry.removeAttribute( 'normal' );
64                                 boxMesh.geometry.removeAttribute( 'uv' );
65
66                                 boxMesh.onBeforeRender = function ( renderer, scene, camera ) {
67
68                                         var scale = camera.far;
69
70                                         this.matrixWorld.makeScale( scale, scale, scale );
71                                         this.matrixWorld.copyPosition( camera.matrixWorld );
72
73                                         this.material.polygonOffsetUnits = scale * 10;
74
75                                 };
76
77                                 geometries.update( boxMesh.geometry );
78
79                         }
80
81                         boxMesh.material.uniforms.tCube.value = background;
82
83                         renderList.push( boxMesh, boxMesh.geometry, boxMesh.material, 0, null );
84
85                 } else if ( background && background.isTexture ) {
86
87                         if ( planeCamera === undefined ) {
88
89                                 planeCamera = new OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
90
91                                 planeMesh = new Mesh(
92                                         new PlaneBufferGeometry( 2, 2 ),
93                                         new MeshBasicMaterial( { depthTest: false, depthWrite: false, fog: false } )
94                                 );
95
96                                 geometries.update( planeMesh.geometry );
97
98                         }
99
100                         planeMesh.material.map = background;
101
102                         // TODO Push this to renderList
103
104                         renderer.renderBufferDirect( planeCamera, null, planeMesh.geometry, planeMesh.material, planeMesh, null );
105
106                 }
107
108         }
109
110         function setClear( color, alpha ) {
111
112                 state.buffers.color.setClear( color.r, color.g, color.b, alpha, premultipliedAlpha );
113
114         }
115
116         return {
117
118                 getClearColor: function () {
119
120                         return clearColor;
121
122                 },
123                 setClearColor: function ( color, alpha ) {
124
125                         clearColor.set( color );
126                         clearAlpha = alpha !== undefined ? alpha : 1;
127                         setClear( clearColor, clearAlpha );
128
129                 },
130                 getClearAlpha: function () {
131
132                         return clearAlpha;
133
134                 },
135                 setClearAlpha: function ( alpha ) {
136
137                         clearAlpha = alpha;
138                         setClear( clearColor, clearAlpha );
139
140                 },
141                 render: render
142
143         };
144
145 }
146
147
148 export { WebGLBackground };